[MQL4 Code Help] How to convert classic Trailing Stop Loss to Virtual Trailing Stop Loss

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
39
Hello,

Normally, Stop Loss execute in broker server and it send a data of Stop Loss value to broker. This has a disadvantage of too many operation and also Stop Hunting from most of the Forex brokers. To avoid this, i heard there is a concept called Virtual Stop loss which act same as normal but it does not send value to Broker server instead it keep in the values in Local meta trader.

Classic Trailing Stop Loss code :

MQL4:
void TrailingStopLoss(int TrailingStopLoss_magic)
  {
// Loop through all open orders
for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
double TrailingStopLoss_entryPrice = OrderOpenPrice();
// Check if the order is a buy order with magic number 1
if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_BUY)
           {
if(Bid - (TrailingStopLoss_entryPrice + Trailing_TP * gPips) > gPips * Trailing_Gap)
              {
if(OrderStopLoss() < Bid - gPips * Trailing_Gap || OrderStopLoss() == 0)
                 {
ResetLastError();
                  RefreshRates();
gOrderModifyCheck = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - gPips * Trailing_Gap, OrderTakeProfit(), 0, clrBlue);
if((_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR) || !gOrderModifyCheck)
Print(__FUNCTION__ + " => Buy Order Error Code : " + GetLastError());
                 }
              }
           }
 
// Check if the order is a sell order with magic number 2
if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_SELL)
           {
if((TrailingStopLoss_entryPrice - Trailing_TP * gPips) - Ask > gPips * Trailing_Gap)
              {
if(OrderStopLoss() > Ask + gPips * Trailing_Gap || OrderStopLoss() == 0)
                 {
ResetLastError();
                  RefreshRates();
gOrderModifyCheck = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + gPips * Trailing_Gap, OrderTakeProfit(), 0, clrPink);
if((_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR) || !gOrderModifyCheck)
Print(__FUNCTION__ + " => Sell Order Error Code : " + GetLastError());
                 }
              }
           }
        }
     }
  }



I have no idea how should i code or convert this to Virtual Trailing Stop Loss. Can someone provide me a idea or sample code how to convert this code which work same as this code except modifying real stop loss value.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,089
1,479
144
Odesa
www.earnforex.com
To have a "virtual trailing stop" running you'd need one two-dimensional array and two cycles.

The two-dimensional array would store the order's ticket and the order's virtual SL.

The first cycle would run through all open orders and make sure that your two-dimensional array is up to date. It would be somewhat similar to your current cycle, but it wouldn't use OrderModify(), instead it would just update the array. It would be also making sure to remove closed orders from the array.

The second cycle would run through the array and check whether the market price has reached (or went over) the SL of each of the orders and use OrderClose() to close the orders that have their virtual SL reached by the price.

PS: I don't recommend doing this. SLs are best handled on the broker's side.