Alright so I've been working on making this indicator which is very similar to fractals and it seems to draw all the arrows correctly when it goes trough the chart the first time but as soon as it draws new ones the positions of the arrows are wrong.
I absolutely have no clue what I'm doing wrong so any help would be appreciated.
I feel like it should have something to do with the last arrow position but I wouldn't know what's wrong with it or even if it's really that or not.
Any help would be appreciated.
Thanks!
I absolutely have no clue what I'm doing wrong so any help would be appreciated.
I feel like it should have something to do with the last arrow position but I wouldn't know what's wrong with it or even if it's really that or not.
Inserted Code
//+------------------------------------------------------------------+ //| FractalsExercise.mq4 | //| AntiVi | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "AntiVi" #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 2 double Highs[]; double Lows[]; double Stored = 0; int LastArrow = 0; //1 = high --- 2 = Low int LastArrowPosition = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- drawing style SetIndexStyle(0, DRAW_ARROW, EMPTY, 1, clrGreen); SetIndexArrow(0, 226); SetIndexStyle(1, DRAW_ARROW, EMPTY, 1, clrRed); SetIndexArrow(1, 225); //--- indicator buffers SetIndexBuffer(0, Highs); SetIndexBuffer(1, Lows); //--- 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 limit = rates_total - prev_calculated - 2; limit = limit < 1 ? 2 : limit; for(int i = limit; i > 1; i--) //Loop from back to front { if(rates_total > prev_calculated) //Checking for new closed candles { if(High[i] >= High[i + 1] && High[i] >= High[i - 1] && Low[i] <= Low[i + 1] && Low[i] <= Low[i - 1]) //Checking for double arrows { Print("---DOUBLE ARROW IGNORED---", i); } else { if(High[i] >= High[i + 1] && High[i] >= High[i - 1]) //Checking for highs { if(LastArrow == 2 || LastArrow == 0) //Is last arrow a low { Lows[LastArrowPosition] = Stored; //Draws arrow LastArrow = 1; //Last arrow = high Stored = High[i]; LastArrowPosition = i; } else if(LastArrow == 1) //Is last arrow a high { if(Stored < High[i]) { Stored = High[i]; LastArrowPosition = i; } else if(Stored >= High[i]) { Stored = Stored; LastArrowPosition = LastArrowPosition; } } } else if(Low[i] <= Low[i + 1] && Low[i] <= Low[i - 1]) //Checking for lows { if(LastArrow == 1 || LastArrow == 0) //Is last arrow a high { Highs[LastArrowPosition] = Stored; //Draws arrow LastArrow = 2; //Last arrow = low Stored = Low[i]; LastArrowPosition = i; } else if(LastArrow == 2) //Is last arrow a low { if(Stored > Low[i]) { Stored = Low[i]; LastArrowPosition = i; } else if(Stored <= Low[i]) { Stored = Stored; LastArrowPosition = LastArrowPosition; } } } } } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
Any help would be appreciated.
Thanks!