EA with 3 Moving Averages Cross and Stochastic

 

Hello friends,

I have been having hard times trying to get my EA to work. It compile correctly but it does not open trades when I backtest

The idea is to check Moving Average cross and check stochastic, on D1, H1, M15 and M5 before initiating a trade.

Can anyone take a look at it figure out what I did not do right?

//+-----------------------------------------------------------------------------------------------------------------------+
//|                                                                                                           TRESORX.mq4 |
//|                                                                                                  Copyright 2020,TrezX |
//|                                                                                                  https://www.mql5.com |
//+-----------------------------------------------------------------------------------------------------------------------+
#property copyright "Copyright 2020,TrezX"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <My Functions.mqh> 
//+-----------------------------------------------------------------------------------------------------------------------+
//| External Variables                                                                                                    |
//+-----------------------------------------------------------------------------------------------------------------------+
extern double LotSize      = 0.5;
extern double StopLoss     = 75;
extern double TakeProfit   = 150;

extern int Slippage        = 10;
extern int MagicNumber     = 13;
extern int FastMAPeriod    = 35;
extern int MediumEMAPeriod = 50;
extern int SlowEMAPeriod   = 200;
//+-----------------------------------------------------------------------------------------------------------------------+
//| Global Variables                                                                                                      |
//+-----------------------------------------------------------------------------------------------------------------------+
int    BuyTicket;
int    SellTicket;
double UsePoint;
int    UseSlippage;
double BuyStopLoss;
double SellStopLoss;
double BuyTakeProfit;
double SellTakeProfit;
double CalcPoint;

double CalcSlippage;
double Closed;
double CalcDigits;
bool CheckIfOpenOrdersByMagicNB;
int MagicNB;
//+-----------------------------------------------------------------------------------------------------------------------+
//| Expert initialization function                                                                                        |
//+-----------------------------------------------------------------------------------------------------------------------+
int OnInit()

 
{
        UsePoint    =    PipPoint (NULL);
        UseSlippage =    GetSlippage(NULL ,Slippage);
        return(INIT_SUCCEEDED);
}

//+-----------------------------------------------------------------------------------------------------------------------+
//|  Pip Point Function                                                                                                   |    
//+-----------------------------------------------------------------------------------------------------------------------+
double PipPoint(string Currency)
  {
        CalcDigits = MarketInfo(Currency,MODE_DIGITS);
   if(CalcDigits == 2 || CalcDigits == 3)
        CalcDigits = 0.01;
   else
      if(CalcDigits== 4 || CalcDigits == 5)
        CalcPoint = 0.0001;
   return (CalcPoint);
  }

//+-----------------------------------------------------------------------------------------------------------------------+
//|  Get Slippage Function                                                                                                |
//+-----------------------------------------------------------------------------------------------------------------------+

int GetSlippage(string Currency, int SlippagePips)

  {
     CalcDigits = MarketInfo(Currency,MODE_DIGITS);
   if(CalcDigits == 2 || CalcDigits == 4)
     {
      CalcSlippage = SlippagePips;
      }
      else if (CalcDigits == 3 || CalcDigits == 5)
       {       CalcSlippage = SlippagePips * 10;
               return(0);
       }
     return(0);
  }



//+-----------------------------------------------------------------------------------------------------------------------+
//| Expert deinitialization function                                                                                      |
//+-----------------------------------------------------------------------------------------------------------------------+

void OnDeinit(const int reason)
  {
//---
   }  
