Trade server return code
10046 TRADE_ RETCODE_ HEDGE_ PROHIBITED
Hedge prohibited: no opposite positions on one symbol
What it means
The account does not allow a buy and a sell on the same symbol at the same time. The rule applies only to hedging accounts (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING); the reference's example is an account holding a buy that can then neither open a sell nor place a pending sell order. ACCOUNT_HEDGE_ALLOWED ("Allowed opposite positions on a single symbol") tells you in advance.
Common causes
- Zone recovery and hedging EAs, which work by opening the opposite side.
- A reversal strategy that opens the new direction before closing the old one.
- A pending order in the opposite direction placed while a position is open.
A real scenario
A zone recovery EA opens a sell when the first buy goes against it by the zone width. On an account where opposite positions are disabled every hedge leg fails with 10046, and the buy is left without its recovery. The zone recovery calculator assumes hedges are allowed; on such an account the EA must stop before it starts, which a check of ACCOUNT_HEDGE_ALLOWED in OnInit() now does.
How to fix it
- Read
ACCOUNT_MARGIN_MODEandACCOUNT_HEDGE_ALLOWEDinOnInit(). - For strategies that need opposite positions, refuse to start on accounts that do not allow them.
- For reversals, close the open position first, then open the new direction.
Fixed MQL5 code
#include <Trade/Trade.mqh>
CTrade trade;
input long InpMagic = 20261130;
bool OppositeAllowed()
{
bool hedging = AccountInfoInteger(ACCOUNT_MARGIN_MODE) == ACCOUNT_MARGIN_MODE_RETAIL_HEDGING;
return hedging && (bool)AccountInfoInteger(ACCOUNT_HEDGE_ALLOWED);
}
// Reverse from buy to sell on accounts without opposite positions: close our buys first.
bool ReverseToSell(const double lots)
{
if(!OppositeAllowed())
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0 && PositionGetString(POSITION_SYMBOL) == _Symbol
&& PositionGetInteger(POSITION_MAGIC) == InpMagic
&& PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
if(!trade.PositionClose(ticket))
return false;
}
}
}
return trade.Sell(lots, _Symbol);
}
A focused example for MetaTrader 5: adapt names, magic number and limits to your EA, and test it on a demo account first.
Related codes
- 10045FIFO Close RuleTRADE_RETCODE_FIFO_CLOSEFIFO rule: positions must be closed oldest first
- 10040Position LimitTRADE_RETCODE_LIMIT_POSITIONSPosition limit reached: the account holds as many positions as the server allows
Sources
Get your EA fixed
Still failing with 10046? Send us the EA and the journal lines: we find the cause in the code and fix it. Or check it yourself first with the EA Doctor.