Application unresponsive during backtest after adding an Indicator that uses Chart.DrawTrendLine()
Application unresponsive during backtest after adding an Indicator that uses Chart.DrawTrendLine()
20 Jun 2024, 08:52
cTrader Desktop for Windows v5.0.25
Steps to reproduce…
Run a large-ish backtest (small number of bars don't present this problem), for example:
Sample cBot Reference SMA with default params
EURUSD m1
Backtest range = 1/May/2024 - 16/June/2024
All fine so far
Add the Test indicator (which I've included below)
Re-run the same backtest, still all fine
Change the indicator and set ShowLines = true
Now every time you re-run the same backtest with ShowLines enabled, the application freezes for several seconds, but eventually comes back to life.
With ShowLines = false, the problem never occurs, so it appears to be related to a high-ish number of backtest bars in combination with Chart.DrawTrendLine()
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)] public class Test : Indicator { [Parameter] public bool ShowLines { get; set; } [Output("Result", LineColor = "Yellow", PlotType = PlotType.Points, Thickness = 3)] public IndicatorDataSeries Result { get; set; } public override void Calculate(int index) { double result = double.NaN; if (Bars.LowPrices[index] > Bars.HighPrices[index - 2]) { result = (Bars.LowPrices[index]+ Bars.HighPrices[index - 2]) * 0.5; if (ShowLines) { Chart.DrawTrendLine($"FVG_U{index}", index - 2, Bars.LowPrices[index], index, Bars.LowPrices[index], Result.Line.Color, 1, LineStyle.Dots); Chart.DrawTrendLine($"FVG_L{index}", index - 2, Bars.HighPrices[index - 2], index, Bars.HighPrices[index - 2], Result.Line.Color, 1, LineStyle.Dots); } } else if (Bars.HighPrices[index] < Bars.LowPrices[index - 2]) { result = (Bars.LowPrices[index - 2] + Bars.HighPrices[index]) * 0.5; if (ShowLines) { Chart.DrawTrendLine($"FVG_U{index}", index - 2, Bars.LowPrices[index - 2], index, Bars.LowPrices[index - 2], Result.Line.Color, 1, LineStyle.Dots); Chart.DrawTrendLine($"FVG_L{index}", index - 2, Bars.HighPrices[index], index, Bars.HighPrices[index], Result.Line.Color, 1, LineStyle.Dots); } } Result[index - 1] = result; } }
Replies
PanagiotisCharalampous
11 Jul 2024, 06:32
Hi all,
Drawing lines on the chart is an resource intensive operation. If you intent to use it heavily, you should expect performance issues.
Best regards,
Panagiotis
@PanagiotisCharalampous
firemyst
10 Jul 2024, 05:57
cTrader doesn't appear to be well architected at the moment in how it handles drawings.
The only way I've been able to find a way around this issue is I have to put a parameter in any indicator that draws anything, to limit the number of drawings it does.
So if for that value I put 200, it'll draw a line or rectangle for the last 200 bars. As soon as a new bar is started, it removes the drawings from 201 bars ago if you see what I mean.
@firemyst