Guarda come scaricare robot di trading gratuitamente
Ci trovi su Telegram!
Unisciti alla nostra fan page
Script interessante?
Pubblica il link!
lasciare che altri lo valutino
Ti è piaciuto lo script? Provalo nel Terminale MetaTrader 5
Librerie

Exposure Cap: one position limit per symbol across all your EAs - libreria per 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.
Visualizzazioni:
67
Pubblicato:
Freelance MQL5 Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance

Four Expert Advisors on the same symbol, each one honest on its own. Each checks "do I already have a position?" with its own magic number, sees none, and enters. Each re-enters on every new bar while already positioned, because the check was per-EA and the fleet was not. On a demo account this reached 22 contracts on a symbol meant to carry one, and an external watchdog had to close 16 positions in a single morning - paying the spread of openings that should never have happened, and filling the history with tiny trades that were nobody's decision.

Closing afterwards is the wrong layer. The cap belongs at the door: the order is refused before it exists.

What the library does. ExposureCap::Allowed(symbol, requestedLots, cap, who) sums the volume of every open position on the symbol - all magic numbers, manual trades included - and answers whether adding the requested lots would stay within the cap. On refusal it logs one line with the three numbers (held, requested, cap) and the caller's name, so the reason is in the experts log and not in anyone's memory. ExposureCap::Held(symbol) returns the gross volume held; ExposureCap::Room(symbol, cap) returns how much is left under the cap. A cap of zero or less disables the check.

How to use it. Include the file and put if(!ExposureCap::Allowed(_Symbol, lots, InpCap, "MyEA")) return; in front of every OrderSend in every EA on the account, with the same cap. The fleet stops adding to itself.

What it does not do. It is deliberately simple: gross exposure on the symbol, no netting of longs against shorts, no per-EA quota. Gross exposure is the number that breaks accounts; refinements can be layered on top. And it is a check, not a lock: two EAs deciding on the same tick can both read "0 held" and both pass. In practice EAs on different charts decide on different ticks, and one refusal per bar is what was observed - but that limit is stated here on purpose.

Demo script inputs: InpCap (1.0) - the gross cap for the chart symbol, in lots; InpRequested (1.0) - the lots a hypothetical entry would request. The script prints held / cap / room for the current symbol, shows the refusal line for a request of room + 1, and trades nothing.

DemoExposureCap: held 0.00, cap 1.00 - a request of 1.00 passes, a request of 2.00 is refused before any order exists
Server Time Offset: the three clocks in MQL5 and how timestamps shift on export Server Time Offset: the three clocks in MQL5 and how timestamps shift on export

A datetime in MQL5 is seconds since 1970 - but server time (TimeCurrent, bar times, deal times) is the broker's wall clock stored AS IF it were UTC. Export it to CSV, read it in Python or a spreadsheet with a "seconds since 1970 UTC" parser, and every timestamp shifts by the server's UTC offset. On a UTC-3 broker that is three hours: the daily bar dated 00:00 reads as 21:00 of the previous day, and "yesterday's session" silently becomes "today's, half formed". That happened to a morning report of mine before this script existed. The script prints TimeTradeServer, TimeCurrent, TimeGMT and TimeLocal, the three offsets between them, the tick lag, and this chart's last bar time in server time and in real UTC - then states the rule: export server timestamps as wall-clock text, or export the epoch together with the offset, and never let the consumer apply its own timezone.

Risk-Based Lot Size Calculator Risk-Based Lot Size Calculator

A simple MQL5 script that calculates the correct lot size for a trade based on your account risk percentage and stop loss distance in pips, so every trade risks a consistent, controlled amount of your balance.

Decision Watchdog: refuse yesterday's decision, fail closed, log once Decision Watchdog: refuse yesterday's decision, fail closed, log once

A daily process writes "today's decision" (which strategy runs, or FLAT) to a file the EAs read at the open. One day the writer did not run. The EAs read yesterday's file, compared its date with TimeCurrent() - the server clock, which had stepped back to the previous day overnight - saw a match, and traded all morning on a 24-hour-old decision. No error anywhere. Two rules, both in this class: 1) staleness is judged against TimeLocal(), which always moves forward; TimeCurrent() is the last tick's stamp - it freezes without ticks and can step back on reconnect. 2) When in doubt the answer is FLAT: missing file, bad date, wrong day, empty line - every failure path returns "do nothing", and each is logged ONCE per state change, not on every tick and not never. File format: line 1 = ISO date, line 2 = decision string. The demo writes a fresh, a stale, a malformed, an empty and a missing file, and shows that only the first is allowed to trade.

Trade Excursion: how much of each winner you actually kept (MFE/MAE from your own history) Trade Excursion: how much of each winner you actually kept (MFE/MAE from your own history)

Your statement tells you what you KEPT. It does not tell you what you HAD. This indicator reads your own closed trades from the account history and draws, for each one, the full range it travelled: the best unrealised profit it ever showed (MFE) and the worst unrealised loss it survived (MAE). The gap between what a trade showed and what it paid is where two common problems hide, and neither is visible in a balance curve: winners closing far below their best point (the exit leaves money behind), and losers whose worst excursion barely passed the stop (the stop sits inside the noise). The number to look at is the CAPTURE RATIO: of the profit a winner showed at its best, how much did you take home? 90% says the exits are well placed; 30% says the market kept offering and the exit kept refusing. Limit, stated up front: excursions come from the bars each trade spanned, on the chart timeframe; the path inside a bar is unknown. Predicts nothing; measures trades that already happened.