Cant find how to set Stop-loss using double instead of pips

 
Greetings developers,
Today I seek your help in a problem that has almost driven me insane.
Thank you in advance for your time, and if i find the solution I'll post it here.

Now for the problem:
Wherever I look I see stop-loss in pips in this form,


int TP_Points = 300;
double norm_tp;

norm_tp = NormalizeDouble(Symbol.Ask() + (TP_Points * _Point), _Digits);

But i already have the stoploss values(double) calculated beforehand and would like to use them as is.

These are my attempts,

double SL=NormalizeDouble((sell_sl) , _Digits);
double SL=NormalizeDouble((sell_sl)*_Point , _Digits);
double SL=NormalizeDouble((sell_sl)*_Point , _Digits);
double SL=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID)+(((sell_sl-sell_stop))* _Point) , _Digits));

//none of these work

trade.Sell(0.1,_Symbol,SymbolInfoDouble(_Symbol,SYMBOL_BID),SL);


when i run this code, the trade places but no stop-loss appears or it prints to the log something to the effect of "invalid stoploss, cant place trade" 
Kindly point me to examples/ topics where stop-loss is set in double instead of pips.

If no such example exists please point out my mistake or if you would be so kind, fix the line for me.


Sincerely,
Hassan.
 
  1. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum #6 (2017)

  2. norm_tp = NormalizeDouble(Symbol.Ask() + (TP_Points * _Point), _Digits);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  3. You used NormalizeDouble, Its use is usually wrong, as it is in your case.

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

 
William Roeder #:
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  2. You used NormalizeDouble, Its use is usually wrong, as it is in your case.

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

i understant it may be wring but that part wasnt my question, i just want to use the sl in double form, assume its just the close of a candle, i want the sl to be at that price level.

 
         double SL=NormalizeDouble((sell_sl) , _Digits);
         double TP = NormalizeDouble((sell_stop - (sell_sl-sell_stop)*2), _Digits);
         float vol = (balance/100)
         trade.Sell(0.1,_Symbol,SymbolInfoDouble(_Symbol,SYMBOL_BID),SL,TP);

figured it out, this works, had to fix some other errors as well
 
Hassan Shafqat #:
         double SL=NormalizeDouble((sell_sl) , _Digits);
         double TP = NormalizeDouble((sell_stop - (sell_sl-sell_stop)*2), _Digits);
         float vol = (balance/100)
         trade.Sell(0.1,_Symbol,SymbolInfoDouble(_Symbol,SYMBOL_BID),SL,TP);

figured it out, this works, had to fix some other errors as well

and I was gonna say...the SL has to be relative to the market price in some way


so you could do 

double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);


trade.Sell(0.1,_Symbol, bid ,SL, TP, "sell position");

or

double candleClose = iClose(Symbol(), Period(), 1);   //close price of previous candle
double SL = candleClose + 200 * _Point;
double TP = candleClose - TP_Target *_Point;

trade.Sell(0.1,_Symbol,candleClose,SL,TP);


Logically SL is over the current market price for Sell positions and TP is under the current price, and so on and so fourth

 
Conor Mcnamara #:

and I was gonna say...the SL has to be relative to the market price in some way


so you could do 

or


Logically SL is over the current market price for Sell positions and TP is under the current price, and so on and so fourth

Thank you, I think your way works fine too but I needed to just set the sl without using points at all, makes me curious about a way to convert the difference in the sl and entry price into point values and use them like that
 
Hassan Shafqat #:
Thank you, I think your way works fine too but I needed to just set the sl without using points at all, makes me curious about a way to convert the difference in the sl and entry price into point values and use them like that

I you want entry price there's already a constant for that actually

double entry_price = PositionGetDouble(POSITION_PRICE_OPEN); 

and it is already in points