VariableSeries inside FunctionSeries = 1 Bar lag ?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

VariableSeries inside FunctionSeries = 1 Bar lag ?

Postby novaleaf » 23 Apr 2014

I think I found a bug, please let me know if it's a misunderstanding, or there is a better workaround for me.

please see the bottom attached src of my "Returns" function.

I am plotting the values of returns.percentChange.Value (a VariableSeries).

Here seems to be the bug:
1) if I do not access returns.Value, the plotted values of returns.percentChange.Value are delayed by 1 bar.
2) However if I access returns.Value (even if I don't actually do anything with the results) the plotted values are not delayed by 1 bar. (simply adding the line var noop = this.returns.Value; makes the plot align properly)

Code: Select all

using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;


namespace PowerLanguage
{
namespace Function
{
public sealed class Returns : FunctionSeries<System.Double>
{
public Returns(CStudyControl _master) : base(_master) { }
public Returns(CStudyControl _master, int _ds) : base(_master, _ds) { }

public VariableSeries<double> percentChange;
public VariableSeries<double> cumulativeChange;
public VariableSeries<bool> isNewDay;

public IInstrument Instrument { get; private set; }

public void Initialize(IInstrument instrument)
{
this.Instrument = instrument;
}

protected override void Create()
{
// create variable objects and function objects
this.percentChange = new VariableSeries<double>(this);
this.cumulativeChange = new VariableSeries<double>(this);
this.isNewDay = new VariableSeries<bool>(this);

}

protected override void StartCalc()
{
// assign inputs
}

protected override System.Double CalcBar()
{
// function logic
var change = this._GetInstrumentChange(this.Instrument);
this.percentChange.Value = change.Item1;
this.isNewDay.Value = change.Item2;

if (this.isNewDay.Value == true)
{
this.cumulativeChange.Value = 0.0;
}
else
{
this.cumulativeChange.Value = this.percentChange.Value + this.cumulativeChange[1];
}
return this.percentChange.Value;
}

private Tuple<double, bool> _GetInstrumentChange(IInstrument instrument)
{
if (instrument.Info.Resolution.IsDayOrHigher())
{
//don't let our close of yesterday impact returns today
//this.Output.WriteLine("daily");
}
else if (instrument.Time[1].Day != instrument.Time[0].Day)
{
//this.Output.WriteLine("start of day");
return Tuple.Create(0.0, true);
}

var closes = instrument.Close;
var isNewDay = false;
var percentChange = (closes[0] - closes[1]) / closes[1];

return Tuple.Create(percentChange, isNewDay);

}


}
}
}

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: VariableSeries inside FunctionSeries = 1 Bar lag bug?

Postby novaleaf » 24 Apr 2014

here is an indicator you can add to a chart to repro the bug. uncomment line 29 to make the bug disapear.

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator{
public class _NL_Normalized : IndicatorObject {
public _NL_Normalized(object _ctx):base(_ctx){}
private IPlotObject deltaReturns;
private IPlotObject cumulativeReturns;

public Returns returns;

protected override void Create() {
// create variable objects, function objects, plot objects etc.
this.deltaReturns = AddPlot(new PlotAttributes("percentChange_i1", EPlotShapes.Histogram,Color.Green));
this.cumulativeReturns = AddPlot(new PlotAttributes("cumulative", EPlotShapes.Line, Color.Yellow));
this.returns = new Returns(this);
}
protected override void StartCalc() {
// assign inputs
this.returns.Initialize(this.Bars);

}
protected override void CalcBar()
{
// indicator logic
var delta = this.returns.percentChange.Value;
//var noop = this.returns.Value;
var cumulative = this.returns.cumulativeChange.Value;
if (delta >= 0)
{
this.deltaReturns.Set(delta,Color.Green);
}
else
{
this.deltaReturns.Set(delta, Color.Red);
}
if (this.returns.isNewDay.Value == true)
{
//erase previous line to make it obvious that there's a break/reset of the line
//this.cumulativeReturns.Set(1, this.cumulativeReturns.Values[1], Color.Black,0);
}
this.cumulativeReturns.Set(cumulative);



}
}
}

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

Re: VariableSeries inside FunctionSeries = 1 Bar lag bug?

Postby Henry MultiСharts » 24 Apr 2014

novaleaf, there is no bug.
You have created a series function. Series functions are calculated on every bar, even if you have not called it's CalcBar method. In your example the function calculation is initiated after the indicator is calculated, so the indicator uses the previous function's value.

When you call this.returns.Value - you initiate the function calculation. In such case you get the actual function value. You can also see that if you output the beginning and the end of CalcBar method calculation of the function in the indicator.

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: VariableSeries inside FunctionSeries = 1 Bar lag bug?  [SOLVED]

Postby novaleaf » 24 Apr 2014

Hello Henry.

It sounds like there is an internal list of objects (functions/indicators/signals) in an "update queue" to be processed every update. Those that are actively referenced are updated "just in time".

This makes perfect sense, and your explanation really does help me understand the workings of Multicharts.

Thank you!

-Jason

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: VariableSeries inside FunctionSeries = 1 Bar lag bug?

Postby JoshM » 02 May 2014

You have created a series function. Series functions are calculated on every bar, even if you have not called it's CalcBar method.
It sounds like there is an internal list of objects (functions/indicators/signals) in an "update queue" to be processed every update. Those that are actively referenced are updated "just in time".
Henry, are more objects calculated on every bar, regardless whether they are called in CalcBar? Or is this behaviour something that is unique to series functions?

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

Re: VariableSeries inside FunctionSeries = 1 Bar lag bug?

Postby Henry MultiСharts » 13 May 2014

Henry, are more objects calculated on every bar, regardless whether they are called in CalcBar? Or is this behaviour something that is unique to series functions?
Hello JoshM,

This is applied to series functions only.


Return to “MultiCharts .NET”