# PositionSelect Strange Behaviour on Hedge orders

Hello,

I have a function to limit the number of trades I open at the same time by using the magicNumber, as shown below. It works perfectly when limited to having one order open at a time.

However I am trying to test being able to have a max of 2 orders, one order in each direction open (1 short and 1 long) on the same Symbol (EURUSD), allowing me to create a hedge. For some reason I cannot find any way to make that work.

Below my code to check if a magic number already exists, I assign different magic numbers to my EA´s short and long orders when openning them. Then I use the function below to check if the magic number already exist to skip openning any further orders.

For some reason when I do that it only limits me on the one side of the trade that occurs first:

i.e. if the model buys first then it correctly identifies buy orders and stops new ones from openning until it is closed (as its supposed to) however on short side it has no limit even though I check also the magic number of the short side with the exact same function below.

I´ve spent days trying to troubleshoot this but cannot figure out how, what am i doing wrong?

```//+------------------------------------------------------------------+
//| Checking Magic Number of Orders                                  |
//+------------------------------------------------------------------+
bool CheckIfOpenOrdersByMagicNB(int EaMagicNumber)
{
//--- variables for returning values from order properties
ulong    ticket;
long     order_magic;
int  total=PositionsTotal(); //replacing below line with positions

//--- go through orders in a loop
for(int i=0; i<total; i++)
{
//--- return order ticket by its position in the list
if((ticket=PositionSelect(_Symbol))>0)
{
//--- return order properties
order_magic   =PositionGetInteger(POSITION_MAGIC);
//--- prepare and show information about the order
if(order_magic== EaMagicNumber)
{
return(true);
}
else
{
return(false);
}
}
}
return(false);

}```
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
• www.mql5.com
order_calc_margin - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

The PositionSelect function was originally only for "netting" accounts where only one position is possible per symbol. It was later allowed for "hedging" accounts, but it is of very little use and just selects the position of the lowest numbered ticket.

For the "netting" interpretation of positions (ACCOUNT_MARGIN_MODE_RETAIL_NETTING and ACCOUNT_MARGIN_MODE_EXCHANGE), only one position can exist for a symbol at any moment of time. This position is a result of one or more deals. Do not confuse positions with valid pending orders, which are also displayed on the Trading tab of the Toolbox window.

If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case, PositionSelect will select a position with the lowest ticket.

So, in essence you are using the wrong function for the job. Rethink the logic of your code and use one of the other position selecting functions:

 PositionGetSymbol Returns the symbol corresponding to the open position PositionSelectByTicket Selects a position to work with by the ticket number specified in it

Fernando Carreiro #:

The PositionSelect function was originally only for "netting" accounts where only one position is possible per symbol. It was later allowed for "hedging" accounts, but it is of very little use and just selects the position of the lowest numbered ticket.

So, in essence you are using the wrong function for the job. Rethink the logic of your code and use one of the other position selecting functions:

 PositionGetSymbol Returns the symbol corresponding to the open position PositionSelectByTicket Selects a position to work with by the ticket number specified in it

Thank you Fernando for your reply, makes sense, I had already tried reworking it using those functions but for some reason couldnt get it to work (probably mostly my lack of ability)

Though I did solve using the SelectByMagic function from the CPositionInfo class, just in case it can help anyone else :)

Reason: