Don't use 'PositionSelect(_Symbol)'!!! - read the documentation!
Correct cycle:
for(int i=PositionsTotal()-1; i>=0; i--) if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic) { *** }
or
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions { ulong ticket=PositionGetTicket(index); if(ticket>0); { if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==InpMagic) { *** } } }
Thanks for your response.
My code is designed so I would only have 1 open position at any given time.
I also do not understand what is meant by 'InpMagic'.
Sorry in advance for being a noob.
InpMagic refers to an input (indicator/expert parameter) variable with the name "InpMagic"
input ulong InpMagic = 1234; // Magic Number this EA will assign to trades and check for when managing trades
It's meant to store the "magic number" of an EA that uniquely identifies the trades that the EA should handle.
Assuming you're on MT5: when your EA opens a trade, it should always set the magic number value for the magic parameter in the MqlTradeRequest struct.
struct MqlTradeRequest { ENUM_TRADE_REQUEST_ACTIONS action; // Trade operation type ulong magic; // Expert Advisor ID (magic number) ulong order; // Order ticket string symbol; // Trade symbol double volume; // Requested volume for a deal in lots double price; // Price double stoplimit; // StopLimit level of the order double sl; // Stop Loss level of the order double tp; // Take Profit level of the order ulong deviation; // Maximal possible deviation from the requested price ENUM_ORDER_TYPE type; // Order type ENUM_ORDER_TYPE_FILLING type_filling; // Order execution type ENUM_ORDER_TYPE_TIME type_time; // Order expiration type datetime expiration; // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type) string comment; // Order comment ulong position; // Position ticket ulong position_by; // The ticket of an opposite position };
If your EA doesn't open any trades but, instead, manages trades, you should still check the magic number of the trade to see if it's value is 0 (the default value) as to not interfere with trades that other EAs are handling.
InpMagic refers to an input (indicator/expert parameter) variable with the name "InpMagic"
It's meant to store the "magic number" of an EA that uniquely identifies the trades that the EA should handle.
Assuming you're on MT5: when your EA opens a trade, it should always set the magic number value for the magic parameter in the MqlTradeRequest struct.
If your EA doesn't open any trades but, instead, manages trades, you should still check the magic number of the trade to see if it's value is 0 (the default value) as to not interfere with trades that other EAs are handling.
Thanks Alexander.
That makes so much more sense.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
P.S. I have used static variables to calculate any R by using the initial Stoploss and Open Price.
P.P.S Also have used a ZeroMemory to set the static variables to zero if no positions are open.
When I compile my code, I do not get an error. However, when I run a backtest, the SL doesn't move.
What am I possibly doing wrong? Please advise.