Invalid Stop - page 2

 
Ahmet Metin Yilmaz:

yes

and it was for your question below

STP=30 and TKP=20 both greater than zero " 0 ".

void Do_Sell(double PriceForSell){
   
   bool Buy_Opened=false;
   bool Sell_Opened=false;
   if(PositionSelect(_Symbol)==true){
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) Buy_Opened=true;  //It is a Buy
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)Sell_Opened=true; // It is a Sell
   }
   if (Sell_Opened==true)return;
   ChartRedraw();   
   double SellStop,SellTake;
   PriceForSell=NormalizeDouble(PriceForSell,_Digits);
   SellStop=NormalizeDouble((PriceForSell+STP),_Digits);
   SellTake=NormalizeDouble((PriceForSell-TKP),_Digits);
   if(!trade.Sell(MyLot,_Symbol,PriceForSell,SellStop,SellTake,"Sell By Expert")){
   Print("Sell() failed. Return code=",trade.ResultRetcode(),". Code description: ",trade.ResultRetcodeDescription()+"At Price:"+DoubleToString(PriceForSell));     
   Print("FAIL Sell " +DoubleToString(SellTake,_Digits)+" ---("+DoubleToString(TKP,_Digits)+")-----["+DoubleToString(PriceForSell,_Digits)+"]-----("+DoubleToString(STP,_Digits)+")---"+DoubleToString(SellStop,_Digits));
   }
   else{
   //Print("Sell() executed successfully. Return code=",trade.ResultRetcode()," (",trade.ResultRetcodeDescription(),")");
   Print("Sell At= "+PriceForSell+" Stop= "+DoubleToString(SellStop)+" Take= "+DoubleToString(SellTake));
   }
}

this Some times works and sometimes gives back Code10016 Invalid Stops

2020.05.14 16:43:17.106 2020.03.16 00:00:01   failed market sell 1.00 GBPUSD sl: 1.22918 tp: 1.22893 [Invalid stops]

2020.05.14 16:43:17.107 2020.03.16 00:00:01   CTrade::OrderSend: market sell 1.00 GBPUSD sl: 1.22918 tp: 1.22893 [invalid stops]

2020.05.14 16:43:17.819 2020.03.16 00:00:01   Sell() failed. Return code=10016. Code description: invalid stopsAt Price:1.22903000

2020.05.14 16:43:18.320 2020.03.16 00:00:01   FAIL Sell 1.22893 ---(0.00010)-----[1.22903]-----(0.00015)---1.22918


 
Boobwood:

this Some times works and sometimes gives back Code10016 Invalid Stops

2020.05.14 16:43:17.106 2020.03.16 00:00:01   failed market sell 1.00 GBPUSD sl: 1.22918 tp: 1.22893 [Invalid stops]

2020.05.14 16:43:17.107 2020.03.16 00:00:01   CTrade::OrderSend: market sell 1.00 GBPUSD sl: 1.22918 tp: 1.22893 [invalid stops]

2020.05.14 16:43:17.819 2020.03.16 00:00:01   Sell() failed. Return code=10016. Code description: invalid stopsAt Price:1.22903000

2020.05.14 16:43:18.320 2020.03.16 00:00:01   FAIL Sell 1.22893 ---(0.00010)-----[1.22903]-----(0.00015)---1.22918


Add the market prices in your log (Bid and Ask) and you will probably understand.
 

Thanks for your advice but I need more help on it 

Here it is how I call my Sell anf Buy Functions

I will be thankful if you explain a little bit more( I am really new in MQL)

if(SellConditions()){Do_Sell(NormalizeDouble(latest_price.ask,_Digits));return;}
if(BuyConditions()) {Do_Buy(NormalizeDouble(latest_price.bid,_Digits));return;}
 
Boobwood:

Thanks for your advice but I need more help on it 

Here it is how I call my Sell anf Buy Functions

I will be thankful if you explain a little bit more( I am really new in MQL)

A SELL is open at the bid price, not the ask. Reverse for the BUY.
 

Thank You very Much 

It Solved My Problem in many positions but Why still have messages like this


Thanks, Thanks.

Excuse me for wasting your time.

 

I find out

the problem was a calculation problem with difference between bid and ask or Spread

spread_Points must not bigger than Stopless and Takeprofit When the position wants to open.


   Chris70:

For the sell order the take profit needs to be lower then the current price, but of course it still needs to be a positive number. According to your screenshot the requested take profit is -9.36716, which is no valid price.

There is nothing wrong with the calculation, because if you go from a price of 0.63284 down by an extreme distance such as ten thousand pips, you get a negative result: -9.36716 is the correct result in your example, but in real life, the price of a currency pair can't be negative. Try a much smaller number for TKP.

About the question regarding the result of SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) : with many brokers there is a minimum distance between the current price and a requested trigger price of stop loss, take profit, stop buy order or stop sell order. Apparently, as you don't have low numbers for STP and TKP, this wasn't the problem in your case. But just keep in mind for the future that sometimes - depending on your broker and the symbol, a very low value for the distance in points (=your STP and TKP) can also sometimes cause errors. For example with my broker, I can't request a stop distance of less than 40 points for EURUSD or less than 52 points for GBPJPY... it may be different with your broker, just be aware that this problem sometimes exists --> that is what the SYMBOL_TRADE_STOPS_LEVEL is for: it tells you which minimum distance exactly won't cause trouble (and if it's zero: you can forget about it).
 
Boobwood:

I find out

the problem was a calculation problem with difference between bid and ask or Spread

spread_Points must not bigger than Stopless and Takeprofit When the position wants to open.


   Chris70:

For the sell order the take profit needs to be lower then the current price, but of course it still needs to be a positive number. According to your screenshot the requested take profit is -9.36716, which is no valid price.

There is nothing wrong with the calculation, because if you go from a price of 0.63284 down by an extreme distance such as ten thousand pips, you get a negative result: -9.36716 is the correct result in your example, but in real life, the price of a currency pair can't be negative. Try a much smaller number for TKP.

About the question regarding the result of SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) : with many brokers there is a minimum distance between the current price and a requested trigger price of stop loss, take profit, stop buy order or stop sell order. Apparently, as you don't have low numbers for STP and TKP, this wasn't the problem in your case. But just keep in mind for the future that sometimes - depending on your broker and the symbol, a very low value for the distance in points (=your STP and TKP) can also sometimes cause errors. For example with my broker, I can't request a stop distance of less than 40 points for EURUSD or less than 52 points for GBPJPY... it may be different with your broker, just be aware that this problem sometimes exists --> that is what the SYMBOL_TRADE_STOPS_LEVEL is for: it tells you which minimum distance exactly won't cause trouble (and if it's zero: you can forget about it).

https://www.mql5.com/en/forum/326731

Error 4756, Invalid Stops
Error 4756, Invalid Stops
  • 2019.11.18
  • www.mql5.com
Hi MT5 Community!! I can't set up my stop loss and take profit for ORDER_TYPE_SELL...
 

Conclusion:

When (Spread Transformed to point )> StopLoss  , ( Invalid stops Error code=10016 )will generate.

so Simply we can use the following code to avoid of it .

   double spread=ask-bid;
   int spread_points=(int)MathRound(spread/SymbolInfoDouble(Symbol(),SYMBOL_POINT));

   if(spread_points > STOPLoss){
        //Do Not Try To Open Sell Or Buy Position
        return;
   }
 
Boobwood: Conclusion: when (Spread Transformed to point )> StopLoss  , ( Invalid stops Error code=10016 )will generate.

Still wrong; when Spread > Stoploss, your code is trying to put the SL between Ask and Buy. You buy at the Ask and sell at the Bid.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the 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 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 spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.
 
William Roeder:

Still wrong; when Spread > Stoploss, your code is trying to put the SL between Ask and Buy. You buy at the Ask and sell at the Bid.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the 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 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 spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

Thanks a lot For this Detailed advice, Thanks again for time you take, I need read it several times to understand it completely.

Reason: