Bug in ordersetnd

1 June 2017, 10:03
jatinpruthvi
0
25

//+------------------------------------------------------------------+
//| OPENBUY FUNCTION                                                 |
//| *Has Input parameters - order type, Current ASK price,           |
//|  Stop Loss, Take Profit, deviation, comment                      |
//| *Checks account free margin before pacing trade if trader chooses|
//| *Prints of a success if position is opened or shows error        |
//+------------------------------------------------------------------+
bool MyExpert::openBuySell(ENUM_ORDER_TYPE otype)
  {

      bool result = false;
 double oprice = 0,stl =0 , tkp = 0 , ask = 0 , bid = 0 ;
 MqlTick latest_price;
 bool bidask = false;
 if(SymbolInfoTick(symbol,latest_price))
{
 bidask = true;
}
  int    digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); // number of decimal places
  double Point = SymbolInfoDouble(symbol,SYMBOL_POINT);
 if( otype == ORDER_TYPE_BUY){
   ask  = bidask ? latest_price.ask :  SymbolInfoDouble(symbol,SYMBOL_ASK) ;
  oprice = NormalizeDouble(ask,digits);              // current Ask price
  stl    = NormalizeDouble(ask - STP*Point,digits); // Stop Loss
  tkp    = NormalizeDouble(ask + TKP*Point,digits); // Take profit
 }else if(otype == ORDER_TYPE_SELL){
  bid  = bidask ? latest_price.bid :  SymbolInfoDouble(symbol,SYMBOL_BID) ;
  oprice=NormalizeDouble(bid,digits);                 // Current Bid price
  stl    = NormalizeDouble(bid + STP*Point,digits); // Stop Loss
  tkp    = NormalizeDouble(bid - TKP*Point,digits); // Take Profit
 }else{
 Print("Invalid Order Type");
 return result;
 }
 
 trequest.action=TRADE_ACTION_DEAL;
      trequest.type=otype;
      trequest.volume=LOTS;
      trequest.price=oprice;
      trequest.sl=stl;
      trequest.tp=tkp;
      trequest.deviation=1000;
      trequest.magic=Magic_No;
      trequest.symbol=symbol;
      trequest.type_filling=ORDER_FILLING_FOK;
// trequest.comment
      //--- send
      OrderSend(trequest,tresult);
      //--- check result
      if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed 
        {
result = true;
         Print("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!");
        }
      else
        {
result = false;
         Errormsg= "The Buy order request could not be completed";
         Errcode =GetLastError();
         showError(Errormsg,Errcode);
        }
     
return result ;

  }

******************************************************************************************************************

bool MyExpert::openBuySell(ENUM_ORDER_TYPE otype)

  {


      bool result = false;

 double oprice = 0,stl =0 , tkp = 0 , ask = 0 , bid = 0 ;

 MqlTick latest_price;

 bool bidask = false;

 if(SymbolInfoTick(symbol,latest_price))

{

 bidask = true;

}

  int    digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); // number of decimal places

if(digits==5)

{

 STP = STP*10;

 TKP = TKP*10;

}

  double Point = SymbolInfoDouble(symbol,SYMBOL_POINT);

 if( otype == ORDER_TYPE_BUY){

   ask  = bidask ? latest_price.ask :  SymbolInfoDouble(symbol,SYMBOL_ASK) ;

  oprice = NormalizeDouble(ask,digits);              // current Ask price

  stl    = NormalizeDouble(ask - STP*Point,digits); // Stop Loss

  tkp    = NormalizeDouble(ask + TKP*Point,digits); // Take profit

 }else if(otype == ORDER_TYPE_SELL){

  bid  = bidask ? latest_price.bid :  SymbolInfoDouble(symbol,SYMBOL_BID) ;

  oprice=NormalizeDouble(bid,digits);                 // Current Bid price

  stl    = NormalizeDouble(bid + STP*Point,digits); // Stop Loss

  tkp    = NormalizeDouble(bid - TKP*Point,digits); // Take Profit

 }else{

 Print("Invalid Order Type");

 return result;

 }

 

 trequest.action=TRADE_ACTION_DEAL;

      trequest.type=otype;

      trequest.volume=LOTS;

      trequest.price=oprice;

      trequest.sl=stl;

      trequest.tp=tkp;

      trequest.deviation=1000;

      trequest.magic=Magic_No;

      trequest.symbol=symbol;

      trequest.type_filling=ORDER_FILLING_FOK;

// trequest.comment

      //--- send

      OrderSend(trequest,tresult);

      //--- check result

      if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed 

        {

result = true;

         Print("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!");

        }

      else

        {

result = false;

         Errormsg= "The Buy order request could not be completed";

         Errcode =GetLastError();

         showError(Errormsg,Errcode);

        }

     

return result ;

  }

  





  
Share it with friends: