Discussing the article: "Where should your stop-loss really sit? An MAE/MFE excursion analyzer in MQL5"
Moderator warning: AI-generated comment
Bug: CSV report duplicates rows on every re-run
The g_csv file handle is opened once and never closed between calls to RunAnalysis() — only OnDeinit() closes it. Since OpenCsv() opens the file with FILE_WRITE | FILE_READ and then does FileSeek(g_csv, 0, SEEK_END) , every time the "Analyze MAE/MFE" button is clicked (or RunAnalysis() runs again for any other reason), the entire history is replayed and AppendCsv() writes every round-trip again — appended after whatever was already written in the previous run. Click the button three times and you get the same trades duplicated three times in MAE_MFE_Report.csv , with no truncation and no dedup, silently corrupting the report for anyone using the CSV downstream (Excel, Python, etc.).
Fix: close and reset the file handle at the start of every RunAnalysis() call, and open the file with plain FILE_WRITE (which truncates) instead of FILE_WRITE | FILE_READ + seek-to-end.
// In RunAnalysis(), right before HistorySelect(): if(g_csv != INVALID_HANDLE) { FileClose(g_csv); g_csv = INVALID_HANDLE; }
// In OpenCsv(): g_csv = FileOpen(InpCsvName, FILE_WRITE | FILE_SHARE_READ | FILE_CSV | FILE_ANSI, ','); if(g_csv == INVALID_HANDLE) { PrintFormat("MAE/MFE: could not open %s (error %d).", InpCsvName, GetLastError()); return; } FileWrite(g_csv, "pos_id", "side", "entry_time", "exit_time", "entry", "exit", "result", "mae_points", "mfe_points", "efficiency_pct");
With this, each "Analyze" click produces a fresh CSV covering exactly that run's trades, instead of accumulating duplicates indefinitely.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Check out the new article: Where should your stop-loss really sit? An MAE/MFE excursion analyzer in MQL5.
Stop-loss and take-profit placement is usually the least-measured decision in a trading system. This Expert Advisor reads your closed history, replays M1 price between each entry and exit to measure Maximum Adverse and Favorable Excursion per trade, and splits winners from losers. From the distributions and trade efficiency it derives data-driven stop and target levels - measured from your own account, not a rule of thumb. Analysis only; it does not trade.
Most traders still set stop-losses and take-profits by habit — a round number, a fixed risk-reward ratio, or the indicator's default — and then wonder why good entries are stopped out by noise or why winners are cut short. The answer is already in the account: past trades encode how far price actually ran for and against each entry. Extracting that information consistently is all it takes to turn guesswork into measurement.
This article presents an Expert Advisor, the MAE/MFE Excursion Analyzer, that does exactly that. Given a symbol (and an optional magic number) and a window of closed history, it reconstructs each round-trip from the deal records, reads the M1 candles between entry and exit, and reports per-trade MAE (maximum adverse excursion), MFE (maximum favorable excursion) and efficiency. From those distributions it produces concrete, strategy-specific guidance: a stop suggestion (for example, winners' p90 MAE), a target suggestion (for example, winners' median MFE), and a CSV of every trade, while making clear that this is an analysis instrument, not a trading robot and not financial advice. Note that the method requires an existing history of closed trades for the symbol or strategy you want to study.
This tool is for analysis and diagnostics. It reads history and does not place orders. It is not financial advice.
Author: Silvina Duarte