else and else if? - page 2

 
Yes, it was like that, but I want a Break Even with fixed values 40/10, 60/20, 90/30 then I want the Trailing Stop to take over the work after 150/40 then he follows with 10 points each time he updates his value.
 
Christer Lundqvist #:
Yes, it was like that, but I want a Break Even with fixed values 40/10, 60/20, 90/30 then I want the Trailing Stop to take over the work after 150/40 then he follows with 10 points each time he updates his value.

I understand . Try this function that does not use specified values but calculates the 2 values . (you need to add the modify codes tho)

void TrailingStopWithGears(ENUM_POSITION_TYPE type, 
                           int first_profit_pts,//the profit in points the process starts
                           int step_profit_pts,//the step of profit in points to increase gears
                           int first_sl_pts,//the first sl additive to open price
                           int step_sl_pts)//the step of the additive per step of profit 
   {
   int total = PositionsTotal();
   for(int i = total-1; i >= 0; i--)
     {
      //good
      if(PositionGetTicket(i) <= 0) continue;
      if(PositionGetInteger(POSITION_MAGIC) != MagicNumber || PositionGetString(POSITION_SYMBOL) != Symbol() || PositionGetInteger(POSITION_TYPE) != type) continue;
      //now we need to do a "little little" here
        //we get the last tick           
          MqlTick last_tick;
          if(SymbolInfoTick(Symbol(), last_tick)){
          //reset the error collector _LastError
            ResetLastError();
            double SL = PositionGetDouble(POSITION_SL);
            double openprice = PositionGetDouble(POSITION_PRICE_OPEN);
            ulong ticket = PositionGetInteger(POSITION_TICKET);    
            //if no error 
            if(_LastError==0){
            //the profit in points 
              int profit_in_points=0;
            //and our order is a buy 
              if(type==POSITION_TYPE_BUY){
              profit_in_points=(int)((last_tick.bid-openprice)/_Point);
              }
            //or a sell
              else if(type==POSITION_TYPE_SELL){
              profit_in_points=(int)((openprice-last_tick.ask));
              }
            /* so now you have the profit in points
               lets say your "Gears" start at 40 
               you subtract profit_pts - 40pts and you can see which gear applies 
               for the trailing stop by flooring the division of that subtraction by the step 
               It's easier in code :
            */
              int distance_in_points_from_start=profit_in_points-first_profit_pts;//first profit pts would be 40 here
              //and you only care if this is positive
                if(distance_in_points_from_start>=0){
                //then you ask (or ax) how many Trailing Stop steps fit in this distance in points ?
                  int trailing_stop_steps=(int)(MathFloor(((double)distance_in_points_from_start)/((double)step_profit_pts)));
                //and then you calculate the additive based on the steps above
                //so the distance the new SL must have from the open price toward the profit side 
                  int sl_distance_from_open=first_sl_pts+trailing_stop_steps*step_sl_pts;
                  //which is the first SL + steps x stepSL
                  //then you apply it to your order based on the order type 
                    
                }  
            }        
          }
     }
}
 
Christer Lundqvist #:

This is how I thought it would look.


Inside your first for loop, you have 3 if statements and one else if. After that you close the bracket for the for loop and you put another else if statement … there is the mistake … that else if doesn’t have an if statement because you close the bracket before 
 

Basically, You use the the if-else conditional operator when you have to make choices. The most basic formal syntax is/prototype is like:

1ST STRUCTURE
if (x==y){
        Print(---your logic here);
}
else {---your logic here---}

2ND STRUCTURE
if (x==y){
        Print(---your logic here);
}
else if (x==z){
        ---your logic here---
}
else if (x != a){
        ---your logic here---
}
___go down as you deem fit___
else{
        ---your logic here---
}

In this case, the first structure can be used to make decisions based on dual choices, while the second structure can be used to make ternary or more choices in this case. 

 
Hello
Control structures are very important
What I had a little trouble understanding is that they make Boolean comparisons, not value games.

I make a complement of the code.

It could very well have other else if.

It gives "openings" 

else if ( c != e){
        ---your logic here---
}
 
Forgive the stupidity of me not sharing the file with you who are trying to help me, you can bring it into MetaEditor and compile it and run it in Strategy tester and try it, then you will see that he only the first one, not number 2 and 3.
 
Now I have solved the problems, 1 and 2 are going fine, but number 3 Large is the problem with he goes up to 30 but he doesn't stop there, when it goes against so last 10 pips can't hold, but he goes down to 20 pips when candle sticks go against me.

At the bottom of the code it starts with an if statement,

if(type == ORDER_TYPE_BUY && last_tick.bid > openprice + profit && (NormalizeDouble(SL, Digits()) <= 20 || openprice > SL)),

where it says 20 , I changed them all, Small = 0, Medi = 10, Large = 20, I have a problem with Large, he can't keep the value of 30 pips so he falls back to 20 pips, so number 2 Medi in the Trade, he can keep it, but not number 3 Large 30 pips, why?

Reason: