Help with MACD

 

Hello everyone,

The idea behind the EA is that the price should cross EA and MACD lines should cross in the same direction to place a trade. The code I am using is attached below. For some reason, strategy tester takes trades even when the MACD is not properly aligned(screenshot attached). Any clues? Thanks

   //BUY
   
   //If the EA crosses first, first test for MACD, then go back by using shift to the EA cross
   if(MACDmain > MACDsignal && MACDmain1 < MACDsignal1) //MACD cross to the upside - when the EA cross
      {                                                  //happens first
      for(int i = 1; i <4; i++)
         {
         double EMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,i);          //The for loop tests whether a crossover
         double EMA2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,i+1);       //happened or not. A candle opening below 
         if(Close[i] > EMA && Close[i+1] < EMA && Close[i+2] < EMA2
            && Close[i+3] < EMA2)                                       //the MA and closing above it signals a cross
            {                                                           //with the additional confirmation of the close of
            BuyOne = true;                                           //previous candle being below the MA
            break;
            }
            
         } 
         
        for(int c = 2; c < 10; c++)
         {
            
            double BuyMain = iCustom(NULL,0,"MACD True",21,55,8,0,c);
            double BuySignal = iCustom(NULL,0,"MACD True",21,55,8,1,c);
            if(BuyMain < BuySignal) MACDvalid++;
            }
                      
         if(MACDvalid >= 8) BuyTwo = true;
      }
      
   //Similarly, if the MACD crosses first, test the EA, then go back using shift to see MACD
   
   if(Close[1] > TwentySMA && Close[2] < TwentySMA1)
      {
         // test to see if the MACD cross is valid or not
         
         for(int y = 1; y <= 4; y++)
            {
               double BuyMain.B = iCustom(NULL,0,"MACD True",21,55,8,0,y);
               double BuySignal.B = iCustom(NULL,0,"MACD True",21,55,8,1,y);
               double BuyMain1.B = iCustom(NULL,0,"MACD True",21,55,8,0,y+1);
               double BuySignal1.B = iCustom(NULL,0,"MACD True",21,55,8,1,y+1);
               
               if(BuyMain.B > BuySignal.B && BuyMain1.B < BuySignal1.B){BuyCross = y+1; break;}
            }
            
          for(int p = BuyCross; p < BuyCross + 10; p++)
            {
               BuyMain.B = iCustom(NULL,0,"MACD True",21,55,8,0,p);
               BuySignal.B = iCustom(NULL,0,"MACD True",21,55,8,1,p);
               if(BuyMain.B < BuySignal.B) MACDvalid++;
               
             }
             
             if(MACDvalid >= 8) BuyOne.B = true;
             
         for(int e = 1; e <4; e++)
         {
         EMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,e);          //The for loop tests whether a crossover
         EMA2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,e+1);       //happened or not. A candle opening below 
         if(Close[e] > EMA && Close[e+1] < EMA && Close[e+2] < EMA2
            && Close[e+3] < EMA2)                                       //the MA and closing above it signals a cross
            {                                                           //with the additional confirmation of the close of
            BuyTwo.B = true;                                           //previous candle being below the MA
            break;
            }
            
         }              

       }
   //SELL
   
      if(MACDmain < MACDsignal && MACDmain1 > MACDsignal1) //MACD cross to the downside - when the EA cross
      {                                                  //happens first
      for(int o = 1; o <4; o++)
         {
         double SellEMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,o);          //The for loop tests whether a crossover
         double SellEMA2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,o+1);       //happened or not. A candle opening below 
         if(Close[o] < SellEMA && Close[o+1] > SellEMA && Close[o+2] > SellEMA2
            && Close[o+3] > SellEMA2)                                       //the MA and closing above it signals a cross
            {                                                           //with the additional confirmation of the close of
            SellOne = true;                                           //previous candle being below the MA
            break;
            }
            
         } 
         
        for(int m = 2; m < 10; m++)
         {
            double SellMain = iCustom(NULL,0,"MACD True",21,55,8,0,m);
            double SellSignal = iCustom(NULL,0,"MACD True",21,55,8,1,m);
            if(SellMain > SellSignal) MACDvalid++;
            
          }
         
         if(MACDvalid >= 8) SellTwo = true;             
     }
      
   //Similarly, if the MACD crosses first, test the EA, then go back using shift to see MACD
   
   if(Close[1] < TwentySMA && Close[2] > TwentySMA1)
      {
         // test to see if the MACD cross is valid or not
         
         for(int g = 1; g <= 4; g++)
            {
               double SellMain.B = iCustom(NULL,0,"MACD True",21,55,8,0,g);
               double SellSignal.B = iCustom(NULL,0,"MACD True",21,55,8,1,g);
               double SellMain1.B = iCustom(NULL,0,"MACD True",21,55,8,0,g+1);
               double SellSignal1.B = iCustom(NULL,0,"MACD True",21,55,8,1,g+1);
               
               if(SellMain.B < SellSignal.B && SellMain1.B > SellSignal1.B){SellCross = g+1; break;}
            }
            
          for(int q = SellCross; q < SellCross + 10; q++)
            {
               SellMain.B = iCustom(NULL,0,"MACD True",21,55,8,0,q);
               SellSignal.B = iCustom(NULL,0,"MACD True",21,55,8,1,q);
               if(SellMain.B > SellSignal.B) MACDvalid++;
               
             }
             
             if(MACDvalid >= 8) SellOne.B = true;

         for(int z = 1; z <4; z++)
         {
         EMA = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,z);          //The for loop tests whether a crossover
         EMA2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,z+1);       //happened or not. A candle opening below 
         if(Close[z] < EMA && Close[z+1] > EMA && Close[z+2] > EMA2
            && Close[z+3] > EMA2)                                       //the MA and closing above it signals a cross
            {                                                           //with the additional confirmation of the close of
            SellTwo.B = true;                                           //previous candle being below the MA
            break;
            }
            
         }           
          
         }      

 
