Bar calculation and iCustom sanity check

 

Hello,

I have been searching for an answer for my question all over the web, but I didn't have any luck. I have a simple indicator that takes an indicator called by iCustom and displays if it is in uptrend or downtrend in a 0/1 histogram. The code works well when placed on the chart and when I tested it in a simulator(not Strategy Tester), however, in Strategy Tester i get values that are just not there.

Image: Started the Strategy Tester on my indicator. Stopped it (not paused) and added the same indicator and the base indicator to the chart

Different values, same indicator, same chart


I tested the indicator called in iCustom in the Strategy Tester, and i'm 100% sure it doesn't repaint and it shows correct values.

Image: Checking that the values from the base indicator are correct

Checking the base indicator


That leaves me with the only option my code is wrong, and I for the life of me don't see it. What am I doing wrong?


#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 3
#property indicator_plots   3
//--- plot Buy
#property indicator_label1  "Buy"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrLimeGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Sell
#property indicator_label2  "Sell"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot Neutral
#property indicator_label3  "Neutral"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color3  clrYellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- indicator buffers
double         BuyBuffer[];
double         SellBuffer[];
double         NeutralBuffer[];


input double         SPF_Period1                   = 10;
input double         SPF_Period2                   = 60;
input int            SPF_CalculationCount          = 10;
int                  SPF_ColorChange               = 2;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BuyBuffer);
   SetIndexBuffer(1,SellBuffer);
   SetIndexBuffer(2,NeutralBuffer);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int counted = IndicatorCounted();
   int lookback = 100;
   
   for(int iBar = Bars -1 - MathMax(lookback, counted); iBar >= 0; --iBar) {
      if(SPF_Uptrend(iBar))
         BuyBuffer[iBar] = 1;
      else if(SPF_Downtrend(iBar))
         SellBuffer[iBar] = 1;
      else
         NeutralBuffer[iBar] = 1;
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
bool SPF_Uptrend(int candle) {
   double MainLine               = iCustom(NULL, 0, "SPF", 0, "", SPF_Period1, SPF_Period2, SPF_CalculationCount, 0, SPF_ColorChange, false, 3, candle);
   double TopLine                = iCustom(NULL, 0, "SPF", 0, "", SPF_Period1, SPF_Period2, SPF_CalculationCount, 0, SPF_ColorChange, false, 0, candle);
   double BottomLine             = iCustom(NULL, 0, "SPF", 0, "", SPF_Period1, SPF_Period2, SPF_CalculationCount, 0, SPF_ColorChange, false, 2, candle);
   
   if(MainLine > TopLine)
      return true;
   else
      return false;
}

bool SPF_Downtrend(int candle) {
   double MainLine               = iCustom(NULL, 0, "SPF", 0, "", SPF_Period1, SPF_Period2, SPF_CalculationCount, 0, SPF_ColorChange, false, 3, candle);
   double TopLine                = iCustom(NULL, 0, "SPF", 0, "", SPF_Period1, SPF_Period2, SPF_CalculationCount, 0, SPF_ColorChange, false, 0, candle);
   double BottomLine             = iCustom(NULL, 0, "SPF", 0, "", SPF_Period1, SPF_Period2, SPF_CalculationCount, 0, SPF_ColorChange, false, 2, candle);
   
   if(MainLine < BottomLine)
      return true;
   else
      return false;
}