//+-----------------------------------------------------------------------------------------------------------------------+
//| Expert Start function                                                                                                 |
//+-----------------------------------------------------------------------------------------------------------------------+
void OnTick()
  {
  

                           
// Moving Averages

   double CurrentFastMA1       = iMA(NULL,1440,FastMAPeriod,0,0,0,0);
   double LastFastMA1          = iMA(NULL,1440,FastMAPeriod,0,0,0,1);
   double CurrentMediumEMA1    = iMA(NULL,1440,MediumEMAPeriod,0,1,0,0);
   double LastMediumEMA1       = iMA(NULL,1440,MediumEMAPeriod,0,1,0,1);
   double CurrentSlowEMA1      = iMA(NULL,1440,SlowEMAPeriod,0,1,0,0);
   double LastSlowEMA1         = iMA(NULL,1440,SlowEMAPeriod,0,1,0,1);
   double Stochastic1          = iStochastic(NULL,1440,5,3,3,0,1,0,0);
   double Stochastic1_2        = iStochastic(NULL,1440,5,3,3,0,1,1,0);

   double CurrentFastMA2       = iMA(NULL,60,FastMAPeriod,0,0,0,0);
   double LastFastMA2          = iMA(NULL,60,FastMAPeriod,0,0,0,1);
   double CurrentMediumEMA2    = iMA(NULL,60,MediumEMAPeriod,0,1,0,0);
   double LastMediumEMA2       = iMA(NULL,60,MediumEMAPeriod,0,1,0,1);
   double CurrentSlowEMA2      = iMA(NULL,60,SlowEMAPeriod,0,1,0,0);
   double LastSlowEMA2         = iMA(NULL,60,SlowEMAPeriod,0,1,0,1);
   double Stochastic2          = iStochastic(NULL,60,5,3,3,0,1,0,0);
   double Stochastic2_2        = iStochastic(NULL,60,5,3,3,0,1,1,0);


   double CurrentFastMA3       = iMA(NULL,15,FastMAPeriod,0,0,0,0);
   double LastFastMA3          = iMA(NULL,15,FastMAPeriod,0,0,0,1);
   double CurrentMediumEMA3    = iMA(NULL,15,MediumEMAPeriod,0,1,0,0);
   double LastMediumEMA3       = iMA(NULL,15,MediumEMAPeriod,0,1,0,1);
   double CurrentSlowEMA3      = iMA(NULL,15,SlowEMAPeriod,0,1,0,0);
   double LastSlowEMA3         = iMA(NULL,15,SlowEMAPeriod,0,1,0,1);
   double Stochastic3          = iStochastic(NULL,15,5,3,3,0,1,0,0);
   double Stochastic3_2        = iStochastic(NULL,15,5,3,3,0,1,1,0);


   double CurrentFastMA4       = iMA(NULL,5,FastMAPeriod,0,0,0,0);
   double LastFastMA4          = iMA(NULL,5,FastMAPeriod,0,0,0,1);
   double CurrentMediumEMA4    = iMA(NULL,5,MediumEMAPeriod,0,1,0,0);
   double LastMediumEMA4       = iMA(NULL,5,MediumEMAPeriod,0,1,0,1);
   double CurrentSlowEMA4      = iMA(NULL,5,SlowEMAPeriod,0,1,0,0);
   double LastSlowEMA4         = iMA(NULL,5,SlowEMAPeriod,0,1,0,1);
   double Stochastic4          = iStochastic(NULL,5,5,3,3,0,1,0,0);
   double Stochastic4_2        = iStochastic(NULL,5,5,3,3,0,1,1,0);
   
  
 
// Buy Order
   while(LastMediumEMA1 < LastSlowEMA1 && CurrentMediumEMA1 > CurrentSlowEMA1 && Stochastic1<60 && Stochastic1>Stochastic1_2)
      {
     if(LastFastMA1 < LastMediumEMA1 && CurrentFastMA1 > CurrentMediumEMA1)
       {
      while(LastMediumEMA2 < LastSlowEMA2 && CurrentMediumEMA2 > CurrentSlowEMA2 && Stochastic2<50 && Stochastic2>Stochastic2_2)
        {
        if(LastFastMA2 < LastMediumEMA2 && CurrentFastMA2 > CurrentMediumEMA2)
          {
         while(LastMediumEMA3 < LastSlowEMA3 && CurrentMediumEMA3 > CurrentSlowEMA3  && Stochastic3<20 && Stochastic3>Stochastic3_2) 
             { 
            if(LastFastMA3 < LastMediumEMA3 && CurrentFastMA3 > CurrentMediumEMA3)
             {
             while(LastMediumEMA4 < LastSlowEMA4 && CurrentMediumEMA4 > CurrentSlowEMA4 && Stochastic4>20 && Stochastic4>Stochastic4_2)
                {
               if(LastFastMA4 < LastMediumEMA4 && CurrentFastMA4 > CurrentMediumEMA4) 
                 {
                 if(Close[1]> CurrentFastMA4 && BuyTicket == 0)
                   {
//+-----------------------------------------------------------------------------------------------------------------------+
//|  One Trade at a Time Function                                                                                         |
//+-----------------------------------------------------------------------------------------------------------------------+
                 if (CheckIfOpenOrdersByMagicNB(MagicNumber))
                
                    {
                      int OpenOrders = OrdersTotal();
                      
                       for(int i=0; i < OpenOrders;i++)
                         {
                      if(OrderSelect(MagicNumber,SELECT_BY_TICKET)== true)
                         {
                         if(OrderMagicNumber()== MagicNumber)
                           {
                           
               //Close Order
              
               if(OrderCloseTime()== 0 && SellTicket> 0)
                 {
                  double CloseLots  = OrderLots();
                  double ClosePrice = Ask;

                  Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
                 }
                 }
               double OpenPrice = Ask;

               // Calcul Stop Loss and TakeProfit
                 BuyStopLoss = OpenPrice - (StopLoss*UsePoint);
                 BuyTakeProfit = OpenPrice + (TakeProfit*UsePoint);

               // Open Buy Order
              
   BuyTicket = OrderSend(NULL,OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);
   SellTicket=0;
              }
            }}}}}}}}}

// Sell Order
   while(LastMediumEMA1 > LastSlowEMA1 && CurrentMediumEMA1 < CurrentSlowEMA1 && Stochastic1>40 && Stochastic1>Stochastic1_2)
      {
     if(LastFastMA1 > LastMediumEMA1 && CurrentFastMA1 < CurrentMediumEMA1)
       {
      while(LastMediumEMA2 > LastSlowEMA2 && CurrentMediumEMA2 < CurrentSlowEMA2 && Stochastic2>50 && Stochastic2>Stochastic2_2)
        {
        if(LastFastMA2 > LastMediumEMA2 && CurrentFastMA2 < CurrentMediumEMA2)
          {
         while(LastMediumEMA3 > LastSlowEMA3 && CurrentMediumEMA3 < CurrentSlowEMA3  && Stochastic3>80 && Stochastic3>Stochastic3_2) 
             { 
           if(LastFastMA3 > LastMediumEMA3 && CurrentFastMA3 < CurrentMediumEMA3)
             {
             while(LastMediumEMA4 > LastSlowEMA4 && CurrentMediumEMA4 < CurrentSlowEMA4 && Stochastic4<80 && Stochastic4>Stochastic4_2)
                {
               if(LastFastMA4 > LastMediumEMA4 && CurrentFastMA4 < CurrentMediumEMA4) 
                 {
                 if(Close[1]< CurrentFastMA4 && SellTicket == 0)
                   {
                      
               //Close Order
               if(OrderCloseTime()== 0 && BuyTicket> 0)
                     {
                        double CloseLots  = OrderLots();
                        double ClosePrice = Bid;
      
                        Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
                       }
                       }
                        double OpenPrice = Bid;
      
                     // Calcul Stop Loss and TakeProfit
                           
                        SellStopLoss = OpenPrice + (StopLoss*UsePoint);
                        SellTakeProfit = OpenPrice - (TakeProfit*UsePoint);
      
                     // Open Sell Order
                     
                     SellTicket = OrderSend(NULL,OP_SELL,LotSize,OpenPrice,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);
                     BuyTicket=0;
                    }                                                
                 }
                 }}}}}}}}
           }