gulzaar:

For some reason, strategy tester takes trades even when the MACD is not properly aligned(screenshot attached). Any clues? Thanks

The code you have shown does not take any trades . . . I see no OrderSend . . .
 

Hello,

My guessing is, BuyCross variable has a wrong init value, or holds an old value.

The next for cycle is starting from here, but if it holds an old value or 0, the cycle will check interval starting from that. And if checking of this wrong interval is ok, it will buy.
 
RaptorUK:
The code you have shown does not take any trades . . . I see no OrderSend . . .


Yeah, I haven't included that code ... its there:)

There are two possibilities - one that the MA crosses first then the MACD crosses, or vice versa - the for loop here:

for(int q = SellCross; q < SellCross + 10; q++)
            {
               SellMain.B = iCustom(NULL,0,"MACD True",21,55,8,0,q);
               SellSignal.B = iCustom(NULL,0,"MACD True",21,55,8,1,q);
               if(SellMain.B > SellSignal.B) MACDvalid++;
               
             }
             
             if(MACDvalid >= 8) SellOne.B = true;

This is to test that the MACD lines before the crossover haven't touched.

Any thoughts?

 
erzo:

Hello,

My guessing is, BuyCross variable has a wrong init value, or holds an old value.

The next for cycle is starting from here, but if it holds an old value or 0, the cycle will check interval starting from that. And if checking of this wrong interval is ok, it will buy.


Hi, BuyCross and SellCross are both initialized at the start() cycle with a value of 0, so every time the program runs, they are set back to zero. MACDvalid is also initialized as = 0.

Any thoughts?

 
gulzaar:


Hi, BuyCross and SellCross are both initialized at the start() cycle with a value of 0, so every time the program runs, they are set back to zero. MACDvalid is also initialized as = 0.

Any thoughts?

If the BuyCross remains 0, becauose the

         if(BuyMain.B > BuySignal.B && BuyMain1.B < BuySignal1.B){BuyCross = y+1; break;}

condition not true (there is no MACD crossing), the next for cycle starts checking from 0, without MACD being crossed.
Try to use an additional if for checking value of BuyCross, before run the MACDvalid checking:

        if (BuyCross > 0) 
        {
         for(int p = BuyCross; p < BuyCross + 10; p++)
            {
               BuyMain.B = iCustom(NULL,0,"MACD True",21,55,8,0,p);
               BuySignal.B = iCustom(NULL,0,"MACD True",21,55,8,1,p);
               if(BuyMain.B < BuySignal.B) MACDvalid++;
               
             }
        }
        if(MACDvalid >= 8) BuyOne.B = true;
 
erzo:

If the BuyCross remains 0, becauose the

condition not true (there is no MACD crossing), the next for cycle starts checking from 0, without MACD being crossed.
Try to use an additional if for checking value of BuyCross, before run the MACDvalid checking:


Thanks, that fixed the problem! :)

Reason: