starting with indicators

 

Hey, I have some calculations to do on a pre-determined number of M1 bars. (120, 240, 360 or 480 or... based on input)
and I want to write my first indicator ever.

from the new indicator wizard, I have created the source file.
added my codes and functions to calculate my variables.
but there's something wrong probably with filling the buffers.
when I run the indicator, terminal slows down, So it seems I'm repeating the calculations more than once per bar (?)
I have read the docs, and indicator codes examples, but I don't understand how to deal with the prev_calculated==0 part.

please note these :

  • what the indicator is calculating, is always from the recent N number of M1 bars. no matter what TF it's dropped on. (is it possible for indicators?)
  • the data collected from the M1 bars are Open, High, Low prices.
  • number of required M1 bars is always constant (e.g. M1_N_input=240, data from bar #1, back to bar #240 is considered)
  • the intra-M1 ticks should not affect the indicator calculations. (and should not initiate new series of calculations. since only new M1 bars should initiate a new buffer value calculation)

I have 2 buffers, both Line, and plotted on chart window, please review my code:

input int NMB= 120;                     // number of M1 bars

double R_band[];
double B_band[];

// some global double arrays and other stuff here...

int OnInit()
{
        SetIndexBuffer(0,R_band,INDICATOR_DATA);
        SetIndexBuffer(1,B_band,INDICATOR_DATA);
        return(INIT_SUCCEEDED);
}

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[])
{
        int cbar;
        if(prev_calculated==0) cbar = 0;
        else cbar = prev_calculated-1;
        for(int i=cbar; i<rates_total && !_StopFlag; i++) update_buffers(i);
        
        return(rates_total);
}

//------------------------------------------------------------------
void update_buffers(int begin)
{
        int copy= CopyOpen(_Symbol, PERIOD_M1, begin, NMB, Os);
        copy += CopyHigh(_Symbol, PERIOD_M1, begin, NMB, Hs);
        copy +=  CopyLow(_Symbol, PERIOD_M1, begin, NMB, Ls);
        if(copy!=NMB*3) return; 
        
        // some calculations on the arrays Os, Hs, Ls ...

        R_band[begin] = (r_High-r_Low)/2.0;
        B_band[begin] = (b_High-b_Low)/2.0;
} // function end
 

Your code is not compilable as there are undeclared identifiers.

You can use Profiling to check which lines are slowing down your program.


 
I know it's not compiling, because when you make sources through the Wizard, it generates that mqproj file which contains many settings (buffers numbers and stuff).
so the source file code isn't like a non project file.

I just need to know :
  1. when filling buffers that are gonna be plotted as lines, we fill them from i=0 , i++  or from i=something, i-- ?
  2. when only one new bar is formed since last OnCalculate() call, value of prev_calculated is 1 more than total_rates ?
  3. should my indicator be dropped on M1 timeframe only ? (based on the specifications I described above)
  4. when prev_calculated==0, how differently I fill my buffers ?