Your code is incomplete; I'll assume you are using a CTrade class from the standard library to send orders and manage open positions.
Let's say you have defined CTrade object like this:
CTrade* ctrade; int OnInit() { ctrade = new CTrade; // instantiate/create the object here, otherwise it'll not work and you'll have a memory error return INIT_SUCCEEDED; }
You can just call the function PositionModify from the CTrade class using the created object like this:
ctrade.PositionModify(position_ticket, new_stop_loss, new_take_profit);
Notice the PositionModify is part of the CTrade class. Since it is not a static function, it can only be called from an instantiated (created) object of the CTrade class type.
Also, pay attention to some things:
1 - Your for loop runs indefinitely. See that the condition
for(int count =0; PositionsTotal() > 0; count++)
If there's a opened position (i.e., PositionsTotal > 0), the count variable keeps incrementing to infinity, so the loop won't break. The correct loop would be defined like this:
for(int count = 0; count < PositionsTotal(); count++)
where the loop keeps going while count is lower than the amount of positions (notice that the last position index is PositionsTotal() - 1, thus the last loop value should also be the total positions - 1).
2 - You call PositionsTotal() too many times. This reduces the code speed. If you do not open/close any positions in this loop, call it only once (before the loop starts), save the result in a variable and work with it.
3 - PositionGetTicket automatically selects a position for you to work with. There's no need to call PositionSelectByTicket, this also reduces the code speed. If you want to check if the call was succesful, just compare the result of the value of the ticket - if it is 0, the function failed.
4 - Same as 2, you can call PositionGetInteger to fetch the position type only once, store the result in a variable and work with it to improve the code speed.
5 - If you wanna check a condition that can only have one outcome possible at a time, use an 'if else' statement to avoid checking all the 'if' blocks. Otherwise, the program will check the same condition multiple times, also reducing the code speed.
I'll leave my suggestion on how the code could be improved:
int total_positions; CTrade* trade; // assume you already initialized this variable on the OnInit function void OnTick() { total_positions=PositionsTotal(); // if(total_positions > 0) ---> there's no need for this if statement. The condition PositionsTotal() > 0 will already be checked when the loop for begins. If there's no position opened, the loop will simply not start. { Check_TralingStop(); // Run the loop to check and set traling stops. } } void Check_TralingStop () { for(int count =0; count < total_positions; count++) { ulong ticket = PositionGetTicket(count); if(ticket != 0) // select position and copy it's details { ENUM_POSITION_TYPE pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); if(pos_type == POSITION_TYPE_BUY) { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double stopLoss = PositionGetDouble(POSITION_SL); double bidPrice = SymbolInfoDouble(_Symbol,SYMBOL_BID); double askPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK); if(stopLoss <= openPrice && bidPrice > openPrice) // check stop loss is bellow and bid price is above to set traling steps // this will only runs once per trade. { ctrade.PositionModify(ticket, stopLoss, 0); // if you want to keep the take profit, you also need to fetch it using PositionGetDouble } } else if(pos_type == POSITION_TYPE_SELL) // ENUM_POSITION_TYPE can only be BUY or SELL. This if could be omitted, leaving only the else, but I'll leave it here. { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double stopLoss = PositionGetDouble(POSITION_SL); double bidPrice = SymbolInfoDouble(_Symbol,SYMBOL_BID); double askPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK); if(stopLoss >= openPrice && askPrice < openPrice) // check stop loss is above and ask price is below to set traling stop // this will only runs once per trade. { ctrade.PositionModify(ticket, stopLoss, 0); // read the comment in the other if block above } } } } }ps: this code was not tested/run. It may contain typos or other errors, but may serve as a starting point for you.

- www.mql5.com
Your code is incomplete; I'll assume you are using a CTrade class from the standard library to send orders and manage open positions.
Let's say you have defined CTrade object like this:
You can just call the function PositionModify from the CTrade class using the created object like this:
Notice the PositionModify is part of the CTrade class. Since it is not a static function, it can only be called from an instantiated (created) object of the CTrade class type.
Also, pay attention to some things:
1 - Your for loop runs indefinitely. See that the condition
If there's a opened position (i.e., PositionsTotal > 0), the count variable keeps incrementing to infinity, so the loop won't break. The correct loop would be defined like this:
where the loop keeps going while count is lower than the amount of positions (notice that the last position index is PositionsTotal() - 1, thus the last loop value should also be the total positions - 1).
2 - You call PositionsTotal() too many times. This reduces the code speed. If you do not open/close any positions in this loop, call it only once (before the loop starts), save the result in a variable and work with it.
3 - PositionGetTicket automatically selects a position for you to work with. There's no need to call PositionSelectByTicket, this also reduces the code speed. If you want to check if the call was succesful, just compare the result of the value of the ticket - if it is 0, the function failed.
4 - Same as 2, you can call PositionGetInteger to fetch the position type only once, store the result in a variable and work with it to improve the code speed.
5 - If you wanna check a condition that can only have one outcome possible at a time, use an 'if else' statement to avoid checking all the 'if' blocks. Otherwise, the program will check the same condition multiple times, also reducing the code speed.
I'll leave my suggestion on how the code could be improved:
Thank you so much brother... I'll got the idea. i'll start from here...

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
This is the Custom function I'm about to use for check and set trailing stops. loop picks each position and check the trailing condition and set it if needed.
but The PostionModify(); function is no appear in the meta editor even though I type it. I have no idea what the reason is. Cz I'm still in the learning period.
Can someone tell me the correct method to do this and what is the correct function to set trilling stops..?