Ticket Lock Copier
- ユーティリティ
-
Jorge Eliecer Valencia Ferreira
🤖 Quantitative Developer & Algorithmic Trading Specialist | MQL5 & Python
Welcome to my official MQL5 profile. I am a Quantitative Developer and Algorithmic Trader focused on creating high-quality, institutionally robust Expert Advisors with strict risk management. - バージョン: 1.0
- アクティベーション: 10
Ticket Lock Copier copies trades from one MetaTrader 5 terminal (the master) to another (the receiver) when both run on the same PC or the same VPS. The two terminals talk through files in the common data folder. There is no DLL, no WebRequest and no external server. One EA has two roles: set it to MASTER on the source account and to SLAVE on the account that copies.
It is for traders who run a signal account and a second account on the same machine, who manage several accounts of their own, or who want a copy with a different lot size on another broker. It is not a trading strategy and it does not create signals. It only repeats what the master account already does.
Reviews of local copiers often describe the same failure: the receiver opens the same master trade again and again, sometimes every second, until the account is gone. This EA is built around that one problem. Before it sends an order, it writes the master ticket into a ticket book saved on disk. The book is read again after a terminal restart. A ticket that is in the book is not opened a second time.
How the ticket lock works
- The master ticket is stored in the book first, with the state "in flight", and only then is the order sent. The copy carries the comment TLC#ticket.
- No new order is sent if the ticket is already in the book, or if a position or order with that tag already exists on the receiver.
- If you close the copy by hand, or its SL or TP is hit, the ticket is marked as closed on the receiver and it is not opened again. There is an optional input that allows one reopen. It is off by default.
- If the broker answers with a doubtful result (timeout, requote, price changed, no answer), the order stays "in flight". Before any retry, the EA checks the live positions and the deal history for the tag. It retries only if nothing is found, up to a set number of attempts per ticket.
- Removing and re-attaching the EA, or restarting the terminal, does not clear the book. The book file is named by receiver account, channel and master account.
- A ticket blocked by a cap or a rule is not retried. It stays in the book and in the CSV log, and the Experts log gets one line with the reason.
Hard caps, on by default
The receiver has limits that work from the first run. You can change them, or set them to 0 to turn them off.
- Maximum lot per copied trade: 2.0. Larger copies are cut to this size.
- Maximum total copied lots open: 10.0. If a new copy does not fit, that ticket is blocked.
- Maximum copied positions open: 10. If the number is reached, that ticket is blocked.
- Daily loss limit of the receiver: 3% of the day-start balance. When it is hit, no new copies are opened until the next day. Optionally it also closes the copied trades.
Stop loss on the server
Every copy is sent with its SL from the first order. If the master trade has no SL, the default policy adds an emergency SL calculated so that the trade risks 2% of the receiver equity. You can choose instead to skip trades that have no SL. A copy never removes an SL that is already set.
What it copies
- Open positions, with SL and TP changes.
- Partial closes (proportional volume) and full closes.
- Pending orders, if you turn that on. Only LIMIT and STOP types.
- Symbol mapping between brokers, for names like XAUUSD and GOLD#.
- Optional inverted copy: buy becomes sell, and SL and TP are swapped.
Lot size on the receiver, by mode:
- Multiplier of the master lot (default 1.0), or the same lot.
- Risk percent of the receiver equity, using the distance to the master SL. If the master trade has no SL, it falls back to the multiplier and logs it.
- Proportional to equity (receiver equity divided by master equity, sent in the snapshot).
- Fixed lot. It is one option among others, not the only one.
All lots go through the same normalization: step, minimum and maximum of the symbol, the symbol volume limit, and a reduction when free margin is not enough.
Other safety rules
- By default it copies only trades opened after the EA started, and never a trade older than 60 seconds.
- Heartbeat: if the master file is not updated for 30 seconds, the receiver opens nothing new and the panel shows STALE. It does not close anything because of this.
- The snapshot file is written to a temporary file and then moved into place. It has a checksum that the receiver verifies. A half-written file is discarded.
- The master never republishes positions whose comment starts with TLC#, so an account that is both receiver and master does not create a loop.
- Master and receiver wait a few seconds after start and until the account data is loaded, to avoid publishing an empty list or reading the account wrongly before the terminal is synchronized.
- SL and TP changes are compared before sending. If the change is not safe for the stops level, freeze level or spread, it is skipped and tried again later.
Symbol mapping
The receiver looks for the symbol in this order:
- The explicit map you write, for example XAUUSD=GOLD#;US30=US30Cash#.
- The same name.
- An automatic match by prefix or suffix. It searches the Market Watch first, then all broker symbols, and takes the shortest name that contains the base symbol.
If no symbol is found, that trade is not copied.
How to set it up
Both terminals must run under the same Windows user on the same PC or VPS, so they share the common data folder.
Master terminal:
- Open any chart and make sure Algo Trading is on.
- Attach the EA and set InpRole to MASTER. Choose a channel name, for example myChannel. Use one channel per master.
- Optional: filter by magic numbers, symbols or comment text, so only some trades are published.
- The panel should show that it is publishing, and the file TLC_myChannel.snap should appear in the common folder.
Receiver terminal:
- Open any chart and make sure Algo Trading is on.
- Attach the EA and set InpRole to SLAVE. Use the same channel name.
- Optional: write the master account number in InpMasterLogin so only that account is accepted.
- Choose the lot mode and check the caps. Write a symbol map if the names differ.
- The panel should show that the master heartbeat is OK.
- Test on two demo accounts first. Open a small trade on the master, close the copy by hand on the receiver, and check that it is not opened again, also after removing and re-attaching the EA.
If one receiver follows several channels, use a different InpMagic for each channel.
Inputs
General
- InpRole (default MASTER): MASTER or SLAVE.
- InpChannel (default "default"): channel name, letters and digits. One per master.
- InpTimerMs (default 250): work timer in milliseconds.
- InpCopyPendings (default false): also publish and copy pending orders.
- InpWriteCsv (default true): write the action log.
- InpAlerts (default false): terminal alerts on important events.
Master: what to publish
- InpMasterMagics (default empty): magic numbers to publish, comma list. Empty = all.
- InpMasterSymbols (default empty): symbols to publish, comma list. Empty = all.
- InpMasterComment (default empty): publish only positions whose comment contains this text.
- InpHeartbeatSec (default 5): rewrite the snapshot at least every N seconds.
Receiver: copy rules
- InpMasterLogin (default 0): accept only this master account. 0 = any.
- InpMagic (default 240923): magic number of copied trades. Use one per channel.
- InpCopyExistingOnStart (default false): copy trades already open when the EA starts.
- InpMaxAgeSec (default 60): never copy a trade older than N seconds. 0 = no limit.
- InpReopenIfClosedOnSlave (default false): allow one reopen of a copy closed on the receiver.
- InpMaxOpenRetries (default 3): maximum send attempts per master ticket.
- InpInflightTimeoutSec (default 8): seconds to wait before checking an unconfirmed order.
- InpMasterTimeoutSec (default 30): no heartbeat for N seconds = no new copies.
- InpCopySlTp (default true): synchronize SL and TP changes.
- InpCopyPartials (default true): synchronize partial closes.
- InpInvert (default false): copy inverted.
- InpSymbolMap (default empty): symbol map, for example XAUUSD=GOLD#;US30=US30Cash#.
- InpSlippagePoints (default 30): maximum slippage in points.
Receiver: lot size
- InpLotMode (default Multiplier): multiplier, same lot, risk percent, proportional to equity, or fixed lot.
- InpLotMultiplier (default 1.0): used in multiplier mode.
- InpRiskPct (default 1.0): percent of equity per trade, in risk mode.
- InpFixedLot (default 0.01): used in fixed lot mode.
Receiver: hard caps
- InpMaxLotPerTrade (default 2.0): maximum lot per copied trade. 0 = off.
- InpMaxTotalLots (default 10.0): maximum total copied lots open. 0 = off.
- InpMaxPositions (default 10): maximum copied positions open. 0 = off.
- InpDailyLossPct (default 3.0): daily loss limit, % of day-start balance. 0 = off.
- InpCloseOnDailyLoss (default false): also close the copied trades when the daily limit is hit.
- InpNoSlPolicy (default Emergency SL): add an emergency SL, or skip trades without SL.
- InpEmergencyRiskPct (default 2.0): percent of equity at risk with the emergency SL.
Panel
- InpShowPanel (default true), InpCorner (default top left), InpFontSize (default 9) and five color inputs.
Strategy Tester
- InpTesterDemo (default true): demo with a simulated master, tester only.
Panel and log
The panel shows the role, channel, heartbeat state, the time of the last copy (from the moment the master wrote the snapshot until the receiver confirmed the order, measured with the system clock, so it is valid only on the same PC), copied positions and lots, counters, the daily lock, the last event and the last error.
With InpWriteCsv on, every action is written to MQL5\Files\TLC_log_channel.csv. Semicolon separated. Columns: time; event; symbol; ticket; volume; price; retcode; equity; detail.
What it does not do
- It does not copy between different PCs or over the internet. There is no network or cloud part, on purpose.
- It does not generate signals and it does not choose trades. It copies the master.
- It does not copy trades older than the age limit, and by default it does not copy trades that were open before it started.
- It does not close the copies when the master heartbeat stops. It only stops opening new ones.
- It does not guarantee the same price, the same fill or the same result as the master. The receiver is a different account, with its own broker, spread, execution and slippage.
- It does not replace your own risk management and it does not make a strategy profitable.
Limitations
- Same PC or VPS only. Both terminals need to share the common data folder, which is normal under the same Windows user.
- One channel per master. If several masters write to the same channel, they overwrite the same file.
- Netting accounts: opposite tickets on the same symbol are offset in the net position, so the copy is approximate. SL and TP are synchronized only when there is a single copied trade on that symbol. Manual closes on the receiver are not detected by ticket (the EA avoids reversing the position, nothing more). Volume increases of a master ticket, when netting adds to a position, are not copied. Hedging accounts keep one position per master ticket.
- Pending orders: only LIMIT and STOP types, not STOP_LIMIT. Expiration is not copied. If the master pending order is triggered, the receiver deletes its pending order and opens at market, as long as the trade is within the age limit.
- If you change the master filters (magic, symbol, comment), the filtered trades disappear from the snapshot and the receiver closes its copies of them.
- The ticket lock depends on the book file in the common folder. If that file is deleted, or you change channel or master account, a new book starts empty.
- The terminal must be running, connected, with Algo Trading enabled. If the master or the receiver is off, nothing is copied. A VPS is a common choice, but that is your decision.
- Closing and opening are at market. Slippage, requotes and rejected orders happen, and a copy can fail or open at a different price.
- Caps limit the exposure of copied trades but do not remove the risk. The final loss can be larger than the daily limit, because of gaps, slippage or trades that cannot be closed when the market is closed.
Strategy Tester demo
The tester has no master terminal. If InpTesterDemo is on (tester only, never on a real or demo account chart), the EA simulates an internal master. At most twice a day it opens a trade with a stop loss based on the H1 ATR (wider, with lower risk, on symbols with a wide spread), moves its SL after one hour, closes part of it after two hours, and closes it after three hours. Each simulated snapshot goes through the same reconciliation code that the receiver uses, so opening, ticket lock, modification, partial close and close are all exercised.
When a trade opens, the same snapshot is processed 4 times and the EA prints one line in the Experts log, in this form:
TicketLockCopier demo: ticket 9000001 processed 4 times -> sends=1 live copies=1 (ticket lock OK, no duplicate)
If the minimum lot risks too much for the account (gold, or USDZAR with a small balance) or there is not enough margin, it opens no trade and prints a message once. The book is not saved in the tester, and the demo is only to show the behavior, not a result.
Notes
- Works on any symbol and any timeframe, on hedging and netting accounts (see the netting limits above).
- Test with two demo accounts and small lots before using it on live accounts.
Risk warning: trading leveraged products carries a high risk of loss. This tool copies trades, it is not trading advice and it does not guarantee any result. The caps reduce, but do not remove, the risk of copying trades from another account.
