Download MetaTrader 5
To add comments, please log in or register
Do you know that MQL5 has structures?
Frank Sarno
40
Frank Sarno 2014.04.11 17:22 

Hello,

Below are the first lines in my first EA.

I get 0 returned for the iTime function.

Is there something else I need to do to allow a single EA on a single chart to

work with multiple symbols on multiple time frames? For example, do I need

to have at least one chart open for each currency pair and timeframe that I

wish to access (20 pairs at 4 timeframes per = 80 charts).

EA attached to a 4 hour EUR/USD chart

Statement Returns Error

bar15 = iTime("EURUSD",PERIOD_M15,0); 0 invalid function parameter value

bar60 = iTime("EURUSD",PERIOD_H1,0); 0 invalid function parameter value

bar4h = iTime("EURUSD",PERIOD_H4,0); A good value

barDay = iTime("EURUSD",PERIOD_D1,0); 0 invalid function parameter value

Alain Verleyen
Moderator
30635
Alain Verleyen 2014.04.11 17:35  
Fsarno:

Hello,

Below are the first lines in my first EA.

I get 0 returned for the iTime function.

Is there something else I need to do to allow a single EA on a single chart to

work with multiple symbols on multiple time frames? For example, do I need

to have at least one chart open for each currency pair and timeframe that I

wish to access (20 pairs at 4 timeframes per = 80 charts).

EA attached to a 4 hour EUR/USD chart

Statement Returns Error

bar15 = iTime("EURUSD",PERIOD_M15,0); 0 invalid function parameter value

bar60 = iTime("EURUSD",PERIOD_H1,0); 0 invalid function parameter value

bar4h = iTime("EURUSD",PERIOD_H4,0); A good value

barDay = iTime("EURUSD",PERIOD_D1,0); 0 invalid function parameter value

Always check documentation firstly :

If local history is empty (not loaded), function returns 0. To check errors, one has to call the GetLastError() function.

Which error number did you get ?

whroeder1
14457
whroeder1 2014.04.11 17:58  
  1. Use SRC to post code
  2. GetLastError
  3. Does your broker use "EURUSD" (exactly) or some other variation.
Frank Sarno
40
Frank Sarno 2014.04.11 19:39  
WHRoeder:
  1. Use SRC to post code
  2. GetLastError
  3. Does your broker use "EURUSD" (exactly) or some other variation.
Frank Sarno
40
Frank Sarno 2014.04.11 19:47  
Fsarno:
I did use GetLastError, and this returned the error description "invalid function parameter". I thought that local history applied to offline backtesting. Should I first check for history and then load? I did google around but didn't see this as a recommendation. I don't think this is a eurusd / eurusd. Problem as I am able to retrieve data from the 4 hour time frame.
Alain Verleyen
Moderator
30635
Alain Verleyen 2014.04.11 20:44  
Fsarno:
I did use GetLastError, and this returned the error description "invalid function parameter". I thought that local history applied to offline backtesting. Should I first check for history and then load? I did google around but didn't see this as a recommendation. I don't think this is a eurusd / eurusd. Problem as I am able to retrieve data from the 4 hour time frame.

GetLastError() return an int, an error number. It should be 4066.

"invalid function parameter" is error 4051 and it seems weird in your case. Can you copy and paste (using SRC button) the exact code you are running ?

Frank Sarno
40
Frank Sarno 2014.04.11 21:00  
Will do later tonight. I am not near my PC right now.
Frank Sarno
40
Frank Sarno 2014.04.11 22:45  

Hello,

Thanks for taking your time to respond to this! Please find attached my

code which tries to detect the opening of candles on 15, 1H, 4H, and daily

time frames. I am looking to write my first EA, but have been unable run

my first lines of code. My goal is to have one EA attached to one

chart, that allows for trading multiple currency pairs in multiple time

frames. I don't want to have to load a separate EA for each currency

pair / time frame combination. Do you have to load history for each

currency pair / time frame combination when the EA loads every time? If

the answer is yes, can you tell me what the MQL command is. (CopyRates,

RefreshRates, ArrayCopyRates, ??) For some reason I haven't yet been able

to find it. I expected it to be more readily available as everybody must

need to do this. Thank you very much in advance.

//+-------------------------------------------------------------+
//| Structure to perform actions when a new bar begins for the  |
//| following time frames:                                      |
//|           15 minute, 1 hour, 4 hour,  Daily                 |
//| of info outside the current chart and time frame            |
//+-------------------------------------------------------------+

#include <stderror.mqh>
#include <stdlib.mqh>

// External variables
extern bool DisplayAlert = true;
extern int maxMessages = 0;

// Global variables
double barOpen, barClose, barHigh, barLow;
string pairs[4] = {"EURUSD", "USDJPY", "USDCAD", "AUDUSD"};
string emailSubject, emailContent;
datetime bar15;
datetime bar60;
datetime bar4h;
datetime barDay;
int retValue, check;

int init()
{
debPrint(StringConcatenate("Init kicks off at ",TimeToStr(TimeCurrent(),TIME_SECONDS)));
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #A Error: ",ErrorDescription(check));
debPrint("TimeGMT() - TimeGMTOffset() = " + TimeToStr((TimeGMT() - TimeGMTOffset()),TIME_MINUTES));
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #B Error: ",ErrorDescription(check));
return(0);
}

// Start function
int start()
{
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #1 Error: ",ErrorDescription(check));

maxMessages = maxMessages + 1;

if (maxMessages == 1)
{
bar15 = iTime("EURUSD",PERIOD_M15,0);
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #x set M15, Error: ",ErrorDescription(check));
bar60 = iTime("EURUSD",PERIOD_H1,0);
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #y set H1, Error: ",ErrorDescription(check));
bar4h = iTime("EURUSD",PERIOD_H4,0);
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #z set H4, Error: ",ErrorDescription(check));
barDay = iTime("EURUSD",PERIOD_D1,0);
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #F set D1, Error: ",ErrorDescription(check));
}

debPrint(StringConcatenate("Begin Start for EUR/USD, ",TimeToStr(TimeCurrent(),TIME_SECONDS), " Top of function"));
debPrint(StringConcatenate("Begin Start, previous barDay opening  =",TimeToStr(barDay,TIME_SECONDS), ", current open  " ,TimeToStr(iTime("EURUSD",PERIOD_D1,0),TIME_SECONDS)));
debPrint(StringConcatenate("Begin Start, previous bar4h opening =",TimeToStr(bar4h,TIME_SECONDS), ", current open  " ,TimeToStr(iTime("EURUSD",PERIOD_H4,0),TIME_SECONDS)));
debPrint(StringConcatenate("Begin Start, previous bar60 opening =",TimeToStr(bar60,TIME_SECONDS), ", current open  " ,TimeToStr(iTime("EURUSD",PERIOD_H1,0),TIME_SECONDS)));
debPrint(StringConcatenate("Begin Start, previous bar15 opening =",TimeToStr(bar15,TIME_SECONDS), ", current open  " ,TimeToStr(iTime("EURUSD",PERIOD_M15,0),TIME_SECONDS)));
check=GetLastError();

if(check!=ERR_NO_ERROR) Print("*** #2 Error: ",ErrorDescription(check));

if (barDay == iTime("EURUSD",PERIOD_D1,0)) {
debPrint("in If barday");
   if (bar4h == iTime("EURUSD",PERIOD_H4,0)) {
debPrint("in If bar4h");
      if (bar60 == iTime("EURUSD",PERIOD_H1,0)) {
debPrint("in If bar60");
         if (bar15 == iTime("EURUSD",PERIOD_M15,0)) {
debPrint("in If bar15");
            // do new tick routine
         }  else {
debPrint("in else bar15");
            retValue = newbar15();
         }
      }  else {
debPrint("in else bar60");
            retValue = newbar15();
            retValue = newbar60();
      }
   }  else {
debPrint("in else bar4h");
            retValue = newbar15();
            retValue = newbar60();
            retValue = newbar4h();
   }
}  else {
debPrint(StringConcatenate("in else bard1, barDay = ",TimeToStr(barDay,TIME_SECONDS), ", iTime=",TimeToStr(iTime("EURUSD",PERIOD_D1,0),TIME_SECONDS)));
            retValue = newbar15();
            retValue = newbar60();
            retValue = newbar4h();
            retValue = newbard1();
}
check=GetLastError();
if(check!=ERR_NO_ERROR) Print("*** #3 Error: ",ErrorDescription(check));
debPrint("Leaving Start Func");
return(0);
}

int newbar15()
{
// EURUSD 15 minute
   bar15 = iTime("EURUSD",PERIOD_M15,0);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15a bar15, Error: ",ErrorDescription(check));
   barOpen = iOpen(pairs[0],PERIOD_M15,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15b barOpen, Error: ",ErrorDescription(check));
   barClose = iClose(pairs[0],PERIOD_M15,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15c barClose, Error: ",ErrorDescription(check));
   barHigh = iHigh(pairs[0],PERIOD_M15,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15d barHigh, Error: ",ErrorDescription(check));
   barLow = iLow(pairs[0],PERIOD_M15,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15e, barLow, Error: ",ErrorDescription(check));
   emailSubject = StringConcatenate(TimeToStr(TimeCurrent(),TIME_MINUTES), " " , StringSubstr(pairs[0],0,3), "/", StringSubstr(pairs[0],3,3), " 15 minute");
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15f, Error: ",ErrorDescription(check));
   emailContent = StringConcatenate("The last 15 minute candle opened at " ,TimeToStr(iTime(pairs[0],PERIOD_M15,1) - (7 * (PERIOD_H1 * 60)),TIME_MINUTES),"\r\n High: ", barOpen,"\r\n Low: ", barClose);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15g, Error: ",ErrorDescription(check));
   emailContent = StringConcatenate(emailContent,"\r\n Open: ", barOpen,"\r\n Close: ", barClose);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15h, Error: ",ErrorDescription(check));
   Print(StringConcatenate("In EURUSD 15 minute func, ",emailSubject,emailContent));
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #15i, Print, Error: ",ErrorDescription(check));
return(0);
}
int newbar60()
{
// USDJPY 1 hour
   bar60 = iTime("EURUSD",PERIOD_H1,0);
   barOpen = iOpen(pairs[1],PERIOD_H1,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #60aError: ",ErrorDescription(check));
   barClose = iClose(pairs[1],PERIOD_H1,1);
   barHigh = iHigh(pairs[1],PERIOD_H1,1);
   barLow = iLow(pairs[1],PERIOD_H1,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #60bError: ",ErrorDescription(check));
   emailSubject = StringConcatenate(TimeToStr(TimeCurrent(),TIME_MINUTES), " " , StringSubstr(pairs[1],0,3), "/", StringSubstr(pairs[1],3,3), " 1 hour");
   emailContent = StringConcatenate("The last 1 hour candle opened at " ,TimeToStr(iTime(pairs[1],PERIOD_H1,1) - (7 * (PERIOD_H1 * 60)),TIME_MINUTES),"\r\n High: ", barOpen,"\r\n Low: ", barClose);
   emailContent = StringConcatenate(emailContent,"\r\n Open: ", barOpen,"\r\n Close: ", barClose);
   Print(StringConcatenate("In USDJPY 1 hour func, ",emailSubject,emailContent));
return(0);
}
int newbar4h()
{
// USDCAD 4 hour
   bar4h = iTime("EURUSD",PERIOD_H4,0);
   barOpen = iOpen(pairs[2],PERIOD_H4,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #240aError: ",ErrorDescription(check));
   barClose = iClose(pairs[2],PERIOD_H4,1);
   barHigh = iHigh(pairs[2],PERIOD_H4,1);
   barLow = iLow(pairs[2],PERIOD_H4,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #240bError: ",ErrorDescription(check));
   emailSubject = StringConcatenate(TimeToStr(TimeCurrent(),TIME_MINUTES), " " , StringSubstr(pairs[2],0,3), "/", StringSubstr(pairs[2],3,3), " 4 hour");
   emailContent = StringConcatenate("The last 4 hour candle opened at " ,TimeToStr(iTime(pairs[2],PERIOD_H4,1) - (7 * (PERIOD_H1 * 60)),TIME_MINUTES),"\r\n High: ", barOpen,"\r\n Low: ", barClose);
   emailContent = StringConcatenate(emailContent,"\r\n Open: ", barOpen,"\r\n Close: ", barClose);
   Print(StringConcatenate("In USDCAD 4 hour func, ",emailSubject,emailContent));
return(0);
}
int newbard1()
{
// AUDUSD 1 day
   barDay = iTime("EURUSD",PERIOD_D1,0);
   barOpen = iOpen(pairs[3],PERIOD_D1,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #D1aError: ",ErrorDescription(check));
   barClose = iClose(pairs[3],PERIOD_D1,1);
   barHigh = iHigh(pairs[3],PERIOD_D1,1);
   barLow = iLow(pairs[3],PERIOD_D1,1);
   check=GetLastError();
   if(check!=ERR_NO_ERROR) Print("*** #D1bError: ",ErrorDescription(check));
   emailSubject = StringConcatenate(TimeToStr(TimeCurrent(),TIME_MINUTES), " " , StringSubstr(pairs[3],0,3), "/", StringSubstr(pairs[3],3,3), " daily");
   emailContent = StringConcatenate("The last daily candle opened at " ,TimeToStr(iTime(pairs[3],PERIOD_D1,1) - (7 * (PERIOD_H1 * 60)),TIME_MINUTES),"\r\n High: ", barHigh,"\r\n Low: ", barLow);
   emailContent = StringConcatenate(emailContent,"\r\n Open: ", barOpen,"\r\n Close: ", barClose);
   Print(StringConcatenate("In AUDUSD func, ",emailSubject,emailContent));
return(0);
}
void debPrint (string msg)
{
if (maxMessages < 50)
{
Print(StringConcatenate("Tic ", maxMessages, " ,", msg));
}
}
Keith Watford
Moderator
9597
Keith Watford 2014.04.11 23:21  
Nowhere in your code do I see ResetLastError(), so if you do get an error, everytime that you check GetLastError, it will still store the previous error
Frank Sarno
40
Frank Sarno 2014.04.12 15:08  

* Thanks for the info about ResetLastError. I now that I should use this

after every non-zero return for getLastError(). I can test this when the

market opens up.

* I now see that iTime needs history data stored as shown below.

\history\FXCM-USDReal05\AUDUSD60.hst

I think we are down to two questions.

- What is recommended the technique to update this for different time

frames and currency pairs in an EA attached to a single chart? The

function "ChartSymbolPeriod" looks promising.

- How do I make sure that all my currency pairs and time frames can

be access with iTime, iClose, etc. while my EA is running? For example,

should I use "RefreshRates"? It looks like RefreshRates can be used

to return True or false, and it can be used to Refresh predefined

variables. I can't believe that I need to loop through

each currency pair and time frame on every tick with the code:

if (RefreshRates() == false) { RefreshRates();}

Keith Watford
Moderator
9597
Keith Watford 2014.04.12 19:16  
RefreshRates won't update for symbols other than the chart symbol
/ /123
To add comments, please log in or register