Virtual Trailing Stop

To add comments, please log in or register
Pedro Severin
1218
Pedro Severin  

Hello guys,

I'm trying to do a virtual trailing stop to replace the normal one. The main idea is to use it in a scalper, but the results I'm getting are not even close when using the normal TS. Why is that? I'll leave my code here to see if anyone can help me out a little bit with this.


void trailing(bool withSpread)
  {
   int Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));
   int  StopLevel=0;//(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
   double stops;
   for(int k=OrdersTotal()-1; k>=0; k--)
     {
      if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
           {
            if(OrderType()==OP_BUY)
              {
               stops=MathMax(OrderOpenPrice()+breakEven*Point(),Bid-SL*_Point);
               if(withSpread)
                  stops=MathMax(OrderOpenPrice()+Spread*Point(),Bid-SL*_Point);

               if(useVirtualTS && stops>vts && (Bid-StopLevel*_Point-Spread*_Point)>OrderOpenPrice())
                 {
                  vts=stops;

                 }
               if(vts>=Bid)
                 {
                  OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrYellow);
                 }
               if(!useVirtualTS && stops>OrderStopLoss() && (Bid-StopLevel*_Point-Spread*_Point)>OrderOpenPrice())
                 {
                  OrderModify(OrderTicket(),Ask,stops,OrderTakeProfit(),0,clrNONE);
                 }
               return;
              }


            else
              {
               if(OrderType()==OP_SELL)
                 {

                  stops=MathMin(OrderOpenPrice()-breakEven*_Point,Ask+SL*_Point);
                  if(withSpread)
                     stops=MathMin(OrderOpenPrice()-Spread*_Point,Ask+SL*_Point);

                  if(useVirtualTS && stops<vts && (OrderOpenPrice()-StopLevel*_Point-Spread*_Point)>Ask)
                    {
                     vts=stops;
                     
                    }
                  if(vts<=Ask)
                    {
                     OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrYellow);
                    }

                  if(!useVirtualTS && stops<OrderStopLoss() && (OrderOpenPrice()-StopLevel*_Point-Spread*_Point)>Ask)
                    {
                     OrderModify(OrderTicket(),Bid,stops,OrderTakeProfit(),0,clrNONE);
                    }
                  return;
                 }
              }
           }
        }
     }
  }

The reason why I set "StopLevel" to 0 is because the broker I am trying to make this work has 25 points of stoplevel, so basically I want to bypass that with the virtual trailing stop.


The original trailing code is this:

void trailing(bool withSpread)
  {
   int Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));
   int  StopLevel=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
   double stops;
   for(int k=OrdersTotal()-1; k>=0; k--)
     {
      if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
           {
            if(OrderType()==OP_BUY)
              {
               stops=MathMax(OrderOpenPrice()+breakEven*Point(),Bid-SL*_Point);
               if(withSpread)
                  stops=MathMax(OrderOpenPrice()+Spread*Point(),Bid-SL*_Point);

               if(stops>OrderStopLoss() && (Bid-StopLevel*_Point-Spread*_Point)>OrderOpenPrice())
                 {
                  OrderModify(OrderTicket(),Ask,stops,OrderTakeProfit(),0,clrBlue);
                  return;
                 }
              }

            else
              {
               if(OrderType()==OP_SELL)
                 {

                  stops=MathMin(OrderOpenPrice()-breakEven*_Point,Ask+SL*_Point);
                  if(withSpread)
                     stops=MathMin(OrderOpenPrice()-Spread*_Point,Ask+SL*_Point);

                  if(stops<OrderStopLoss() && (OrderOpenPrice()-StopLevel*_Point-Spread*_Point)>Ask)
                    {
                     OrderModify(OrderTicket(),Bid,stops,OrderTakeProfit(),0,clrRed);

                     return;
                    }
                 }
              }

           }
        }
     }
  }

This one works pretty well on other brokers that has 0 Stop levels.

In fact, this are 2 backtests:

Broker 1 - 0 pts for Stop levels:


Broker 2 - 25 pts for Stop levels:


So, as you can see, the results are very different as the way the EA exits trades is using the stoploss. Any idea on how to improve this? This scalper is a creation of my own. And by the way, should I believe this results? It's backtested with dukascopy tick data on M1 timeframe. Only optimized for 3 months and then backtested for the whole year.

To add comments, please log in or register