Unisciti alla nostra fan page
CGoldSymbol - find the broker's gold and size the position correctly on it - libreria per MetaTrader 5
- Visualizzazioni:
- 76
- Valutazioni:
- Pubblicato:
-
Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance
Gold is the instrument where brokers agree about almost nothing. The symbol may be XAUUSD, GOLD, XAUUSD.m, XAUUSDpro or GOLD#. One contract may hold 100 ounces, or 10, or one. The price may carry two digits or three. An EA that works out its lot size from a hardcoded "100 ounces, two digits" is not slightly wrong on the next broker - it is wrong by a factor of ten, in the direction that empties the account.
This class asks the terminal instead of assuming.
Detection. Detect() walks every symbol the account offers and scores the names, so XAUUSD wins over XAUUSD.m, which wins over a loose match, while silver, platinum and palladium are excluded outright - XAGUSD must never win a search for gold. A symbol already in Market Watch gets a small bonus. If your broker uses something unusual, Attach("YOURNAME") skips the search.
Specification. ContractSize(), Digits(), Point(), TickSize(), TickValueLoss(), MinLot(), MaxLot(), LotStep(), StopsLevel() and FreezeLevel() come straight from the terminal for that symbol.
Money. LossForDistance(lots, distance) says what a move against you costs in the currency of the account. LotForRisk(riskMoney, distance) is the reverse and the one that matters: it converts through SYMBOL_TRADE_TICK_VALUE_LOSS, so it stays correct on a 10-ounce contract and on a euro-denominated account, then rounds the result down onto the broker's volume step and into its min and max. It returns zero rather than a silent minimum lot when even the smallest volume would risk more than you allowed - a case most sizing code gets wrong by rounding up.
Diagnostics. Attach() collects warnings for a contract size that is not 100, an unusual number of digits, a tick value the terminal reports as zero, and a non-zero stops level. PrintReport() prints the whole specification with a worked example, which is usually the fastest way to find out why a strategy behaves differently on two brokers.
On a live account with a 100-ounce contract, a euro balance and a 1500-point stop, 1 % of 10 000 EUR comes out as 0.07 lots risking 90.42 EUR. The same call on a 10-ounce contract returns 0.70 lots, and on a one-ounce contract 7.00 lots. Same risk, same stop, a hundredfold difference in volume - which is exactly what a hardcoded constant gets wrong.
The package includes GoldSymbolDemo.mq5, a script that detects the gold symbol, prints the report and converts a percentage of the balance into a lot size over several stop distances. Run it on any chart to see what your own broker's gold really is.
The class opens no trades and modifies no positions. It answers questions.

OHLC Reality Check - at what stop distance does your backtest start lying?
Opens a virtual bracketed trade on every M1 bar, walks it forward on the real tick history, and reports for a sweep of stop distances how often 1-minute OHLC modelling would score the trade the wrong way round.
SessionReopenEA
Gold has a daily maintenance break on the CME. The first hour after it rises more than chance explains, in every calendar year of an 11-year sample - while every other hour of the day measures flat. One trade per session, a volatility-scaled server-side stop, no averaging or grid. Then my cost model turned out to be wrong. A real-tick backtest showed the true round-trip cost at the reopen is about 60 points, not the 19 my research had charged - the M1 bar spread field is a per-bar summary and understates it roughly threefold. Re-running 11 years at the corrected cost: +3.34 bps, t 7.40, 59.3% wins -> +1.60 bps, t 3.42, 50.8% wins The edge survives, at less than half its original strength. That second number is the real one. What it is not: about +3.3% a year with 4.6% drawdown, roughly one year in nine negative. At 0.01 lots that is ~130 a year - a figure that measures the position size, not the strategy.
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.
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.