Hi,
You’re off to a good start for only two weeks. If you’re investing time in learning MQL5 I would advise picking up a good c++ tutorial (there aren’t many good modern-MQL tutorials) so you can get a good foundation in OOP.
One thing I want to point out that will kill your EA is the fact that you hard coded all your symbols, and as soon as you use a broker with appended symbols (eg. EURUSDi) your entire system will fail to work.
Here is an example of using OOP and the standard library to accomplish the task (and more) while reducing a lot of redundant code. Feel free to take this EA and set a break-point at the OnInit() and step through the code… it will help you learn by watching what’s happening at every step. Good luck on your learning journey!
//+------------------------------------------------------------------+
//| MultiCurrency.mq5 |
//| nicholishen |
//| www.reddit.com/u/nicholishenFX |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link "www.reddit.com/u/nicholishenFX"
#property version "1.00"
#include <Trade\SymbolInfo.mqh>
#include <Arrays\ArrayObj.mqh>
class MySymbol : public CSymbolInfo
{
protected:
ENUM_TIMEFRAMES m_period;
MqlRates m_rates[];
datetime m_bartime;
public:
MySymbol(const string symbol,ENUM_TIMEFRAMES period):m_period(period)
{
m_name = symbol;
ArraySetAsSeries(m_rates,true);
}
int BarsTotal() const { return ArraySize(m_rates);}
bool RefreshRates()
{
datetime btime = (datetime)SeriesInfoInteger(m_name,m_period,SERIES_LASTBAR_DATE);
if(m_bartime != btime || BarsTotal() < 300)
{
if(CopyRates(m_name,m_period,0,Bars(m_name,m_period),m_rates)<300)
return false;
m_bartime = btime;
}
return CSymbolInfo::RefreshRates();
}
double Open (const int i) const { return m_rates[i].open; }
double High (const int i) const { return m_rates[i].high; }
double Low (const int i) const { return m_rates[i].low ; }
double Close (const int i) const { return m_rates[i].close;}
datetime Time (const int i) const { return m_rates[i].time; }
};
class MySymbolCollection : public CArrayObj
{
public:
MySymbol* operator[](const int index)const{return(MySymbol*)At(index);}
bool Init(string &syms[])
{
Clear();
for(int i=0;i<ArraySize(syms);i++)
{
for(int j=0;j<SymbolsTotal(false);j++)
{
if(StringFind(SymbolName(j,false),syms[i])>=0)
{
Add(new MySymbol(SymbolName(j,false),Period()));
break;
}
}
}
EventSetTimer(1);
return true;
}
bool RefreshRates()
{
bool res = true;
for(int i=0;i<Total();i++)
if(!this[i].RefreshRates())
res = false;
return res;
}
};
string sym[]={ "AUDCAD","AUDCHF","AUDJPY","AUDNZD","AUDUSD","EURAUD","GBPAUD",
"CADCHF","CADJPY","EURCAD","GBPCAD","NZDCAD","USDCAD","CHFJPY",
"EURCHF","GBPCHF","NZDCHF","USDCHF","EURGBP","EURJPY","EURNZD",
"EURUSD","GBPJPY","GBPNZD","GBPUSD","NZDJPY","USDJPY","NZDUSD"};
MySymbolCollection symbols;
//+------------------------------------------------------------------+
int OnInit()
{
//---
symbols.Init(sym);
symbols.RefreshRates();
//---
return(INIT_SUCCEEDED);
}
void OnTick()
{
//---
if(!symbols.RefreshRates())
{
Print(__FUNCTION__+" Waiting for data");
return;
}
else
{
for(int i=0;i<symbols.Total();i++)
Print("My Symbol is ",symbols[i].Name()," and I have copied ",symbols[i].BarsTotal()," bars to rates.");
ExpertRemove();
}
// --- stuff with symbol data....
}
//+------------------------------------------------------------------+
void OnTimer()
{
symbols.RefreshRates();
EventKillTimer();
}
Files:
MultiCurrency.mq5 4 kb