# Doing Different Things in "CalcBar()" Affects what is Treated as 1st Bar

Questions about MultiCharts .NET and user contributed studies. Jobauma
Posts: 104
Joined: 16 Apr 2013
Has thanked: 23 times
Been thanked: 4 times

### Doing Different Things in "CalcBar()" Affects what is Treated as 1st Bar

Hi.

I've discovered that doing different things in "CalcBar()" affects what is treated as 1st bar on a chart, or where the calculation of an indicator starts.

I have done some work on discovering what was the cause. For example, doing this...

Code: Select all

``` // Core Functions: m_CF_curHigh = Bars.High; m_CF_curLow = Bars.Low; m_CF_preHigh = Bars.High; m_CF_preLow = Bars.Low; m_CF_curClose = Bars.Close; ```
...somewhat affects this.

Or this:

Code: Select all

``` p_PlotObject.Reset(55); ```
I use IQFeed, and the greatest chart with minutes I use is M233 with an indicator 55 bars back.. I need 343 bars back on each chart, and this is enough with IQFeed.

Code: Select all

``` if ( Bars.FullSymbolData.Current > Bars.FullSymbolData.Count - ( 288 + 55 ) ) Counter++; ```
This should return "343", but it doesn't. This causes problems with greater charts.

The solution would be to force MultiCharts to start the calculation on the 1st bar every time. Can this be fixed in the next upcoming version of MultiCharts? Best regards, Jobauma
Posts: 104
Joined: 16 Apr 2013
Has thanked: 23 times
Been thanked: 4 times

### Re: Doing Different Things in "CalcBar()" Affects what is Treated as 1st Bar

If a moving average is, for example, "34" in length, it would calculate the moving average of bar "1", and the rest is calculated as "0" ( or the same as value of the 1st bar ).

The solution would be to add virtual bars (values), as "0", from actual 1st bar to where the calculation starts, as bars back of virtual bars. To prevent moving averages to bring virtual bars:

Code: Select all

``` if ( Length <= Bars.FullSymbolData.Current ) Length = Bars.FullSymbolData.Current; ```
This would also apply to, for example, TrendLine Objects. If the length is above 1st bar, the 1st point is applied to some virtual bar ( or the 1st bar ), outside the chart. To manually decide where the calculation starts with, for example, "89" bars back, and a moving average of length "34" ( to skip virtual bars ):

Code: Select all

``` public double Average( VariableSeries<double> Value, int Length ) { // SOME CODE } if ( Bars.FullSymbolData.Current > Bars.FullSymbolData.Count - 55 ) Average( Value, 34 ); ```
Here the calculation of the moving average is calculated after "34" bars, and the rest is "55" bars. Alternatively there could be a preference for this behaviour for virtual bars, in "Preferences...", or in the study it self; "[ForceCalculationFrom1stBar(true)]". Problem solved.  ~Zola~
Posts: 20
Joined: 28 Nov 2016
Has thanked: 5 times
Been thanked: 1 time

### Re: Doing Different Things in "CalcBar()" Affects what is Treated as 1st Bar

If a moving average is, for example, "34" in length, it would calculate the moving average of bar "1", and the rest is calculated as "0" ( or the same as value of the 1st bar ).

The solution would be to add virtual bars (values), as "0", from actual 1st bar to where the calculation starts, as bars back of virtual bars. To prevent moving averages to bring virtual bars:

Code: Select all

```if ( Length <= Bars.FullSymbolData.Current ) Length = Bars.FullSymbolData.Current; ```
This would also apply to, for example, TrendLine Objects. If the length is above 1st bar, the 1st point is applied to some virtual bar ( or the 1st bar ), outside the chart. To manually decide where the calculation starts with, for example, "89" bars back, and a moving average of length "34" ( to skip virtual bars ):

Code: Select all

```public double Average( VariableSeries<double> Value, int Length ) { // SOME CODE } if ( Bars.FullSymbolData.Current > Bars.FullSymbolData.Count - 55 ) Average( Value, 34 ); ```
Here the calculation of the moving average is calculated after "34" bars, and the rest is "55" bars. Alternatively there could be a preference for this behaviour for virtual bars, in "Preferences...", or in the study it self; "[ForceCalculationFrom1stBar(true)]". Problem solved. I noted your this issue previously, could I know how to use [ForceCalculationFrom1stBar(true)] in detail, i.e. the syntax.

Svetlana MultiCharts
Posts: 267
Joined: 19 Oct 2017
Has thanked: 1 time
Been thanked: 65 times

### Re: Doing Different Things in "CalcBar()" Affects what is Treated as 1st Bar

Hello, Jobauma,

By design, the calculation may start not from the first bar if the study refers to a certain number of previous bars for initial calculation. In this case MultiCharts steps that number of bars from the begging of the data series to let the study access the values of those historical bars. One should take it into account when coding a study.