CopyRates memory & cpu usage

 

Hi every body.

I wrote a script that scans all symbols in the system. It copies some data of each symbol and no more.

I use a customized version of MetaTrader version 5.0 build 2085 that my broker provided & its difference with official version of MetaTrader is ability to connect to the broker server that  have about 500 symbols.

In a for loop, I call CopyRates for each symbol. I monitor cpu & memory usage of MetaTrader in TaskManager and see both of them are increasing. after a while, my sysytem will be so slow and I should close MetaTrader & run it again!!!

What is the problem of CopyRates?

void CheckAllSymbols()
{
   int      symbolsCount = SymbolsTotal(false);
   string   symbolName;
   int      i;
   MqlRates   candleData[];

   for(i=0 ; i<symbolsCount ; i++)
   {
   
      bool alreadySelected = false;
      symbolName = SymbolName(i, false);

      if(SymbolInfoInteger(symbolName, SYMBOL_SELECT))
         alreadySelected = true;
      
      if(!alreadySelected)
         if(!SymbolSelect(symbolName, true))
            continue;

      CopyRates(symbolName, PERIOD_D1, 0, 50, candleData);
      
      if(!alreadySelected)
         SymbolSelect(symbolName, false);
   }
}

int OnStart(void)
{
   CheckAllSymbols();
   return 0;
}
 
rbigdelis :

Hi every body.

I wrote a script that scans all symbols in the system. It copies some data of each symbol and no more.

I use a customized version of MetaTrader version 5.0 build 2085 that my broker provided & its difference with official version of MetaTrader is ability to connect to the broker server that  have about 500 symbols.

That doesn't exist.

In a for loop, I call CopyRates for each symbol. I monitor cpu & memory usage of MetaTrader in TaskManager and see both of them are increasing. after a while, my sysytem will be so slow and I should close MetaTrader & run it again!!!

What is the problem of CopyRates?

There is no problem with CopyRates(). You are initiating data download on 500 symbols, you can't expect it to be fast. See the documentation.

When requesting data from the indicator, if requested timeseries are not yet built or they need to be downloaded from the server, the function will immediately return -1, but the process of downloading/building will be initiated.

When requesting data from an Expert Advisor or script, downloading from the server will be initiated, if  the terminal does not have these data locally, or building of a required timeseries will start, if data can be built from the local history but they are not ready yet. The function will return the amount of data that will be ready by the moment of timeout expiration, but history downloading will continue, and at the next similar request the function will return more data.

 
Alain Verleyen:

That doesn't exist.


There is no problem with CopyRates(). You are initiating data download on 500 symbols, you can't expect it to be fast. See the documentation.

Thanks for your comment.
Customized version is in my opinion, but it is part of MetaTrader 5 solution MetaQuotes provides for brokers.

..


What is better solution to scan all symbols and check our required conditions based on price information of them?

 
What is better solution to scan all symbols and check our required conditions based on price information of them?

1. Eventually MT5 will download all the historical data, so it won't have to initiate downloads any more. Then it should operate quickly.

2. Run your "scanner" from OnTimer() instead of OnTick(). Most scanners I write scan every 5 seconds because I don't need instant updates across dozens of symbols.

3. Run your code through the Profiler. There may be an inefficiency in your code that is slowing it down. I had one issue—I think it was converting an enum to a string every tick—that made my code 30% slower.

Then the standard answers: of a faster PC, more cores, more RAM, faster Internet

 
rbigdelis:

Thanks for your comment.
Customized version is in my opinion, but it is part of MetaTrader 5 solution MetaQuotes provides for brokers.

They can only customize "cosmetics" of the MT5Terminal.

What is better solution to scan all symbols and check our required conditions based on price information of them?

See @Anthony Garot answer.

I would add : ALWAYS check the return value of a function which can fail. In your script, detect the CopyRates error and "Sleep" a bit. On an EA or an indicator, work with events.