MT5 OrdersTotals() for active pending orders does't works in backtest strategy - page 2

 

So, the most important thing: I am testing in the licensed version of Windows, I do not use any emulators for Linux or the semi-axis.

2019.12.23 06:45:32.586 Terminal        MetaTrader 5 x64 build 2284 started (MetaQuotes Software Corp.)
2019.12.23 06:45:32.599 Terminal        Windows 10 (build 18363) x64, IE 11, UAC, Intel Core i3-3120M  @ 2.50GHz, Memory: 3941 / 8077 Mb, Disk: 87 / 415 Gb, GMT+2
2019.12.23 06:45:32.599 Terminal        C:\Users\barab\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075

The simplest EA:

//Create an instance of CTrade
#include<Trade\Trade.mqh>
CTrade m_trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   MqlTick tick;
   SymbolInfoTick(Symbol(),tick);
   m_trade.BuyLimit(1.0,tick.ask-100*Point());
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int orders_total=OrdersTotal();
   int d=0;
  }
//+------------------------------------------------------------------+


And the result of work (click on picture):


Files:
1.mq5  2 kb
 
Vladimir Karputov:

So, the most important thing: I am testing in the licensed version of Windows, I do not use any emulators for Linux or the semi-axis.

The simplest EA:


And the result of work (click on picture):


i test your expert ad works


this is my code for open buy limit i create my own class:

***

   

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The...
 
termi_80 :

i test your expert ad works


this is my code for open buy limit i create my own class:

***

   

Please provide the FULL MQL5 code. Please insert CORRECTLY your code - use the button 

 


this my terminal

 
Vladimir Karputov:

So, the most important thing: I am testing in the licensed version of Windows, I do not use any emulators for Linux or the semi-axis.

The simplest EA:


And the result of work (click on picture):


in your way you can't use buyasync and sellasync? correct?
 
Vladimir Karputov:

Please provide the FULL MQL5 code. Please insert CORRECTLY your code - use the button 

//+------------------------------------------------------------------+
//|                                                    SendOrder.mqh |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#include <Trade\Trade.mqh>
#include <TrailingClass.mqh>
CTrade  Trader;
class WorkWithOrder
 {

  public:
  bool SellAsync(string Commento,double volume,double sl,double tp,int magic)
  {
//--- prepare the request
   MqlTradeRequest req={0};
   req.action      =TRADE_ACTION_DEAL;

   req.symbol      =_Symbol;
   req.magic       = magic;
   req.volume      =volume;
   req.type        =ORDER_TYPE_SELL;
   req.price       =SymbolInfoDouble(req.symbol,SYMBOL_BID);
   req.deviation   =5;  
   req.type_filling = ORDER_FILLING_IOC;
   req.comment     = Commento;
   if (sl ==0){
   req.sl =0; 
   }else
   {
   req.sl          =SymbolInfoDouble(req.symbol,SYMBOL_BID)+ sl;
   }
   if (tp ==0){
   req.tp          =0;
   }else
   {
   req.tp  = SymbolInfoDouble(req.symbol,SYMBOL_BID) - tp;
   }
   double ST = req.sl;
   //MessageBox(ST);
   MqlTradeResult  res={0};
   if(!OrderSendAsync(req,res))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
    //  BotTlg.Token(InpTokenTlg);
      return false;
     }
     else
     {
      return true;
     }
//---
    }
  bool BuyAsync(string Commento,double volume,double sl,double tp,int magic)
  {
//--- prepare the request
   MqlTradeRequest req={0};
   req.action      =TRADE_ACTION_DEAL;
   req.symbol      =_Symbol;
   req.magic       =magic;
   req.volume      =volume;
   req.type        =ORDER_TYPE_BUY;
   req.price       =SymbolInfoDouble(req.symbol,SYMBOL_ASK);
   req.deviation   =5;
   if (sl ==0){
   req.sl =0; 
   }else
   {
   req.sl          =SymbolInfoDouble(req.symbol,SYMBOL_ASK)- sl;
   }
   if (tp ==0){
   req.tp          =0;
   }else
   {
   req.tp  = SymbolInfoDouble(req.symbol,SYMBOL_ASK) + tp;
   }
   req.type_filling = ORDER_FILLING_IOC;
   req.comment     =Commento;

   
   MqlTradeResult  res={0};
    if(!OrderSendAsync(req,res))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
   //   BotTlg.Token(InpTokenTlg);
     // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore Apertura Ordine error: " + GetLastError() + " retcode = " + res.retcode );
      return false;
     }
     else
     {
      return true;
     }
//---
  } 
    
   bool CloseOrder(ulong Ticket)
    {
    
    if(!Trader.PositionClose(Ticket,ULONG_MAX))
     {
     Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",Trader.ResultRetcode());
   //   BotTlg.Token(InpTokenTlg);
     // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore Chiusura Ordine error: " + GetLastError() + " retcode = " + Trader.ResultRetcode() );

      return false;
     }
     else
     {
      return true;
     }
    
    }
    
    bool ModifyOrder(ulong Ticket,double lotti)
    {
     if(!Trader.PositionClosePartial(Ticket,lotti,ULONG_MAX))
     {
     Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",Trader.ResultRetcode());
    //  BotTlg.Token(InpTokenTlg);
    // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore chiusura parziale Ordine error: " + GetLastError() + " retcode = " + Trader.ResultRetcode() );
      return false;
     }
     else
     {
      return true;
     }
    }
    
    
     bool ModifyPending(ulong Ticket,double prezzo,double sl,double tp)
    {
     if(!Trader.OrderModify(Ticket,prezzo,sl,tp,ORDER_TIME_GTC,0,0))
     {
     Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",Trader.ResultRetcode());
    //  BotTlg.Token(InpTokenTlg);
    // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore chiusura parziale Ordine error: " + GetLastError() + " retcode = " + Trader.ResultRetcode() );
      return false;
     }
     else
     {
      return true;
     }
    }
    
    
    
     bool ModifyTp(ulong Ticket,double sl,double tp)
    {
     if(!Trader.PositionModify(Ticket,sl,tp))
     {
     Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",Trader.ResultRetcode());
    //  BotTlg.Token(InpTokenTlg);
    // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore  tp error: " + GetLastError() + " retcode = " + Trader.ResultRetcode() );
      return false;
     }
     else
     {
      return true;
     }
    }
    
   bool SendSellLimit(string Commento,double volume,double pr,double sl,double tp,int magic)
  {
//--- prepare the request
   MqlTradeRequest req={0};
   req.action      =TRADE_ACTION_PENDING;
   req.symbol      =_Symbol;
   req.magic       =magic;
   req.volume      =volume;
   req.type        =ORDER_TYPE_SELL_LIMIT;
   req.price       =pr;
   req.deviation   =5;
   if (sl ==0){
   req.sl =0; 
   }else
   {
   req.sl          =sl;
   }
   if (tp ==0){
   req.tp          =0;
   }else
   {
   req.tp  = req.price - tp;
   }
   req.type_filling = ORDER_FILLING_IOC;
   req.comment     =Commento;
   req.type_time = ORDER_TIME_GTC;
   req.expiration = 0;
   //req.expiration = TimeTradeServer() + 600;

   
   MqlTradeResult  res={0};
    if(!OrderSendAsync(req,res))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
     // BotTlg.Token(InpTokenTlg);
     // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore Apertura Ordine error: " + GetLastError() + " retcode = " + res.retcode );
      return false;
     }
     else
     {
      return true;
     }
//---
  } 
    
    
  bool SendBuyLimit(string Commento,double volume,double pr,double sl,double tp,int magic)
  {
//--- prepare the request
   MqlTradeRequest req={0};
   req.action      =TRADE_ACTION_PENDING;
   req.symbol      =_Symbol;
   req.magic       =magic;
   req.volume      =volume;
   req.type        =ORDER_TYPE_BUY_LIMIT;
   req.price       =pr;
   req.deviation   =5;
   if (sl ==0){
   req.sl =0; 
   }else
   {
   req.sl          =sl;
   }
   if (tp ==0){
   req.tp          =0;
   }else
   {
   req.tp  = req.price  + tp;
   }
   req.type_filling = ORDER_FILLING_IOC;
   req.comment     =Commento;
   req.type_time = ORDER_TIME_GTC;
   req.expiration = 0;
   //req.expiration = TimeTradeServer() + 600;

   
   MqlTradeResult  res={0};
    if(!OrderSendAsync(req,res))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
     // BotTlg.Token(InpTokenTlg);
     // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore Apertura Ordine error: " + GetLastError() + " retcode = " + res.retcode );
      return false;
     }
     else
     {
      return true;
     }
