Emmanuel Nawita Were
Emmanuel Nawita Were
  • Informations
1 année
expérience
1
produits
12
versions de démo
0
offres d’emploi
0
signaux
0
les abonnés
Développeur MQL5 qui conçoit des Expert Advisors multi-symboles pour MetaTrader 5, centrés sur le contrôle de l'exposition et la mesure : les paramètres de sortie de mes EA sont tirés de transactions enregistrées, pas de l'habitude.

Produit : Statera Genesis - EA multi-symboles H1 avec plafonds d'exposition nette par devise et sorties par classe d'actifs. Articles : analyse des transactions en MQL5 - un enregistreur d'excursions MFE/MAE et des statistiques pour des transactions corrélées. CodeBase : SafeRequest - compléter et vérifier chaque MqlTradeRequest en un seul endroit.

Questions sur mes produits : envoyez-moi un message privé.
Emmanuel Nawita Were
Emmanuel Nawita Were
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
Emmanuel Nawita Were
Code publié SafeRequest - complete and check every MqlTradeRequest in one place
An include file that fills every field of a trade request on purpose, rounds prices to the tick grid, respects stops and freeze levels, and never removes a take profit by accident.
Emmanuel Nawita Were
Article publié Your Take Profit Is a Guess Until You Log MFE: Building an Excursion Recorder in MQL5
Your Take Profit Is a Guess Until You Log MFE: Building an Excursion Recorder in MQL5

A compact MFE/MAE recorder in pure MQL5 samples excursion while a position is open and writes one CSV row per closed position, measured in R and frozen at entry. A companion script prints a reach-rate table per asset class: the share of trades that ever reached each level. In a 226-position sample, three of the four classes never reached the 3R target the system was using. Use it to test whether a target is reachable before trusting it.

Emmanuel Nawita Were Produits publiés

Description Statera Genesis est un Expert Advisor multi-symboles pour MetaTrader 5, conçu autour du contrôle de l'exposition. Son nom vient de statera, le mot latin pour la balance. Signal en direct : [collez le lien] Comment il trade Cinq moteurs de signaux votent sur chaque bougie H1, avec des pondérations définies par classe d'actifs : forex, métaux, cryptomonnaies, indices et énergie, et indices synthétiques. Une position n'est ouverte que si le vote pondéré dépasse le seuil et que tous les

Emmanuel Nawita Were
Enregistré à MQL5.community