Indicator values correct from Script, but struggling from EA

 

I have this test indicator:

#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 1
#property indicator_width2 1

#define BuyBufferIndex 0
#define SellBufferIndex 1

input int FastMA = 5;
input int SlowMA = 21;
input int BuyArrowCode = 233;
input int SellArrowCode = 234;

double buyBuffer[];
double sellBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   SetIndexBuffer(BuyBufferIndex, buyBuffer);
   SetIndexStyle(BuyBufferIndex, DRAW_ARROW, EMPTY, indicator_width1, indicator_color1);
   SetIndexLabel(BuyBufferIndex, "Buy");
   SetIndexArrow(BuyBufferIndex, BuyArrowCode);
   SetIndexEmptyValue(BuyBufferIndex, 0);
   
   SetIndexBuffer(SellBufferIndex, sellBuffer);
   SetIndexStyle(SellBufferIndex, DRAW_ARROW, EMPTY, indicator_width2, indicator_color2);
   SetIndexLabel(SellBufferIndex, "Sell");
   SetIndexArrow(SellBufferIndex, SellArrowCode);
   SetIndexEmptyValue(SellBufferIndex, 0);
   
   //---
   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[])
{
   //---
   for (int shift = (rates_total - prev_calculated - 3); shift >= 1; shift--)
   {
      double fastMA = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, shift);
      double slowMA = iMA(NULL, 0, SlowMA, 0, MODE_EMA, PRICE_CLOSE, shift);
      double prev_fastMA = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, shift + 1);
      double prev_slowMA = iMA(NULL, 0, SlowMA, 0, MODE_EMA, PRICE_CLOSE, shift + 1);
      
      if (shift < ArraySize(buyBuffer) && prev_fastMA < prev_slowMA && fastMA > slowMA)
         buyBuffer[shift] = open[shift];
      else if (shift < ArraySize(sellBuffer) && prev_fastMA > prev_slowMA && fastMA < slowMA)
         sellBuffer[shift] = open[shift];
   }
   
   //--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+

I test it from a Script as follows and works fine:

#property version   "1.00"
#property strict

string symbol = Symbol();
int timeframe = Period();

string TEST = "Test";
input int FastMA = 5;
input int SlowMA = 21;
input int BuyArrowCode = 233;
input int SellArrowCode = 234;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // Always false
   double buy;
   double sell;
   
   // Iterate through a day worth of candles
   for (int shift = 0; shift < 1440; shift++)
   {
      buy = iCustom(symbol, timeframe, TEST, FastMA, SlowMA, BuyArrowCode, SellArrowCode, 0, shift);
      sell = iCustom(symbol, timeframe, TEST, FastMA, SlowMA, BuyArrowCode, SellArrowCode, 1, shift);
      
      if (buy != 0 || sell != 0)
         Print("[" + shift + "] " + Time[shift] + ": " + buy + "," + sell);
   }
}

Struggling to get values from an EA as follows:

// Indicators
double buy = iCustom(symbol, timeframe, TEST, FastMA, SlowMA, BuyArrowCode, SellArrowCode, 0, 1);
double sell = iCustom(symbol, timeframe, TEST, FastMA, SlowMA, BuyArrowCode, SellArrowCode, 1, 1);

What am I missing?! Very frustrated.

Strategy Testing - Algorithmic Trading, Trading Robots - MetaTrader 5 Help
Strategy Testing - Algorithmic Trading, Trading Robots - MetaTrader 5 Help
  • www.metatrader5.com
The Strategy Tester allows you to test and optimize trading strategies ( Expert Advisors ) before using them for live trading. During testing, an...
 
  1. Sameer Shariff: What am I missing?! Very frustrated.
       for (int shift = (rates_total - prev_calculated - 3); shift >= 1; shift--)

    After the initial run (where prev_calculated is zero), prev_calculated equals rates_total, and your loop does nothing.

  2.       double prev_fastMA = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, shift + 1);
          double prev_slowMA = iMA(NULL, 0, SlowMA, 0, MODE_EMA, PRICE_CLOSE, shift + 1);

    The look back is max(FastMA-1, SlowMA-1)+1 or just SlowMA.
              How to do your lookbacks correctly #9#14 & #19

 
William Roeder #:
  1. After the initial run (where prev_calculated is zero), prev_calculated equals rates_total, and your loop does nothing.

  2. The look back is max(FastMA-1, SlowMA-1)+1 or just SlowMA.
              How to do your lookbacks correctly #9#14 & #19

Awesome! Thank you. Sometimes the obvious gets the better of us.