Understanding Trailing Stop Loss

shanmugapradeep

Active Trader
Dec 18, 2020
112
4
34
38
Hello,

I am finding little difficult in understanding Trailing Stop loss. How does it work, does it take value from Stop Limit, Trade Entry Price or Current Price or Highest Peak in Favorable direction.

After reading some blogs about Trailing Stop, This what i understand :-

  1. Condition: If the current price is greater than the entry price.
  2. Condition: If the difference between the current price and the entry price is greater than the trailing stop value.
  3. Action: Set a new stop loss level at the current highest price minus the trailing stop value.
  4. Additional Condition(favorable direction): If the difference between the new stop loss level and the current price is greater than the trailing stop value.
  5. Additional Action (favorable direction): Set a new stop loss level at the current price minus the trailing stop value.
Is this correct?
 
  • 👍
Reactions: penaltev

hayseed

Master Trader
Jul 27, 2010
1,048
262
149
usa
Hello,

I am finding little difficult in understanding Trailing Stop loss. How does it work, does it take value from Stop Limit, Trade Entry Price or Current Price or Highest Peak in Favorable direction.

After reading some blogs about Trailing Stop, This what i understand :-

  1. Condition: If the current price is greater than the entry price.
  2. Condition: If the difference between the current price and the entry price is greater than the trailing stop value.
  3. Action: Set a new stop loss level at the current highest price minus the trailing stop value.
  4. Additional Condition(favorable direction): If the difference between the new stop loss level and the current price is greater than the trailing stop value.
  5. Additional Action (favorable direction): Set a new stop loss level at the current price minus the trailing stop value.
Is this correct?
//------

it depends on how you code it...... traditionally the trailing stop is set by using ask or bid, not the current highest price......

modifying large numbers of orders at every tick can cause problems.....

occasionally the stoploss is set by other creative means..... such as, moving average, a trend line, zigzag points and similar.....

it all depends on how you want to code it......h
 

shanmugapradeep

Active Trader
Dec 18, 2020
112
4
34
38
it depends on how you code it...

Here is my code. it makes changes to every tick.

MQL4:
extern int Trailing = 5;
 
if(Digits == 3 || Digits == 5)
      Gd_376 = 10.0 * Point;
   else
      Gd_376 = Point;
 
double buyorderprice()
  {
   int cmd_16;
   double Ld_ret_0 = 0;
   double Ld_8 = 0;
   for(int pos_20 = OrdersTotal() - 1; pos_20 >= 0; pos_20--)
     {
      Sleep(1);
      if(OrderSelect(pos_20, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() != Symbol() || OrderMagicNumber() != BuyLimitMagic)
            continue;
         cmd_16 = OrderType();
         if(cmd_16 == OP_BUY)
           {
            Ld_ret_0 += OrderOpenPrice() * OrderLots();
            Ld_8 += OrderLots();
           }
        }
     }
   if(Ld_8 > 0.0)
      Ld_ret_0 = NormalizeDouble(Ld_ret_0 / Ld_8, MarketInfo(OrderSymbol(), MODE_DIGITS));
   return (Ld_ret_0);
  }
 
  void buytrailing()
  {
   double price_0 = buyorderprice();
   for(int pos_8 = OrdersTotal() - 1; pos_8 >= 0; pos_8--)
     {
      OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == BuyLimitMagic)
        {
         if(OrderType() == OP_BUY)
           {
            if(Bid - price_0 > Gd_376 * Trailing)
              {
               if(OrderStopLoss() < Bid - Gd_376 * Trailing)
                 {
                  OrderModify(OrderTicket(), price_0, Bid - Gd_376 * Trailing, OrderTakeProfit(), 0, Blue);
                  return;
                 }
              }
           }
        }
     }
  }
 

hayseed

Master Trader
Jul 27, 2010
1,048
262
149
usa
Here is my code. it makes changes to every tick.
//------

