Did I understand your desire correctly? (I warn you, I used telepathy only 1%) - we opened position No. 1 by the symbol "A", opened position No. 2
by the symbol "B" and if you opened position No. 3 by the symbol "A", then you need to close position No. 1.
The algorithm will be this: in OnTradeTransaction we catch the transaction TRADE_TRANSACTION_DEAL_ADD. If this is a deal DEAL_ENTRY_IN -
we count the number of positions by symbol and if there are more than one positions - close the oldest one.
The code will be tomorrow.
I prefer this way, that's how I do on my advisors. The difference is that this one work all symbols from one chart.
Here is the version (easy) - it works when positions rarely appear.
I will release the full version later.
For fast markets, I think to use the following algorithm:
two arrays are declared in the "header" of the EA - one 'string' for symbol names, and the second 'ulong' for tickets.
This will work like this: as soon as we catch the transaction 'TRADE_TRANSACTION_DEAL_ADD' in OnTradeTransaction and this transaction is
'DEAL_ENTRY_IN' - add the name of the character to the array 'string'.
In OnTick, we check the size of the 'string' array - if there is something in it, we begin to count the positions.
This is the right direction: the adviser works on an event. An event is the addition of a transaction to a trading history. That is, it is not
necessary to recalculate the positions for each symbol at each tick.
It can work on all symbols from one chart in any case, if you code it for doing that.
If buy condition -> close sells and open buy
If sell condition -> close buys and open sell
Seriously I don't understand why you are complicating this so much.