SeriesInfoInteger returns 0

 

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)

 
After all these years, we still have to ask for code, seriously ?
 
Alain Verleyen:
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);
  }
//+------------------------------------------------------------------+
 
chinaski: Code for SeriesInfoInteger call. OK.

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++.
  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. 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:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. 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


 
Fernando Carreiro:

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

 
chinaski:Same problem. In my case [DAX30] weekly, Admiral Markets MT5 v2885
  1. Remove it or comment out the one in "OnInit".
  2. Show your log file output!
  3. Show that the DAX30 is in the "Market Watch" window (screenshot)
  4. 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);


 
Fernando Carreiro:
  1. Remove it or comment out the one in "OnInit".
  2. Show your log file output!
  3. Show that the DAX30 is in the "Market Watch" window (screenshot)
  4. 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.

Files:
20210426.log  3 kb
 
chinaski:

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.

Please read my second edit and use "long" instead of "int" and adjust your "PrintFormat" to output a long as well!
 
chinaski:

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.

Thanks for the Journal log, but I actually meant the Experts log with your "Print" output!
 

I did, long instead of int. Same problem. PrintFormat accepts %d for long also, according help. checked also %i %x.

Still returns 0.

Reason: