Expert Adviser for hiding Stop Loss / Take Profit points from your Broker

Introduction

It is common practice for professional trades to hide their stop loss / take profit from their brokers. Either from keeping their strategy to the themselves or from the fear that their broker works against them.
In this post, I will share a simple, stand alone expert adviser which draws the SL / TP on the screen using the bid price. Thus, as soon as the line is hit, the position is closed.

Code

Initialisation Code


extern double DefaultSLPIPS=15;//Default Stop Loss Pips
extern double DefaultTPPips=25;//Default Take Profit Pips

const string SYMBOL=Symbol();
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(IsTradeAllowed()==false)
     {
      Alert("Error, please switch on Auto Trading button");
      return INIT_FAILED;
     }

   DefaultSLPIPS=DefaultSLPIPS*(MarketInfo(SYMBOL,MODE_DIGITS)==4?MarketInfo(SYMBOL,MODE_POINT):10*MarketInfo(SYMBOL,MODE_POINT));
   DefaultTPPips=DefaultTPPips*(MarketInfo(SYMBOL,MODE_DIGITS)==4?MarketInfo(SYMBOL,MODE_POINT):10*MarketInfo(SYMBOL,MODE_POINT));
//---
   return(INIT_SUCCEEDED);
  }

In this part, I am using two default setting for the SL / TP as external input. Meaning, the default stop loss will be 15 pips away, while the take profit 25 pips away.
In the OnInit function I am checking if trading is allowed, otherwise I am throwing an error. Otherwise, I adjust the SL / TP to the broker decimal points. If the broker supports mini pips I multiply the value with 10, otherwise I leave it unchanged, i.e:
  1. If a broker shows the price as 1.1234, the value is unchanged 15 / 25.
  2. Else if a broker shows the price as 1.12345, the value becomes 150 / 250.

Main Code

void OnTick()
  {
//---
   for(int i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS) && StringCompare(OrderSymbol(),SYMBOL)==0)
        {
         if(ObjectFind(0,StringConcatenate(IntegerToString(OrderTicket()),"-SLLine"))<0)
            HLineCreate(0,StringConcatenate(IntegerToString(OrderTicket()),"-SLLine"),0,(OrderType()==OP_BUY?Bid-DefaultSLPIPS:Bid+DefaultSLPIPS),clrRed);
         else if(ObjectFind(0,StringConcatenate(IntegerToString(OrderTicket()),"-SLLine"))>=0 && (
            (OrderType()==OP_BUY && Bid<ObjectGetDouble(0,StringConcatenate(IntegerToString(OrderTicket()),"-SLLine"),OBJPROP_PRICE)) ||
            (OrderType()==OP_SELL && Bid>ObjectGetDouble(0,StringConcatenate(IntegerToString(OrderTicket()),"-SLLine"),OBJPROP_PRICE))) &&
            OrderClose(OrderTicket(),OrderLots(),Bid,1)==false)
            Alert(StringConcatenate("Error closing ticket #",IntegerToString(OrderTicket())," on SL, please close manually"));

         if(ObjectFind(0,StringConcatenate(IntegerToString(OrderTicket()),"-TPLine"))<0)
            HLineCreate(0,StringConcatenate(IntegerToString(OrderTicket()),"-TPLine"),0,(OrderType()==OP_BUY?Bid+DefaultTPPips:Bid-DefaultTPPips),clrGreen);
         else if(ObjectFind(0,StringConcatenate(IntegerToString(OrderTicket()),"-TPLine"))>=0 && (
            (OrderType()==OP_BUY && Bid>ObjectGetDouble(0,StringConcatenate(IntegerToString(OrderTicket()),"-TPLine"),OBJPROP_PRICE)) ||
            (OrderType()==OP_SELL && Bid<ObjectGetDouble(0,StringConcatenate(IntegerToString(OrderTicket()),"-TPLine"),OBJPROP_PRICE))) &&
            OrderClose(OrderTicket(),OrderLots(),Bid,1)==false)
            Alert(StringConcatenate("Error closing ticket #",IntegerToString(OrderTicket())," on TP, please close manually"));
        }

   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && StringCompare(OrderSymbol(),SYMBOL)==0)
        {
         if(ObjectFind(0,StringConcatenate(IntegerToString(OrderTicket()),"-SLLine"))>=0)
            ObjectDelete(0,StringConcatenate(IntegerToString(OrderTicket()),"-SLLine"));

         if(ObjectFind(0,StringConcatenate(IntegerToString(OrderTicket()),"-TPLine"))>=0)
            ObjectDelete(0,StringConcatenate(IntegerToString(OrderTicket()),"-TPLine"));
        }
  }

The main OnTick function has two parts:

  1. Running over the currently open orders and either drawing two lines, one red for SL, one green for TP in the predefined distance from the current bid price. Or, if the lines exists already, checking if the bid price touched any of them which then results with closing this position.
  2. Running over the currently closed orders and checking if there is any line drawn. If yes, deleting this line from the chart. 
That's all, with each tick we will check the current status and draw, close orders and remove lines form the chart without having to fill in the SL / TP to the broker. 

Graphical Utility


bool HLineCreate(const long chart_ID=0,const string name="HLine",const int sub_window=0,double price=0,const color clr=clrRed,const ENUM_LINE_STYLE style=STYLE_SOLID,const int width=1) export
  {
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

   ResetLastError();

   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))
     {
      Print(__FUNCTION__,": failed to create a horizontal line! Error code = ",GetLastError());
      return(false);
     }

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,true);

   return(true);
  }
To draw the line, I use this function which create a line which can be dragged on the chart, thus giving the trader the ability to modify the SL / TP visually. 

Usage

Once attached to the chart, the expert adviser scans the open orders to attach lines for them and then waits for new one. So if there are no open orders for this product, the screen will look like that:

As soon as I open a position, in this case a long, the red stop loss will be drawn below and the green take profit above it:

As soon a the bid price will hit any of these prices, the order will get closed by the expert adviser and the lines will be deleted.

Summary

In this post, I introduced a straight forward single file expert adviser which you can use as is to control your SL / TP points without setting it on the position. It draws a SL and TP lines which can be dragged to new values upon trader discretion. 

Besides that:
- I published my profile on Fiverr, the largest site for freelancers - https://www.fiverr.com/meshulro, so if you are in need of any coding for your ideas, you are welcome to submit your requests there and I will deliver a high standards, Swiss quality result.
- My algorithmic trading system has now more than 1.5 million $ investors funds, for more information visit - https://www.darwinex.com/darwin/GFA.4.21

Comments