Custom Indicator. Tester vs Live chart. Different values

 

Hello

I feel sure that there is an answer in a post somewhere to my problem - but I cannot find it despite a search. Basically a custom indicator displays different values for one of the indicator lines (thick line, purple/Plum) when dropped onto StrategyTester in visual mode. The indicator line is using iMaOnArray. Source coude and screenshots attached. If anyone can help, or point to a post that answers this, I would be very grateful

Many thanks!

The code is below. The indicator lines 1-3 are fine. Indictor line #4 is the problem.

#property indicator_chart_window
#property  indicator_buffers 4
#property  indicator_color1  Yellow
#property  indicator_color2  Crimson
#property  indicator_color3  SkyBlue
#property  indicator_color4  Plum
#property  indicator_width4  2

extern int  avg                                                 = 6;

double      dHi, dLo, maInd[], midTop[], midBot[], midT, midB, ma[];
datetime    now, sod, last;
int         count, prd;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
    SetIndexBuffer(0, maInd);
    SetIndexBuffer(1, midTop);
    SetIndexBuffer(2, midBot);
    SetIndexBuffer(3, ma);

    SetIndexDrawBegin(3, avg);
    
    IndicatorShortName("Median");
    SetIndexLabel(0,"H+L/2");
    SetIndexLabel(1,"Median to MaxHigh");
    SetIndexLabel(2,"Median to MaxLow");
    SetIndexLabel(3,"Average of Median");

    prd = PERIOD_D1 * avg / Period();
        
    return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()  {    return(0);   }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
    int limit;
    int counted_bars=IndicatorCounted();

    //---- last counted bar will be recounted
    if(counted_bars>0) counted_bars--;
    limit=Bars-counted_bars-1;
    
    for(int i=limit; i>=0; i--) {
        now = Time[i];
        sod = now - TimeHour(now)*60*60 - TimeMinute(now)*60 - TimeSeconds(now);
        
        if (now == sod) {
            dHi = High[i]; dLo = Low[i]; 
            if (last != now) { 
                midT = Open[i]; midB = Open[i]; 
            }
        }
        dHi = MathMax(dHi, High[i]);
        dLo = MathMin(dLo, Low[i]);
        
        maInd[i] = (dHi + dLo) * 0.5;
        
        midTop[i] = MathMax(midT, (dHi + maInd[i]) * 0.5);
        midBot[i] = MathMin(midB, (dLo + maInd[i]) * 0.5);
        midT = midTop[i];
        midB = midBot[i];
        last = now;
    }
    
    if (counted_bars < avg) counted_bars = avg;       
    limit=Bars-counted_bars-1;
    for(i=limit; i>=0; i--)
        ma[i] = iMAOnArray(maInd, 0, prd, 0, MODE_EMA, i);
        
    return(0);
}
//+------------------------------------------------------------------+
 
HarriMQL4:

Hello

I feel sure that there is an answer in a post somewhere to my problem - but I cannot find it despite a search. Basically a custom indicator displays different values for one of the indicator lines (thick line, purple/Plum) when dropped onto StrategyTester in visual mode. The indicator line is using iMaOnArray. Source coude and screenshots attached. If anyone can help, or point to a post that answers this, I would be very grateful

Many thanks!

The code is below. The indicator lines 1-3 are fine. Indictor line #4 is the problem.

The Strategy Tester is designed to test Strategies  (EAs)  not Indicators . . .   so when you place an Inddicator on a ST chart you have to consider what data the Indicator is seeing. 

For example,  does it get the correct value when it does this . . .

TimeHour(now)

 does it get the correct value for Bars ?

 
RaptorUK:

The Strategy Tester is designed to test Strategies  (EAs)  not Indicators . . .   so when you place an Inddicator on a ST chart you have to consider what data the Indicator is seeing. 

For example,  does it get the correct value when it does this . . .

 does it get the correct value for Bars ?


Ok. I put a Comment line in the indicator to report Bars and Time(now) - both seem to report correct values, ie, Bars increases, TimeHour matches the simulated time in tester. I ran it by using a "dummy" EA that does nothing.

Sorry for being "thick", but is it incorrect use to drop custom indicators on StrategyTester charts in order to visually see what the EA I'm testing "sees"?

 
HarriMQL4:

Ok. I put a Comment line in the indicator to report Bars and Time(now) - both seem to report correct values, ie, Bars increases, TimeHour matches the simulated time in tester. I ran it by using a "dummy" EA that does nothing.

Sorry for being "thick", but is it incorrect use to drop custom indicators on StrategyTester charts in order to visually see what the EA I'm testing "sees"?

I think it depends on how your Indicator works . . .  I have Indicators that work just fine,  I have other bits of code that will just not work in the Strategy Tester.  The only thing that you should assume will work correctly,  because it is being given the bar data generated by the Strategy tester,  is an EA.  Indicators may work . . .  some do,  but others don't.  When an Indicator doesn't work it will be due to it getting data from a chart other than the one being generated in the Strategy Tester . . .   in my opinion.  If you can find the incorrect information that the Indicator is getting you may be able to re-code to avoid the issue.
 
RaptorUK:
I think it depends on how your Indicator works . . .  I have Indicators that work just fine,  I have other bits of code that will just not work in the Strategy Tester.  The only thing that you should assume will work correctly,  because it is being given the bar data generated by the Strategy tester,  is an EA.  Indicators may work . . .  some do,  but others don't.  When an Indicator doesn't work it will be due to it getting data from a chart other than the one being generated in the Strategy Tester . . .   in my opinion.  If you can find the incorrect information that the Indicator is getting you may be able to re-code to avoid the issue.

Thanks for your advice with this. I gave up in the end and used iMA within my custom indicator, rather than iMAOnArray. It displays correctly, and the values are approximately similar to the values I was trying to get. Since I do not use the 4th indicator line to control the EA I'm building, but as a visual aid for me, it is good enough. Thanks again.
 
OK,  yesterday I was testing an Indicator in the Strategy Tester and I was having some problems . . .  then I figured out what  was causing my problem,  I was using Bid but the Bid price was coming from current market price not from the Strategy Tester current price.  So I replaced Bid with Close[0] and my code worked.  

This issue wouldn't happen with an EA in the Strategy Tester . . . .  Bid would be the Bid price produced by the Strategy Tester.
 
  1.  now = Time[i];
     sod = now - TimeHour(now)*60*60 - TimeMinute(now)*60 - TimeSeconds(now);
    Can be simplified
    now = Time[i];
    sod = DateOfDay(now);
    ///////
    #define HR2400      86400           // 24 * 3600
    datetime TimeOfDay(datetime when){  return( when % HR2400          );       }
    datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );       }
    
    now=Time[i]; TimeHour(now) is fine but Hour() would not be.
  2. HarriMQL4:

    Ok. I put a Comment line in the indicator to report Bars and Time(now) - both seem to report correct values, ie, Bars increases, TimeHour matches the simulated time in tester. I ran it by using a "dummy" EA that does nothing.

    Sorry for being "thick", but is it incorrect use to drop custom indicators on StrategyTester charts in order to visually see what the EA I'm testing "sees"?

    You can put CI on the chart, but remember that if the iCustom call uses different parameters than the CI, the two won't be the same. This is why I wrote my Polyline, to display the actual values the EA is getting.
  3.  if(counted_bars>0) counted_bars--;
    Delete that line. Bars - 1 - counted_bars is correct https://www.mql5.com/en/forum/132447
  4.    if (counted_bars < avg) counted_bars = avg;       
        limit=Bars-counted_bars-1;
        for(i=limit; i>=0; i--)
            ma[i] = iMAOnArray(maInd, 0, prd, 0, MODE_EMA, i);
    
    
    prd not avg