CopyBuffer on iCustom in EA returns -1

 

Hello everyone

I cannot get this thing to work.

I am trying to read a moving average buffer from a downloaded indicator (AllAverages_v4.9_MT5), and want to read a few values to calculate entry (eg close above MA for a buy).

I'll attach the indi file and paste my EA code.

#include <Trade\Trade.mqh>

enum ENUM_PRICE                                                                                                 //copied from indicator
{
   close,               // Close
   open,                // Open
   high,                // High
   low,                 // Low
   median,              // Median
   typical,             // Typical
   weightedClose,       // Weighted Close
   medianBody,          // Median Body (Open+Close)/2
   average,             // Average (High+Low+Open+Close)/4
   trendBiased,         // Trend Biased
   trendBiasedExt,      // Trend Biased(extreme)
   haClose,             // Heiken Ashi Close
   haOpen,              // Heiken Ashi Open
   haHigh,              // Heiken Ashi High   
   haLow,               // Heiken Ashi Low
   haMedian,            // Heiken Ashi Median
   haTypical,           // Heiken Ashi Typical
   haWeighted,          // Heiken Ashi Weighted Close
   haMedianBody,        // Heiken Ashi Median Body
   haAverage,           // Heiken Ashi Average
   haTrendBiased,       // Heiken Ashi Trend Biased
   haTrendBiasedExt     // Heiken Ashi Trend Biased(extreme)   
};
enum ENUM_MA_MODE                                                                                               //copied from indicator
{
   SMA,                 // Simple Moving Average
   EMA,                 // Exponential Moving Average
   Wilder,              // Wilder Exponential Moving Average
   LWMA,                // Linear Weighted Moving Average
   SineWMA,             // Sine Weighted Moving Average
   TriMA,               // Triangular Moving Average
   LSMA,                // Least Square Moving Average (or EPMA, Linear Regression Line)
   SMMA,                // Smoothed Moving Average
   HMA,                 // Hull Moving Average by Alan Hull
   ZeroLagEMA,          // Zero-Lag Exponential Moving Average
   DEMA,                // Double Exponential Moving Average by Patrick Mulloy
   T3_basic,            // T3 by T.Tillson (original version)
   ITrend,              // Instantaneous Trendline by J.Ehlers
   Median,              // Moving Median
   GeoMean,             // Geometric Mean
   REMA,                // Regularized EMA by Chris Satchwell
   ILRS,                // Integral of Linear Regression Slope
   IE_2,                // Combination of LSMA and ILRS
   TriMAgen,            // Triangular Moving Average generalized by J.Ehlers
   VWMA,                // Volume Weighted Moving Average
   JSmooth,             // Smoothing by Mark Jurik
   SMA_eq,              // Simplified SMA
   ALMA,                // Arnaud Legoux Moving Average
   TEMA,                // Triple Exponential Moving Average by Patrick Mulloy
   T3,                  // T3 by T.Tillson (correct version)
   Laguerre,            // Laguerre filter by J.Ehlers
   MD,                  // McGinley Dynamic
   BF2P,                // Two-pole modified Butterworth filter by J.Ehlers
   BF3P,                // Three-pole modified Butterworth filter by J.Ehlers
   SuperSmu,            // SuperSmoother by J.Ehlers
   Decycler,            // Simple Decycler by J.Ehlers
   eVWMA,               // Modified eVWMA
   EWMA,                // Exponential Weighted Moving Average
   DsEMA,               // Double Smoothed EMA
   TsEMA,               // Triple Smoothed EMA
   VEMA                 // Volume-weighted Exponential Moving Average(V-EMA)
};   



input ENUM_MA_MODE ma_mode;
input ENUM_PRICE ma_price;
input double lots;
input int ma_period;
input int atr_period;


CTrade trade;
int hCustom;
double mabuf[];
MqlRates ratesbuf[];


bool BuySignal()
{
   if (CopyBuffer(hCustom,0,0,3,mabuf)<0)                                                                       //ERROR HERE. ALWAYS RETURNS -1 !!!!
        {PrintFormat("Data copied %i",CopyBuffer(hCustom,0,0,3,mabuf)); return false;}     
   if (!CopyRates(_Symbol,PERIOD_CURRENT,0,3,ratesbuf)) return false;
   if(ratesbuf[2].close<=mabuf[2] && ratesbuf[1].close>mabuf[1]) return true;
   return false;
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if (hCustom = iCustom(_Symbol,PERIOD_CURRENT,"AllAverages_v4.9_MT5",
               PERIOD_CURRENT,          
               ma_price,                                //price as per above ENUM
               ma_period,                       
               0,                                       //indicator shift
               ma_mode,
               false) == INVALID_HANDLE)  //false = solid color, true = color trend
      { Print("iCustom invalid handle");  
        return INIT_FAILED;
      }
   PrintFormat("Indicator handle %i",hCustom);                                  //just checking - this works
   ArraySetAsSeries(ratesbuf,true);
   ArraySetAsSeries(mabuf,true);     
               
               
   return(INIT_SUCCEEDED);
   
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   static long curtime;
   bool isNewBar;
   MqlRates dt[];
   double bid,ask;
   
  
   ArraySetAsSeries(dt,true);
   
   if (!CopyRates(_Symbol,PERIOD_CURRENT,0,1,dt)) return;
   isNewBar=false;
   if (dt[0].time != curtime)
      {curtime = dt[0].time;
       isNewBar=true; 
      }
   if (isNewBar && PositionsTotal()<1 && BuySignal())
   {
      ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
            
      trade.Buy(lots,_Symbol,ask,0,0,"CST_BUY");
    }
    
  
  }
//+------------------------------------------------------------------+

Pardon the unused variables.

I just cannot get a value from any of the 3 buffers (but it looks to me buffer 0 is the main moving average buffer.)

Can anybody help me with this please?

Files: