Error 10015 with a buy limit order

Hi

I have been trying to modify this bit of sample code for use. I keep getting the Invalid Price error. Does anyone have any ideas

Thanks

double volume=0.1;
   string symbol="GBPUSD";    // specify the symbol, at which the order is placed
   int    digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); // number of decimal places
   double point=SymbolInfoDouble(symbol,SYMBOL_POINT);         // point
   double ask=SymbolInfoDouble(symbol,SYMBOL_ASK);             // current buy price
   double price=1000*point;                                   // unnormalized open price
   price=NormalizeDouble(price,digits);                        // normalizing open price
   int SL_pips=300;                                            // Stop Loss in points
   int TP_pips=500;                                            // Take Profit in points
   double SL=price-SL_pips*point;                              // unnormalized SL value
   SL=NormalizeDouble(SL,digits);                              // normalizing Stop Loss
   double TP=price+TP_pips*point;                              // unnormalized TP value
   TP=NormalizeDouble(TP,digits);                              // normalizing Take Profit
   datetime expiration=TimeTradeServer()+PeriodSeconds(PERIOD_D1);
   string comment=StringFormat("Buy Limit %s %G lots at %s, SL=%s TP=%s",
                               symbol,volume,
                               DoubleToString(price,digits),
                               DoubleToString(SL,digits),
                               DoubleToString(TP,digits));
//--- everything is ready, sending a Buy Limit pending order to the server
   if(!trade.BuyLimit(volume,price,symbol,SL,TP,ORDER_TIME_GTC,expiration,comment))
     {
      //--- failure message
      Print("BuyLimit() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
     }
   else
     {
      Print("BuyLimit() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
     }

This part doesn’t make sense, and it’s the reason you aren’t getting filled. This needs to be a relevant entry price.

double price=1000*point;                                   // unnormalized open price
   price=NormalizeDouble(price,digits);                        // normalizing open price

Your pip calculations are also wrong and need to be adjusted for 3|5 digit brokers (all of them). Also, you need to be using volume that’s within the parameters of the symbol and also you need to be rounding to the tick-size and not the point. CSymbolInfo will help make your code more readable and provide easier access to the API data.

#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   const int num_pips_entry = 10;
   const string symbol_to_trade = "GBPUSD";
   
   CSymbolInfo symbol;
   symbol.Name(symbol_to_trade);
   symbol.RefreshRates();
   double volume = symbol.LotsMin();
   double xpip = symbol.Point();
   if(symbol.Digits() == 3 || symbol.Digits() == 5)
      xpip *= 10;
   double price_entry = round_tick(
      symbol.Ask() - num_pips_entry * xpip,
      symbol
   );
   double sl = round_tick(price_entry - 300 * xpip, symbol);
   double tp = round_tick(price_entry + 500 * xpip, symbol);
   datetime expiration = TimeCurrent() + PeriodSeconds(PERIOD_D1);
   int digits = symbol.Digits();
   string comment = StringFormat(
      "Buy Limit %s: %.2f lots @ %s, SL=%s TP=%s",
       symbol.Name(), volume,
       DoubleToString(price_entry, digits),
       DoubleToString(sl, digits),
       DoubleToString(tp, digits)
   );
   CTrade trade;
   bool is_success = trade.BuyLimit(
      volume, price_entry, symbol.Name(), sl, tp, 
      ORDER_TIME_GTC, expiration, comment
   );
   printf("BuyLimit() method %s. Return code=%d. Code description: %s",
      is_success ? "Successful" : "Failed",
      trade.ResultRetcode(), 
      trade.ResultRetcodeDescription()
   );
}
//+------------------------------------------------------------------+

double round_tick(double price, CSymbolInfo &symbol)
{
   return round(price / symbol.TickSize()) * symbol.TickSize();

Hi

Thank you for your help. Just an update in order to eventually get round this problem, i used the following code:

#include <Trade\Trade.mqh>
CTrade  trade;
// at the top of the page  


double askNow =NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

double bidNow =NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);  

  if(OrdersTotal()==0 && PositionsTotal()==0)
   {
   
      trade.SellLimit(0.10,(askNow+(200*_Point)),_Symbol,0,(bidNow-(200*_Point)),ORDER_TIME_GTC,0,0);
      
   }

I then modified it for my purposes.