MT4, please help the community. 1. In the Expert Advisor, working (by ticks or on a timer 0.5 sec) on the M1 chart, you need to make calculations at the moment of a new candle on the H1 chart. We can catch the start of the M1 candle, following the last M1 candle of each hour (NN:00 minutes), but it is not very nice ... Besides, the H1 candlestick may not appear at that moment. Maybe somebody did it more elegantly ? 2. How much longer it takes to get the indicator values through iCustom(), compared to the indicator code in the Expert Advisor text? 3. if the code of the indicator in the text of the Expert Advisor is much faster to be calculated, how in the Expert Advisor attached to the M1 chart, to calculate the indicator value on the H1 chart ?
MT4, I am asking for help from the community. 1. In the Expert Advisor, working (by ticks or by timer 0.5 sec) on the M1 chart, you need to make calculations at the moment when a new candle appears on the H1 chart. We can catch the start of the M1 candle, following the last M1 candle of each hour (NN:00 minutes), but it is not very nice ... Besides, the H1 candlestick may not appear at that moment. Maybe somebody did it more elegantly ? 2. How much longer to get the indicator values through iCustom(), compared to the indicator code in the Expert Advisor text? 3. if the code of the indicator in the text of the Expert Advisor is much faster to be calculated, how in the Expert Advisor attached to the M1 chart, to calculate the indicator value on the H1 chart ?
The answer to the first question, the function defines a new bar.
Sergey Gritsay: This is the first time I've heard about them, I wonder what's so scary about them, I rarely use them too, but they are better suited for certain tasks.
The matter is that a static variable can be used only if NevDay() is called for the same symbol and timeframe during the entire runtime of the EA. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(
MikeZv: The point is that static variable can only be used if NevDay() is called for the same symbol and timeframe. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(
If you need it for multicurrency mode, here is an example, it works in MT4 and MT5
MikeZv: The point is that the static variable can only be used if NevDay() is called for the same symbol and timeframe. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(
Here is another option using the class
//+------------------------------------------------------------------+//| тест.mq4 |//| Copyright 2016, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2016, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.00"//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+class CNevBar
{
private:
datetime curbar;
datetime lastbar;
public:
CNevBar();
~CNevBar();
bool new_bar(string symbol,ENUM_TIMEFRAMES period);
};
//+------------------------------------------------------------------+void CNevBar::CNevBar()
{
}
//+------------------------------------------------------------------+void CNevBar::~CNevBar(void)
{
}
//+------------------------------------------------------------------+bool CNevBar:: new_bar(string symbol,ENUM_TIMEFRAMES period)
{
curbar=(datetime) SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);
if(lastbar==0)lastbar=(datetime)SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);
if(lastbar!=curbar)
{
lastbar=curbar;
return(true);
}
return(false);
}
CNevBar *eurusd_H1; // объявим экземпляр класса для симмвола EURUSD
CNevBar *eurusd_M15; // объявим экземпляр класса для симмвола EURUSD
CNevBar *audusd_M30; // объявим экземпляр класса для симмвола AUDUSD
CNevBar *audusd_M5; // объявим экземпляр класса для симмвола AUDUSD//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//---
eurusd_H1=new CNevBar();// создадим экземпляр класса для симмвола EURUSD
audusd_M30=new CNevBar();// создадим экземпляр класса для симмвола AUDUSD
eurusd_M15=new CNevBar();// создадим экземпляр класса для симмвола EURUSD
audusd_M5=new CNevBar();// создадим экземпляр класса для симмвола AUDUSD//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//---delete eurusd_H1;// удалим экземпляр класса для симмвола EURUSDdelete audusd_M30;// удалим экземпляр класса для симмвола AUDUSDdelete eurusd_M15;// удалим экземпляр класса для симмвола EURUSDdelete audusd_M5;// удалим экземпляр класса для симмвола AUDUSD
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
if(eurusd_H1.new_bar("EURUSD",PERIOD_H1))
{
Alert("EURUSD"," Новый бар на Н1");
}
if(eurusd_M15.new_bar("EURUSD",PERIOD_M15))
{
Alert("EURUSD"," Новый бар на M15");
}
if(audusd_M30.new_bar("AUDUSD",PERIOD_M30))
{
Alert("AUDUSD"," Новый бар на M30");
}
if(audusd_M5.new_bar("AUDUSD",PERIOD_M5))
{
Alert("AUDUSD"," Новый бар на M5");
}
//---
}
//+------------------------------------------------------------------+//+------------------------------------------------------------------+
Test result
2016.02.2021:09:432015.01.1000:00 тест3 test started
2016.02.2021:09:452015.01.1200:00 тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.2021:09:472015.01.1200:00 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M30
2016.02.2021:09:502015.01.1200:00 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:09:532015.01.1200:05 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:09:592015.01.1200:10 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:042015.01.1200:15 тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.2021:10:042015.01.1200:15 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:102015.01.1200:20 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:132015.01.1200:25 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:162015.01.1200:30 тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.2021:10:162015.01.1200:30 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M30
2016.02.2021:10:162015.01.1200:30 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:232015.01.1200:35 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:262015.01.1200:40 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:292015.01.1200:45 тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.2021:10:292015.01.1200:45 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:342015.01.1200:50 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:362015.01.1200:55 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:392015.01.1201:00 тест3 EURUSD,M1: Alert: EURUSD Новый бар на Н12016.02.2021:10:392015.01.1201:00 тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.2021:10:392015.01.1201:00 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M30
2016.02.2021:10:392015.01.1201:00 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:462015.01.1201:05 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:502015.01.1201:10 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:552015.01.1201:15 тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.2021:10:552015.01.1201:15 тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.2021:10:572015.01.1201:17 Tester: stop button pressed
MT4, please help the community.
1. In the Expert Advisor, working (by ticks or on a timer 0.5 sec) on the M1 chart, you need to make calculations at the moment of a new candle on the H1 chart.
We can catch the start of the M1 candle, following the last M1 candle of each hour (NN:00 minutes), but it is not very nice ... Besides, the H1 candlestick may not appear at that moment.
Maybe somebody did it more elegantly ?
2. How much longer it takes to get the indicator values through iCustom(), compared to the indicator code in the Expert Advisor text?
3. if the code of the indicator in the text of the Expert Advisor is much faster to be calculated, how in the Expert Advisor attached to the M1 chart, to calculate the indicator value on the H1 chart ?
MT4, I am asking for help from the community.
1. In the Expert Advisor, working (by ticks or by timer 0.5 sec) on the M1 chart, you need to make calculations at the moment when a new candle appears on the H1 chart.
We can catch the start of the M1 candle, following the last M1 candle of each hour (NN:00 minutes), but it is not very nice ... Besides, the H1 candlestick may not appear at that moment.
Maybe somebody did it more elegantly ?
2. How much longer to get the indicator values through iCustom(), compared to the indicator code in the Expert Advisor text?
3. if the code of the indicator in the text of the Expert Advisor is much faster to be calculated, how in the Expert Advisor attached to the M1 chart, to calculate the indicator value on the H1 chart ?
The answer to the first question, the function defines a new bar.
example of use
int OnInit() { //--- NevBar(PERIOD_H1); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(NevBar(PERIOD_H1)) { производим вычисления. } } //+------------------------------------------------------------------+The answer to the first question, the function defines a new bar.
Thanks a lot Sergey, I've looked through the documentation, but somehow I missed iTime() ... :
And whycall NevBar() inOnInit() ?
The first function call, otherwise it will be triggered immediately. To make surethat OnInit() is not called
The first function call, otherwise it will be triggered immediately. To make surethat OnInit() is not called
Thank you Sergei ! On the forum, I was scared stiff: "Don't use static variables ! " :)
Thank you Sergei ! On the forum, I was scared stiff: "Don't use static variables ! " :)
This is the first time I've heard about them, I wonder what's so scary about them, I rarely use them too, but they are better suited for certain tasks.
The point is that static variable can only be used if NevDay() is called for the same symbol and timeframe. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(
If you need it for multicurrency mode, here is an example, it works in MT4 and MT5
The point is that the static variable can only be used if NevDay() is called for the same symbol and timeframe. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(
Here is another option using the class
Test result