the first function is returning the average lot weighted price...... average lot weighted price is the breakeven price of multiple orders with multiple open prices and multiple lot sizes.....

the second function uses that 'breakeven price to determine whether or not to modify the orders......

to me, that does not look feasible....... there might be some martingale type order setup where it would....... just shootin from the hip i'd say just use the tradition trailing stop logic......

try to use more ( ) to keep operation precedence clear......h

//------ i edited your code for clarity......

MQL4:
extern int Trailing = 5;
 
if(Digits == 3 || Digits == 5)
      point = 10.0 * Point;
   else
      point = Point;
 
 
 
double buyorderprice()
  {
   int type;
   double ave = 0;
   double lots = 0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      Sleep(1);
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() != Symbol() || OrderMagicNumber() != BuyLimitMagic)
            continue;
         type = OrderType();
         if(type == OP_BUY)
           {
            ave += OrderOpenPrice() * OrderLots();         // average lot weighted price
            lots += OrderLots();
           }
        }
     }
   if(lots > 0.0)
      ave = NormalizeDouble(ave / lots, MarketInfo(OrderSymbol(), MODE_DIGITS));
   return (ave);
  }
 
 
 
  void buytrailing()
  {
   double price = buyorderprice();
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == BuyLimitMagic)
        {
         if(OrderType() == OP_BUY)
           {
            if((Bid - price) > (point * Trailing))                      //  use (   )
              {
               if(OrderStopLoss() < (Bid - (point * Trailing)))         //  use (  )
                 {
                  OrderModify(OrderTicket(), price, Bid - (point * Trailing), OrderTakeProfit(), 0, Blue);    //  use (  )  and OrderOpenPrice() not price
                  return;
                 }
              }
           }
        }
     }
  }
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
18,622
1,367
144
Odesa
www.earnforex.com
Condition: If the current price is greater than the entry price.
If we are talking about buy order, yes.
Condition: If the difference between the current price and the entry price is greater than the trailing stop value.
That's an unnecessary condition for a classic trailing stop. This is a condition for "trailing stop on profit". So, if you want the TS to kick in only at a certain level of profit you could use such a condition. But it's best to use a separate value for checking there, not the actual trailing stop value. For example, you might want the TS to kick in only when you trade is in 20 pips of profit, but you want your TS distance to be 15 pips.
Action: Set a new stop loss level at the current highest price minus the trailing stop value.
That's almost like the classic TS works, but you don't want to use the High price, but rather Bid (for buy orders).
Additional Condition(favorable direction): If the difference between the new stop loss level and the current price is greater than the trailing stop value.
You mean the old stop-loss? Or rather the current stop-loss. That's the most important condition in the classic TS.
Additional Action (favorable direction): Set a new stop loss level at the current price minus the trailing stop value.
This should be the only action if we are talking about the classic TS.
 

fargana

Active Trader
Nov 14, 2022
139
20
29
34
Hello,

I am finding little difficult in understanding Trailing Stop loss. How does it work, does it take value from Stop Limit, Trade Entry Price or Current Price or Highest Peak in Favorable direction.

After reading some blogs about Trailing Stop, This what i understand :-

  1. Condition: If the current price is greater than the entry price.
  2. Condition: If the difference between the current price and the entry price is greater than the trailing stop value.
  3. Action: Set a new stop loss level at the current highest price minus the trailing stop value.
  4. Additional Condition(favorable direction): If the difference between the new stop loss level and the current price is greater than the trailing stop value.
  5. Additional Action (favorable direction): Set a new stop loss level at the current price minus the trailing stop value.
Is this correct?

Trailing stop loss is intended to maintain closing price on your position (stop loss price) as a function of a current price. Two cases:

If a price change is favourable , trailing stop loss adjusts closing price so it is equal (1-C)*current price where C is stop loss level in relative terms (%)

If price changes against you, stop loss level remains intact.

Closing price is updated after every new tick.
 
  • 👍
Reactions: penaltev