Question from a newbie in MQL5 : How to draw a buffer line with calculated values from previous bars ?

 

I'm trying to create an indicator in a seperate window with calculated values from previous bars. Although my values are correct, I get this : a straight line with a unique and totally different value. I would really appreciate if anyone could check the simple program I created and put the finger on what is wrong with it.


Here is my code base :

//+------------------------------------------------------------------+
//|                                                   Sep_Window.mq5 |
//|                                                   Steve Van Laer |
//|                                      http://www.stevevanlaer.com |
//+------------------------------------------------------------------+
#property copyright "Steve Van Laer"
#property link      "http://www.stevevanlaer.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
//---
#property indicator_label1 "Buffer"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//---
input int Periode=1;
double Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   SetIndexBuffer(0,Buffer,INDICATOR_DATA);
   PlotIndexGetInteger(0,PLOT_DRAW_BEGIN,Periode-1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--- 1) Copy rates to get highs, lows, closes and opens
   string symbol=_Symbol;        //any symbol
   ENUM_TIMEFRAMES tf=_Period;   //period on screen
   int bars=Bars(symbol,tf);     //number of bars on screen
   MqlRates rates[];             
   int copied=CopyRates(symbol,tf,0,bars,rates);
   
//--- 2) Check copy rates
   if(copied!=bars)
      {
      Print(__FUNCTION__,"2.1) copied rates size(",copied,") is not equal to bars size(",bars,")");
      return(0);
      }
   else ArraySetAsSeries(rates,true);

//--- 3) declare buffer to collect calculation
   double average[];
   ArrayResize(average,bars); //resize average buffer on bars size on screen

//--- 4) Calculate average
   int i=0;
   for(i=bars-1-Periode;i>-1;i--)
      {
      average[i]=(rates[i+Periode].high+rates[i+Periode].low+rates[i+Periode].close+rates[i+Periode].open)/4;
      Buffer[i]=average[i];
      //the values returned from the average array are correct...
      //...but if I set the values returned from the average array...
      //...the buffer shows a straight line
      if(i<=5)Print("i ",i,", average ",(string)average[i]);
      }
//---
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

            
 
Please insert the code correctly.
 
Karputov Vladimir:
Please insert the code correctly.
Here you are. It is indeed easier to read like this.
 
  1. This indicator and need to access the rates_total and prev_calculated.
  2. The indicator should not be such functions: 
    int bars=Bars(symbol,tf);     //number of bars on screen

 
Karputov Vladimir:
  1. This indicator and need to access the rates_total and prev_calculated.
  2. The indicator should not be such functions: 

Ok, thanks for having checked. I'll try to rewrite the code with previous_calculated and rates_total. May I come back to you if still don't succeed to get what I want ? I'm sucking on it for days although the values collected are correct.
 
steve.vanlaer:
Ok, thanks for having checked. I'll try to rewrite the code with previous_calculated and rates_total. May I come back to you if still don't succeed to get what I want ? I'm sucking on it for days although the values collected are correct.
Of course you can.
 

Indicator:

//+------------------------------------------------------------------+
//|                                                 Sep_Window_2.mq5 |
//|                                                   Steve Van Laer |
//|                                      http://www.stevevanlaer.com |
//+------------------------------------------------------------------+
#property copyright "Steve Van Laer"
#property link      "http://www.stevevanlaer.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Close
#property indicator_label1  "Close"          //Buffer for close prices
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Indicator
#property indicator_label2  "Indicator"      //Buffer for calculated values
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int NumBars=14;                        //Number of bars to go backwards

//--- indicator buffers
double         CloseBuffer[];
double         IndicatorBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,CloseBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,IndicatorBuffer,INDICATOR_DATA);
//---- sets drawing line empty value--
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--- 1) Check if enough bars
   if(rates_total<NumBars+1)
      return(0);            //exit program if not enough bars
//--- 2) First loop or new history
   int limit=-1;
   if(prev_calculated==0)
     {
      limit=0;//rates_total-1-NumBars;
      for(int j=limit;j<NumBars;j++)
        {
         CloseBuffer[j]=0.0;
         IndicatorBuffer[j]=0.0;
        }
      for(int i=NumBars;i<rates_total;i++)
        {
         CloseBuffer[i]=close[i];
         IndicatorBuffer[i]=(high[i-NumBars]+low[i-NumBars]+open[i-NumBars]+close[i-NumBars])/4;
        }
     }
   else
     {
      limit=prev_calculated-1;
      //--- 3) loop
      for(int i=limit;i<rates_total;i++)
        {
         CloseBuffer[i]=close[i];
         IndicatorBuffer[i]=(high[i-NumBars]+low[i-NumBars]+open[i-NumBars]+close[i-NumBars])/4;
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Picture:

MetaTrader Trading Platform Screenshots

EURUSD, H1, 2015.11.12

MetaQuotes Software Corp., MetaTrader 5, Demo

Sep_Window_2.mq5

EURUSD, H1, 2015.11.12, MetaQuotes Software Corp., MetaTrader 5, Demo


Files:
 
Karputov Vladimir:

Indicator:

Picture:


Thanks a lot, Vladimir. I really appreciate the time you spend on it. I will study the code to fully understand what I did wrong and how it works.

Since I'm a beginner in programing, I guess I will not be able to help you on this matter. But, if you need any information I could answer, about english for example, don't hesitate to contact me and I'll do the most I can to help you.

Thank you once more
 
steve.vanlaer:
Thanks a lot, Vladimir. I really appreciate the time you spend on it. I will study the code to fully understand what I did wrong and how it works.

Since I'm a beginner in programing, I guess I will not be able to help you on this matter. But, if you need any information I could answer, about english for example, don't hesitate to contact me and I'll do the most I can to help you.

Thank you once more
You can ask (in the Forum) on each line of the code-I am happy to answer