invalid stops error

 

I get an invalid stop error during the mql5 market validation process. I dont undertand why. I suspect it could be that the TP is too close to current price +/- broker stop level. I use the below code to to adjust TP to match broker conditions. I dont know what else could be causing the error. Any ideas would be appreciated.


 int stopLevel = (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
 int spread = (int)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);

   if(AskPrice + buyTPprice < (stopLevel + spread)*_Point) {
      buyTPprice = AskPrice + (stopLevel + spread)*_Point;
   }


 

 
  1. if(AskPrice + buyTPprice

    Price plus price is nonsense.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    Always post all relevant code (using Code button) or attach the source file.

  2. buyTPprice = AskPrice + (stopLevel + spread)*_Point;

    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. TP/SL are prices. Normalize them to ticksize.
              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)

 
William Roeder #:
  1. Price plus price is nonsense.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    Always post all relevant code (using Code button) or attach the source file.

  2. 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. TP/SL are prices. Normalize them to ticksize.
              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)

Thank you for your reply. I see your point with adding price to price.. Here is the entire function after correcting your point, but I still get the same error message.


double BuyTP()
{
   double B_lots = 0;
   double B_price = 0;
   double B_comission =0;
   double BuyMoneyProfitClose;
   double tickValue = SymbolInfoDouble(inpSymbol,SYMBOL_TRADE_TICK_VALUE);

   if(inpCloseModel==PercentageProfit)
      BuyMoneyProfitClose = NormalizeDouble((AccountInfoDouble(ACCOUNT_BALANCE)/100) * inpPctProfitToClose,2);
   else
      BuyMoneyProfitClose = MoneyProfitToClose;

   for(int i=PositionsTotal()-1; i>=0; i--) {
      ulong ticket = PositionGetTicket(i);
      if(!PositionSelectByTicket(ticket))
         continue;
      if(PositionGetInteger(POSITION_MAGIC)!=inpMagicEA)
         continue;
      if(PositionGetString(POSITION_SYMBOL)!=inpSymbol)
         continue;
      if(PositionGetInteger(POSITION_TYPE) != POSITION_TYPE_BUY)
         continue;

      double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
      double posLots = PositionGetDouble(POSITION_VOLUME);
      ulong type = PositionGetInteger(POSITION_TYPE);
      double dealcommission = HistoryDealGetDouble(ticket, DEAL_COMMISSION) * 2;
      double CurrentTP = positionInfo.TakeProfit(); //(avoid modify order error: no change)

      if(type==POSITION_TYPE_BUY) {
         B_price += posLots * posOpenPrice;
         B_lots += posLots;
         B_comission += dealcommission;
      }
   }
   double buyTPprice =  NormalizeDouble(((B_price / B_lots)+ _Point*B_comission*B_lots)+ _Point*(BuyMoneyProfitClose/tickValue/B_lots) + _Point*(PositionGetDouble(POSITION_SWAP)),_Digits);

    // Making sure that tp is placed minimum at stop level distance
   int stopLevel = (int)SymbolInfoInteger(inpSymbol,SYMBOL_TRADE_STOPS_LEVEL);
   int spread = (int)SymbolInfoInteger(inpSymbol,SYMBOL_SPREAD);
   double BuyTPPoints = NormalizeDouble((buyTPprice-AskPrice),_Digits);
   double Ask = SymbolInfoDouble(inpSymbol,SYMBOL_ASK);

   if(Ask + BuyTPPoints < Ask+(stopLevel + spread)*_Point) {
      buyTPprice = Ask + (stopLevel + spread)*_Point;

  }
  

   if(B_lots == 0)
      return 0;
   else
      return buyTPprice ;
}