Portfolio Trader: MaxBarsBack not working?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Portfolio Trader: MaxBarsBack not working?

Postby dnickless » 06 Jun 2016

Hi there

In MC.NET Portfolio Trader, there's something odd happening on my end. ;) Either I misinterpret something here or the MaxBarsBack feature is broken...?

What I observe is that I have an XAverage Function referenced by my trading signal like that:

Code: Select all

private XAverage xAverageLong;

protected override void Create() {
xAverageLong = new XAverage(this, 1);
}

protected override void StartCalc() {
xAverageLong.Price = Bars.Close;
xAverageLong.Length = 80;
}
MaxBarsBack is set to 150. My expectation would be that by the time the CalcBar events kick in, I should have the xAverage value calculated already based on the first 150 bars? What I see, though, is that upon the first CalcBar call, the xAverage starts with the Close value of the current bar and only from the 80th bar onwards the xAverage is really what it should be...

Does that make any sense to anyone?

Thanks


Daniel

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

Re: Portfolio Trader: MaxBarsBack not working?

Postby Henry MultiСharts » 07 Jun 2016

Hello Daniel,

Please check the source code of the XAverage function to make sure it is the one you need.

dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Re: Portfolio Trader: MaxBarsBack not working?

Postby dnickless » 07 Jun 2016

It is! ;) The issue appears to be that the system does not precalculate the xAverage before the trading starts (first call to CalcBar)...

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

Re: Portfolio Trader: MaxBarsBack not working?

Postby Henry MultiСharts » 07 Jun 2016

It is! ;) The issue appears to be that the system does not precalculate the xAverage before the trading starts (first call to CalcBar)...
dnickless, that is expected behavior.

dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Re: Portfolio Trader: MaxBarsBack not working?

Postby dnickless » 07 Jun 2016

Ouch, I was afraid I would get that answer. So what is the recommended way not to trade as long as the indicators are not calibrated properly? Just an if statement in combination with a counter in the CalcBar method?

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Portfolio Trader: MaxBarsBack not working?  [SOLVED]

Postby ABC » 08 Jun 2016

dnickless,

this should only be an "issue" with indicators like an exponential average, where the length is not connected to a number of bars (like it is in the simple average for example).

Two solutions come to mind, either use the one you proposed and have the code wait an additional number of bars or code your own XAverage and loop over the preceding bars once on Currentbar = 1 (basically all bars that fall inside the max bars back range).

Regards,

ABC

dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Re: Portfolio Trader: MaxBarsBack not working?

Postby dnickless » 09 Jun 2016

Riiiiight. Thanks, got it.

I eventually came up with a customized version of the XAverage class that initializes the first value to the SMA instead of the current bar's close value - there are lots of references on the web where they went for this approach, too, and it seems to be a nicer approximation to me given the fact that I want to take trading decisions based on the EMA value from the first day on:

Code: Select all

protected override double CalcBar()
{
if (Bars.CurrentBar == 1)
{
// return Simple Moving Average for the past <Length> days upon first bar
var value = 0.0;
for (var i = 0; i < Length; i++)
value += Price[i];
return value / Length;
}
else
{
var prev = this[1];
return prev + 2.0 / (Length + 1) * (Price[0] - prev);
}
}


Return to “MultiCharts .NET”