How to debug with drawing on chart?

 

Hey I am trying to create a Macd EA that also has a stochastic and MA indicator integrated together in it. I'm currently working on the Macd portion of it and am confused on how i can make the EA publish a drawing/arrow on the chart when the condition to buy is met on the macd. I need to debug the program so i need to make sure the code is giving the proper buy signal at the right time, I plan to use the arrow to make sure its giving the buy signal at the proper time. When macd crosses i want an arrow published on the chart to show where the code was triggered. is this possible? Also is this possible in back testing the code so i can see where the buy signal would have been generated in the past? This is what i have so far... 


THIS IS ALL ON THE ONTICK FUNCTION

//MACD CODE ==================================================================
  
  //create an array for several prices
  double myMacdPriceArray[];
  double myMacdSignalPriceArray[];
  
  //define the Macd EA
  int MacdDefinition = iMACD(_Symbol,_Period,6,15,9,PRICE_CLOSE);
  
  //defined Macd and store values, one buffer, current candle, 3 candles, store result
  CopyBuffer(MacdDefinition,0,0,3,myMacdPriceArray);
  CopyBuffer(MacdDefinition,1,0,3,myMacdSignalPriceArray);
  
  //get the value of the macd and signal current candle
  double MacdValue=(myMacdPriceArray[0]);
  double MacdSignalValue = myMacdSignalPriceArray[0];
  
  //set buy on if conditions are met
  bool MacdBuySignal =false;
  //buy signal for Macd (if lower than 0 and cross over signal)
  if (MacdValue<0 && MacdValue > MacdSignalValue){
   MacdBuySignal=true;
   ObjectCreate(
     _Symbol,//current chart
     "Buy Arrow", //name of object
     OBJ_ARROW_BUY, //type of object
     1,
     TimeCurrent(), //in main window
     MacdBuySignal,
     Close);
   Print("buy conditions are met for Macd on ",InpIdentifier );
   };
  
Arrow Code Function
Arrow Code Function
  • 2011.01.25
  • www.mql5.com
I would like a function I can add to my EA (any of them) that I can use to test indicator language...
 
thecerealslayer:

Hey I am trying to create a Macd EA that also has a stochastic and MA indicator integrated together in it. I'm currently working on the Macd portion of it and am confused on how i can make the EA publish a drawing/arrow on the chart when the condition to buy is met on the macd. I need to debug the program so i need to make sure the code is giving the proper buy signal at the right time, I plan to use the arrow to make sure its giving the buy signal at the proper time. When macd crosses i want an arrow published on the chart to show where the code was triggered. is this possible? Also is this possible in back testing the code so i can see where the buy signal would have been generated in the past? This is what i have so far... 

Sure it's possible.

  //define the Macd EA
  int MacdDefinition = iMACD(_Symbol,_Period,6,15,9,PRICE_CLOSE);

Should not be in OnTick() but in OnInit().

  //defined Macd and store values, one buffer, current candle, 3 candles, store result
  CopyBuffer(MacdDefinition,0,0,3,myMacdPriceArray);
  CopyBuffer(MacdDefinition,1,0,3,myMacdSignalPriceArray);

CopyBuffer() is function which can fail and return a value. Always add error checking if you want some reliability.

 //get the value of the macd and signal current candle
  double MacdValue=(myMacdPriceArray[0]);
  double MacdSignalValue = myMacdSignalPriceArray[0];