//+-----------------------------------------------------------------------------------------------------------------------+

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
NJERASSEM:

Hello friends,

I have been having hard times trying to get my EA to work. It compile correctly but it does not open trades when I backtest

The idea is to check Moving Average cross and check stochastic, on D1, H1, M15 and M5 before initiating a trade.

Can anyone take a look at it figure out what I did not do right?

Program never enter inside the first loop <=> condition for order buy are never fullfiled

// Buy Order
   while( LastMediumEMA1 < LastSlowEMA1 && 
          CurrentMediumEMA1 > CurrentSlowEMA1 &&
          Stochastic1<60 && Stochastic1>Stochastic1_2)
      { 
       Print("kjhkjhkjhkjhkjhkjhkjh");
     if(LastFastMA1 < LastMediumEMA1 && CurrentFastMA1 > CurrentMediumEMA1)
      
 

By the time the medium crosses the slow, the fast has long crossed. Stop looking for crosses — look for proper order.

NJERASSEM:

Drop your loops — nothing will ever change inside them. Return and retest on the next tick.

 
ffoorr:

Program never enter inside the first loop <=> condition for order buy are never fullfiled

William Roeder:

By the time the medium crosses the slow, the fast has long crossed. Stop looking for crosses — look for proper order.

Drop your loops — nothing will ever change inside them. Return and retest on the next tick.

Thank you for your time, 

I have been debugging with prints too, nothing so far!

Here is the strategy

1. On D1 Time frame, check for MA 30 crosses EMA 50 when both are above EMA 200 and stochastic below 60. (This shows a bullish trend setup)

2.on H1 Time frame,  check for MA 30 crosses EMA 50 when both are above EMA 200 and stochastic below 50. (This will bring us close to a good entry Price Action).

3. On M15 Time frame, check for MA 30 crosses EMA 50 when both are above EMA 200 and stochastic below  20. (This narrows the Price Action down to a more accurate entry).

4. On M5 Time frame, check for MA 30 crosses EMA 50 when both are above EMA 200 and stochastic above 20.( to pin point entry)

5. Initiate a Buy order!

Then the Opposite for Sell order!

NB: In Manual Trading, it triggers accurate signals!

This is the strategy I want to code, Is there a better way to code it?

 Please do help 🙏🏽
 
hi 
i have coded a similar strategy but in different way.
in backtest try to choose m5 as unit for back test.