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

 
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.

 

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...
 
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).

 

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.

 

Thank you Enrico. The issue disappeared.

/Karl

 
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)
 
fxsaber:
I see. Yes, you are right.
 

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
 

Do these methods work with your MT5Bridge?

 

Hello Masters!

I have a serious problem with understanding "Candle counting" (First, second, third = idx, idx++, ...) in following Mql Signal code which belongs to SignalAC class.

Does anybody could help to penetrate to idx number when moving in codes down?

Thanks in advance.

//+------------------------------------------------------------------+
//| "Voting" that price will grow.                                   |
//+------------------------------------------------------------------+
int CSignalAC::LongCondition(void)
  {
   int result=0;
   int idx   =StartIndex();
//--- if the first analyzed bar is "red", don't "vote" for buying
   if(DiffAC(idx++)<0.0)
      return(result);
//--- first analyzed bar is "green" (the indicator has no objections to buying)
   if(IS_PATTERN_USAGE(0))
      result=m_pattern_0;
//--- if the second analyzed bar is "red", there is no condition for buying
   if(DiffAC(idx)<0.0)
      return(result);
//--- second analyzed bar is "green" (the condition for buying may be fulfilled)
//--- if the second analyzed bar is less than zero, we need to analyzed the third bar
   if(AC(idx++)<0.0)
     {
      //--- if the third analyzed bar is "red", there is no condition for buying
      if(DiffAC(idx++)<0.0)
         return(result);
     }
//--- there is a condition for buying
   if(IS_PATTERN_USAGE(1))
      result=m_pattern_1;
//--- if the previously analyzed bar is "red", the condition for buying has just been fulfilled
   if(IS_PATTERN_USAGE(2) && DiffAC(idx)<0.0)
      result=m_pattern_2;
//--- return the result
   return(result);
  }
Reason: