MT5 EA causing my CPU to MAX OUT .... same alorithm works fine in MT4

 

Hi all,

I have written and currently run a mulit-symbol EA in MT4 which monitors and trades a portfolio of appox 300 stock-CFD’s.

I have effectively re-written it for MT5 because the broker I now prefer, uses MT5 instead.

The code uses a timer initiated For Loop which cycles through the symbols, tests conditions and skips ones that don’t meet requirements using the continue; statement.

.......................................................................

Symbols[] = { “AAPL”,”GOOGLE”,………., + 600 symbols }

For (  i = 1 : i < arraysize(Symbols): i++  )

{
     If (number of bars < 300)
           continue;
}

.....................................................................

Now this is a simplification of what’s going on but here’s the thing!

Running this basic For-Loop which executes on the MT5 platform in less than a second, sends all the cores on my i7 8700k CPU into 100% usage for up to 3 minutes after the loop is completed.

I can set the CPU affinity to one core and the loop still completes in a second or two… but that core is running at 100% afterwards for what looks like no reason at all…

This exact For-Loop and system when running on MT4 still completes in the same time but doesn’t have this effect on my CPU.

Can someone explain what is happening and / or what I am doing wrong….?

string watchlist[] =
{

"3MCo","AIG","AMC","AMEX","AT&T","Abbott","AbercFitch","Activision","Adobe","AdvMicroDev","Aes","Aetna","Aflac","Akamai","Alcoa",
"Alibaba","Allstate","Altria","Amazon","Apple","Blackberry","Blackrock","Boeing","BofAmerica","BofNewYork","BristolMyer","Caterpillar",
"Chevron","Cisco","Citigroup","Coca-Cola","Comcast","ConocoPhil","Costco","Delta","Disney","Ebay","ExxonMobil","Facebook","Fedex",
"Ford","GeneralElec","GeneralMot","GoldmSachs","Google","HP","Halliburton","HarleyDav","HomeDepot","Honeywell","IBM","Intel","J&J",
"JPMorgan","L.V.Sands","MGMResorts","Mastercard","McDonalds","Merck&Co","Microsoft","Moodys","MorganStanl","Motorola","Netflix","Nike",
"Nvidia","Oracle","Pepsico","PetroleoBra","Pfizer","PhilipMorri","Procter&Gam","Qualcomm","RalphLauren","Sprint","Starbucks","Symantec",
"Tesla","Tiffany&Co","TimeWarner","UPS","UnitedTech","Verizon","Visa","WalMart","WellsFargo","WesternUni","Xerox","YumBrands","3iGroup",
"Aviva","BAESystems","BP","BTGroup","Barclays","Barratt","Betfair","Burberry","G4S","Easyjet","GSK","HSBC","LSE","Legal&Gen","Lloyds",
"ManGroup","Marks&Spen","Next","Prudential","RBS","Rolls-Royce","SHELL","SKY","Sainsbury","StdCharter","Tesco","ThomasCook","Unilever",
"Vodafone","AXA","AirFrance","BNPParibas","Carrefour","CreditAgric","LVMH","Loreal","Peugeot","Renault","Sanofi","SocieteGen","Total",
"Vivendi","Adidas","Airbus","AllianzAG","BASFAG","BMW","BayerAG","Commerzbank","DTBoerse","DTTelekom","Daimler","DeutschBank",
"DeutschPost","E.ON","Lufthansa","SAP","Siemens","Volkswagen","Heineken","BBVA","Repsol","Santander","CreditSuiss","Nestle","Novartis",
"UBS","Chubb","Altaba","Piraeus Bank","NBG","Alpha Bank","HTO","Eurobank","Jumbo","OPAP","PPC","EEE","Intesa Sanpaolo","UniCredit",
"Fiat Chrysler","Eni","Enel","UBI Banca","Telecom Italia","Banco BPM","Assicurazioni Generali","Snam","Atlantia","CNH Industrial",
"Mediobanca","UnipolSai","Saipem","Mediaset","Unipol","A2A","STMicroelectronics","Terna","Poste Italiane","Engie","ABInbev",
"Banco Comercial","EDP","Ericsson","SEB","SSAB","SCA","Volvo","H&M","Nordea Bank","Fingerprint Cards","Sandvik","TeliaSonera",
"Outokumpu","Nokia","Fortum","NorskHydro","PGS","RECSilicon","Iberdola","Telefonica","Banco de Sabadell","Inditex","Mapfre","Gamesa",
"DIA","IAG","Caixabank","Bankia","Roche","Swiss Re","ABB","Relx","Ahold Delhaize","Aegon","KPN","Philips","Altice","ArcelorMittal",
"ABN AMRO","ING","Danone","Veolia","Vinci","Orange","Infineon","RWE","BHP Billiton","Centrica","CRH","Diageo","Morrison","Rio Tinto",
"Shire","Taylor Wimpey","Kingfisher","BATobacco","Old Mutual","National Grid","ITV","Glencore","Compass","ConvaTec","Micron","JD",
"Applied Materials","FOX","Paypal","Kraft Heinz","Gilead","Analog Devices","Boston Scientific","CSX","CVS","Cabot Oil & Gas","Celgene",
"Chesapeake Energy","Corning","Target","Macys","Freeport-McMoRan","Gap","Huntington","KeyCorp","Kimco","Kroger","Mattel","Noble Energy",
"ONEOK","PulteGroup","Schlumberger","Charles Schwab","US Bancorp","Texas Instruments","Marathon Oil","Walgreens","CenturyLink",
"Transocean","Mondelez","Regions Financial","Kinder Morgan","AbbVie","American Airlines","Synchrony Financial","IHS Markit",
"HP Enterprise","Amgen","Berkshire Hathaway","Lowes","Medtronic","Southern","Metlife","Kirby","Golar","Teekay","Twitter","Buzzi Unicem",
"EXOR","Yoox","Salvatore","Moncler","FinecoBank","Ferrari","Italgas","Banca Generali","Tenaris","Prysmian","Banca Mediolanum","Leonardo",
"BPER Banca","Brembo","Luxottica","Recordati","Davide Campari","Azimut","Ackermans","Ageas","APERAM","Bekaert","Bpost","Cofinimmo",
"Colruyt","Bruxelles Lambert","Galapagos","KBC","Ontex","Proximus","Sofina","Solvay","Telenet","UCB","Umicore","Altri",
"Corticeira Amorim","CTT-Correios","EDP Renovaveis","Mota-Engil","Galp Energia","Jeronimo Martins","NOS","Navigator","Pharol","REN",
"Semapa","Sonae","Alfa Laval","Assa Abloy","AstraZeneca","Atlas Copco","Boliden","Electrolux","Getinge","Investor","Lundin Petroleum",
"Kinnevik","Securitas","Skanska","SKF","Svenska Handelsbanken","Swedbank","Swedish Match","Tele2","Autoliv","Essity","Amer Sports",
"Cargotec","Sampo","UPM-Kymmene","Metso","Kone","Konecranes","Nokian Renkaat","Orion","Outotec","Valmet","Tieto","Kesko","Huhtamaki",
"Stora Enso","Elisa","Wartsila","Neste","Metsa","Aker","Grieg Seafood","Golden Ocean","DNB","DNO","Nordic Nanovector","Norwegian Air",
"Gjensidige","Marine Harvest","Orkla","Schibsted","BW","Storebrand","Statoil","Subsea","Telenor","TGS NOPEC","Yara International",
"Aker Solutions","Bakkafrost","Leroy Seafood","Salmar","DowDuPont","Mytilineos","OMV","Voestalpine","Erste Group Bank","Immofinanz",
"Raiffeisen Bank","Admiral","Anglo American","Antofagasta","Ashtead","Associated British Foods","Babcock","British Land","Bunzl",
"Carnival","Croda","DCC","Direct Line","Experian","Ferguson","Fresnillo","GKN","Hammerson","Hargreaves","Imperial Brands","Informa",
"InterContinental","Intertek","Johnson Matthey","Land Securities","Mediclinic","Merlin Entertainments","Micro Focus","Mondi","Pearson",
"Persimmon","Provident","RSA Insurance","Randgold","Reckitt Benckiser","Rentokil","Royal Mail","SEGRO","SSE","Sage","Schroders",
"Scottish Mortgage","Severn Trent","Beiersdorf","Continental","Fresenius","Fresenius Medical","Heidelbergcement","Henkel","Linde",
"MRG","Merck","Prosiebensat","Thyssenkrupp","Vonovia","Accor","Air Liquide","Atos","Bouygues","Capgemini","Compagnie Generale",
"Compagnie de Saint Gobain","Essilor","Kering","Legrand","Pernod Ricard","Publicis","Safran","Schneider Electric","Sodexo","TechnipFMC",
"Unibail Rodamco","Valeo","Adecco","Geberit","Givaudan","Julius Baer","Lafargeholcim","Lonza","Richemont","SGS","Sika","Swatch",
"Swiss Life","Swisscom","Zurich Insurance","ACS","Abertis","Acciona","Acerinox","Aena","Amadeus","Bankinter","Cellnex","Enagas",
"Endesa","Ferrovial","Gas Natural SDG","Grifols","Indra Sistemas","Inmobiliaria","Mediaset Espana","Melia Hotels","Merlin Properties",
"Red Electrica","Tecnicas Reunidas","Viscofan","ASML Holding","Aalberts Industries","Akzo Nobel","Boskalis Westminster","DSM",
"Gemalto","NN Group","Randstad Holding","SBM","Vopak","Wolters Kluwer","Alexion Pharmaceuticals","Allergan","American Electric",
"AmerisourceBergen","Anadarko","Anthem","Apache","Archer Daniels","Autodesk","Automatic Data","Autozone","BB&T","Baidu","Ball Corp",
"Baxter","Best Buy","Biogen","Biomarin","Black&Decker","Boston Properties","Broadcom","Brown-Forman","CA","CBS","CH Robinson",
"CME","Campbell","Capital One","Centene","Cerner","Charter","Check Point","Chipotle","Cigna","Cintas","Citrix Systems","Cognizant",
"Colgate-Palmolive","Comerica","Constellation","Ctrip","DISH","DTE","Deere","Dentsply Sirona","Devon","Discover Financial","Discovery",
"Dollar General","Dollar Tree","Dominion","Duke","EOG Resources","Edwards","Electronic Arts","Eli Lilly","Emerson","Exelon","Expedia",
"Expeditors","Express Scripts","Fastenal","Fifth Third","FirstEnergy","Fiserv","Foot Locker","Franklin","General Dynamics",
"General Mills","Hartford Financial","Hasbro","Henry Schein","Hershey","Hilton","Hologic","Horton","Humana","ICE","IDEXX","Illumina",
"Incyte","International","Intuit","Intuitive Surgical","J B Hunt","J M Smucker","Johnson Controls","Juniper","KLA-Tencor",
"Kimberly-Clark","L Brands","Lam Research","Lennar","Liberty","Liberty Interactive","Lockheed Martin","Loews","Marriott",
"Marsh&McLennan","Maxim","Mercadolibre","Michael Kors","Microchip Technology","Monsanto","Monster","Mylan","NRG","Nasdaq",
"NetApp","NetEase","Newmont","NextEra","Norfolk Southern","Northern","Northrop","Norwegian Cruise Line","Nucor","O Reilly","Occidental",
"Oilwell Varco","PACCAR","PNC Financial","Patterson","Paychex","Perrigo","Progressive","Prudential Financial","Public Service",
"Public Storage","Quest","Red Hat","Regeneron","Robert Half","Rockwell","Ross Stores","Salesforce","Seagate","Signet","Sirius XM",
"Skyworks","State Street","SunTrust","Sysco","T-Mobile","Tractor Supply","Travelers Companies","Smith & Nephew","Smiths Group",
"Smurfit Kappa","St James","Standard Life","Tui","United Utilities","WPP","Whitbread","LUKOIL","Gazprom","Ulta Beauty","Under Armour",
"Union Pacific","United Continental","UnitedHealth","VF","Valero","Verisign","Verisk","Vertex","Viacom","Vornado","Western Digital",
"Weyerhaeuser","Whirlpool","Williams","Wynn Resorts","Xilinx","A&NZBank","AGLEnergy","AMP","Amcor","CommonwealthBank","Fortescue",
"NationalAustralia","NewcrestMining","QBEInsurance","QantasAirways","Santos","Scentre","Suncorp","SydneyAirport","Transurban","Vicinity",
"Wesfarmers","Westpac","WoodsidePetroleum","Woolworths","Motor Oil"
};

void TradeExecutor()
{   
   // --- Cycle through watchlist
   for( int i = 0; i<ArraySize(watchlist); i++ )
   {
      //--- Assign _Symbol to variable symbol
      string symbol = watchlist[i];
     
      // Are there enough bars for the EA? 
      if ( Bars( symbol, timeFrame ) < minBars )
      {
         if( errorPrint )
         {
             Print( symbol, " Not Enough Bars on Chart ... Line 455" );
         }
         continue;
      }
   }
}   

The above code is only a snippet of the whole EA, I placed a time capture around the For Loop which shows that the for loop is completed without error with a second or two.

I have isolated this particular call ... Bars( symbol, timeFrame ) ... as one of the culprits for my HIGH CPU usage (simply using a process of elimination) ...

Since the fully coded EA is otherwise running flawlessly... my current solution is to set CPU affinity to ONE CORE and then use a 3rd party program ( BES ) to physically restrict MT5 from using more than 25% of that core...

I'd truly appreciate any advice...

Cheers

 
MrRex87: Running this basic For-Loop which executes on the MT5 platform in less than a second, sends all the cores on my i7 8700k CPU into 100% usage for up to 3 minutes after the loop is completed.

Of course it does. You access a symbol, which isn't current, the terminal starts to download data for it. And you do this 600 times.

On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
          Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5

 

Hi whroeder1,

Thanks for your response!

I've looked at the link you attached, I'm not bad at MQL4 but MQL5 is new to me...

Why then does the equivalent function call in MQL4............... iBars(symbol,timeFrame) , not cause the same problem ?

Does MQL4 iBars not request that information from the server and instead pulls it from the terminal ?


This still doesn't explain why all CPU cores go to 100%... with affinity set to 1 core and that core limited to 25% of Max the EA still runs well...


Thanks Again!

 
MrRex87: Why then does the equivalent function call in MQL4............... iBars(symbol,timeFrame) , not cause the same problem ?
It does. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors.
          Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
It's just less evident because MT4 only downloads that TF whereas MT5 downloads M1 data and generates all others.
 

Mate, your a legend!

I'll change my approach and only call these functions once per trading session...

I'll store their results in variable arrays thus not needing to re-call the data every time the timer executes.

The CPU will crank for 3 minutes while it generates the data and fills the arrays but after that the EA will only need to monitor the BID and ASK for the rest of the trading session / per day.

Thanks Sincerely for your help!

Cheers

 
MrRex87:

I'll change my approach and only call these functions once per trading session...

I'll store their results in variable arrays thus not needing to re-call the data every time the timer executes.

No need to store results, the terminal is already doing that. Just check if you have updated data per symbol and then process that symbol. If not, trigger the update (MT5) for that symbol and return.
Reason: