What the function is going on?

Questions about MultiCharts .NET and user contributed studies.
MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

What the function is going on?

Postby MidKnight » 16 Dec 2012

Hi guys,

I don't even know what to title this problem as I have zero idea what is going on. So extremely frustrating these built-in functions are - there is more behind the scenes stuff going on here and I have no idea what. For whatever reason, it is reloading the bars several times and recalculating the indicator OR it is resetting Bars.CurrentBar property.

Code: Select all

private ISeries<Boolean> m_popCondition;
private CountIf m_popCount;

protected override void Create()
{
m_popCount = new CountIf(this);

plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));
}

protected override void StartCalc()
{
m_popCondition =
new Lambda<Boolean>(
delegate(int _idx)
{ return ((Bars.Open[_idx] >= Bars.Low[1 + _idx]) && (Bars.Open[_idx] <= Bars.High[1 + _idx])); });
m_popCount.test = m_popCondition;
m_popCount.length = new Lambda<Int32>(delegate { return Bars.CurrentBar; });
}

protected override void CalcBar()
{
Output.WriteLine("{0}", Bars.CurrentBar);

plot1.Set(m_popCount[0]);
}
If I comment out the call to m_popCount[0] within CalcBar(), the Output.WriteLine() will output the bar numbers correctly. However, once m_popCount[0] is called, it will output all sorts of nonsense from Bars.CurrentBar. Here as an example of part of what it outputs:

Code: Select all

1
1
2
3
4
5
6
7
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
I've spent hours trying to figure this out already - any clues would be greatly appreciated.

Tediously tired and confused,
MK

rjelles
Posts: 36
Joined: 04 Feb 2010
Location: Calgary, AB Canada
Has thanked: 7 times
Been thanked: 19 times
Contact:

Re: What the function is going on?

Postby rjelles » 16 Dec 2012

Can you confirm the setting "Maximum number of bars study will reference" for your study? Try setting to 'User Specified' if it is set to "Auto-detect" - this will avoid the multiple calc scans that takes place on loading your indicator to determine the number of bars being referenced.

Hope this helps.

Jeff

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: What the function is going on?

Postby MidKnight » 17 Dec 2012

Hi Jeff,

Thanks for the reply and the suggestion. The chart I'm testing has 200 bars on it. When I set to user specified and try to alter these settings, it still gives erroneous results. It still performs the multiple passes over the chart and just like it did with max number of bars set to auto-detect, the Bars.CurrentBar output does not match the bar number shown in the data window. For example, the last completed bar on the chart is a bar number of 199 (real-time is disabled), but the code above doesn't output anything close to that from using Bars.CurrentBar.

:(
MK

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: What the function is going on?

Postby MidKnight » 17 Dec 2012

Support please.

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

Re: What the function is going on?

Postby Henry MultiСharts » 18 Dec 2012

Hello MidKnight,

Your code works as expected. In your code you are constantly increasing the Length parameter of CountIf function:

Code: Select all

CountIf (m_popCount.length = new Lambda<Int32>(delegate { return Bars.CurrentBar; });)
This forces the function to reference further and further historical data with every calculation. On a certain bar the MaxBarsBack you have selected is not enough because length >= MaxBarsBack. MultiCharts increases the MaxBarsBack automatically and recalculates the study.
In order to avoid that you need to make the Length value constant.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: What the function is going on?

Postby MidKnight » 18 Dec 2012

Thanks for the reply Henry, even though I didn't really understand it. If there are 100 bars on the chart, the last bars number is 100 as output by Bars.CurrentBar. With my posted code, the last bar will output something much lower than 100 when Bars.CurrentBar is output. But if you say this is "correct" - OK.

Because there is no documentation in the HELP file on the CountIf class I have had to try and figure out how to use it. From my investigation, it appears that the length property is how many bars back it will apply your count on - is that correct? I say this, because I had set the length to 2 at one stage but never ever got a count greater than 2. I want the test condition to be applied and counted across ALL the bars on the chart - that is why I set the length to Bars.CurrentBar.

Maybe you can offer a suggestion for how I would go about applying my test condition and perform a count across the entire chart data.

With thanks,
MK

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

Re: What the function is going on?

Postby Henry MultiСharts » 19 Dec 2012

If there are 100 bars on the chart, the last bars number is 100 as output by Bars.CurrentBar. With my posted code, the last bar will output something much lower than 100 when Bars.CurrentBar is output.
Because MaxBarsBack value is substracted from this output value.
The CurrentBar reserved word used in an indicator or signal starts counting after the MaxBarsBack amount of bars have passed. In other words, if you have the MaxBarsBack set to 20, the indicator starts calculating on chart's bar number 21, but this will be the first bar for the indicator (so CurrentBar = 1).
Because there is no documentation in the HELP file on the CountIf class I have had to try and figure out how to use it.
The CountIF function counts the number of true custom test condition occurrences over some number of bars. This function is inherited from PowerLanguage.
From my investigation, it appears that the length property is how many bars back it will apply your count on - is that correct?
That is correct. Length sets the number of bars to consider for testing the condition.
Maybe you can offer a suggestion for how I would go about applying my test condition and perform a count across the entire chart data.
You can create your own CountIF function that will do the calculation on all bars disregard MaxBarsBack. You need to use IInstrument.FullSymbolData for that.


Return to “MultiCharts .NET”