Index 0 is only the current candle value if your arrays are indexed as series. They are not in your code.

  //buy signal for Macd (if lower than 0 and cross over signal)
  if (MacdValue<0 && MacdValue > MacdSignalValue){

This code doesn't find a cross, it finds that MACD main is greater then MACD signal.

   ObjectCreate(
     _Symbol,//current chart
     "Buy Arrow", //name of object
     OBJ_ARROW_BUY, //type of object
     1,
     TimeCurrent(), //in main window
     MacdBuySignal,
     Close);

Look better on the documentation what is the first parameter of ObjectCreate.

With this fixed object name you will have only 1 arrow for the last signal, not the old ones, is it really what you want ?

"1" (window parameter), how do you know it's always in the window 1 you want to draw the arrow ?

What is "MacdBuySignal" supposed to do there ?

Good luck.

 
Alain Verleyen #:

hey thanks for the response, i will look into your solution. cheers!

 
Alain Verleyen #:

Sure it's possible.

Hey so I had a chance to look at your notes and I have some questions.


1: you said:  CopyBuffer() is function which can fail and return a value. Always add error checking if you want some reliability.

Q: How do you recommend doing this exactly? I am new to mql5 so i do not know many functions. I tried what might work, i dont know.


2:  You said: Index 0 is only the current candle value if your arrays are indexed as series. They are not in your code. 

Q: I believe I have fixed this, please check.


This is the New Code, Please take a look and see if its all good, also thanks for the help, it means a lot.


  //MACD CODE ==================================================================
  
  //define the Macd (Moved to global scope)
  //int MacdDefinition = iMACD(_Symbol,_Period,6,15,9,PRICE_CLOSE);
  
  
  //create an array for several prices
  double myMacdPriceArray[];
  double myMacdSignalPriceArray[];
  //set to series to move data down on new tick
  ArraySetAsSeries(myMacdPriceArray,true);
  ArraySetAsSeries(myMacdSignalPriceArray,true);
  
  
  //defined Macd and store values, one buffer, current candle, 3 candles, store result
  CopyBuffer(MacdDefinition,0,0,3,myMacdPriceArray);
  CopyBuffer(MacdDefinition,1,0,3,myMacdSignalPriceArray);
  
  //catch errors with copybuffer
  if (CopyBuffer(MacdDefinition,0,0,3,myMacdPriceArray)==-1){
   Print("Error copying Macd data to array.",GetLastError() );
   return;
  }
   if (CopyBuffer(MacdDefinition,1,0,3,myMacdSignalPriceArray)==-1){
   Print("Error copying Macd data to array.",GetLastError() );
   return;
  }
  
  //get the value of the macd and signal current candle (looks like pointless code so i muted it)
  //double MacdValue=(myMacdPriceArray[0]);
  //double MacdSignalValue = myMacdSignalPriceArray[0];
  
  //buy signal for Macd (if lower than 0 and cross over signal)
  if (myMacdPriceArray[0]<0 && myMacdPriceArray[0] > myMacdSignalPriceArray[0] && myMacdPriceArray[1] < myMacdSignalPriceArray[1]){
   MacdBuyArrowID++; //(Arrow ID i just an int counter to allow multiple arrows.
   //set macd buy on if conditions are met, this will be use further down the line if working properly.
   MacdBuySignal=true;
   ObjectCreate(
     0,//current chart
     MacdBuyArrowID , //name of object
     OBJ_ARROW_BUY, //type of object
     1,
     TimeCurrent(), //in main window
     MacdBuySignal,
     Close);
   Print("buy conditions are met for Macd on ",InpIdentifier );
   };
  
  
 
 
 //MACD CODE END ==================================================================
  
 






 
Alain Verleyen #:

Sure it's possible.

Should not be in OnTick() but in OnInit().

CopyBuffer() is function which can fail and return a value. Always add error checking if you want some reliability.

Index 0 is only the current candle value if your arrays are indexed as series. They are not in your code.

This code doesn't find a cross, it finds that MACD main is greater then MACD signal.

Look better on the documentation what is the first parameter of ObjectCreate.

With this fixed object name you will have only 1 arrow for the last signal, not the old ones, is it really what you want ?

"1" (window parameter), how do you know it's always in the window 1 you want to draw the arrow ?

What is "MacdBuySignal" supposed to do there ?

Good luck.

Hey So I ran the code in the strategy tester and there are no arrows being created

Reason: