error 10013 in sell limit why ?

 

hi guys  i have  this part of code , i call it  from button

void CPanelDialog::OnClickButtonSellLimit(void)
{
    // Magic Number
    double lotSize    = StringToDouble(m_edit.Text());  // Reads the lot size value
    double stopLoss   = StringToDouble(m_editPrice1STP1oco.Text());  // Stop Loss (0 if you don't want to set it)
    double takeProfit = StringToDouble(m_editPrice1TP1oco.Text());  // Take Profit (0 if you don't want to set it)
    
    // Get the current market price
    double marketPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);  // Selling price for a symbol (for SELL)

    // If entryPrice is not set or is 0, calculate it using limitStartPipS
    double entryPrice = StringToDouble(m_editPrice1.Text()); // Price entered by the user
    if (entryPrice <= 0) {
        double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Get the value of a point
        entryPrice = marketPrice + (limitStartPipS * point); // Add the value of limitStartPipS converted into points
        PrintFormat("Entry price not specified. Using current price + %d pips: %f", limitStartPipS, entryPrice);
    }

    // Check whether the order should be a SELL LIMIT or a SELL STOP
    if (entryPrice > marketPrice) {
        // Place a SELL LIMIT order
        bool result = trade.PositionOpen(_Symbol, ORDER_TYPE_SELL_LIMIT, lotSize, entryPrice, stopLoss, takeProfit, "TM_SELL_LIMIT");
        if (result) {
            Print("SELL LIMIT order placed successfully!");
        } else {
            Print("Error sending SELL LIMIT order: ", trade.ResultRetcode());
        }
    } else if (entryPrice < marketPrice) {
        // Place a SELL STOP order
        bool result = trade.PositionOpen(_Symbol, ORDER_TYPE_SELL_STOP, lotSize, entryPrice, stopLoss, takeProfit, "TM_SELL_STOP");
        if (result) {
            Print("SELL STOP order placed successfully!");
        } else {
            Print("Error sending SELL STOP order: ", trade.ResultRetcode());
        }
    } else {
        Print("Error: Entry price is equal to the current market price.");
    }
}

but  when i try to run  return me  always 10013   i insert 0.10 lot size  and  stay so far from market price  but nothing anyone can help me ?   thanks

 
Stefano Cerbioni:

hi guys  i have  this part of code , i call it  from button

but  when i try to run  return me  always 10013   i insert 0.10 lot size  and  stay so far from market price  but nothing anyone can help me ?   thanks

Hi , you can use trade.SellStop 

or even better 

