Question from a newbie in MQL5 : How to draw a buffer line with calculated values from previous bars ?
- This indicator and need to access the rates_total and prev_calculated.
- The indicator should not be such functions:
int bars=Bars(symbol,tf); //number of bars on screen
Karputov Vladimir:
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.
- This indicator and need to access the rates_total and prev_calculated.
- The indicator should not be such functions:
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.
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.
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
MetaQuotes Software Corp., MetaTrader 5, Demo
Sep_Window_2.mq5

Files:
Sep_Window_2.mq5
4 kb
Karputov Vladimir:
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.Indicator:
Picture:
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
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
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 :