Attachments forums

Re: AI reverse engineering to mq5 code

chris006, Mon Nov 30, 2026 5:11 am

Asked Claude to check for performance issues at high volume and accepted its suggestions for changes in a new version.

Referring to RSI_averages_Bollinger_bands_mtf_arrows_MQL5.mq5, please check the code of this mq5 indicator for performance issues. Also, could this indicator not work correctly or cause the terminal to freeze at high volume instances?

I reviewed the code by reading it. I haven't compiled or run it, so none of this is measured.

Verdict: under normal use it should not freeze. The main per-tick work is incremental. The freeze risk comes from very long histories, the object drawing, and alerts.

What is fine

The per-tick path only recalculates the last 1–2 target bars, plus the chart bars covered by them when TimeFrame is higher than the chart.
Every window loop (SMA, LWMA, Cuttler RSI, standard deviation) is bounded by its period, and the RSX uses a fixed number of steps per bar. Nothing is O(n²) in the calculation.
The calls that run on every tick (the 17 ArrayResize calls, the binary searches) are cheap.
Arrows and lines are drawn only for the last 3 target bars per tick (the last 3 chart bars on a same-timeframe chart).

Performance issues, most serious first

Object count. With arrows or lines on, each signal is a chart object, and a full recalculation creates all of them. On an M1 chart with 100k or more bars that can be thousands or tens of thousands of objects, and the terminal gets slow and sticky well before it truly hangs. The MQ4 original had the same weakness. A "max bars for arrows/lines" input would fix this, for example the last 2000 target bars.
Full rebuilds repeat. The whole series and every object are rebuilt on timeframe change, history back-fill and parameter changes, and whenever the higher timeframe's history grows by more than 50 bars. While a higher timeframe is still downloading, that can mean several full rebuilds in a row. Each rebuild deletes and recreates every object.
Slow object deletion. DeleteByPrefix loops over ObjectName(...) and builds a string for every object on the chart. ObjectsDeleteAll(0, prefix) is much faster, and the code runs it at every full rebuild and in OnDeinit.
Memory. Per bar the indicator holds about 22 arrays on the target timeframe, 11 chart buffers, and 12 RSX state doubles. That is roughly 350 bytes per bar, so 1M bars is about 350 MB and 5M bars is nearly 2 GB. The 12 RSX doubles are allocated even when the RSI type isn't RSX, and the full RSX state is only needed for the last two bars.
Per-tick churn. The last 3 bars' arrows and lines are deleted and recreated on every tick, even when nothing changed. That causes needless redraws. They should only be touched when the signal changes.
Data not ready. If CopyRates on the higher timeframe fails, the next tick starts again from scratch (prev becomes 0 and g_n is reset). A long-history copy could then be retried on every tick until the data is ready.

Behaviour risks at high volume

SendMail is synchronous. A misconfigured or slow mail server stalls the indicator thread, and with it every chart on that symbol. It is off by default, but this is the most likely real freeze.
Alert storms. The duplicate check on a bar compares the message text, so a RSX line flickering across the middle band within one bar alerts on every flip. alertsOnCurrent is on by default, so that is the likely case. The same check resets when the indicator reloads, so a timeframe or parameter change can repeat the last alert.
Mixed-type expression. (arrowsOnNewest ? (datetime)chartShift : 0) mixes a datetime and an int. It should compile, but I'd make it explicit.

I suggest these changes, all in the new file:

Add a max-bars-for-objects input.
Use ObjectsDeleteAll for deletion.
Only touch an object when its signal changes.
Allocate RSX state only for the RSX type, and only for the last two bars.
Add a guard so a failed history copy doesn't retry the full copy on every tick.
Throttle alerts to one per bar per event.

Want me to apply them? I would write the result as a new file.

Please go ahead as suggested and implement all suggestions in a new file / version

I applied all six suggestions and saved the result as a new file, RSI_averages_Bollinger_bands_mtf_arrows_1.20_MQL5.mq5, so the previous file is untouched. It is not compiled or tested here, so please send me any compile errors.

Object limit: there is a new input MaxBarsObjects, default 2000. Arrows and lines are only drawn for the last N bars of the chosen time frame, and 0 means all of them. It sits right before Interpolate.
Faster deletion: object cleanup now uses ObjectsDeleteAll with the prefix instead of looping over every object name.
Less per-tick churn: the last bars' arrows and lines are now only created, moved, recoloured or deleted when something changed. An arrow follows its bar's high or low while the bar is forming.
Memory:
The RSX state now holds only the last two bars.
The helper arrays for the standard and Wilder RSI types, and for the Heiken Ashi prices, are only allocated when you select them.
Arrays grow with a reserve, so there are far fewer reallocations.
Failed copy retry: after a failed higher-time-frame copy, the full copy is retried at most every 2 seconds.
Alerts:
Each event type alerts at most once per bar. A cross that flips back and forth within one bar no longer repeats, but a flip back inside the same bar is not alerted.
Nothing is alerted for the state that already exists when the indicator loads or reloads, so a timeframe or parameter change no longer re-fires the last alert.
E-mails are limited to one per 30 seconds. SendMail is still synchronous and I can't change that from inside the indicator, so leave alertsEmail off unless your mail setup is reliable.

I also made the arrowsOnNewest time shift an explicit datetime cast.

Files are in /mnt/user-data/outputs:

RSI_averages_Bollinger_bands_mtf_arrows_1.20_MQL5.mq5
RSI averages Bollinger bands mtf arrows 1.20 MQL5
MQ5

Putting the last file on chart creates a subwindow but there is nothing shown in it

I found one bug that could explain the empty subwindow and fixed it in RSI_averages_Bollinger_bands_mtf_arrows_1.21_MQL5.mq5. I can't compile or run MT5 here, so I haven't confirmed that this is what blanked your chart.

The bug is in the retry guard I added in v1.20. It compared (int)(GetTickCount() - g_retryAt) < 0 with g_retryAt starting at 0. When the PC has been up for more than about 24.8 days, GetTickCount() is above 2³¹, so the cast goes negative. The indicator then thinks a retry is still pending and never calculates anything. A subwindow is created but stays empty, which matches what you saw. The guard now only applies after an actual failed copy, and a successful copy clears it.

If the window is still empty with v1.21, please check these and send me what you find:

the Experts tab for messages from the indicator
whether TimeFrame is set to a timeframe above the chart's, since its history may still be downloading
whether the older v1.10 file draws on the same chart

Files are in /mnt/user-data/outputs:

RSI_averages_Bollinger_bands_mtf_arrows_1.21_MQL5.mq5

OK, now the indicator shows

Good, that confirms the retry-guard bug was the cause. v1.21 is the version to keep, and v1.20 should be deleted because it has that bug.

If you try the arrows and lines, check these:

Switches: arrowsVisible and verticalLinesVisible are off by default.
Arrow codes and colours: the defaults (233/234, blue and yellow) are guesses from your screenshots, so adjust them if they don't match.
Object limit: MaxBarsObjects is 2000 by default. Set it to 0 if you want signals across the whole history.

Send me any compile warnings or odd behaviour you find, such as arrows on the wrong bar or alerts that don't match the arrows.
All files in topic