Hello everyone,
I have an extremely simple mq4 indicator that displays the upticks and downticks in the buffers for each candle. I am not competent with MQL5 and consequently have failed at converting the indicator into an MQ5 file. I would appreciate a clear explanation and solution as to what I am doing wrong.
Many thanks.
MQ4 Indicator code:
My failed MQL5 code:
Here you go , i printed the buffers for debugging
#property version "1.00" #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 2 //--- plot upticks #property indicator_label1 "upticks" #property indicator_type1 DRAW_LINE #property indicator_color1 clrYellowGreen #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot downticks #property indicator_label2 "downticks" #property indicator_type2 DRAW_LINE #property indicator_color2 clrFuchsia #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- indicator buffers double UpTicks[]; double DownTicks[]; double dXecn = 1; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,UpTicks,INDICATOR_DATA); SetIndexBuffer(1,DownTicks,INDICATOR_DATA); if(_Digits==3||_Digits==5){ dXecn=10; } //--- 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[]) { //--- int from=MathMax(0,prev_calculated-1); for(int i=from;i<rates_total;i++) { UpTicks[i]=(((double)tick_volume[i])+(close[i]-open[i])/_Point/dXecn)/2; DownTicks[i]=((double)tick_volume[i])-UpTicks[i]; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
Hey Lorentzos,
Thank you so much, it works perfectly. Forgive me for my stupid question but is variable 'from' the same as the lookback? Why do you subtract 1 from prev_calculated?
All the best and many thanks for your help!
Hey Lorentzos,
Thank you so much, it works perfectly. Forgive me for my stupid question but is variable 'from' the same as the lookback? Why do you subtract 1 from prev_calculated?
All the best and many thanks for your help!
I confused myself too in the beggining but here is what happens .
prev_calculated counts how many bars have been processed from the sum of rates_total.
But if we request i=prev_calculated in the for loop we have a problem , that we wont be accessing the live bar .
So if rates_total = 20 , (so we have 20 bars) and prev calculated is 0 , the first time it will execute from 0 to 19 , which is fine
But then , with prev calculated 20 and rates total 20 it will try to go from 20 to 19 so it wont execute anything and it will perform a calculation when
rates total becomes 21 but it will have skipped one bar .
So the solution is to call from prev calculated - 1 , but then we have the issue that we cannot start from -1 when prev calculated is 0 (0-1=-1)
So we request the maximum from zero or prev calculated - 1, which handles all cases.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone,
I have an extremely simple mq4 indicator that displays the upticks and downticks in the buffers for each candle. I am not competent with MQL5 and consequently have failed at converting the indicator into an MQ5 file. I would appreciate a clear explanation and solution as to what I am doing wrong.
Many thanks.
MQ4 Indicator code:
My failed MQL5 code: