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:
Returns the symbol corresponding to the open position
Selects a position to work with by the ticket number specified in it
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:
Returns the symbol corresponding to the open position
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 :)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?