Functionalizing my sending order code

 

When I put the following code in the OnTick() event handler, it is working:

   MqlTradeRequest request;
   MqlTradeResult result;
   
   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.volume = 1;
   request.type = ORDER_TYPE_BUY;
   request.type_filling = ORDER_FILLING_IOC;
   request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   request.sl = 0;
   request.tp = 0;
   request.magic = 123;
   
   OrderSend(request,result);
   Print(result.retcode);


But, when I place the exactly same code in a function, it does not work and gives the error 10013 - invalid request:

//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

void Trade()
{
   MqlTradeRequest request;
   MqlTradeResult result;
   
   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.volume = 1;
   request.type = ORDER_TYPE_BUY;
   request.type_filling = ORDER_FILLING_IOC;
   request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   request.sl = 0;
   request.tp = 0;
   request.magic = 123;
   
   OrderSend(request,result);
   Print(result.retcode);
}
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
     
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(PositionSelect(_Symbol))
     {
      return;
     }
     
   Trade();

  }
//+------------------------------------------------------------------+


What is the problem with this functionalized code?

Thanks a lot.

 

Hi

try change code:

MqlTradeRequest request={0};


 
tuoitrecuoi:

Hi

try change code:

MqlTradeRequest request={0};


Ahh, yes ... alternatively I should use ZeroMemory(),

I've remembered this after seeing your post.

Thanks a lot.