Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Safe Logger: why your log lines vanish while FileOpen keeps returning success - library for MetaTrader 5

Joao Prata Silva Yglesias
Joao Prata Silva Yglesias
Trader and algorithmic developer focused on risk-managed automation for the Forex market. I build Expert Advisors that prioritize capital protection and survivability over hype — real strategies, transparent results, and live monitoring.
Views:
54
Published:
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

SafeLogger.mqh appends one line to a file shared by several EAs without losing lines. It opens the file exclusively (no FILE_SHARE_WRITE), retries up to 8 times with 40 ms between attempts (320 ms of patience) while another EA holds the file, and prints a loud error with the lost line when the retries run out.

Functions: SafeLogWrite(file, fields) prepends the TimeLocal() timestamp and the symbol and appends time;symbol;fields ; SafeLogHeader(file, header) writes the header once if the file does not exist yet.

Demo inputs: InpFile = "demo_log.csv", InpLines = 200, InpSafeMode = true (false reproduces the wrong way, with FILE_SHARE_WRITE).

The demo prints the mode, the calls that returned success, the line count before and after, the time taken, and >>> N LINE(S) LOST with a successful FileOpen when lines went missing.

Limit: the loss only shows with concurrent writers. Drag the script onto four charts at once with InpSafeMode = false to see it; on a single chart there is no contention and both modes give the same count.

Safe Logger demo: safe mode on one chart
Clock Diagnostic: TimeCurrent() freezes and steps backwards Clock Diagnostic: TimeCurrent() freezes and steps backwards

TimeCurrent() is not a clock. It is the stamp of the LAST TICK. Two consequences break robots in production: 1. It freezes. With no tick it does not move: illiquid instrument, end of session, unstable connection - and any rule based on it stops with it. 2. It steps backwards: on a symbol switch, a reconnection or a tick from another instrument, the value can go back. The case that cost me a whole protection: I compared the date of a daily decision with TimeCurrent() to reject an expired one. The server clock stepped back to the previous day, the comparison matched, and four EAs accepted YESTERDAY's decision as valid. The gate that should have failed closed failed open - without a single error in the log. Rule: TimeLocal() for timestamps, dates, day changes, expiry - all that must always move forward; TimeCurrent() for session hours and market data. The script measures the divergence in your environment and reports both symptoms live. Run it with the market closed.

Gold Hour Profile - when gold moves, and when the spread eats it Gold Hour Profile - when gold moves, and when the spread eats it

Measures, hour by hour, how far a symbol travels and how much of that the spread takes away. Built for gold, works on any symbol, compares several side by side.

Contract Sizer: ladder, floor and a breaker that survives a deposit Contract Sizer: ladder, floor and a breaker that survives a deposit

Three position-sizing protections that do different things; confusing them is why so many accounts get wiped out: - ladder: one contract per X of balance, always applied as a CAP, even with manual lot sizing; - floor: below the minimum capital it does not trade; a new deposit is needed; - breaker: stops at X% below the peak, at any account size, and does not rearm by itself. What this library solves and almost none does: a deposit is not profit, and a withdrawal is not a loss. The breaker measures the drop against the balance peak. Untreated, a deposit made DURING a drawdown lifts balance and peak together, and the protection stops seeing the drop exactly when it would help. Here deposits and withdrawals shift the peak by the same amount. The peak is persisted to a file: a breaker that forgets the peak on a terminal restart is not a breaker. The demo simulates a deposit at the bottom of a drawdown. Run it with the deposit on and off and compare the "drop" column.

Extract information from the MT5 broker Extract information from the MT5 broker

This MT5 EA is a diagnostic tool that does not execute trades, but collects and displays technical specifications relating to the broker and the trading environment. The code is structured into four main blocks: Data Collection (CollectAll): Retrieves information on the account (leverage, currency), the symbol (digits, spread, swap, volumes, stop levels), latency (ping) and time zone (Server-UTC difference). Calculates the swap in real currency. Sampling (OnTick): Updates spread statistics (minimum, average, maximum) in real time. Output: Prints the data to the ‘Experts’ log, saves it to a CSV file and displays it on a graphical panel on the chart (UpdatePanel). Helper: Functions such as CalcSwapInCurrency and various *ToStr functions convert the broker’s raw numerical data into readable strings.