CopyBuffer returns -1 for long periods of built-in MAs

 

Hey guys,


long time since I've been on here....

English is not my native language, so I'm sorry in advance if my phrasing aren't clear enough.


I've encountered some weird issue today with following built-in MAs- DEMA, TEMA, FrAMA.

It seems that whenever I try to set a long period on them, I can't programatically get the MA value (CopyBuffer returns -1),

However, if I manually attach the above MAs to the chart, and setting the same time period- the MA plots just fine (meaning the above calculation is done just fine by the indicator).


I have several years of tick data, so I have enough data for the above calculations.


Attached a sample code to illustrate the issue-


//+------------------------------------------------------------------+
//|                                                    Dema Test.mq5 |
//|                                Copyright 2023, Amichay Rotenberg |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Amichay Rotenberg"
#property link      "https://www.mql5.com"
#property version   "1.0"

input int iDEMAPeriod = 131;

int iDEMAHandle;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
 
   // Create DEMA Handle
   iDEMAHandle = iDEMA(Symbol(), Period(), iDEMAPeriod, 0, PRICE_CLOSE);
   
   // Check if handle created succesfully
   if(iDEMAHandle == INVALID_HANDLE) {
      Print(__FUNCTION__ + "()- Failed to create DEMA Handle");
      return INIT_FAILED;
   } // if(iDEMAHandle == INVALID_HANDLE)
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
   // Test only on new bar, to not copy buffer on each tick
   if(IsNewBar()) {
      
      ResetLastError();

      double dVal[1];
      
      if(CopyBuffer(iDEMAHandle, 0, 0, 1, dVal) == -1) {
         Print(__FUNCTION__ + "()- Failed to get MA data.");
         Print("Error no.- ", GetLastError());
         ExpertRemove();
      } else {
         Print("MA Val = ", dVal[0]);
      } // if(CopyBuffer(iDEMAHandle, 0, 0, 1, dVal) == -1)
      
   } // if(IsNewBar())
   
  }
//+------------------------------------------------------------------+ 
//| Returns true if new bar and false if not                         | 
//+------------------------------------------------------------------+ 
bool IsNewBar() {

   static datetime dtLastBarTime = 0;
   // Use CopyTime to get the last bar open time
   // CopyTime returns -1 if unsuccesfull
   datetime dtLastOpenTimeArray[1];
   if(CopyTime(Symbol(), Period(), 0, 1, dtLastOpenTimeArray) == -1) {
      Print(__FUNCTION__ + "()- Failed to get last open time");
      Print("Error No.- ", GetLastError());
      ExpertRemove();
   } // if(CopyTime(Symbol(), Period(), 0, 1, dtLastOpenTimeArray) == -1)
   
   if(dtLastBarTime != dtLastOpenTimeArray[0]) {
      dtLastBarTime = dtLastOpenTimeArray[0];
      return true;
   }

   return false;
}
//+------------------------------------------------------------------+



When I'm setting a period of 131 or higher on D1 chart- CopyBuffer returns -1

If I set any lower period- CopyBuffer returns the actual value.


Again- I have no problem when manually adding the above MA to D1 chart and setting any period above 130.


I tried looking thru the documentation, to see if there any sort of limitation on the above MAs, but couldn't find anything.


Am I missing something here?