You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Your are building your indicator buffers in the wrong direction :
Your "previous" is from the future.
I changed the code using i-- in the loop.
It seems to return correct values, but now the problem is that it does not display past values. It only works on prices received after I load the indicator.
Is there a problem with the loop, or the limit maybe?
#property strict #property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 White #property indicator_width1 3 #property indicator_style1 3 #property indicator_color2 Magenta #property indicator_width2 2 #property indicator_style2 3 //------------------------------------------------------------------- //--- indicator buffers double HiDir[], LoDir[]; int DirHigh_0 = 0, DirHigh_1 = 0, DirLow_0, DirLow_1; double H1, H0, H2, L0, L1, L2; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,HiDir); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexBuffer(1,LoDir); SetIndexStyle(1,DRAW_HISTOGRAM); //--- 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 counted_bars=IndicatorCounted(), limit; if(counted_bars>0) counted_bars--; limit=Bars-counted_bars; for(int i=limit-2; i==0 ;i--) { H0 = High[i]; H1 = High[i+1]; H2 = High[i+2]; L0 = Low[i]; L1 = Low[i+1]; L2 = High[i+2]; if (H0>H1) DirHigh_0 = 1; else if (H0<H1) DirHigh_0 = -1; else DirHigh_0 = 0; if (H1>H2) DirHigh_1 = 1; else if (H1<H2) DirHigh_1 = -1; else DirHigh_1 = 0; if(H0>H1) HiDir[i] = 1; else if (H0<H1) HiDir[i] = -1; if (DirHigh_0 == 0 && DirHigh_1 != 0) {HiDir[i] = DirHigh_1; DirHigh_0 = DirHigh_1;} if (L0>L1) DirLow_0 = 1; else if (L0<L1) DirLow_0 = -1; else DirLow_0 = 0; if (L1>L2) DirLow_1 = 1; else if (L1<L2) DirLow_1 = -1; else DirLow_1 = 0; if(L0>L1) LoDir[i] = 1; else if (L0<L1) LoDir[i] = -1; if (DirLow_0 == 0 && DirLow_1 != 0) {LoDir[i] = DirLow_1; DirLow_0 = DirLow_1;} } //--- return value of prev_calculated for next call return(rates_total); }
You are right. It referred to the future. I didn't notice. Thank you!
I changed the code using i-- in the loop.
It seems to return correct values, but now the problem is that it does not display past values. It only works on prices received after I load the indicator.
Is there a problem with the loop, or the limit maybe?
Of course, don't use IndicatorCounted() it's obsolete, or at least use it correctly. Also your loop condition i==0 isn't correct.
for(int i=limit; i>=0 ;i--)
With rates_total-2 it displays nothing.
I don't really understand it. I must return to the basics of MQL4 I think.
Thank you very much.
Your suggested code works fine, with rates_total-3.
With rates_total-2 it displays nothing.
I don't really understand it. I must return to the basics of MQL4 I think.
Thank you very much.
L2 = High[i+2];
Which I didn't notice previously.
-3 because you are using this
Which I didn't notice previously.
-3 because you are using this
Which I didn't notice previously.