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.
StartCalc() & Multiple Chart Instruments [SOLVED]
Re: StartCalc() & Multiple Data Sources
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.
I am running the current MC.NET 64 version.
Thanks.
Re: StartCalc() & Multiple Data Sources
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:
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.
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.
Re: StartCalc() & Multiple Chart Instruments
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 857 times
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: StartCalc() & Multiple Chart Instruments
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.
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.