Download MetaTrader 5

set pending order

To add comments, please log in or register
Share your knowledge. Become a well-known developer!
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.05.01 20:26 

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);
  }
//+------------------------------------------------------------------+
Artem Temnikov
1027
Artem Temnikov 2014.05.03 12:23  

Для функции 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
   }
  }
Alain Verleyen
Moderator
30752
Alain Verleyen 2014.05.03 13:39  
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).
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.05.05 17:47  
angevoyageur:
You have to initialize your variable (you can use ZeroMemory() also).
Thanks, your advice worked !
To add comments, please log in or register