Or reset your Time0 variable in OnInit()
Can use a switch for example
switch(Period())
{
case 0:// Current
break;
case 1:// M1
break;
case 5:// M5
break;
case 15:// M15
break;
case 30:// M30
break;
case 60:// H1
break;
case 240:// H4
break;
case 1440:// D1
break;
case 10080:// W1
break;
case 43200:// MN1
break;
}
Thanks guys, your help is greatly appreciated!! I absolutely didn't think about the same candle time....!
Marcus Riemenschneider:
Current bars for different time frames can have exactly the same time value. EAs are not resetting variable values the same way as indicators. Do the check the following way :Hi guys,
I wrote a small code to demonstrate my problem:
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if (Time0!=iTime(NULL, _Period, 0)) {
Time0=iTime(NULL, _Period, 0);
Alert(_Period);
}
}
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if (Time0!=iTime(NULL, _Period, 0)) {
Time0=iTime(NULL, _Period, 0);
Alert(_Period);
}
}
When I switch the chart period I expect the EA to pop up the alert window displaying the current chart period. Interestingly, sometimes it works and sometimes not. Here is the journal of my test:
I started with the daily and the alert was alright. Then I switched to H4 and the alert was also ok. But after switching to the H1 I waited for almost 2 minutes and there was no alert. The M30 period worked again but M15 and M5 didn't work. The weekly was ok again. Why doesn't the EA recognize all period changes?
Can somebody tell me what's wrong with the code? I can't see any mistake here...?!
void OnTick()
{
static int _prevPeriod=0;
if (Time0!=iTime(NULL, _Period, 0) || _prevPeriod!=_Period) {
Time0 =iTime(NULL, _Period, 0); _prevPeriod =_Period;
Alert(_Period);
}
}
{
static int _prevPeriod=0;
if (Time0!=iTime(NULL, _Period, 0) || _prevPeriod!=_Period) {
Time0 =iTime(NULL, _Period, 0); _prevPeriod =_Period;
Alert(_Period);
}
}
Problem solved! Thanks again, guys!! :)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi guys,
I wrote a small code to demonstrate my problem:
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if (Time0!=iTime(NULL, _Period, 0)) {
Time0=iTime(NULL, _Period, 0);
Alert(_Period);
}
}
When I switch the chart period I expect the EA to pop up the alert window displaying the current chart period. Interestingly, sometimes it works and sometimes not. Here is the journal of my test:
I started with the daily and the alert was alright. Then I switched to H4 and the alert was also ok. But after switching to the H1 I waited for almost 2 minutes and there was no alert. The M30 period worked again but M15 and M5 didn't work. The weekly was ok again. Why doesn't the EA recognize all period changes?
Can somebody tell me what's wrong with the code? I can't see any mistake here...?!