Get the X symbols with highest volume in a week without crash MT5

 

I'm trying to get the 100 more transacted stocks of the last week (stocks only; no options; no indexes; no any other).


To do this, I created the function below, but I'm unable to run the MT5 with it because in some time the MT5 crashs with unknown runtime error.

When I call SymbolsTotal(false) the results is > 72k.


I notice that MT5 download the history data of the symbols automatically and I think that it's the cause of the problem. All symbols showed in the log (below) are options and I only got then to see the ISIN. I didn't need the history.


There is some way to disable the automatic history download OR a better way to do what I want?

 

string GetTopVolumeSymbols(const uint size = 50){
   long keys[];
   CSortedMap<long, string> map;
   string symbols = "";
   for(int i=SymbolsTotal(false)-1; i>=0; i--){
      string symbol = SymbolName(i, false);
      
      string isin = SymbolInfoString(symbol, SYMBOL_ISIN);
      if(StringFind(isin, "BR")!=0) continue; //only brazilian assets
      if(StringFind(isin, "ACNPR")<0 && StringFind(isin, "ACNOR")<0) continue; //only ON and PN stocks
      
      //remove forbidden
      if(StringFind(InForbiddenSymbols, symbol)>=0) continue;

      //remove no operated by 1week (probably disabled)
      datetime lastQuote = (datetime) SymbolInfoInteger(symbol, SYMBOL_TIME);
      if(lastQuote < TimeCurrent() - 7*8*60*60*1000 /*1w*/) continue;
      
      //remove disabled to long trades
      ENUM_SYMBOL_TRADE_MODE tradeMode = (ENUM_SYMBOL_TRADE_MODE)SymbolInfoInteger(symbol, SYMBOL_TRADE_MODE);
      if(tradeMode == SYMBOL_TRADE_MODE_CLOSEONLY){
         if(!PositionSelect(symbol)) continue;
         if(PositionGetDouble(POSITION_VOLUME) == 0) continue;
      } else {
         if(tradeMode != SYMBOL_TRADE_MODE_LONGONLY && tradeMode != SYMBOL_TRADE_MODE_FULL){
            continue;
         }
      }
      
      //only enable if there is enougth bars (to be used by indicators)
      int bars = Bars(symbol, InTimeframe2);
      if(bars<80) continue;
      
      long wVolume = iVolume(symbol, PERIOD_W1, 1);

      string value;
      if(map.TryGetValue(wVolume, value)){
         symbol += (";"+value);
      } else {
         int kSize = ArraySize(keys);
         ArrayResize(keys, kSize+1);
         keys[kSize] = wVolume;
      }
      map.Add(wVolume, ";"+symbol);
   }

   uint counter = 0;
   if(ArraySort(keys)){
      for(int i=ArraySize(keys)-1; i>=0;i--){
         string value;
         if(!map.TryGetValue(keys[i], value)) continue;
         
         string splitted[];
         if(StringSplit(value, ';', splitted) <= 0) continue;
         counter += ArraySize(splitted)-1;
         if(counter > size){
            for(uint j=1; j<counter-size; j++){
               symbols += (";"+splitted[i]);
            }
         }else {
            symbols += value;
         }
      }
   }

   
   return StringSubstr(symbols, 1);
}

LOGs:


Reason: