Cannot preload bollingers data

 

Hi everybody. Could someone help me? I'd like to preload bollingers bands data in the OnInit function with 20 previous points. I'm trying with this scripts

counter=1;
points_counter=1;

for(counter; counter < 20; counter=counter+1)
{

handle1=iBands(NULL,PERIOD_M1,15,counter,1.4,PRICE_OPEN);
handle2=iMA(NULL,PERIOD_M1,15,counter,MODE_SMA,PRICE_MEDIAN);


ArraySetAsSeries(lower_band,true);
CopyBuffer(handle1,2,0,2,lower_band);
string str1=(lower_band[0]);

ArraySetAsSeries(upper_band,true);
CopyBuffer(handle1,1,0,2,upper_band);
string str3=(upper_band[0]);

ArraySetAsSeries(middle_band,true);
CopyBuffer(handle2,0,0,LOWER_BAND,middle_band);
string str2=(middle_band[0]);



boll15a = NormalizeDouble(str1,2);
boll15b = NormalizeDouble(str2,2);
boll15c = NormalizeDouble(str3,2);


pointsa[points_counter]=boll15a;
pointsb[points_counter]=boll15b;
pointsc[points_counter]=boll15c;

points_counter=(points_counter+1);



}


but the array is full of zeros and only from the 21 point the values begin to fill the array.

Bollinger Bands - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
Bollinger Bands (BB) are similar to Envelopes. The only difference is that the bands of Envelopes are plotted a fixed distance (%) away from the...
 
handle1=iBands(NULL,PERIOD_M1,15,counter,1.4,PRICE_OPEN);

ArraySetAsSeries(lower_band,true);
CopyBuffer(handle1,2,0,2,lower_band);
  1. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)


  2. Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 

Hi, thanks for the answer.


I tried the code within the OnCalculate, it worked in the live trading but it didn't work in the strategy tester. I found the problem is the program cannot load the data on the first try in the strategy tester, then I included the code within this one
int preload;

OnCalculate   //parametres
{
                  if(preload < 15)
                  {

                             preload=preload+1;
                        


                                // code that loads 20 bollingers data from the past
        }


/// rest of the code

}
                        
that way it tries 15 times and that is sufficient to catch the data stream.
 
danerstizz #:

Hi, thanks for the answer.


I tried the code within the OnCalculate but it didn't work either. I found the problem is the program cannot load the data on the first try, then I included the code within this one that way it tries 15 times and that is sufficient to catch the data stream.
Please read, understand and apply the answer you get.
 
Thanks to you, but I edited the answer because it wasn't precise. Happy to contribute.
 
danerstizz #:
Thanks to you, but I edited the answer because it wasn't precise. Happy to contribute.

Are you getting your handles in OnInit() ?

 
No, I didn't. Could be that the problem?
 
danerstizz #: No, I didn't. Could be that the problem?

Answered in #1.1

 
Ok, thanks.
Reason: