Using iCustom in Strategy Tester - page 2

 

Always use

#property strict

in your codes so that you don't get into the habit of writing sloppy code that tries to retrieve values from a bar that doesn't exist.

You are not getting the iCustom value that you expect, because you are only calling it from your EA when a new bar opens, but the indicator draws the arrow sometime after the new bar has opened.

 
//+------------------------------------------------------------------+
//|                                                 Test_buffers.mq4 |
//|      Copyright 2018, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int bar=1;// bar number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---> create timer
   EventSetTimer(1);
//--->
   ObjectCreate(0,"line",OBJ_VLINE,0,0,0);
//--->
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---> destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--->
   if(MQLInfoInteger(MQL_TESTER))
    {
     OnTimer();
    }
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--->
   string comment="";
   comment=comment+"Bar Number: "+IntegerToString(bar)+"\n";
   comment=comment+"Buffer 0: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",0,bar))+"\n";
   comment=comment+"Buffer 1: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",1,bar))+"\n";
   comment=comment+"Buffer 2: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",2,bar))+"\n";
   comment=comment+"Buffer 3: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",3,bar))+"\n";
   comment=comment+"Buffer 4: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",4,bar))+"\n";
   comment=comment+"Buffer 5: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",5,bar))+"\n";
   comment=comment+"Buffer 6: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",6,bar))+"\n";
   comment=comment+"Buffer 7: "+DoubleToStr(iCustom(Symbol(),PERIOD_CURRENT,"CombinedCandle2",7,bar))+"\n";
//--->  
   Comment(comment);
   ObjectMove(0,"line",0,iTime(Symbol(),PERIOD_CURRENT,bar),0);
//--->
   bar++;
  }
//+------------------------------------------------------------------+
 
Keith Watford:

Always use

in your codes so that you don't get into the habit of writing sloppy code that tries to retrieve values from a bar that doesn't exist.

You are not getting the iCustom value that you expect, because you are only calling it from your EA when a new bar opens, but the indicator draws the arrow sometime after the new bar has opened.

Confirm I am using #Property Strict.

Can you confirm why you say I am only calling it from the EA when a new bar opens?

I ask because when I watch what happens in 'real time' in strategy tester, the price goes forward by 4 points on the current building candle, the Indicator puts entry arrow next to that candle at the correct point, nothing happens but immediately that candle finally completes it opens the trade!

 
Bostock58:

Confirm I am using #Property Strict.

Can you confirm why you say I am only calling it from the EA when a new bar opens?

        total=OrdersTotal();
        static datetime candletime = 0;
        if(candletime != Time[0])
        if(Market>0)
        {
         //Did it make an up arrow on candle 1?
         double bullentry = iCustom(NULL,0,"CombinedCandle2",2,1);
         if(bullentry != EMPTY_VALUE)
            if(total<1)
               EnterTrade(OP_BUY);

Maybe you don't as you do not include the complete block of code, but it is reasonable to assume that you update candletime.

 
Marco vd Heijden:

That is REALLY clever!

I have tried it on strategy tester, it took me a while to figure out how to control it but it correctly shows the Arrow Buffer value is the same as the entry arrow on the chart, on the candle it is placed.

It also confirms when the Arrow Buffer becomes empty i.e when that candle closes and the value goes back to 2147483647.00000000

So it seems to be working correctly, bonus.

 
Keith Watford:

Maybe you don't as you do not include the complete block of code, but it is reasonable to assume that you update candletime.

At the end of the four entry options I am using (Bull or Bear for each Indicator) I have the following.

          Comment(" ");
      candletime=Time[0];

Which is meant to stop multiple entry attempts on the same candle however I am also using.

total=OrdersTotal();
 if(total<1)
  EnterTrade(OP_BUY);
 
Moderators, can you move mql4 topic to the right section please ? Thank you.
Reason: