Hi
i ve got the following code that is trying to make an MA by calculating Close-Low of the current bar. My main problem is that:
When i first place the indicator it is drawn fine. But after it it starts drawing a straight line... cant really figure out why.
I have located that the error is cause of the two while loops i ve got and it has to do with the candles variable. If i ll replace the candles variable in the while loops with the history variable then it works fine but by doing that it will keep repainting in every tick all the bars again and again.
Any ideas/advise?
Here is the code
i ve got the following code that is trying to make an MA by calculating Close-Low of the current bar. My main problem is that:
When i first place the indicator it is drawn fine. But after it it starts drawing a straight line... cant really figure out why.
I have located that the error is cause of the two while loops i ve got and it has to do with the candles variable. If i ll replace the candles variable in the while loops with the history variable then it works fine but by doing that it will keep repainting in every tick all the bars again and again.
Any ideas/advise?
Here is the code
Inserted Code
#include <stderror.mqh> #include <stdlib.mqh> #property indicator_level1 0 #property indicator_separate_window //#property indicator_chart_window // Indicator is drawn in the main window #property indicator_buffers 2 #property indicator_color1 White //#property indicator_color2 Blue //--- buffers double Buf_0[]; // --- parameters extern int history=3000; extern bool log_on=false; extern int MAperiod=14; //extern bool separate_window=true; string build=" ver 0.29g-TEST-MA"; bool isHistoryLoading; int lastperiod=0; int init() { isHistoryLoading = true; SetIndexStyle(0,DRAW_LINE,DRAW_SECTION,2); SetIndexBuffer(0,Buf_0); SetIndexLabel (0,"MA"); IndicatorShortName("MA ("+MAperiod+") "+build); SetLevelValue (0, 0.0000); // The horizontal line level is set return(0); } int deinit() { return(0); } int start() { double ARM=0 , barARM=0, ASB=0; double O,C,H,L; datetime dt,mdt,dt1,dtTemp; int i, j, k, candles, counted_bars; //int btbars=backtest_bars; double Buffer[10000]; if (isHistoryLoading){ dt=iTime(Symbol(),PERIOD_M1,0); if (dt == 0){ isHistoryLoading = true; return;} isHistoryLoading = false; counted_bars = 0; } else counted_bars = IndicatorCounted(); if (candles<0) return (0); candles=Bars-counted_bars; // Index of the first uncounted if (candles>history-1) // If too many bars .. { candles=history-1; // ..calculate specified amount } //********************************* MAIN WHILE LOOP **************************** while ( (i<=candles)) { L=Low[i];C=Close[i]; ARM=ARM+C-L; wlog("i change i["+i+"]"); Buffer[i]= ARM; //Draw indicator on separate window wlog("Buffer["+i+"]="+Buf_0[i]); i++; ARM=0; } // END Year int vi=0; double sum=0.0; while ( vi<=candles) //CALCULATE MA { vi++; for (int vk=vi; vk<vi+MAperiod; vk++) { sum=sum+Buffer[vk]; } //for sum=sum / MAperiod; Buf_0[vi]= sum;//; sum=0; }//while return(0); } void wlog(string s) { if (log_on) { Print(s); } }