The Expert advisor works very slowly

 
Hello, dear friends and experts.
I have an expert advisor that works based on the the Trix indicator crosses. Its main part is coded as follows:


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()

  {
//---

   bool newbar = true ;
   int bar_shift = 0 ;

   if(trade_in_new_bar == true)
     {
      newbar = m_newbar.CheckNewBar(_Symbol,PERIOD_CURRENT);
      bar_shift = 1 ;
     }
   if(newbar == true)
      main_code();
  }
//+------------------------------------------------------------------+
//| MAIN                                                             |
//+------------------------------------------------------------------+
void main_code()
  {
   MqlRates bar[] ;
   ArraySetAsSeries(bar, true) ;
   CopyRates(_Symbol,_Period, 0, 10, bar) ;
//--- Timer check

   MqlDateTime timestruct ;
   TimeToStruct(TimeCurrent(),timestruct);

   bool timeron = true ;
   bool rollOver_time = false ;
   bool closeall_time = false ;

   if(Use_Timer)
     {
      if(timestruct.day_of_week == 5) // if Friday
         timeron = m_timer.DailyTimer(friday_start_hour,friday_start_minutes,friday_end_hour,friday_end_minutes,Use_local_time);
      else
         timeron = m_timer.DailyTimer(start_hour,start_minutes,end_hour,end_minutes,Use_local_time);
     }
      
   if(closeall_time_today)
      closeall_time = m_timer.DailyTimer(close_time_hour,close_time_minutes,close_time_hour,close_time_minutes+5,Use_local_time) ;
   else
      closeall_time = false ;
     

//--- set trix_buffer1&2
   double trix_buffer[] ;
   ArraySetAsSeries(trix_buffer,true) ;
   double signal_buffer[] ;
   ArraySetAsSeries(signal_buffer,true) ;


   int trix_handle = iCustom(_Symbol,_Period,"Examples\\trix-arrows - 2",InpPeriod,InpMethod1,InpMethod2,InpMethod3,InpPeriodSig,InpMethodSig,InpAppliedPrice);

   CopyBuffer(trix_handle,1,0,10,trix_buffer);
   CopyBuffer(trix_handle,0,0,10,signal_buffer);


//--- step 1
   int spread = m_symbol.Spread() ;

//--- MODE1

   if(trix_buffer[1] > signal_buffer[1] && trix_buffer[0] < signal_buffer[0]  && trix_buffer[1] < Low_l)
     {
      Print("1 :",trix_buffer[1],"  ",signal_buffer[1]) ;
      Print("2 :",trix_buffer[2],"  ",signal_buffer[2]) ;
      Print("cross mode 1");
      Print("timer is :",timeron," roll over is :",rollOver_time);
      if(timeron && rollOver_time == false)
        {
         Print("max spread = ",max_spread, " spread = ",spread) ;
         if(Both_mode && positions_total_mode_1==0 && spread <= max_spread && tradetime != bar[0].time)
           {
            MODE_1(vol1);
            if(multi_trade_in_one_bar == false)
               tradetime = bar[0].time ;
            Print("start MODE 1 with both mode");
           }
         else
           {
            if(positions_total_mode_2==0 && positions_total_mode_1==0 && spread <= max_spread && tradetime != bar[0].time)
              {
               MODE_1(vol1);
               if(multi_trade_in_one_bar == false)
                  tradetime = bar[0].time ;
               Print("start MODE 1 without both mode");
              }
           }
        }
     }

//--- MODE2
   if(trix_buffer[1] < signal_buffer[1] && trix_buffer[0] > signal_buffer[0] && trix_buffer[1]> High_l)
     {
      Print("1 :",trix_buffer[1],"  ",signal_buffer[1]) ;
      Print("2 :",trix_buffer[2],"  ",signal_buffer[2]) ;
      Print("cross mode 2");
      Print("timer is :",timeron," roll over is :",rollOver_time);
      if(timeron && rollOver_time == false)
        {
         Print("max spread = ",max_spread, " spread = ",spread) ;
         if(Both_mode && positions_total_mode_2 == 0 && spread <= max_spread && tradetime != bar[0].time)
           {
            MODE_2(vol1);
            if(multi_trade_in_one_bar == false)
               tradetime = bar[0].time ;
            Print("start mode 2 with Both_mode");
           }
         else
           {
            if(positions_total_mode_1==0 && positions_total_mode_2 == 0 && spread <= max_spread && tradetime != bar[0].time)
              {
               MODE_2(vol1);
               if(multi_trade_in_one_bar == false)
                  tradetime = bar[0].time ;
               Print("start mode 2 without Both_mode");
              }
           }
        }

     }
//+------------------------------------------------------------------+
//--- take Profit Mode 1 calculate
   Profit_Mode1 = 0 ;
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
     {
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
        {
         if(m_position.Magic()==magic_number_MODE1)
           {
            Profit_Mode1 += m_position.Profit();
           }
        }
     }
   if(Profit_Mode1!=0)
      Print("Profit_Mode1 = ",Profit_Mode1);

//--- take Profit Mode 2 calculate
   Profit_Mode2 = 0 ;
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
     {
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
        {
         if(m_position.Magic()==magic_number_MODE2)
           {
            Profit_Mode2 += m_position.Profit();
           }
        }
     }
   if(Profit_Mode2!=0)
      Print("Profit_Mode2 = ",Profit_Mode2);


 When working alive or during backtesting, it gradually fills up the entire of pc RAM and is very slow. Is there a solotion?
 

Your topic has been moved to the section: Expert Advisors and Automated Trading

In the future, please consider which section is most appropriate for your query.

 
Fernando Carreiro #:

Your topic has been moved to the section: Expert Advisors and Automated Trading

In the future, please consider which section is most appropriate for your query.

Yes that's right. I did not choose the right category.
Thanks a lot.
 
habibie61 When working alive or during backtesting, it gradually fills up the entire of pc RAM and is very slow. Is there a solotion?
   int trix_handle = iCustom(_Symbol,_Period,"Examples\\trix-arrows - 2",InpPeriod,InpMethod1,InpMethod2,InpMethod3,InpPeriodSig,InpMethodSig,InpAppliedPrice);

   CopyBuffer(trix_handle,1,0,10,trix_buffer);

Solution: stop creating millions of indicators in RAM.

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
William Roeder #:

Solution: stop creating millions of indicators in RAM.

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Thank you very much Mr. William Roeder. I will follow your advice carefully and update this thread.

 
William Roeder #:

Solution: stop creating millions of indicators in RAM.

...

It was great. Using the first link you posted, I was able to modify the code and the performance is great now. It's strange that I've seen that thread before but I didn't succeed in the first attempt. Now I proceeded with the reverse engineering method and the result was good. Instead of modifying my own codes, I changed the expert codes that were mentioned in the topic according to my expectations.


Now I came across a strange thing. Error 4003. Of course, it has been solved, but the cause of its occurrence is unknown to me. When I call Trix indicator normally:

In the visual test mode, there is no problem, but in the backtest mode without visual, it shows error 4003. I disabled the indicator window and the problem was solved. But is there a way to not see the error when the indicator display window is active?


Please excuse my grammar mistakes as English is not my first language