Download MetaTrader 5

What does "invalid stops" actually mean?

To add comments, please log in or register
All activity at MQL5.community is clearly visible. Have a look at the Wall!
Ian Harris
106
Ian Harris 2012.01.03 03:39 
ike a lot of other people in this forum I am having serious problems understanding what "invalid stops" means (and the infamous 4756 error). Apparently this error is returned when a stop loss or take profit order is too close to the current price, but how close is too close? How is this calculated? Does it vary over time or is it constant for a symbol and time frame combination? (in other words do I need to get the minimum level every time I am entering an order, or can I do it once in the initialise section?)

In my EA I have the sl and tp values set as per the examples sprinkled all around the forum and articles (in particular here https://www.mql5.com/en/articles/100) and found I was getting Invalid stops errors all the time. After a bit more searching around the forum I found a reference to SYMBOL_TRADE_STOPS_LEVEL, which is "Minimal indention in points from the current close price to place Stop orders". So I added a piece of code like this (sorry, haven't been able to figure out how to insert code fragments into a post)

      //Check sl and tp greater than minimum.
         double short_sl = p_ShortStop;
         double short_tp = p_ShortProfit;
         int MinStop = (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
         if(MinStop > short_sl)
            short_sl = MinStop+1;
         if (MinStop > short_tp)
            short_tp = MinStop+1;
         Alert("Short Signal short_sl=",short_sl," short_tp=",short_tp," MinStop=",MinStop);  
         Print("Short Signal short_sl=",short_sl," short_tp=",short_tp," MinStop=",MinStop);  

But I still get the same error


I entered the trade manually and found I had to set the stop loss to at least 182 points before it would accept the modification, so it would seem that the function has returned the incorrect value. Or I'm missing something. Do I need to consider the spread as well? Should I be adding the stop level to the Ask and subtracting if from the Bid?

All advice gratefully received,

Ian

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
Stephen Njuki
5179
Stephen Njuki 2012.01.03 09:26  
mogplus8:
ike a lot of other people in this forum I am having serious problems understanding what "invalid stops" means (and the infamous 4756 error). Apparently this error is returned when a stop loss or take profit order is too close to the current price, but how close is too close? How is this calculated? Does it vary over time or is it constant for a symbol and time frame combination? (in other words do I need to get the minimum level every time I am entering an order, or can I do it once in the initialise section?)

In my EA I have the sl and tp values set as per the examples sprinkled all around the forum and articles (in particular here https://www.mql5.com/en/articles/100) and found I was getting Invalid stops errors all the time. After a bit more searching around the forum I found a reference to SYMBOL_TRADE_STOPS_LEVEL, which is "Minimal indention in points from the current close price to place Stop orders". So I added a piece of code like this (sorry, haven't been able to figure out how to insert code fragments into a post)

      //Check sl and tp greater than minimum.
         double short_sl = p_ShortStop;
         double short_tp = p_ShortProfit;
         int MinStop = (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
         if(MinStop > short_sl)
            short_sl = MinStop+1;
         if (MinStop > short_tp)
            short_tp = MinStop+1;
         Alert("Short Signal short_sl=",short_sl," short_tp=",short_tp," MinStop=",MinStop);  
         Print("Short Signal short_sl=",short_sl," short_tp=",short_tp," MinStop=",MinStop);  

But I still get the same error


I entered the trade manually and found I had to set the stop loss to at least 182 points before it would accept the modification, so it would seem that the function has returned the incorrect value. Or I'm missing something. Do I need to consider the spread as well? Should I be adding the stop level to the Ask and subtracting if from the Bid?

All advice gratefully received,

Ian

As an example when buying...

 lets say you need you tp at 400 points and your sl at 40 points.

 

int
SL=40,
TP=400;

double
sl_price = SymbolInfoDouble(Symbol(),SYMBOL_BID)-MathMax(MathMax(SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL),SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL)),SL)*Point(),
tp_price = SymbolInfoDouble(Symbol(),SYMBOL_BID)+MathMax(MathMax(SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL),SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL)),TP)*Point();

 

 It is a bit untidy and you may want to better arrange this in a class, but that is the general idea.

 

 

Ian Harris
106
Ian Harris 2012.01.03 14:44  

Thanks ssn, it's going to take me a while to work my way through this I think...  :-(
Ian Harris
106
Ian Harris 2012.01.04 04:39  

Hi ssn,

Okay, confusion reigns. And it's all related to bid and ask prices, which I'm am still trying to wrap my brain around.

My understanding of your code fragment in words is "the stop loss is the greatest of: Stops level, Freeze level, and my Stop Loss level)" and ditto for the profit level. Correct? I did a quick test and found that (on the Metaquotes server) EURUSD stops level is 18 and freeze level is zero. For AUDCHF the stops level is 120 and the freeze level is, again, zero. Not sure if that's realistic, but it's only a demo. Happy with that, but now the ask and bid prices raise their ugly heads. My understanding is this.

If I go long, I buy at the ask price. I am prepared to buy at the lowest price, which is the lowest ask price. To me that means that my take profit level should be the ask price plus my TP points, the profit I'm hoping to make on the trade. If I use the bid price then I am not taking into account the spread. Having said that, there seems to be no restriction on the profit level. I entered a proft level of one point and the server accepted it. Go figure... Whatever, the end result it seems is that, if I enter a profit target of bid plus (say) 200, then my real profit, should my target by hit, is 200 minus the spread, i.e. 200 - 84 or 116 (for AUDCHF). So if I'm really expecting to profit by 200 points my profit price should be the ask plus profit points.

To exit my long, I sell at the bid price. I'm willing to sell my position to the highest bidder. So therefore, my stop loss should be the current bid price minus my stop loss (as per  your code). This means that I am prepared to lose (at the point of entering the trade) the spread plus my stop loss level. I have found that, if I enter a trade manually for AUDCHF, the minimum stop level I can enter is 204 (just now anyway, it seems to vary a bit), which is the stops level (120) plus the spread (84). So if I enter a trade with a stop loss of bid price - 121, my trade will be rejected. The stop level needs to be at least the stop level (or freeze level presumably, if it is greater than the stops level) plus the spread.

That's my understanding so far anyway. I'm looking forward to more playing and testing...

;-) Ian

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
Stephen Njuki
5179
Stephen Njuki 2012.01.04 06:13  
mogplus8:

Hi ssn,

Okay, confusion reigns. And it's all related to bid and ask prices, which I'm am still trying to wrap my brain around.

My understanding of your code fragment in words is "the stop loss is the greatest of: Stops level, Freeze level, and my Stop Loss level)" and ditto for the profit level. Correct? I did a quick test and found that (on the Metaquotes server) EURUSD stops level is 18 and freeze level is zero. For AUDCHF the stops level is 120 and the freeze level is, again, zero. Not sure if that's realistic, but it's only a demo. Happy with that, but now the ask and bid prices raise their ugly heads. My understanding is this.

If I go long, I buy at the ask price. I am prepared to buy at the lowest price, which is the lowest ask price. To me that means that my take profit level should be the ask price plus my TP points, the profit I'm hoping to make on the trade. If I use the bid price then I am not taking into account the spread. Having said that, there seems to be no restriction on the profit level. I entered a proft level of one point and the server accepted it. Go figure... Whatever, the end result it seems is that, if I enter a profit target of bid plus (say) 200, then my real profit, should my target by hit, is 200 minus the spread, i.e. 200 - 84 or 116 (for AUDCHF). So if I'm really expecting to profit by 200 points my profit price should be the ask plus profit points.

To exit my long, I sell at the bid price. I'm willing to sell my position to the highest bidder. So therefore, my stop loss should be the current bid price minus my stop loss (as per  your code). This means that I am prepared to lose (at the point of entering the trade) the spread plus my stop loss level. I have found that, if I enter a trade manually for AUDCHF, the minimum stop level I can enter is 204 (just now anyway, it seems to vary a bit), which is the stops level (120) plus the spread (84). So if I enter a trade with a stop loss of bid price - 121, my trade will be rejected. The stop level needs to be at least the stop level (or freeze level presumably, if it is greater than the stops level) plus the spread.

That's my understanding so far anyway. I'm looking forward to more playing and testing...

;-) Ian

Hi Ian,

If you wat to avoid paying the spread then you should add it to your tp because even if your base price for take profit is the Ask, you will have to exit at Bid. The point of having Bid as your base price is you take on less risk since your exit price is lower (when buying).

Ian Harris
106
Ian Harris 2012.01.04 18:35  

Hi ssn,

Agree, however it's a matter of perception really. If you were trading equities, you could view your profit as, say, $200 less commission of $5, so nett prfit of $195. But if I really wanted a $200 profit I'd set my stop at $205, i.e. profit plus commission. Same with AUDCHF, if I set my profit at 200 points plus spread, than I'm really expecting a nett profit of 200 points. Mathematically that equals Ask + profit target. Swings and roundabouts.

Perception is reality.

;-) Ian

To add comments, please log in or register