"to_copy" method does not work when copying buffer of an "Technical Indicator"

karp wak
129
karp wak  
I am trying to make an ADXR indicator and I am using the "to_copy" method from the examples folder but it does not work correctly in my code.

I think that this method (in this way) does not work with "Technical Indicators". I think so... because I have checked all the code several times.

A little help please?

Thank you very much !!
The code below
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot signal
#property indicator_label1  "Signal"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int adxPeriod = 14;
input int period = 14;
//--- buffers
double    signal[];
double    adxData[];
//--- Handlers
int adxHandler;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0,signal,INDICATOR_DATA);
   SetIndexBuffer(1,adxData,INDICATOR_CALCULATIONS);
   
   IndicatorSetInteger(INDICATOR_LEVELS,3);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,30);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,20);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,10);
   
   adxHandler = iADXWilder(NULL,PERIOD_CURRENT,adxPeriod);   
   if(adxHandler==INVALID_HANDLE)return(INIT_FAILED);
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
bool CopyIndicatorBuffer(int pHandler, int pToCopy, double &pArray[], int pIndicatorID=0)
{
      if(IsStopped())return false;
      
      if(CopyBuffer(pHandler,pIndicatorID,0, pToCopy, pArray)<=0)
      {
         Print("Handle error =>",GetLastError());
         return false;
      }  
      return true;
}
//+------------------------------------------------------------------+
int GetSizeToCopy(const int rates_total, const int prev_calculated)
{
      int to_copy=0;
      if(prev_calculated>rates_total || prev_calculated<0) 
      {
         to_copy=rates_total;
      }
      else
      {
         to_copy=rates_total-prev_calculated;
         
         if(prev_calculated>0) to_copy++;
      }      
      return to_copy;           
}
//+------------------------------------------------------------------+
//| 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[])
  {
//---         
      //+------------------------------------------------------------------+
      if(BarsCalculated(adxHandler)<rates_total) return(0);     
      //+------------------------------------------------------------------+       
       if(prev_calculated<rates_total)
      {
         //int to_copy=GetSizeToCopy(rates_total, prev_calculated);// --> it does not works
         int to_copy=rates_total;
         if(!CopyIndicatorBuffer(adxHandler,to_copy, adxData)) return 0;
      }
      //+------------------------------------------------------------------+ 

      for( 
            int i = prev_calculated < 1 ? 0 : prev_calculated -1; 
            !IsStopped() && ( i < rates_total ); 
            i++ 
         ) 
      {
         if(i>period)
         {            
               double res = (adxData[i] + adxData[i-period])/2;
               
               if(res<100)
                  signal[i] = res; 
               else signal[i] = 20;           
         }
         else signal[i] = EMPTY_VALUE;
      }       

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int  reason)
{
    if(adxHandler!=INVALID_HANDLE)IndicatorRelease(adxHandler);
}
//+------------------------------------------------------------------+
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
  • www.mql5.com
order_calc_margin - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5