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.


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()
      Alert(“Error, please switch on Auto Trading button”);
      return INIT_FAILED;

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)
         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))) &&
            Alert(StringConcatenate(“Error closing ticket #”,IntegerToString(OrderTicket()),” on SL, please close manually”));
         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))) &&
            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)
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

      Print(__FUNCTION__,”: failed to create a horizontal line! Error code = “,GetLastError());

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. 


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.


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 –, 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 –

This entry was posted by . Bookmark the . Titled

Leave a Reply

Your email address will not be published. Required fields are marked *