else and else if?

 
When should you have else and else if, is there a rule to follow, I've seen both in code?
And I can't get a clear answer from the documentation.
 
Christer Lundqvist:
When should you have else and else if, is there a rule to follow, I've seen both in code?
And I can't get a clear answer from the documentation.

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.


 
I in my naivety had thought to have three BE with an else in between I can get one more to work but not two or three, what am I doing wrong?
 

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);
     }
  }

  }

 
Christer Lundqvist #:

This is how I thought it would look.

v


i think you are missing the first if there , if i'm not mistaken

 
Christer Lundqvist #:

This is how I thought it would look.


I'm completely lost, should it be an if in BE_Small, what do you advise?

 
Christer Lundqvist #:

I'm completely lost, should it be an if in BE_Small, what do you advise?

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.
 
Christer Lundqvist #:
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?

 
I have seen it done, but it was a Trailing Stop, instead of running it in it would go on the candles he had after another trade, then the EA followed, as he went on the candle an array so many candles backwards we say 10, as there was a new candle the TS fell by as many pips the candle had gone down, finally he was down where he took the Trade, so in the future he could make money, he who did it all, he had a normal day job.
Doesn't EA need someone to tell him how to do it, the first one goes great, but it's when the second one comes in that it gets tricky, if he compiles properly but he only takes one of them, so if I could connect them together, he should know the ones he has driven, and take the next in order, so he drives all three one after the other.
 
Christer Lundqvist #:
I have seen it done, but it was a Trailing Stop, instead of running it in it would go on the candles he had after another trade, then the EA followed, as he went on the candle an array so many candles backwards we say 10, as there was a new candle the TS fell by as many pips the candle had gone down, finally he was down where he took the Trade, so in the future he could make money, he who did it all, he had a normal day job.
Doesn't EA need someone to tell him how to do it, the first one goes great, but it's when the second one comes in that it gets tricky, if he compiles properly but he only takes one of them, so if I could connect them together, he should know the ones he has driven, and take the next in order, so he drives all three one after the other.

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 .