this with request and result

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
void OnTick()
  {
  static bool PLACED=false;//ignore
  if(!PLACED){//ignore 
    PLACED=true;//ignore
  //-----------------------------------  
      /*
      your user provides 4 values
      SL TP LOT ENTRY
      users are blessed with the magical ability to go STRAIGHT to an issue in the code
      the first time they use your product , so , you have to anticipate that.
      */
      string targetSymbol="EURUSD";
      double userLotSize=0.01;
      double userSL=1.09;
      double userTP=1.06;
      double userEntry=1.07;
      ENUM_ORDER_TYPE userType=ORDER_TYPE_SELL_STOP;    
      
    MqlTick TICK;
    if(SymbolInfoTick(targetSymbol,TICK)){
    
    //1. get the symbol specs
      ResetLastError();
      int errors=0;    
      double symbolLimitDistance=(double)SymbolInfoInteger(targetSymbol,SYMBOL_TRADE_STOPS_LEVEL);errors+=GetLastError();ResetLastError();
         int symbolDigits=(int)SymbolInfoInteger(targetSymbol,SYMBOL_DIGITS);errors+=GetLastError();ResetLastError();
      double symbolMinVolume=(double)SymbolInfoDouble(targetSymbol,SYMBOL_VOLUME_MIN);errors+=GetLastError();ResetLastError();
      double symbolMaxVolume=(double)SymbolInfoDouble(targetSymbol,SYMBOL_VOLUME_MAX);errors+=GetLastError();ResetLastError();
      double symbolPoint=(double)SymbolInfoDouble(targetSymbol,SYMBOL_POINT);errors+=GetLastError();ResetLastError();
      symbolLimitDistance*=symbolPoint;
    //2. if there are 0 errors
      if(errors==0){     
      userEntry=NormalizeDouble(userEntry,symbolDigits);
      userSL=NormalizeDouble(userSL,symbolDigits);
      userTP=NormalizeDouble(userTP,symbolDigits);
      //2A : Has the user provided a proper lot size ?
        if(userLotSize>=symbolMinVolume&&userLotSize<=symbolMaxVolume){
        //2B : Has the user provided a proper entry price ?
          bool propperEntry=false;
          if(userEntry>0.0){
          propperEntry=true;
          if(userType==ORDER_TYPE_BUY_LIMIT||userType==ORDER_TYPE_SELL_STOP){
            /*sell stop and buy limit the price must be away from the current price at least 
              symbolLimitDistance points 
              In this iteration we will not be extending the price to the valid position
              but we'll straight out reject it.
              You can correct it if you want but the user wont like it as they 100% 
              are unaware of the broker limitation being there.(if it is)
            */
            double mustBeBelow=TICK.bid-symbolLimitDistance;
            if(userEntry>=mustBeBelow){propperEntry=false;Print("2B:Invalid price 1");}
            }
          else if(userType==ORDER_TYPE_SELL_LIMIT||userType==ORDER_TYPE_BUY_STOP){
            //same here but above price
            double mustBeAbove=TICK.ask+symbolLimitDistance;
            if(userEntry<=mustBeAbove){propperEntry=false;Print("2B:Invalid price 2");}
            }
          else{
            propperEntry=false;
            Print("We don't sell these hot dogs here");
            }
          //--- if the entry is propper
            if(propperEntry){
            Print("2A+2B passed");
            //--- stop loss checks 
              bool propperSL=false;
              if(userSL>=0.0){
              propperSL=true;
              if(userSL>0.0){
                //for buy types the sl must be below the open price by distance 
                  if(userType==ORDER_TYPE_BUY_LIMIT||userType==ORDER_TYPE_BUY_STOP){
                    double mustBeBelow=userEntry-symbolLimitDistance;
                    if(userSL>=mustBeBelow){propperSL=false;Print("Invalid SL 1");}
                    }
                //for sell types the sl must be above the open price by distance
                  else if(userType==ORDER_TYPE_SELL_LIMIT||userType==ORDER_TYPE_SELL_STOP){
                    double mustBeAbove=userEntry+symbolLimitDistance;
                    if(userSL<=mustBeAbove){propperSL=false;Print("Invalid SL 2");}
                    }
                }
              }
            //--- stop loss checks end here 
            //--- take profit checks
              bool propperTP=false;
              if(userTP>=0.0){
              propperTP=true;
              if(userTP>0.0){
                //for buy types the tp must be above the open price by distance 
                  if(userType==ORDER_TYPE_BUY_LIMIT||userType==ORDER_TYPE_BUY_STOP){
                    double mustBeAbove=userEntry+symbolLimitDistance;
                    if(userTP<=mustBeAbove){propperTP=false;Print("Invalid TP 1");}
                    }
                //for sell types the tp must be below the open price by distance
                  else if(userType==ORDER_TYPE_SELL_LIMIT||userType==ORDER_TYPE_SELL_STOP){
                    double mustBeBelow=userEntry-symbolLimitDistance;
                    if(userTP>=mustBeBelow){propperTP=false;Print("Invalid TP 2");}
                    }
                }
              }
            //--- take profit checks end here 
            //--- valid stops
              if(propperSL&&propperTP){
              Print("Passed TP + SL checks");
              ResetLastError();
              MqlTradeRequest req={};
              MqlTradeResult  res={};
              req.type_filling=ORDER_FILLING_RETURN;//not 
              req.action=TRADE_ACTION_PENDING;//not
              req.type_time=ORDER_TIME_GTC;
              req.symbol=targetSymbol;
              req.type=userType;
              req.price=userEntry;
              req.volume=userLotSize;
              req.sl=userSL;
              req.tp=userTP;
              req.deviation=100;
              req.expiration=0;
              req.magic=0;
              req.comment="";
             
              if(OrderSend(req,res)){
                Print("Done Ticket #ORDER "+IntegerToString(res.order)+" #DEAL "+IntegerToString(res.deal));
                }else{
                Print("Cannot send #"+IntegerToString(res.retcode)+" "+res.comment);
                }
              }
            //--- valid stops 
            }
          //--- if the entry is propper ends here 
          }
        //2B : ends here
        }else{Print("Wrong lot size");}
      //2A : ends here
      }else{Print("errors in fetching specs from broker");}
    }else{
    Print("Cannot get tick");
    }
    
    ExpertRemove();
  //-----------------------------------  
  }//ignore this its for testing 
  }
 
thanks so much