AI reverse engineering to mq5 code

1
I am blown away with what AI can do:

If I remember correctly mrtools has lost the code for RSI - averages + Bollinger bands (lines + alerts + arrows + mtf).ex4 (failed drive).

I have asked Claude the following:

See attached. This is an mt4 indicator. I would like an mt5 port. Or is it better to produce an mq4 port in order to compare visual consistency and then convert that to mq5? For the moment I am not interested in alerts, vertical lines and arrows if it would help to simplify things.


Re: AI reverse engineering to mq5 code

2
Reply from Claude after approximately 5 minutes (attached screenshot is from the mq5 generated indicator):

I've written the MT5 version from your screenshots, with no MQ4 source for this variant. It's saved as RSI_averages_Bollinger_bands_mtf_MQL5.mq5. It is uncompiled and untested, so please compile it and send me any errors.

Port straight to MT5. I'd skip the MQ4 step. There's no source to port from, and I can't compile or run either language here. The Data Window screenshots are the only reference, and they work equally well against an MT5 build. After you compile it, put the same bars side by side. Row values for the first four plots are the quickest comparison.

What the screenshots showed

Plots: Plots 1 to 3 are the upper, middle and lower bands, and plot 4 is the RSX line in goldenrod. Plots 5 to 10 are the shadow and the up and down colour segments.
Calculation: I matched them to the 1.03 file you sent earlier and to your Data Window numbers. It averages the price, runs the RSX on that average, then draws bands around the RSX.
Band check: Your readings fit this: the middle band is exactly the midpoint of the upper and lower bands, and all four values read 100 when the RSX is pinned.
Colours: The line turns lime above the upper band and red below the lower band. Otherwise it stays goldenrod.
Levels: The levels are 25 and 75, dotted.

Left out, as you said: alerts, vertical lines and arrows. The time frame and interpolate inputs work.

Guesses I can't confirm

RSX formula: I used Jurik RSX with the usual smoothing constant. If your Data Window values differ slightly from mine, this is the first thing I'd check.
Other RSI types: The other three types (Regular, Wilder's, Cuttler's) are my reconstruction. Your screenshots only use RSX.
Menu texts: The drop-down texts for the average methods and the price list are my guesses.
Shadow colour: I added a ShadowColor input at the end, defaulting to black as in your Colors tab. The original file may fix it in code.

If values differ from the original, send me a Data Window screenshot of the same bar from both versions and I'll adjust.
These users thanked the author chris006 for the post:
Banzai

Re: AI reverse engineering to mq5 code

4
Taking this one step further in order to implement arrows, vertical lines and alerts:

I uploaded 25 screenshots with the data window showing arrows and vertical lines in mt4.

Taking as a base RSI_averages_Bollinger_bands_mtf_MQL5.mq5 that you have written yesterday let's try and implement the arrows, vertical lines and alerts. As I understand it the conditions for arrows and vertical lines are identical. Please verify this from the screenshots I attach here . The conditions for the alerts should be the same as well. The attached inputs screenshot applies / relates to all other screenshots I am attaching now.

About 5 minutes of wait....

Still working on the arrows/lines/alerts. Screenshots so far: 6 of 7 arrow/line pairs sit on the same bar, but one blue pair is a bar apart and the newest bar has a line with no arrow. I'm now reading the sibling MQ4's logic to settle the exact conditions, then I'll build the new version.

I've added arrows, vertical lines and alerts to your base file and saved it as a new file, RSI_averages_Bollinger_bands_mtf_arrows_MQL5.mq5. It is not compiled or tested here, so please send me any compile errors.

The arrow and line conditions are not identical, going by your screenshots and the MQ4 source. In the batches I checked (the 2000px view and the pixel analysis of the arrows30 shot), 6 of the 7 arrow/line pairs sit on the same bar. The seventh pair is the exception. The blue arrow at about x=320 is one bar earlier than its blue line at about x=331. The newest bar also has a blue line with no arrow. I haven't seen arrows08–10 and 23–29.

The MQ4 explains the difference:

Arrows: the RSX line crossing the middle band. A cross to up gives a blue up arrow below the low, and a cross to down gives a yellow down arrow above the high.
Vertical lines: a trend change, meaning the RSX crossing the upper or lower band. Blue is up and red is down. Going back inside the bands draws nothing.
Why they mostly coincide: when the RSX moves steeply it crosses the middle and outer bands on the same bar. On a slow move the arrow comes first, as at x=320.
Alerts: these follow the MQ4, with "middle band cross" and "outer bands cross" as separate switches. They use the same two conditions, one alert per bar and event, on the current bar if alertsOnCurrent is on.

I implemented it that way rather than forcing the two conditions to be the same. If you want arrows and lines on one condition, tell me which one and I'll change it.

I used the MQ4 input names, so your inputs screenshot should line up, but I haven't checked it against the new file. All the new inputs sit before ShadowColor, which is still last. The defaults for line colours, arrow colours and arrow codes (233/234, size 2) are guesses from the screenshots. Arrows and lines are off by default, as in the MQ4. Turn on arrowsVisible and verticalLinesVisible to see them.

With a higher TimeFrame, signals are evaluated on that timeframe's bars. The objects sit on the chart bar that opens each one, and the arrow gap uses a 20-bar ATR of the chart timeframe.

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

RSI_averages_Bollinger_bands_mtf_arrows_MQL5.mq5

PS
The sibling Claude refers to is RSI + Bollinger bands 1.03 (lines + alerts + arrows + mtf).mq4 that also has arrows and vertical lines. This was given to Claude previously.

Alerts have not been tested.

Re: AI reverse engineering to mq5 code

5
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.