Simple, if breaks the control to two paths, depending on a condition when two paths are needed.
When you write an if, you break the flow to two paths(if (cond) X1 else X2). When one of the paths(X1 or X2) needs another break into two flows(whether in the X1 part or the X2 part), you put another if.
This is how I thought it would look.
void TrailingStopBE_Small (ENUM_ORDER_TYPE type, double profit, double add) //set Stop Loss to open price if in profit { profit = NormalizeDouble(profit, Digits()); int total = PositionsTotal(); for(int i = total-1; i >= 0; i--) { if(PositionGetTicket(i) <= 0) continue; if(PositionGetInteger(POSITION_MAGIC) != MagicNumber || PositionGetString(POSITION_SYMBOL) != Symbol() || PositionGetInteger(POSITION_TYPE) != type) continue; MqlTick last_tick; SymbolInfoTick(Symbol(), last_tick); double SL = PositionGetDouble(POSITION_SL); double openprice = PositionGetDouble(POSITION_PRICE_OPEN); ulong ticket = PositionGetInteger(POSITION_TICKET); if(type == ORDER_TYPE_BUY && last_tick.bid > openprice + profit && (NormalizeDouble(SL, Digits()) <= 0 || openprice > SL)) myOrderModify(ORDER_TYPE_BUY, ticket, openprice + add, 0); else if(type == ORDER_TYPE_SELL && last_tick.ask < openprice - profit && (NormalizeDouble(SL, Digits()) <= 0 || openprice < SL)) myOrderModify(ORDER_TYPE_SELL, ticket, openprice + add, 0); } else if (TrailingStopBE_Medi (ENUM_ORDER_TYPE type, double profit, double add) //set Stop Loss to open price if in profit) { profit = NormalizeDouble(profit, Digits()); int total = PositionsTotal(); for(int i = total-1; i >= 0; i--) { if(PositionGetTicket(i) <= 0) continue; if(PositionGetInteger(POSITION_MAGIC) != MagicNumber || PositionGetString(POSITION_SYMBOL) != Symbol() || PositionGetInteger(POSITION_TYPE) != type) continue; MqlTick last_tick; SymbolInfoTick(Symbol(), last_tick); double SL = PositionGetDouble(POSITION_SL); double openprice = PositionGetDouble(POSITION_PRICE_OPEN); ulong ticket = PositionGetInteger(POSITION_TICKET); if(type == ORDER_TYPE_BUY && last_tick.bid > openprice + profit && (NormalizeDouble(SL, Digits()) <= 0 || openprice > SL)) myOrderModify(ORDER_TYPE_BUY, ticket, openprice + add, 0); else if(type == ORDER_TYPE_SELL && last_tick.ask < openprice - profit && (NormalizeDouble(SL, Digits()) <= 0 || openprice < SL)) myOrderModify(ORDER_TYPE_SELL, ticket, openprice + add, 0); } } }
Define the logic of what you want to do.
Why do you need two for loops, running on the same start..end indexes ? Try to think how you would do it manually, do you need to run over the positions twice?
Also specifying {,} is for enclosing more than one statement inside those brackets. You seem to not use that correctly, you close a block } which was not opened (and it was not needed to open as there was just one statement in the else if).
I want 3 Break Even Small, Medi and Large, so I can set them to different values, for future trading, I go with long sequences, but I only have a few Trades, 0 to 5-700 pips on each trade, then I don't have to guard the computer so much, once a day is enough. Therefore I need 3 BE.
1. Run over all positions - once
2. Select each position
3. Decide the type of BE you need for the position selected(if statements inside the for), and execute it
Do you think it can be done like that?
So you want to trail based on the candles after the trade opened in other words .
You would have to find the candle where the position started first , for each position and run the loop or you could deploy custom structures and keep track of it after the trade opens . You would also need saving / loading and "catching up" however in this case .
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use