Custom Indicator max and min of last 50 bars

To add comments, please log in or register
mtsTrader
49
mtsTrader  

Hello all,

at the moment I'm traying to create an indicator. The indicator should plot every bar the maximum and the minimum of the last 50 bars to the chart-window. Also it should calculate the difference of maximum and minimum.

I tried it like this:


#property copyright ""

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   2
#property indicator_type1   DRAW_ARROW
#property indicator_type2   DRAW_ARROW
#property indicator_color1  DodgerBlue
#property indicator_color2  Red
 
//--- External parametrs
input int                   InpBigPeriod = 50;                                           //BigPeriod

//---- buffers
double                      ExtDeltaBigBuffer[];
double                      ExtHighBuffer[];
double                      ExtLowBuffer[];

//--- global variables
int                         ExtBigPeriod; 
double                      ExtHigh;
double                      ExtLow;

//------------------------------------------------------------------
// Custom indicator initialization function                         
//------------------------------------------------------------------
void OnInit()
{
        //--- checking input data
        if(InpBigPeriod < 0)
    {
                ExtBigPeriod = 50;
                Print("Input parameter InpBigPeriod has incorrect value. Indicator will use value", ExtBigPeriod,"for calculations.");
    }
        
        else ExtBigPeriod=InpBigPeriod;
        

        //---- indicator buffers
        SetIndexBuffer(0,ExtDeltaBigBuffer);
        SetIndexBuffer(1,ExtHighBuffer,INDICATOR_CALCULATIONS);
        SetIndexBuffer(2,ExtLowBuffer,INDICATOR_CALCULATIONS);
        //--- set arrow symbol
        PlotIndexSetInteger(1,PLOT_ARROW,159);          //Plot High
        PlotIndexSetInteger(2,PLOT_ARROW,159);          //Plot Low
        //--- set indicator digits
        IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
        //--- set label name
        PlotIndexSetString(0,PLOT_LABEL,"DeltaBig("+
                      DoubleToString(ExtHigh,5)+","+
                      DoubleToString(ExtLow,5)+")");
}
//------------------------------------------------------------------
// 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(rates_total < ExtBigPeriod + 1)      
                                                                                
    return(0);
        
        
        int pos=prev_calculated-1;


        if(pos<1)
        {
                pos=1;                                                                          
                ExtHighBuffer[0] = 0;   
                ExtLowBuffer[0] = 0;
                ExtDeltaBigBuffer[0] = 0;
        }

        //---main cycle
        for(int i=pos;i<rates_total-1 && !IsStopped();i++)   
    {           
                ExtHighBuffer[i+1] = GetHigh(i,i-InpBigPeriod,high);
                ExtLowBuffer[i+1] = GetLow(i,i-InpBigPeriod,low);
                ExtDeltaBigBuffer[i+1] = ExtHighBuffer[i+1] - ExtLowBuffer[i+1];                
    }    //if main cycle close
     
//---- OnCalculate done. Return new prev_calculated.
   return(rates_total);
   
}   //oncalculate close

//------------------------------------------------------------------
// Find highest price from start to current position                
//------------------------------------------------------------------
double GetHigh(int nPosition,int nStartPeriod,const double &HiData[])
{
        //--- calculate
        double result=HiData[nStartPeriod];
        for(int j=nStartPeriod;j<=nPosition;j++) if(result<HiData[j]) result=HiData[j];
        return(result);
}
//------------------------------------------------------------------
// Find lowest price from start to current position                 
//------------------------------------------------------------------
double GetLow(int nPosition,int nStartPeriod,const double &LoData[])
{
        //--- calculate
        double result=LoData[nStartPeriod];
        for(int j=nStartPeriod;j<=nPosition;j++) if(result>LoData[j]) result=LoData[j];
        return(result);
}

If I trie to debug it I get the message that the array is out of range.

Does anybody know what the problem is?

Alain Verleyen
38024
Alain Verleyen  

You are trying to access array with a negative index (out of range). Change this line :

        if(pos<1)
        {
                pos=InpBigPeriod;                                                                          
                ExtHighBuffer[0] = 0;   
                ExtLowBuffer[0] = 0;
                ExtDeltaBigBuffer[0] = 0;
        }
To add comments, please log in or register