How to set correct stoploss and takeproffit on BUYSTOP orders

 

I am writing an EA to place buystop and sellstop orders, everything works great but I do not think I am calculating the sl and tp values correctly in my code.

 

in the code:

BST = 10

SL = 30

TP = 30

xecn = 10

 

the broker uses a 5 digit quote and spread is 19

 

here is the code for buystop:

      double priceBUY  = NormalizeDouble(Ask +  BST       *xecn * Point, Digits);
      double slBUY     = NormalizeDouble(Bid + (BST - SL) *xecn * Point, Digits);
      double tpBUY     = NormalizeDouble(Bid + (TP + BST) *xecn * Point, Digits);
      orderticket = OrderSend(Symbol(),OP_BUYSTOP ,lots, priceBUY, slippage, slBUY, tpBUY, "BuyStop", MagicNum, Time[0] + expr);

 

for sellstop:

      double priceSELL = NormalizeDouble(Bid -  BST       *xecn * Point, Digits);
      double slSELL    = NormalizeDouble(Ask + (SL - BST) *xecn * Point, Digits);
      double tpSELL    = NormalizeDouble(Ask - (TP + BST) *xecn * Point, Digits);
    
     orderticket = OrderSend(Symbol(),OP_SELLSTOP ,lots, priceSELL, slippage, slSELL, tpSELL, "BuyStop", MagicNum, Time[0] + expr);

 

 

An example order from testing is as follows:

2015.10.26 15:20:09.493    2015.07.23 09:00  TriangleTS v3.1 EURUSD,M30: open #25 buy stop 0.10 EURUSD at 1.09971 sl: 1.09652 tp: 1.10252 ok

2015.10.26 15:20:09.493    2015.07.23 09:00  TriangleTS v3.1 EURUSD,M30: open #26 sell stop 0.10 EURUSD at 1.09752 sl: 1.10071 tp: 1.09471 ok
 

When i do the math for the buystop order, I get a stop loss of 319 pips (1.09971-1.09652) and take profit of 281 pips (1.10252 - 1.09971), when I am expecting them both to be 300. 

 

Should I be using Ask to calculate my stop loss and take profit?? 

 

please help this very confused new guy

 
Algo-FX: When i do the math for the buystop order, I get a stop loss of 319pips (1.09971-1.09652) and take profit of 281 pips (1.10252 - 1.09971),
  1. 1.09971-1.09652=0.00319 which is 31.9 pips
  2. 1.10252-1.09971=0.00281 which is 28.1 pips
  3. There is Tick, PIP, and Point. They are all different in general. A tick is the smallest change of price. A Point is the least significant digit quoted. In currencies a pip is defined as 0.0001 (or for JPY 0.01)

    On a 4 digit broker a point (0.0001) = pip (0.0001). [JPY 0.01 == 0.01] On a 5 digit broker a point (0.00001) = 1/10 pip (0.00010/10). Just because you quote an extra digit doesn't change the value of a pip. (0.0001 == 0.00010) EA's must adjust pips to points (for mq4.) In currencies a tick is a point. Price can change by least significant digit (1.23456 -> 1.23457)

    In metals a Tick is still the smallest change but is larger than a point. If price can change from 123.25 to 123.50, you have a TickSize of 0.25 and a point of 0.01. Pip has no meaning.

    This is why you don't use TickValue by itself. Only as a ratio with TickSize. See DeltaValuePerLot()


 

HRoeder


  1. 1.09971-1.09652=0.00319 which is 31.9 pips
  2. 1.10252-1.09971=0.00281 which is 28.1 pips
My question is why is it not 30 pips when I entered with a stop loss of 30 and a take profit of 30, what am I doing wrong in my code that I am always off by the spread?
 
Algo-FX:


An example order from testing is as follows:

2015.10.26 15:20:09.493    2015.07.23 09:00  TriangleTS v3.1EURUSD,M30: open #25 buy stop 0.10 EURUSD at 1.09971 sl: 1.09652 tp:1.10252 ok

2015.10.26 15:20:09.493    2015.07.23 09:00  TriangleTS v3.1EURUSD,M30: open #26 sell stop 0.10 EURUSD at 1.09752 sl: 1.10071 tp:1.09471 ok
 

When i do the math for the buystop order, I get a stop loss of 319pips (1.09971-1.09652) and take profit of 281 pips (1.10252 - 1.09971),when I am expecting them both to be 300. 

 

Should I be using Ask to calculate my stop loss and take profit?? 

 double priceBUY  = NormalizeDouble(Ask +  BST       *xecn * Point, Digits);
 double slBUY     = NormalizeDouble(Bid + (BST - SL) *xecn * Point, Digits);
 double tpBUY     = NormalizeDouble(Bid + (TP + BST) *xecn * Point, Digits);
 
orderticket = OrderSend(Symbol(),OP_BUYSTOP ,lots, priceBUY, slippage, slBUY, tpBUY, "BuyStop", MagicNum, Time[0] + expr);

 

Hello,

As you imagine, the problem lies in the fact that you are using Bid price for calculating slBUY and tpBUY in your buy order. In OrderSend() you are simply passing a number, so in fact you are doing Ask-Spread+(BST-SL), and vice versa for sell order 

 

best regards

 
Algo-FX: My question is why is it not 30 pips when I entered with a stop loss of 30 and a take profit of 30, what am I doing wrong in my code that I am always off by the spread?

You buy at the Ask and sell at th Bid. On a buy order, you pay the spread on open. On a sell order you pay the spread on close.

Your buy order is the open=Ask and SL is Bid - 30 pips (close price - 30 pips,) so the difference is 30 pips + spread. On sell orders open=Bid and SL is Ask + 30 pips (close price + 30 pips.) Both of these means the 30 pips does not include the spread.

If you want the 30 pips to include the spread use the open price only in your calculation, not the close price.