set pending order

 

Hello,

I  try to compose a simple EA. Below is the code.

Please answer why the Invalid request is posted in Journal

Thanks in advance! 

//+------------------------------------------------------------------+
//|                                                  SellBuyNEWS.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
input double Lots               = 1;
input double StopLoss           = 0;
input double TakeProfit         = 0;
double price;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
     BuyPendingOrder1();
     Alert(tm);
     Alert(price);
  }
//+------------------------------------------------------------------+
void BuyPendingOrder1()
  {
   price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)+0.0002;
   double SL=price-StopLoss*0.00001;
   if(StopLoss==0) SL=0;
   double TP=price+TakeProfit*0.00001;
   if(TakeProfit==0) TP=0;

//--- prepare the request
   MqlTradeRequest request;
   request.action=TRADE_ACTION_PENDING;         
   request.symbol = Symbol();                   
   request.volume = Lots;                       
   request.sl = SL;                             
   request.tp = TP;                             
   request.deviation=4;                         
   request.price=price;
//--- form the order type
   request.type=ORDER_TYPE_BUY_STOP;           // order type ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP, ORDER_TYPE_SELL_STOP
//--- form the price for the pending order
   request.type_filling=ORDER_FILLING_RETURN;
//    request.type_time = ORDER_TIME_GTC;
   request.expiration=ORDER_TIME_GTC;
   MqlTradeResult result;
   OrderSend(request,result);
  }
//+------------------------------------------------------------------+
 

Для функции OrderSend надо делать проверку:

 void BuyPendingOrder1()
  {
   price= SymbolInfoDouble ( Symbol (), SYMBOL_ASK )+ 0.0002 ;
   double SL=price-StopLoss* 0.00001 ;
   if (StopLoss== 0 ) SL= 0 ;
   double TP=price+TakeProfit* 0.00001 ;
   if (TakeProfit== 0 ) TP= 0 ;

//--- prepare the request
   MqlTradeRequest request;
   request.action= TRADE_ACTION_PENDING ;         
   request.symbol = Symbol ();                   
   request.volume = Lots;                       
   request.sl = SL;                             
   request.tp = TP;                             
   request.deviation= 4 ;                         
   request.price=price;
//--- form the order type
   request.type= ORDER_TYPE_BUY_STOP ;           // order type ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP, ORDER_TYPE_SELL_STOP
//--- form the price for the pending order
   request.type_filling= ORDER_FILLING_RETURN ;
//    request.type_time = ORDER_TIME_GTC;
   request.expiration= ORDER_TIME_GTC ;
   MqlTradeResult result;
   bool success= OrderSend (request,result);
   if (!success)
   {
     //check here
   }
  }
 
tenlau:

Hello,

I  try to compose a simple EA. Below is the code.

Please answer why the Invalid request is posted in Journal

Thanks in advance! 

//--- prepare the request
   MqlTradeRequest request={0};
You have to initialize your variable (you can use ZeroMemory() also).
 
angevoyageur:
You have to initialize your variable (you can use ZeroMemory() also).
Thanks, your advice worked !
Reason: