Marina,

I've a problem when calculating a formula (let's say a EMA) which depends on the BarNumber and two dataN series on the chart.

Take a chart with two data series with MaxBarsBack = 5 for the same symbol with different time frames (10 minutes for data1 and 50 minutes for data2, for example). Make the following indicator and bound it to data2:

print ("Indicator: ", date, " ", time, " BarNumber: ", BarNumber);

plot1 (1);

Make the following signal:

print ("Strategy: ", date, " ", time, " BarNumber: ", BarNumber of data2);

You can see that the BarNumber = 1 for the indicator has not the same date/time as the strategy. When you calculate a formula like EMA, you need -of course- that the BarNumbers are in sync otherwise you have different results between the indicator and the strategy.

Maybe I overlooked something, but I don't think so. Can you confirm this behaviour and do you have a work around?

Thanks,

Rob.

# BarNumber out of sync with dataN series on chart

- Marina Pashkova
**Posts:**2758**Joined:**27 Jul 2007

- Marina Pashkova
**Posts:**2758**Joined:**27 Jul 2007

The first and second data series starting on exactly the same start date (start of a session): because the data1 series has another timeframe (eg. 10 minutes), the first (visual) bar of this timeframe is -of course- earlier after the session start than the first bar of the data2 series (eg. 50 minutes).

Because the strategy is running in the timeframe of data1 (10 min.) I'm using the "XAverage (..) of data2" (50 min.) to get the same result as the indicator. But there it goes wrong because BarNumber = 1 for data2 has a different date/time in the strategy which uses "... of data2" than the indicator which uses directly the data2 series. And the XAverage (EMA) function which uses CurrentBar = 1 calculates in this case a different result:

{ Exponential average }

inputs:

Price( numericseries ),

Length( numericsimple ) ; { this input assumed to be a constant >= 1 }

variables:

SmoothingFactor( 2 / ( Length + 1 ) ) ;

if CurrentBar = 1 then

XAverage = Price

else

XAverage = XAverage[1] + SmoothingFactor * ( Price - XAverage[1] ) ;

With regards,

Rob.

- Marina Pashkova
**Posts:**2758**Joined:**27 Jul 2007

Dear Rob,

If I understand the description correctly the reasons why you have different bars as the starting point for the indicator and the strategy are as follows:

The indicator references one data series only while the strategy uses both data series for calculations. This alone is enough to cause the differences because there will be multiple factors influencing the choice of the starting point for the strategy. Also, 5 bars (which is what the strategy references according to your first post) might not be enough for its calculation. In such a case the strategy will take whatever number of bars it needs for calculations. This might be another source of differences.

This is just a very general explanation. If you would like to get further details please let me know.

If I understand the description correctly the reasons why you have different bars as the starting point for the indicator and the strategy are as follows:

The indicator references one data series only while the strategy uses both data series for calculations. This alone is enough to cause the differences because there will be multiple factors influencing the choice of the starting point for the strategy. Also, 5 bars (which is what the strategy references according to your first post) might not be enough for its calculation. In such a case the strategy will take whatever number of bars it needs for calculations. This might be another source of differences.

This is just a very general explanation. If you would like to get further details please let me know.

Let me explain my problem in more detail.

I have a chart with two data series in different timeframes. I plot a XAverage via an indicator on top of this chart, based on data2 (50 min.). Now I want to use this XAverage value of data2 in my strategy (signal) which is bound (automatically) to data1. I thought I could do this with the statement XAverage(..) of data2 in my strategy code but then I get a (slightly) different result than my (visual) indicator on the same time/date bar because the XAverage calculation does not start (BarNumber = 1) on the same bar.

With regards,

Rob.

- Marina Pashkova
**Posts:**2758**Joined:**27 Jul 2007

Dear Rob,

In the scenario above you still need to make sure that both the signal and the strategy start their calculations on the same bar.

As you pointed out above, with the same start time (which is the session open time) the first bar on the 10 min. chart will have a different timestamp compared to the first bar on the 50 min. chart. Which is only natural because the timeframe is different.

However, this difference is crucial for the work of strategies and indicators - when you set the number of bars back the study will reference. Because timewise the 1st bar on the 50 min. chart will correspond to the 5th bar on the 10 min chart. And using XAverage does not change the fact that for synchronization you must adjust the values in the number of bars reference in such a way that they begin on exactly the same bar. Even if you feed data calculated on data2 the strategy is still tied to data1 (even if it uses those other values).

If the above explanation is not clear enough please let me know.

In the scenario above you still need to make sure that both the signal and the strategy start their calculations on the same bar.

As you pointed out above, with the same start time (which is the session open time) the first bar on the 10 min. chart will have a different timestamp compared to the first bar on the 50 min. chart. Which is only natural because the timeframe is different.

However, this difference is crucial for the work of strategies and indicators - when you set the number of bars back the study will reference. Because timewise the 1st bar on the 50 min. chart will correspond to the 5th bar on the 10 min chart. And using XAverage does not change the fact that for synchronization you must adjust the values in the number of bars reference in such a way that they begin on exactly the same bar. Even if you feed data calculated on data2 the strategy is still tied to data1 (even if it uses those other values).

If the above explanation is not clear enough please let me know.