Variables outside start() event

 

Say I want to count the number of times a price is above the upper Bollinger band. I would want to do this on the basis of the current period, but the test will be made every tick. So am I right in thinking that the only two ways to do this are:

1. Loop back as many periods as necessary within the start() procedure; or

2. Check at the start of the start() procedure that enough time has passed for us now to be on a different bar of the chart and have a variable outside the start() procedure where I keep count?

Both seem a little awkward. I suppose the former is better but it's rather duplicative because the same things have to be calculated in successive loops of the start() procedure which seems inefficient.


Any advice?

 
Arbu:

Say I want to count the number of times a price is above the upper Bollinger band. I would want to do this on the basis of the current period, but the test will be made every tick. So am I right in thinking that the only two ways to do this are:

1. Loop back as many periods as necessary within the start() procedure; or

2. Check at the start of the start() procedure that enough time has passed for us now to be on a different bar of the chart and have a variable outside the start() procedure where I keep count?

Both seem a little awkward. I suppose the former is better but it's rather duplicative because the same things have to be calculated in successive loops of the start() procedure which seems inefficient.


Any advice?

I am not quite sure what you are trying to do but either a global variable (outside the start procedure) or a static variable within the start procedure can certainly hold its value between ticks. If you want to count the number of ticks of the current bar that are outside the boly band then that is a simple counter incrementing either a static or global variable. If you want to look back to previous bars it is not possible to retrospectically calculate the number of ticks outside the boly band because the system does not store tick data. However, if you create your static/global variable as an array then you can fill it with data as you go, bar by bar.

As for finding the new bar, just check when Time[0], the time of the start of the current bar, changes.

 
1. Loop back as many periods as necessary within the start() procedure; or

That comment makes no sense. Start is called once per tick

2. Check at the start of the start() procedure that enough time has passed for us now to be on a different bar of the chart and have a variable outside the start() procedure where I keep count?

int     start(){
    static datetime Time0;  bool newBar  = Time0 != Time[0];
    if (!newBar){ count++; return(0); }    Time0 = Time[0];
Keep the count as a global or static.
 
OK, thanks. I wasn't aware of the Time[] array.
Reason: