Trailling StopLoss

 

Hello everyone,


I hope you are doing well this morning.


You have helped me a lot before, I really appreciate this community :)


So!

I have a little problem with my Trailing Stop Loss

extern int TraillingStart = 15;
extern int TraillingStep = 2;
extern int StopLoss = 100;
extern int TakeProfit = 100;


orderID = OrderSend(...............)

else // if order already open, if modify here
        {
        Alert("Order already open");
           if(OrderSelect(orderID,SELECT_BY_TICKET)==true); // I take my current order
           {
            int orderType = OrderType();// 0 = long(buy) // 1 = short(sell) //
            double NewStopLoss = ( Bid - ( TraillingStep * Pips )); //NewSL = (CurrentPrice - (TrailingStep))
            if(orderType ==0)//buy
               {
               if(( Bid - OrderOpenPrice() ) > (TraillingStart * Pips )) // Bid - OrderOpenPrice for exemple(1.16-1)=0.16 and my TraillingStart is 15 pips
               { 
               bool Ans =OrderModify(orderID,OrderOpenPrice(),NewStopLoss,OrderTakeProfit(),0);
               if(Ans == true)
               {
               Alert("Order modify, Congratulation");
               }
               }
               }
            }
            }

When I start this code in the backtest, when the Bid - OrderOpenPrice is much higher than my traillingStart (15 pips) then there is a hundred OrderModify and in a few seconds my SL this teleports to -15 pips below my current Bid .. the SL is able to decrease and increase but it does not go below the OrderOpenPrice ..


Here are the errors:


1. The price must not be teleported but before step by step with TraillingStep

2. The SL must not be able to grow (go down in buy)


I tried a lot of manipulation but nothing works .. And so as usual when I'm up against the wall I come here because I know you will surely see what is wrong with my code ..


thanks a lot for your help



Information in case ...

I would like a TraillingStop Break Event (I think its called that)

If the current market price is above my TraillingStart (15 pips) then I would like my StopLoss to decrease (go up in buy) and be 15 pips below the current market price. but if the market drops then the StopLoss should not move .. it will only re-cap if the current market price is above 15 pips of the new StopLoss ..


Thank you

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
 
  1. orderID = OrderSend(...............)

    Always post all relevant code (using Code button) or attach the file.

    We can't know what type orderID is, or whether it is globally declared or static. If not, you've lost the ticket number and your OrderSelect will never work.

  2.            if(OrderSelect(orderID,SELECT_BY_TICKET)==true); // I take my current order
    
    You select by ticket and check if it is a buy, but never check if the order has already closed.
  3. ThomasBrd51: then there is a hundred OrderModify and in a few seconds
    ERR_NO_RESULT
    You Server
    Change the SL to X It is at X!
    Change the SL to X It is at X!
    Change the SL to X You are insane
    Insanity: doing the same thing over and over again and expecting different results.
              Unknown

    Compute the new value, then check that you are moving the existing value at least a tick.

  4. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

Reason: