It's not ideal to make copybuffer return 0 if an error is encountered, it can be destructive because it can stop the indicator calculating...return rates_total, or just print an error message
You should not copy rates_total - prev_calculated, it will miss incoming data.
adjust it to this:
int to_copy; if(prev_calculated > rates_total || prev_calculated < 0) { to_copy = rates_total; } else { to_copy = rates_total - prev_calculated + 1; } //--- Copy MA values if(CopyBuffer(ma1_handle, 0, 0, to_copy, ExtMapBuffer1) <= 0) return(rates_total); if(CopyBuffer(ma2_handle, 0, 0, to_copy, ExtMapBuffer2) <= 0) return(rates_total);
check this blog post so you understand:

Indicator calculation loops and the start index
- www.mql5.com
When an indicator first loads, prev_calculated is 0 on the first execution. On subsequent calls, prev_calculated holds the number of bars processed in the previous execution. So when the indicator has
Hi Conor
Thank you for your assistance. I have implemented some updates, but the issue persists. Could you please provide further guidance or insights to help resolve this matter?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[]) { if(rates_total < MathMax(ma_1, ma_2)) return(0); //--- Handle data copy int to_copy; if(prev_calculated > rates_total || prev_calculated < 0) to_copy = rates_total; else to_copy = rates_total - prev_calculated + 1; //--- Copy MA values if(CopyBuffer(ma1_handle, 0, 0, to_copy, ExtMapBuffer1) <= 0 || CopyBuffer(ma2_handle, 0, 0, to_copy, ExtMapBuffer2) <= 0) { return(prev_calculated); } //--- Calculate positions int start_pos = (prev_calculated < 1) ? 0 : prev_calculated - 1; //--- Calculate MACD values for(int i = start_pos; i < rates_total && !IsStopped(); i++) MACD_Map[i] = ExtMapBuffer1[i] - ExtMapBuffer2[i]; //--- Calculate signal lines using proper MQL5 moving averages CalculateMA(s_method, signal, MACD_Map, SignalMap, rates_total, prev_calculated); CalculateMA(s_method2, signal2, MACD_Map, SignalMap2, rates_total, prev_calculated); //--- Update output buffers for(int i = start_pos; i < rates_total && !IsStopped(); i++) { SignalLine[i] = ExtMapBuffer2[i] + SignalMap[i]; SignalLine2[i] = ExtMapBuffer2[i] + SignalMap2[i]; } return(rates_total); } //+------------------------------------------------------------------+ //| Custom MA calculation function | //+------------------------------------------------------------------+ void CalculateMA(ENUM_MA_METHOD ma_method, int period, double &src[], double &dst[], int rates_total, int prev_calculated) { int start_pos = (prev_calculated < 1) ? 0 : prev_calculated - 1; switch(ma_method) { case MODE_SMA: SimpleMAOnBuffer(rates_total, prev_calculated, start_pos, period, src, dst); break; case MODE_EMA: ExponentialMAOnBuffer(rates_total, prev_calculated, start_pos, period, src, dst); break; case MODE_SMMA: SmoothedMAOnBuffer(rates_total, prev_calculated, start_pos, period, src, dst); break; case MODE_LWMA: LinearWeightedMAOnBuffer(rates_total, prev_calculated, start_pos, period, src, dst); break; } }

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
Hello everyone,
I'm currently facing an issue with my chart script. Although there are no errors in the code, the singleline2 is not appearing on the chart. Additionally, the MA2 Histogram is extending across the entire chart(attached), which is not the intended behavior.
I would appreciate any insights or suggestions on how to resolve these issues. Thank you in advance for your help!
Best regards,