Why is this simple bit of code not working?

 

I'm doing a MACD crossover EA to test my MQL5 skills, however I cannot understand why this simple comparison isn't working at times, but is working at others when I run the stategy tester.

At 2019.09.02 01:03:00 on the strategy tester journal (red box on screenshot), the conditions (MACD0 > MACD1) appear to be TRUE, however the IF statement clearly doesn't trigger, as there is no print statement in the journal.

Later on in the test, it appears to function (green box), but not for this cross-over.

I hope I've included the right screenshots and posted this to the right place - if I haven't I apologise and will rectify immediately.


Thank you in advance for your kind assistance at this deeply frustrating time!!

 
Southerndown:

I'm doing a MACD crossover EA to test my MQL5 skills, however I cannot understand why this simple comparison isn't working at times, but is working at others when I run the stategy tester.

At 2019.09.02 01:03:00 on the strategy tester journal (red box on screenshot), the conditions (MACD0 > MACD1) appear to be TRUE, however the IF statement clearly doesn't trigger, as there is no print statement in the journal.

Later on in the test, it appears to function (green box), but not for this cross-over.

I hope I've included the right screenshots and posted this to the right place - if I haven't I apologise and will rectify immediately.


Thank you in advance for your kind assistance at this deeply frustrating time!!

Please insert the code correctly.

Reference: how to insert code

The   Code button of the object-insertion menu is intended to insert the source code into the text of the message. An empty window in which to paste the code appears as soon as you click this button. To finish adding the code, click the   Insert button. To cancel the operation, you should click the Cancel button.

Insert source code to a message

 
#include<Trade\Trade.mqh>
CTrade trade;

int TickCounter =0;

void OnTick()
  {
      
      TickCounter++;

      //Calculate Ask price
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      
      //bid price
      double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
      MqlRates PriceInfo[];
      
      double myPriceArray[];
      
      int PriceData = CopyRates(_Symbol,_Period,0,3,PriceInfo);
      
      string signal="";
      
      int MACDDefinition = iMACD(_Symbol,_Period,12,26,9,PRICE_CLOSE);
      
      CopyBuffer(MACDDefinition,0,0,3,myPriceArray);
      
      //float MACDValue = myPriceArray[0];
    
      Print(TickCounter);     
         Print("MACD0 is: ",myPriceArray[0]);
         Print("MACD1 is: ",myPriceArray[1]);
         
         
         
         
      if(myPriceArray[0]>0 && myPriceArray[1]<0 && TickCounter>10)
      {
         signal = "sell";
         Print("MACD0 is: ",myPriceArray[0]);
         Print("MACD1 is: ",myPriceArray[1]);
      }  
      
      if(myPriceArray[1]<0 && myPriceArray[1]>0 && TickCounter>10)
      {
         signal = "buy";
         Print("MACD0 is: ",myPriceArray[0]);
         Print("MACD1 is: ",myPriceArray[1]);
      }           
      
      
      
      if(signal == "sell" && PositionsTotal()<1)
      {   
         trade.Sell(0.10,NULL,Bid,0, (Bid-150 * _Point),NULL);
         Print("MACD0 is: ",myPriceArray[0]);
         Print("MACD1 is: ",myPriceArray[1]);
      }   
      if(signal == "buy" && PositionsTotal()<1)
      {   
         trade.Buy(0.10,NULL,Ask,0, (Ask+150 * _Point),NULL);   
         Print("MACD0 is: ",myPriceArray[0]);
         Print("MACD1 is: ",myPriceArray[1]);
      }
      //Print("signal is now :",signal);  
   
  }

 

The mistake is very big:

int MACDDefinition = iMACD(_Symbol,_Period,12,26,9,PRICE_CLOSE);

in MQL5, the handle of the indicator SHOULD BE CREATED ONCE (in OnInit)!


Do not fix it yet - it makes no sense to look further.

 

Oooh interesting - I got this from a tutorial series, one I've since abandoned in preference of another series.


Thank you very much Vladimir for taking the time to review my code - it is very much appreciated!!!!


I will continue with my new series of tutorials, though if anyone has another series to recommend, I'm all ears!!!


Thank you all!

 
Southerndown :

Oooh interesting - I got this from a tutorial series, one I've since abandoned in preference of another series.


Thank you very much Vladimir for taking the time to review my code - it is very much appreciated!!!!


I will continue with my new series of tutorials, though if anyone has another series to recommend, I'm all ears!!!


Thank you all!

You have at least one more error in your code.

 
Vladimir Karputov:

You have at least one more error in your code.


Thank you Vladimir. I have changed tutorial to a completely different teacher now and am having much better results with the coding.


Thank you again for your replies, much appreciated!

Reason: