//+------------------------------------------------------------------+
//|                                               TestCopyArray3.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot TSIsCD
#property indicator_label1  "Relation"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Orange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

//--- input parameters
input int r=25;
input int s=13;
input int sp=5;
input ENUM_MA_METHOD sm=MODE_EMA;
input ENUM_APPLIED_PRICE price=PRICE_CLOSE; 
input ENUM_TIMEFRAMES    InpTimePeriod = PERIOD_CURRENT;
//--- indicator buffers

double         IndiBuffer[];


int Handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
 
   SetIndexBuffer(0,IndiBuffer,INDICATOR_DATA);
 
   //Handle=iCustom(_Symbol,PERIOD_CURRENT,"TSIs",r,s,sp,sm);
   
    Handle=iCustom(NULL,0,"Relation",300,true,InpTimePeriod,"EURUSD",price);
  
   if(Handle==INVALID_HANDLE)
     {
      Print("Invalid handle ",GetLastError());
      return(INIT_FAILED);
     }

   return(INIT_SUCCEEDED);
   
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);
   IndicatorSetInteger(INDICATOR_DIGITS,2);      
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // size of the price[] array
                const int prev_calculated,// number of bars processed at the previous call
                const int begin,          // where the significant data start from
                const double &price[]     // array for calculation
                )
  {

   static bool error=true;
   int start;
   if(prev_calculated==0)
     {
      error=true;
     }
   if(error)
     {
      start=begin+1;
      error=false;
     }
   else
     {
      start=prev_calculated-1;
     }

   if(CopyBuffer(Handle,0,0,rates_total-start,IndiBuffer)==-1)
     {
      error=true;
      return(0);
     }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+