//---
  }   
    
    
     bool SendSellStop(string Commento,double volume,double pr,double sl,double tp,int magic)
  {
//--- prepare the request
   MqlTradeRequest req={0};
   req.action      =TRADE_ACTION_PENDING;
   req.symbol      =_Symbol;
   req.magic       =magic;
   req.volume      =volume;
   req.type        =ORDER_TYPE_SELL_STOP;
   req.price       =pr;
   req.deviation   =5;
   if (sl ==0){
   req.sl =0; 
   }else
   {
   req.sl          =sl;
   }
   if (tp ==0){
   req.tp          =0;
   }else
   {
   req.tp  = req.price  - tp;
   }
   req.type_filling = ORDER_FILLING_IOC;
   req.comment     =Commento;
   req.type_time = ORDER_TIME_GTC;
   req.expiration = 0;
  // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Invio Sell Stop " + req.price + " Prezzo attuale " + SymbolInfoDouble(req.symbol,SYMBOL_ASK) );
 
   
   MqlTradeResult  res={0};
    if(!OrderSendAsync(req,res))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
     // BotTlg.Token(InpTokenTlg);
    // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore Apertura Ordine error: " + GetLastError() + " retcode = " + res.retcode );
      return false;
     }
     else
     {
      return true;
     }
//---
  } 
    
    
  bool SendBuyStop(string Commento,double volume,double pr,double sl,double tp,int magic)
  {
//--- prepare the request
   MqlTradeRequest req={0};
   req.action      =TRADE_ACTION_PENDING;
   req.symbol      =_Symbol;
   req.magic       =magic;
   req.volume      =volume;
   req.type        =ORDER_TYPE_BUY_STOP;
   req.price       =pr;
   req.deviation   =5;
   if (sl ==0){
   req.sl =0; 
   }else
   {
   req.sl          =sl;
   }
   if (tp ==0){
   req.tp          =0;
   }else
   {
   req.tp  = req.price  + tp;
   }
   req.type_filling = ORDER_FILLING_IOC;
   req.comment     =Commento;
   req.type_time = ORDER_TIME_GTC;
   req.expiration = 0;
  
  // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Invio Buy stop " + req.price + " Prezzo attuale " + SymbolInfoDouble(req.symbol,SYMBOL_ASK) );
   
   MqlTradeResult  res={0};
    if(!OrderSendAsync(req,res))
     {
      Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
     // BotTlg.Token(InpTokenTlg);


    // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore Apertura Ordine error: " + GetLastError() + " retcode = " + res.retcode );
      return false;
     }
     else
     {
      return true;
     }
//---
  }     
   bool CloseOrderPending(ulong Ticket)
    {
    
    if(!Trader.OrderDelete(Ticket))
     {
     Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",Trader.ResultRetcode());
     // BotTlg.Token(InpTokenTlg);
     // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore Chiusura Ordine error: " + GetLastError() + " retcode = " + Trader.ResultRetcode() );

      return false;
     }
     else
     {
      return true;
     }
    
    } 
    
 };


 bool ModifyPending(ulong Ticket,double prezzo,double sl,double tp)
    {
     if(!Trader.OrderModify(Ticket,prezzo,sl,tp,ORDER_TIME_GTC,0,0))
     {
     Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",Trader.ResultRetcode());
    //  BotTlg.Token(InpTokenTlg);
    // TelegramSendText(InpTokenTlg,InpChannelNameTlg,"Errore chiusura parziale Ordine error: " + GetLastError() + " retcode = " + Trader.ResultRetcode() );
      return false;
     }
     else
     {
      return true;
     }
    }
 
termi_80 :

I'm tired of using a soldering iron to pull words out of you.

You have shown NON-WORKING code. You showed a code that CANNOT be compiled.


Look for errors in your code YOURSELF. I responsibly declare that OrdersTotal () works fine.

 
Vladimir Karputov:

I'm tired of using a soldering iron to pull words out of you.

You have shown NON-WORKING code. You showed a code that CANNOT be compiled.


Look for errors in your code YOURSELF. I responsibly declare that OrdersTotal () works fine.

thanks for your support.

For me it's enough..

Regards

 
termi_80:

You probably call OrdiniApertiPending() before the order is open, but once your breakpoint is reached the environment is updated and your pending is showing in the Strategy Tester.

Just a guess, as you didn't post code to reproduce the issue.

The Sleep() function is not doing what you may think in the Strategy Tester.

 
Alain Verleyen:

You probably call OrdiniApertiPending() before the order is open, but once your breakpoint is reached the environment is updated and your pending is showing in the Strategy Tester.

Just a guess, as you didn't post code to reproduce the issue.

The Sleep() function is not doing what you may think in the Strategy Tester.

i call after open the order as you can see in the pics, but with suggestion of Vladimir i solved this problem into stategy tester
Reason: