BarNumber out of sync with dataN series on chart

Questions about MultiCharts and user contributed studies.
RS
Posts: 39
Joined: 28 Sep 2007

BarNumber out of sync with dataN series on chart

Postby RS » 29 Oct 2007

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.

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 31 Oct 2007

Hi Rob,

We need to test the code and as soon as I have the results I will post a reply.

Regards.

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 01 Nov 2007

Hi Rob,

Which bar does the signal begin to be calculated on? You need to make sure that both indicator and signal are calculated starting with the first bar. Because you might not be looking at the same bar in the strategy and in the indicator.

Regards.

RS
Posts: 39
Joined: 28 Sep 2007

Postby RS » 01 Nov 2007

Marina,

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.

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 02 Nov 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.

RS
Posts: 39
Joined: 28 Sep 2007

Postby RS » 02 Nov 2007

Marina,

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.

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 08 Nov 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.


Return to “MultiCharts”