Why is my "series" not able to remember its history?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
SSS
Posts: 14
Joined: 01 Apr 2013
Has thanked: 10 times
Been thanked: 2 times

Why is my "series" not able to remember its history?

Postby SSS » 18 Apr 2013

I'm trying to build an array which holds some values (the Open, High, Low, and Close), and remembers what those values were for previous bars.

I've created a function which puts the OHLC values into an array, and returns that double[] array to the calling indicator. I've created the indicator which calls the function and gets the array from it.

However, I can't get the indicator code to produce a "series" with a history. I want to be able to check things like: what was the first element of the array 10 bars ago? what was the 3rd element of the array 50 bars ago, etc.

In the code below, if i try to plot myArray[0][2], it works. If I try myArray.Value[2] I think it also works. But I can't refer to previous bars. For example myArray[1][2] or myArray[10][2] produces an error.

What am i doing wrong?

Code: Select all


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

namespace PowerLanguage.Indicator{
[SameAsSymbolAttribute(true)]
public class _ohlcPlotter : IndicatorObject {

public _ohlcPlotter(object _ctx):base(_ctx){}
private IPlotObject plot1;

private _ohlcArrayFunction myArray;

protected override void Create() {
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));
myArray = new _ohlcArrayFunction(this);
}
protected override void StartCalc() {
// assign inputs
}
protected override void CalcBar(){
// indicator logic
plot1.Set(myArray[0][2]); // << this works
}
}
}
Below is the function code. It creates an array of four elements (for now i'm putting the OHLC values of the current bar in it).

Code: Select all

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

namespace PowerLanguage
{
namespace Function
{
public sealed class _ohlcArrayFunction : FunctionSeries<double []>
{
public _ohlcArrayFunction(CStudyControl _master) : base(_master) { }
public _ohlcArrayFunction(CStudyControl _master, int _ds) : base(_master, _ds) { }

protected override void Create()
{
// create variable objects and function objects
}

protected override void StartCalc()
{
// assign inputs
}
protected override double[] CalcBar()
{
// function logic
double [] ohlcArray;
ohlcArray = new double [4];
ohlcArray[0] = Bars.Open[0];
ohlcArray[1] = Bars.High[0];
ohlcArray[2] = Bars.Low[0];
ohlcArray[3] = Bars.Close[0];
return ohlcArray;
}
}
}
}
Any help is appreciated.

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

Re: Why is my "series" not able to remember its history?

Postby Henry MultiСharts » 18 Apr 2013

Hello SSS,

Do you receive an exception message? If that is the case please setup the max bars back value for your indicator manually (Format->Study->Format your study->Properties->Max number of bars the study will reference:User specified). If the error still appears - please post the exact error message you have.

SSS
Posts: 14
Joined: 01 Apr 2013
Has thanked: 10 times
Been thanked: 2 times

Re: Why is my "series" not able to remember its history?

Postby SSS » 18 Apr 2013

Hello Henry.

Manually setting max Bars Back didn't help. I still get this error:

System.NullReferenceException:Object reference not set to an instance of an object.

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

Re: Why is my "series" not able to remember its history?  [SOLVED]

Postby Henry MultiСharts » 19 Apr 2013

SSS, you can try to make your function FunctionSimple<double [] > instead of FunctionSeries<double []>.

If you still want the function to be FunctionSeries<double []> then you need to make sure that it's result is not null:

Code: Select all

double[] d = myArray[10];
if(!object.Equals(d, null))
plot1.Set(d[3]);

HellGhostEvocatorX
Posts: 74
Joined: 10 Feb 2022
Has thanked: 44 times
Been thanked: 8 times

Re: Why is my "series" not able to remember its history?

Postby HellGhostEvocatorX » 17 Jun 2022

Hello, I'll join this one.

I also get a null reference exception.

I programmed the following function and indicator, which also work like this, but now when I call the function in a signal, I always get a null reference ecxeption in the function

return price. Value
(unfortunately only visible via the extended debug methods of visualstudie :( (ctrl + alt + p ---> select multicharts. net, then debug )

I had tried to change the function, as you can see from the comments. Then the signal no longer gives an error, but the indicator only returns zero...

Whether series or simple made no difference...

Any tip?

Basically I just wanted to make it selectable/backtestable in indicators/signals what to use for the calculation.

HellGhostEvocatorX
Posts: 74
Joined: 10 Feb 2022
Has thanked: 44 times
Been thanked: 8 times

Re: Why is my "series" not able to remember its history?

Postby HellGhostEvocatorX » 17 Jun 2022

Code: Select all

using System; using System.Drawing; using System.Linq; namespace PowerLanguage { namespace Function { public sealed class HLOC : FunctionSeries<System.Double> { //public ISeries<double> High_1__Low_2__Open_3__Close_4 { get; set; } private ISeries<double> price; //{ get; set; } public int High_1__Low_2__Open_3__Close_4 { get; set; } public HLOC(CStudyControl _master) : base(_master) { } public HLOC(CStudyControl _master, int _ds) : base(_master, _ds) { } protected override void Create() { // create variable objects and function objects } protected override void StartCalc() { // function logic } protected override System.Double CalcBar() { // else // { // price = Bars.Close; //} //ISeries<Double> price = Bars.High; //ISeries<Double> price1; //if (!object.Equals(price, null)) // { if (High_1__Low_2__Open_3__Close_4 == (1)) { price = Bars.High; } if (High_1__Low_2__Open_3__Close_4 == (2)) { price = Bars.Low; } if (High_1__Low_2__Open_3__Close_4 == (3)) { price = Bars.Open; } if (High_1__Low_2__Open_3__Close_4 == (4)) { price = Bars.Close; } return price.Value; //} // else // { // return default(System.Double); //return 0; //} //return default(System.Double); } } } }

Code: Select all

using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; namespace PowerLanguage.Indicator { public class Test_HLOC : IndicatorObject { public Test_HLOC(object _ctx) : base(_ctx) { High_1__Low_2__Open_3__Close_4 = 2; } [Input] public int High_1__Low_2__Open_3__Close_4 { get; set; } Function.HLOC HLOC; private IPlotObject plot1; protected override void Create() { Output.Clear(); Output.WriteLine("Create Phase begonnen"); // create variable objects, function objects, plot objects etc. plot1 = AddPlot(new PlotAttributes("HLOC", EPlotShapes.Line, Color.Red, Color.Red, 5, EPlotStyle.Solid, true)); HLOC = new Function.HLOC(this); Output.WriteLine("Create Phase beendet"); } protected override void StartCalc() { Output.WriteLine("StartCalc Phase begonnen"); // assign inputs HLOC.High_1__Low_2__Open_3__Close_4 = High_1__Low_2__Open_3__Close_4; Output.WriteLine("StartCalc Phase beendet"); } protected override void CalcBar() { Output.WriteLine("CalcBar Phase begonnen"); // indicator logic plot1.Set(HLOC[0]); Output.WriteLine("CalcBar Phase beendet"); } } }

By the way, the exception in the signal is Unaccessible property(method): Bars.Initialize (StartCalc method) or Execute (CalcBar method).


Return to “MultiCharts .NET”