Discussion of article "Cross-Platform Expert Advisor: Signals" - page 3

To add comments, please log in or register
Enrico Lambino
5851
Enrico Lambino  
Karl Klang:

Hi Enrico,

I have found a way to handle the Calculate function now including a hysteresis feature which seems to work when the MA and the TEMA indicator crosses.

However I have run into a problem with the Order Manager. It happens when I have run the strategy tester for some loops during optimization. For the first loops it work fine but after some loops an order is placed for every candle.

Here it works fine:


But here the Order Manager starts to place multiple orders:


When I debugged the following code part of the COrderManager, the statement orders_total = OrdersTotal(); becomes 1 when it works but becomes 0 when it fails, which leads to that the if condition m_max_order>orders_total always evaluates to true.

Hopefully you can help to figure this out.

Best Regards/
Karl

Hi Karl,

I have never encountered this issue before. The OrdersTotal() method simply counts the number of active COrder objects. If there is something wrong with the coded strategy then it must be corrected. If not, then there must be some issue on how the COrder instances are managed (created/destroyed) and/or how they are counted. I will let you know as soon as I come across this problem.

Karl Klang
36
Karl Klang  

Hi Enrico,

Googling on mt5 OrdersTotal() issues reveals others have problems as well. E.g. https://www.mql5.com/en/forum/1995

I'll upload the latest version of my strategy. Maybe you are able to reproduce the same issue if you try the strategy tester with this strategy. The Expert Set is also uploaded so you can run the strategy tester with the same input as I did.

Best Regards/
Karl

How to use the OrdersTotal() correctly?
How to use the OrdersTotal() correctly?
  • 2010.09.09
  • www.mql5.com
I tried many times to check OrdersTotal(), but I found it did not work...
Enrico Lambino
5851
Enrico Lambino  
Karl Klang:

Hi Enrico,

Googling on mt5 OrdersTotal() issues reveals others have problems as well. E.g. https://www.mql5.com/en/forum/1995

I'll upload the latest version of my strategy. Maybe you are able to reproduce the same issue if you try the strategy tester with this strategy. The Expert Set is also uploaded so you can run the strategy tester with the same input as I did.

Best Regards/
Karl

Thanks for the files Karl. I encountered the problem you were describing.

On the main include file, change this (OnInit):

file.Open(savefile,FILE_READ);
if(!experts.Load(file.Handle()))
   return(INIT_FAILED);
file.Close();

to this:

#ifdef __MQL5__
if (!(MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION)))
#else
if (!(IsTesting() || IsOptimization()))
#endif
{
   file.Open(savefile,FILE_READ);
   if(!experts.Load(file.Handle()))
      return(INIT_FAILED);
   file.Close();
}


And this (OnDeinit):

file.Open(savefile,FILE_WRITE);
experts.OnDeinit(reason,file.Handle());
file.Close();

to this:

#ifdef __MQL5__
if (!(MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION)))
#else
if (!(IsTesting() || IsOptimization()))
#endif
{
  file.Open(savefile,FILE_WRITE);
  experts.OnDeinit(reason,file.Handle());
  file.Close();
}

For some reason, the data saved from previous sessions is being reloaded to future sessions. That is why the problem only exists after the initial backtest, sometimes with some leak in memory. Based on my testing, the issue appears to exist only on netting mode, not on MT4 or on MT5 hedging mode. But let me know if you continue to experience this problem. The code above prevents loading and saving data during backtesting and optimization (I overlooked mentioning this while writing the final article).

Enrico Lambino
5851
Enrico Lambino  

Also, regarding the OrdersTotal() in MT5, it only counts the pending orders (not the equivalent of MT4's OrdersTotal). The code in TradeOpen for OrderManager:

bool COrderManager::TradeOpen(const string symbol,ENUM_ORDER_TYPE type,double price,bool in_points=true)
  {
   bool ret=false;
   double lotsize=0.0;
   int trades_total =TradesTotal();
   int orders_total = OrdersTotal();

uses the OrdersTotal() method of the class, which is just a wrapper of COrders' method (also the same name). COrders extends CArrayObj, so it is not really using the MT5 native OrdersTotal() function. The above is also equivalent to using:

int orders_total = this.OrdersTotal();

On the other hand, if we use:

int orders_total = ::OrdersTotal();

this explicitly calls the MT5 native function. You may already be familiar with this, but just in case for other people who encounter a similar problem.

Karl Klang
36
Karl Klang  

Thank you Enrico. The issue disappeared.

/Karl

fxsaber
18064
fxsaber  
Enrico Lambino:
#ifdef __MQL5__
if (!(MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION))) // MQL4-code, not only MQL5.
#else
if (!(IsTesting() || IsOptimization()))
#endif
MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION) == MQLInfoInteger(MQL_TESTER)
Enrico Lambino
5851
Enrico Lambino  
fxsaber:
I see. Yes, you are right.
Karl Klang
36
Karl Klang  

Hi Enrico,

In the Github at iceron/MQLx, there is a change notice of m_new_signal and m_new_signal_close. 

m_signal_new can be changed by the void CSignalsBase::NewSignal(const bool value) method, but there are no method available to change m_new_signal_close.

Could you please elaborate on the usage of m_new_signal and m_new_signal_close? 

   if(m_invert)
     {
      CSignal::SignalInvert(m_signal_open);
      CSignal::SignalInvert(m_signal_close);
     }
   if(m_new_signal)
     {
      if(m_signal_open==m_signal_open_last)
         m_signal_open = CMD_NEUTRAL;      
     }
   if(m_new_signal_close)
     {
      if(m_signal_close==m_signal_close_last)
         m_signal_close = CMD_NEUTRAL;      
     }

Best Regards/

Karl

iceron - Overview
iceron - Overview
  • github.com
Sign up for your own profile on GitHub, the best place to host code, manage projects, and build software alongside 36 million developers. Sign up
smatt2008
69
smatt2008  

Do these methods work with your MT5Bridge?

123
To add comments, please log in or register