Failed Pending Order - Invalid Price

 

Hi,

I am trying to place a buy stop and sell stop 1 pip above and below close price of the previous candle but keep getting invalid price error.

I will appreciate insight on where I am going wrong.

Below is what I have:

//Import Trade Library
#include<Trade\Trade.mqh>

//Create an instance for Ctrade
CTrade trade;

void OnTick()
  {
      //Create variable for time
      static datetime timestamp;
      
      //Get time for current candle on chart
      datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
      
      if (timestamp != time)
      {
         timestamp = time;
         
         //Get account balance
         double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
         
         //Get account equity
         double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
         
         //Get Previous Candle Close Price
         double closePrice=iClose(Symbol(),0,1);
         
         //Get Previous Candle Open Price
         double openPrice=iOpen(Symbol(),0,1);
         
         double cl=NormalizeDouble(closePrice,_Digits);
         
         double op=NormalizeDouble(openPrice,_Digits);
          
         //Create string for the signal
         string signal="";
         
         //Chart output
         //Comment("Open price for previous candle: ",op);
         //Comment("Close price for previous candle: ",cl);
         
         if (closePrice > openPrice)       
         {
            signal="BUY";
            
            //Print Comment on chart
            Comment("Current signal is: ",signal);
         }
         
         if (openPrice > closePrice)       
         {
            signal="SELL";
            
            //Print Comment on chart
            Comment("Current signal is: ",signal);
         }
         
         //Open buy stop order 
         if (signal =="BUY" && Equity>=Balance)
         
         //Buy stop order 1 pip above previous candle close price. SL 5 pips. TP 7 Pips. Order valid for current candle duration
         trade.BuyStop(0.01,(cl+10*_Point),_Symbol,(cl-50*_Point),(cl+70*_Point),ORDER_TIME_GTC,0,NULL);
         
         //Open sell stop order
         if (signal =="SELL" && Equity>=Balance)
         
         //Sell stop order 1 pip below previous candle close price. SL 5 pips. TP 7 Pips. Order valid for current candle duration 
         trade.SellStop(0.01,(cl-10*_Point),_Symbol,(cl+50*_Point),(cl-70*_Point),ORDER_TIME_GTC,0,NULL);
         
      }
  }

Thanks.

 
George Ndwiga: I am trying to place a buy stop and sell stop 1 pip above and below close price of the previous candle but keep getting invalid price error. I will appreciate insight on where I am going wrong. Below is what I have:

You must take in consideration your symbol's contract specifications, taking into account the Freeze Level, Stops Level and also current spread.

Usually you cannot place pending orders so close to current market prices. For such situations, it is best for the EA to monitor conditions and place Market Orders when the conditions are met.

Pending orders are best used for manual trading or long-term trading strategies, not for short-term strategies using EAs.

 
George Ndwiga:

Hi,

I am trying to place a buy stop and sell stop 1 pip above and below close price of the previous candle but keep getting invalid price error.

I will appreciate insight on where I am going wrong.

Below is what I have:

Thanks.

its about your broker, at first check stoplevel amount with (MarketInfo)

check your strategy on ecn acount and there stoplevel has no meaning

 
Stoplevels have no meaning?

I guess what you meant is, they are set to zero. But that is not always the case. There are brokers, who have stoplevels set also on ECN accounts.

Anyways, the spread, if given, can be considered an implicit Stoplevel.
 

Thanks for this. I just looked at it taking consideration your comments and all is well now.

Thanks so much :)



Fernando Carreiro:

You must take in consideration your symbol's contract specifications, taking into account the Freeze Level, Stops Level and also current spread.

Usually you cannot place pending orders so close to current market prices. For such situations, it is best for the EA to monitor conditions and place Market Orders when the conditions are met.

Pending orders are best used for manual trading or long-term trading strategies, not for short-term strategies using EAs.

 
George Ndwiga: Thanks for this. I just looked at it taking consideration your comments and all is well now. Thanks so much :)
You are welcome!
 
Fernando Carreiro:

You must take in consideration your symbol's contract specifications, taking into account the Freeze Level, Stops Level and also current spread.

Usually you cannot place pending orders so close to current market prices. For such situations, it is best for the EA to monitor conditions and place Market Orders when the conditions are met.

Pending orders are best used for manual trading or long-term trading strategies, not for short-term strategies using EAs.

Thank you so much Fernando, for the wonderful technical explanation. I am joining this thread because I am having a similar problem.

As of now, most of the terms are vague for me and gradually learning the terms....

I am currently working on one EA beign tested on EURUSD symbol running on a 1 min chart where intermittently reports "Invalid Stop" or "Invalid price" error

The EA attempted following Sell Trade for which it got error: 130: "invalid stops"

All the data at the time of the order entry was logged and is as follows:

ORDER CMD 5 : OP_SELL_STOP

Setup Candle OHLC O[1] 1.18456 H[1] 1.18458 L[1] 1.18445 C[1] 1.18448

Trade Candle OHLC O[0] 1.18449 H[0] 1.18449 L[0] 1.18449 C[0] 1.18449

Bid 1.18449 Ask 1.18464

Indicator1 1.18452 Indicator2 1.18453

Entry 1.18430 S.L. 1.18493 T.P. 1.18365

Entry is set 15 points below L[1]

Stop loss is set 40 points above Indicator2

Take profit is set 80 points L[1]

All the price values were filtered thru NormaliseDouble function

 
sammcdj: All the price values were filtered thru NormaliseDouble function

You used NormalizeDouble, It's use is usually wrong, as it is in your case.

  1. Floating point has a infinite number of decimals, it's your 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.06.07

  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) 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.02.16

  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.08.17) or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012.01.02)

  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. (2013.08.17)

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

  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.08.03

 
William Roeder:

You used NormalizeDouble, It's use is usually wrong, as it is in your case.

  1. Floating point has a infinite number of decimals, it's your 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.06.07

  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) 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.02.16

  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.08.17) or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012.01.02)

  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. (2013.08.17)

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

  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.08.03

Please forgive me WIlliam but I am unable to comprehend so much of information in one go!!

Please first tell me that which one of these three [Entry 1.18430/ S.L. 1.18493/ T.P. 1.18365] for OP_SELL_STOP would need to be rectified to make the "Invalid Stop" error go away

Bid value 1.18449 and Ask value was 1.18464

MarketInfo(Symbol(), MODE_DIGITS) returns 5.00000000

MarketInfo(Symbol(), MODE_TICKSIZE) returns 0.00001000

Global Point variable has value 0.00001000


Referring to the webpage Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial 

For SellStop order, rules are 

R1) Open Price of a Pending Order needs to be below Below the current Bid price

R2) SL - OpenPrice  StopLevel

R3)  OpenPrice - TP  StopLevel

--------

My EA's order open price was 1.18430 which was indeed below the Bid at 1.18449

Rule 2 of stop loss also met. 

1.18493  -  1.18430 = 0.00063 and stop level is 0.00030 so this field is correct too.

Rule 3 of Target also met. 

1.18430  - 1.18365 = 0.00065 which is also above stop level.

In absolute laymen terms, I am honestly unable to comprehend what exactly does "Invalid Stops" error refers to.

What exactly does word "Stops" linked to in this context?

Is it referring to open price of stop order? 

Is it referring to stop loss ?

Or is it referring to both target and stop loss since they "stop" (close out) the would be trade...

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
 
2021.08.12 21:30:37.022 SammExpert EURUSD,M1: OrderOpen Error: 130 invalid stops
2021.08.12 21:30:37.022 SammExpert EURUSD,M1: OrderOpen 0 CMD[4  OP_BUYSTOP] LONG Entry
Entr 1.17366000 S.L. 1.17305000 T.P. 1.17434000
O[1] 1.17350000 H[1] 1.17354000 L[1] 1.17341000 C[1] 1.17341000
O[0] 1.17342000 H[0] 1.17349000 L[0] 1.17340000 C[0] 1.17349000
Bid. 1.17349000 Ask. 1.17365000 LA 1.17357000 SB EMPTY Edge up 1.17347000 Edge down 1.17345000


Entry Price (Order's Opening price) is above Ask

Stop loss / Target is sufficiently far from Bid and ask... 

No extra digits.....

All rules mentioned in document are beign followed, why this Invalid Stop?

 
sammcdj:
2021.08.12 21:30:37.022 SammExpert EURUSD,M1: OrderOpen Error: 130 invalid stops
2021.08.12 21:30:37.022 SammExpert EURUSD,M1: OrderOpen 0 CMD[4  OP_BUYSTOP] LONG Entry
Entr 1.17366000 S.L. 1.17305000 T.P. 1.17434000
O[1] 1.17350000 H[1] 1.17354000 L[1] 1.17341000 C[1] 1.17341000
O[0] 1.17342000 H[0] 1.17349000 L[0] 1.17340000 C[0] 1.17349000
Bid. 1.17349000 Ask. 1.17365000 LA 1.17357000 SB EMPTY Edge up 1.17347000 Edge down 1.17345000


Entry Price (Order's Opening price) is above Ask

Stop loss / Target is sufficiently far from Bid and ask... 

No extra digits.....

All rules mentioned in document are beign followed, why this Invalid Stop?

Found the offending rule

OpenPrice-Ask ≥ StopLevel

1.17366 -  1.17365 = 0.00001 which is less than 0.00030 (stop level)