Copy only new values after the first run.
//Checking for start point of calculation int first = (prev_calculated==0) ? ATR_Range-1 : (rates_total!=prev_calculated) ? prev_calculated : prev_calculated-1;; //Getting & Calling values in Handle int toCopy = (rates_total!=prev_calculated) ? rates_total-prev_calculated : 1; if(CopyBuffer(Handle,0,0,toCopy,Value_ATRBuffer) <=0) { return(0); } //Calculatinf Basic Upper and Lower Lines for(int i=first ; i<rates_total ; i++) {
Many thanks dear. Performance improved a lot. However, still not as fast as ATR. Also certain times, miscalculations are plotted.
Thanks for your guidance.
Welcome. Calculating initial values outside the loop will only make a slight difference, so speed would depend on the custom indicator's code ("ATR_Manjit").
//Getting & Calling values in Handle int toCopy = (rates_total!=prev_calculated) ? rates_total-prev_calculated : 1; if(CopyBuffer(Handle,0,0,toCopy,Value_ATRBuffer) <=0) { return(0); } //Checking for start point of calculation int first = (rates_total!=prev_calculated) ? prev_calculated : prev_calculated-1; //Initial values if( prev_calculated==0) { first = ATR_Range-1; BUpper_LineBuffer[first] = FUpper_LineBuffer[first] = ((high[first] + low[first])/2)+(Multiplier * Value_ATRBuffer[first]); BLower_LineBuffer[first] = FLower_LineBuffer[first] = ((high[first] + low[first])/2)-(Multiplier * Value_ATRBuffer[first]); ST_LineBuffer[first] = (close[first] <= FUpper_LineBuffer[first]) ? FUpper_LineBuffer[first] : FLower_LineBuffer[first]; first++; } //Calculatinf Basic Upper and Lower Lines for(int i=first ; i<rates_total ; i++) { BUpper_LineBuffer[i] = ((high[i] + low[i])/2)+(Multiplier * Value_ATRBuffer[i]); BLower_LineBuffer[i] = ((high[i] + low[i])/2)-(Multiplier * Value_ATRBuffer[i]); //Calculating Final Upper Line if((BUpper_LineBuffer[i] < FUpper_LineBuffer[i-1])||(close[i-1] > FUpper_LineBuffer[i-1])) { FUpper_LineBuffer[i] = BUpper_LineBuffer[i]; } else { FUpper_LineBuffer[i] = FUpper_LineBuffer[i-1]; } //Calculating Final Lower Line if((BLower_LineBuffer[i] > FLower_LineBuffer[i-1])||(close[i-1] < FLower_LineBuffer[i-1])) { FLower_LineBuffer[i] = BLower_LineBuffer[i]; } else { FLower_LineBuffer[i] = FLower_LineBuffer[i-1]; }
Welcome. Calculating initial values outside the loop will only make a slight difference, so speed would depend on the custom indicator's code ("ATR_Manjit").
Many Many thanks dear. You have been very helpful and kind to spend time and support me.
After analyzing your inputs and studying my code, i observed that I was not calculating SuperTrend for very starting bar (i-1).
The code is working perfectly well after correcting.
Many many thanks sir.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi experts. Your expert guidance requested.
I am a newbie and recently learnt MQL5. This is my 2nd code i have developed.
The super trend indicator often does not load. It also vanishes when i change timeframes.
It appears when i compile the it again from Meta Editor.
Please help me to identify where am I missing.