After all these years, we still have to ask for code, seriously ?
Code for SeriesInfoInteger call. OK.
#property version "1.00" #property indicator_chart_window //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { ResetLastError(); int bars=SeriesInfoInteger(NULL,PERIOD_CURRENT,SERIES_BARS_COUNT); PrintFormat("bars=%d code=%d",GetLastError()); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
Calling "SeriesInfoInteger" in the OnInit handler is unreliable because the Chart and Symbol may not yet be up and ready at that time. Only in the OnCalculate event handling can it be relied on!
Please read the following post (on another thread) by @William Roeder for more insight:
Forum on trading, automated trading systems and testing trading strategies
setting SL to be the lowest price based on the numbers of bars strategy
William Roeder, 2021.04.24 15:58
int highestPriceShift = iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,5,0); int lowestPriceShift = iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,5,0); // double highestPrice = iHigh(Symbol(),PERIOD_CURRENT,highestPriceShift); double lowestPrice = iLow(Symbol(),PERIOD_CURRENT,lowestPriceShift);Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
-
They are initialized once on program load.
-
They don't update unless you assign to them.
-
In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).
MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and
Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
external static variable - MQL4 programming forum #2 2013.02.10
Calling "SeriesInfoInteger" in the OnInit handler is unreliable because the Chart and Symbol may not yet be up and ready at that time. Only in the OnCalculate event handling can it be relied on!
Please read the following post (on another thread) by @William Roeder for more insight:
#property version "1.00" #property indicator_chart_window //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { ResetLastError(); int bars=SeriesInfoInteger(NULL,PERIOD_CURRENT,SERIES_BARS_COUNT); PrintFormat("bars=%d code=%d",GetLastError()); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- ResetLastError(); int bars=SeriesInfoInteger(NULL,PERIOD_CURRENT,SERIES_BARS_COUNT); PrintFormat("bars=%d code=%d",GetLastError()); return(rates_total); }
Same problem. In my case [DAX30] weekly, Admiral Markets MT5 v2885
#property version "1.00" #property indicator_chart_window //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void get_data(void) { ResetLastError(); int bars=SeriesInfoInteger(NULL,PERIOD_CURRENT,SERIES_BARS_COUNT); PrintFormat("bars=%d code=%d",GetLastError()); ResetLastError(); MqlRates rates_array[]; int count_copied= CopyRates( NULL,PERIOD_CURRENT,0,100,rates_array); PrintFormat("rates copied=%d code=%d",count_copied,GetLastError()); } int OnInit() { get_data(); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- get_data(); return(rates_total); } //+------------------------------------------------------------------+
Using this code, i see in console: bars=0 code=0 rates copied=100 code=0
- Remove it or comment out the one in "OnInit".
- Show your log file output!
- Show that the DAX30 is in the "Market Watch" window (screenshot)
- Open a Chart on DAX30 Weekly to make sure it has data (screenshot)
If you don't make it easy for us to help you, we will just give up. So give us and show as as much detail as possible.
EDIT: One more thing, instead of "NULL", please use "_Symbol" instead!
EDIT2: use "long" not "int". "SERIES_BARS_COUNT" returns a "long" value!
long bars=SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_BARS_COUNT);
- Remove it or comment out the one in "OnInit".
- Show your log file output!
- Show that the DAX30 is in the "Market Watch" window (screenshot)
- Open a Chart on DAX30 Weekly to make sure it has data (screenshot)
If you don't make it easy for us to help you, we will just give up. So give us and show as as much detail as possible.
EDIT: One more thing, instead of "NULL", please use "_Symbol" instead!
Same with _Symbol instead of NULL. On Init call outcommented. Same problem.
The chart is opened (because indicator above attached) and has ticks.
Log attached.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
i have the problem that
SeriesInfoInteger
returns 0 in my indicator.
When i switch timeframe to another and back, it returns a valid value.
Have no idea how to fix this. Even when i repeatetly call
SeriesInfoInteger
in a timer, it returns 0, until i toggle timeframe.
Even SeriesInfoInteger returns 0, CopyRates works and returns # of bars > 0.
What is the reason for that or how to fix it.
Thank you
(MT5 2885)