StartCalc() & Multiple Chart Instruments  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
FistPeso
Posts: 16
Joined: 01 Sep 2013
Has thanked: 3 times
Been thanked: 8 times

StartCalc() & Multiple Chart Instruments  [SOLVED]

Postby FistPeso » 02 Sep 2013

Greetings,

I am new to the Multicharts.NET API and after reading the documentation and debugging through some indicators I ran into a couple of puzzles that I am trying to solve.

On the Spread_Ratio indicator, BarsOfData(2).Close is assigned to the dataseries2 variable in the StartCalc() method. However, when I stepped through the code, Bars was instantiated and populated with data, however BarsOfData(2) was null. It was finally instantiated and populated when CalcBar() fired. It seems odd that the Bars would exist and not the remaining data series. This is also an issue when trying to feed another function with calculated results in the StartCalc() method.

When creating my own indicator, I noticed that the StartCalc() method fired twice, much to my surprise. I read somewhere else about MaxBarsBack and I did notice the first time it fires MaxBarsBack is one, the second time six. I am not sure what this is all about.

Any guidance would be appreciated.
Last edited by FistPeso on 04 Sep 2013, edited 1 time in total.

FistPeso
Posts: 16
Joined: 01 Sep 2013
Has thanked: 3 times
Been thanked: 8 times

Re: StartCalc() & Multiple Data Sources

Postby FistPeso » 03 Sep 2013

I have confirmed this with other indicators. In general, I am not able to access the secondary data series via the BarsOfData(2) object at the init stage unless I do it through the Dataloader . I have also not been able to determine why the StartCalc() method gets called twice. I turned off the Real-Time Tick option to confirm that it was not causing the second call.

I am running the current MC.NET 64 version.
Thanks.

FistPeso
Posts: 16
Joined: 01 Sep 2013
Has thanked: 3 times
Been thanked: 8 times

Re: StartCalc() & Multiple Data Sources

Postby FistPeso » 04 Sep 2013

I have found the solution to these questions and I am leaving it here to help others who are getting started with the API.

If you are using multiple instruments on a chart, and you intend to do custom work with that bar data, you would be wise to create reference variables to all your instrument bars as the first statements of the StartCalc() method, such as the following:

Code: Select all

protected override void StartCalc()
{
var instrument1 = Bars;
var instrument2 = BarsOfData(2);
var instrument3 = BarsOfData(3);

//Remainder of your code...
}


MultiCharts only initializes Bars (Instrument #1) by default, BarsOfData(x) will only get initialized after it is encountered in the code and trips an application exception. Once the application exception is thrown, it starts the StartCalc() operation all over again. It will do this process for each instrument. So if you are referencing two instrument bars, you will run the StartCalc() process twice. If you are referencing three instrument bars, you will run the StartCalc() process three times, etc... By placing the instrument references at the beginning of the StartCalc() method you will get this init process out of the way and you will not find your variables and classes in unexpected states. As a side note, if the first time MultiCharts runs into your BarsOfData(x) reference is in CalcBar(), it will do the same thing and restart StartCalc(). This could be very bad if you have objects or variables already initialized from the StartCalc() process. So keep your instrument references at the beginning of StartCalc() and you will save yourself much grief.

FistPeso
Posts: 16
Joined: 01 Sep 2013
Has thanked: 3 times
Been thanked: 8 times

Re: StartCalc() & Multiple Chart Instruments

Postby FistPeso » 05 Sep 2013

I have created an Instruments function that manages this init process for you. As you add/remove instruments to the chart, the indicator automatically adjusts to the number of instruments and automatically inits them for you.

Code: Select all

protected override void StartCalc()
{
var instruments = new Instruments(this);

var series1 = instruments[0]; //Instrument #1 Bars
var series2 = instruments[1]; //Instrument #2 BarsOfData(2)
}
Attachments
Instruments.pln
(941 Bytes) Downloaded 838 times

gztanwei
Posts: 32
Joined: 15 Aug 2015
Has thanked: 6 times
Been thanked: 5 times

Re: StartCalc() & Multiple Chart Instruments

Postby gztanwei » 15 Sep 2015

Hi just to report that I am using 9.1 beta, and the attached function can not compile when it is imported.

Thanks.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: StartCalc() & Multiple Chart Instruments

Postby Henry MultiСharts » 24 Sep 2015

Hello gztanwei,

There is no issue with the study compilation. Just change the "namespace PowerLanguage" to "namespace PowerLanguage.Function" to get the study properly identified by the editor.

et1hugo
Posts: 9
Joined: 25 Oct 2017
Has thanked: 5 times
Been thanked: 1 time

Re: StartCalc() & Multiple Chart Instruments

Postby et1hugo » 17 Aug 2019

Hi FistPeco

Thanks for the post, was just sitting here with the same issue trying to determine how many instruments are connected to my strategy.


Return to “MultiCharts .NET”