Title: Every Field You Did Not Assign: Why a 10009 Can Still Remove Your Take Profit
Tags: MqlTradeRequest, OrderSend, TRADE_ACTION_SLTP, hedging, trade checks
An expert advisor I had been running for months was closing trades earlier than the strategy intended. The journal was clean: every request returned 10009, TRADE_RETCODE_DONE.
The cause was a trailing function that built a MqlTradeRequest, assigned the stop loss, and sent it. The take profit field was never assigned, so it held 0.0. The server read the request exactly as written: set this stop, and remove the take profit. It did both and reported success.
That is not a defect. MqlTradeRequest describes the state a position should be in, not a change to it. Every field is read, including the ones you did not touch.
Fields whose zero is an instruction
Field Value when unassigned What the server does
tp, sl in TRADE_ACTION_SLTP 0.0 Removes the level
position in TRADE_ACTION_DEAL (hedging) 0 Opens a new position instead of closing one
deviation 0 Accepts no slippage: requotes, PRICE_CHANGED
type_filling 0 = ORDER_FILLING_FOK Fill-or-kill; INVALID_FILL where FOK is not offered
The fix for the first one is a single line:
cpp
MqlTradeRequest r;
ZeroMemory(r);
r.action = TRADE_ACTION_SLTP;
r.position = ticket;
r.symbol = symbol;
r.sl = new_sl;
r.tp = PositionGetDouble(POSITION_TP); // carry the target over
r.magic = PositionGetInteger(POSITION_MAGIC);
The hedging case is worth a start-up line in the journal, so the assumption is recorded:
cpp
const ENUM_ACCOUNT_MARGIN_MODE mode =
(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);
PrintFormat("account margin mode: %s",
(mode == ACCOUNT_MARGIN_MODE_RETAIL_HEDGING ? "hedging" : "netting"));
Three symbol limits, briefly
SYMBOL_TRADE_STOPS_LEVEL: minimum distance for a new stop or target. Zero does not mean "no limit"; the server usually derives it from the spread, so keep a margin above the spread.
SYMBOL_TRADE_FREEZE_LEVEL: while price is that close to an existing level, the level cannot be moved (TRADE_RETCODE_FROZEN). Skip and retry next tick.
SYMBOL_TRADE_TICK_SIZE: not always equal to SYMBOL_POINT. NormalizeDouble rounds to decimals, not ticks, and on a 0.25 or 0.05 tick symbol produces prices the server cannot use. Round a stop away from the market and a target towards it:
cpp
double RoundToTick(double price, double tick, int digits, int dir) // -1 down, +1 up
{
if(tick <= 0.0) return NormalizeDouble(price, digits);
double n = (dir < 0) ? MathFloor(price / tick + 1e-8)
: MathCeil (price / tick - 1e-8);
return NormalizeDouble(n * tick, digits);
}
One function for every call site
A program sends requests from several places: entry, trailing, partial exit, close. Written at each call site, the rules drift. The pattern I now use is one function that starts from what the position already has and applies only what was asked, with a sentinel for "leave this level alone":
cpp
#define SR_KEEP (-1.0)
bool SetStops(ulong ticket, double want_sl, double want_tp, MqlTradeResult &res)
{
ZeroMemory(res);
if(!PositionSelectByTicket(ticket)) return false;
double have_sl = PositionGetDouble(POSITION_SL);
double have_tp = PositionGetDouble(POSITION_TP);
double sl = (want_sl == SR_KEEP ? have_sl : want_sl);
double tp = (want_tp == SR_KEEP ? have_tp : want_tp);
// ... round to tick, check stops and freeze levels ...
if(sl == have_sl && tp == have_tp) return true; // nothing to send
MqlTradeRequest r;
ZeroMemory(r);
r.action = TRADE_ACTION_SLTP;
r.position = ticket;
r.symbol = PositionGetString(POSITION_SYMBOL);
r.sl = sl;
r.tp = tp;
r.magic = PositionGetInteger(POSITION_MAGIC);
return OrderSend(r, res) && res.retcode == TRADE_RETCODE_DONE;
}
The only way to remove a level is to pass 0.0 on purpose. The early return also stops a trailing loop from sending hundreds of requests that change nothing.
Check before sending
OrderCheck takes the same structure and reports margin, remaining equity and a return code without sending anything. It catches the structural mistakes (volume, filling mode, margin, off-grid price) in the tester as well as live. Keep it behind a flag in your send function during development.
State the terminal does not keep
A scale-out counter held in a variable resets to zero on every restart, recompile or timeframe change, and the expert takes the first partial exit again on a position that is already half closed. The deal history cannot be out of step: read it back with HistorySelectByPosition in OnInit and count the DEAL_ENTRY_OUT deals.
Takeaway
Fill in every field of every request on purpose. Read symbol limits when you send, not at start-up. Put it all in one function. When a code other than 10009 comes back, log the request, not only the code.
The full include file (SafeRequest.mqh), a script that checks every Market Watch symbol's stop limits, and a script that rebuilds scale-out state from deal history are free in CodeBase: https://www.mql5.com/en/code/66629.
These checks are built into my multi-asset EA, Statera Genesis: https://www.mql5.com/en/market/product/197907
Tags: MqlTradeRequest, OrderSend, TRADE_ACTION_SLTP, hedging, trade checks
An expert advisor I had been running for months was closing trades earlier than the strategy intended. The journal was clean: every request returned 10009, TRADE_RETCODE_DONE.
The cause was a trailing function that built a MqlTradeRequest, assigned the stop loss, and sent it. The take profit field was never assigned, so it held 0.0. The server read the request exactly as written: set this stop, and remove the take profit. It did both and reported success.
That is not a defect. MqlTradeRequest describes the state a position should be in, not a change to it. Every field is read, including the ones you did not touch.
Fields whose zero is an instruction
Field Value when unassigned What the server does
tp, sl in TRADE_ACTION_SLTP 0.0 Removes the level
position in TRADE_ACTION_DEAL (hedging) 0 Opens a new position instead of closing one
deviation 0 Accepts no slippage: requotes, PRICE_CHANGED
type_filling 0 = ORDER_FILLING_FOK Fill-or-kill; INVALID_FILL where FOK is not offered
The fix for the first one is a single line:
cpp
MqlTradeRequest r;
ZeroMemory(r);
r.action = TRADE_ACTION_SLTP;
r.position = ticket;
r.symbol = symbol;
r.sl = new_sl;
r.tp = PositionGetDouble(POSITION_TP); // carry the target over
r.magic = PositionGetInteger(POSITION_MAGIC);
The hedging case is worth a start-up line in the journal, so the assumption is recorded:
cpp
const ENUM_ACCOUNT_MARGIN_MODE mode =
(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);
PrintFormat("account margin mode: %s",
(mode == ACCOUNT_MARGIN_MODE_RETAIL_HEDGING ? "hedging" : "netting"));
Three symbol limits, briefly
SYMBOL_TRADE_STOPS_LEVEL: minimum distance for a new stop or target. Zero does not mean "no limit"; the server usually derives it from the spread, so keep a margin above the spread.
SYMBOL_TRADE_FREEZE_LEVEL: while price is that close to an existing level, the level cannot be moved (TRADE_RETCODE_FROZEN). Skip and retry next tick.
SYMBOL_TRADE_TICK_SIZE: not always equal to SYMBOL_POINT. NormalizeDouble rounds to decimals, not ticks, and on a 0.25 or 0.05 tick symbol produces prices the server cannot use. Round a stop away from the market and a target towards it:
cpp
double RoundToTick(double price, double tick, int digits, int dir) // -1 down, +1 up
{
if(tick <= 0.0) return NormalizeDouble(price, digits);
double n = (dir < 0) ? MathFloor(price / tick + 1e-8)
: MathCeil (price / tick - 1e-8);
return NormalizeDouble(n * tick, digits);
}
One function for every call site
A program sends requests from several places: entry, trailing, partial exit, close. Written at each call site, the rules drift. The pattern I now use is one function that starts from what the position already has and applies only what was asked, with a sentinel for "leave this level alone":
cpp
#define SR_KEEP (-1.0)
bool SetStops(ulong ticket, double want_sl, double want_tp, MqlTradeResult &res)
{
ZeroMemory(res);
if(!PositionSelectByTicket(ticket)) return false;
double have_sl = PositionGetDouble(POSITION_SL);
double have_tp = PositionGetDouble(POSITION_TP);
double sl = (want_sl == SR_KEEP ? have_sl : want_sl);
double tp = (want_tp == SR_KEEP ? have_tp : want_tp);
// ... round to tick, check stops and freeze levels ...
if(sl == have_sl && tp == have_tp) return true; // nothing to send
MqlTradeRequest r;
ZeroMemory(r);
r.action = TRADE_ACTION_SLTP;
r.position = ticket;
r.symbol = PositionGetString(POSITION_SYMBOL);
r.sl = sl;
r.tp = tp;
r.magic = PositionGetInteger(POSITION_MAGIC);
return OrderSend(r, res) && res.retcode == TRADE_RETCODE_DONE;
}
The only way to remove a level is to pass 0.0 on purpose. The early return also stops a trailing loop from sending hundreds of requests that change nothing.
Check before sending
OrderCheck takes the same structure and reports margin, remaining equity and a return code without sending anything. It catches the structural mistakes (volume, filling mode, margin, off-grid price) in the tester as well as live. Keep it behind a flag in your send function during development.
State the terminal does not keep
A scale-out counter held in a variable resets to zero on every restart, recompile or timeframe change, and the expert takes the first partial exit again on a position that is already half closed. The deal history cannot be out of step: read it back with HistorySelectByPosition in OnInit and count the DEAL_ENTRY_OUT deals.
Takeaway
Fill in every field of every request on purpose. Read symbol limits when you send, not at start-up. Put it all in one function. When a code other than 10009 comes back, log the request, not only the code.
The full include file (SafeRequest.mqh), a script that checks every Market Watch symbol's stop limits, and a script that rebuilds scale-out state from deal history are free in CodeBase: https://www.mql5.com/en/code/66629.
These checks are built into my multi-asset EA, Statera Genesis: https://www.mql5.com/en/market/product/197907
