VariableObject - why use it?

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

VariableObject - why use it?

Postby MidKnight » 27 Sep 2012

Hi there,

I keep seeing MC.NET supplied code that uses VariableObject types and I can't figure out why.

Floor_Trader_Pivots Indicator

Code: Select all

private VariableObject<Double> m_s1;
The code later doesn't do anything fancy with it except assign a default value and then of course, set and get basically. Why not just use primitive double instead?

With thanks in advance,
MK

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

Re: VariableObject - why use it?

Postby Henry MultiСharts » 27 Sep 2012

Double is just a number, it has no past and future values. It has only current value.
VariableObject is similar to variables of PowerLanguage. It has history and can be Intrabarpersist.

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

Re: VariableObject - why use it?

Postby MidKnight » 27 Sep 2012

When you say history, you mean like a series? The intrabar persistence is a handy thing though, I guess I should be using this instead.

JohnTaylorHK
Posts: 4
Joined: 16 Nov 2012

Re: VariableObject - why use it?

Postby JohnTaylorHK » 04 May 2013

Hi Support,

This kind of question is one of those that seems perplexing to the non-expert. I can find no comprehensive description or example of usage of either VariableObject or VariableSeries types. Please consider adding these to the Wiki,

best

John

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

Re: VariableObject - why use it?

Postby Henry MultiСharts » 31 May 2013

"Series" variable assumes preservance of its historical values and the possibility of retrieval.
Variables not having this property are called simple variables (VariableObject).

There are two classes that implement the behavior of series and simple variables:
• VariableObject <T> base class for simple variables, provides access to the value of the variable in the current context of execution of scripts.
• VariableSeries<T> base class for a series of variables, making access to the variable in the history (preserves historical value of the variable). All Series variables have the length equal to MaxBarsBack.

Note: simple variables, like the series variables, have certain properties required for correct calculation of a script:
• Variable value rollback (when calculating on each tick within the bar variable rolls back its value before calling Execute() )
• Fixing the variable at the closing of the bar.
• Binding to a specific data-stream (in the presence of several symbols on the chart), by default, is bound to the series, on which the indicator is based on.

Both classes are inherited from interfaces:
• VariableObject <T>
• VariableSeries <T>

The main difference between VariableObject and VariableSeries can be defined by the following sample script:

Code: Select all

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

namespace PowerLanguage.Indicator{
public class variable_sample : IndicatorObject {
public variable_sample(object _ctx):base(_ctx){}

private IPlotObject plot1, plot2;
private AverageFC f_avg;

VariableObject <double> simple_var;
VariableSeries <double> series_var;

[Input]
public int length { get; set; }

protected override void Create() {
plot1 = AddPlot(new PlotAttributes("1", EPlotShapes.Line, Color.Red));
plot2 = AddPlot(new PlotAttributes("2", EPlotShapes.Line, Color.Yellow));

f_avg = new Function.AverageFC(this);

simple_var = new VariableObject<double>(this);
series_var = new VariableSeries<double>(this);
}

protected override void StartCalc() {
f_avg.length = length;
f_avg.price = Bars.Close;
}

protected override void CalcBar() {
simple_var.Value = series_var.Value = f_avg[0];
plot1.Set(simple_ [0] - simple_var[var 1]);
plot2.Set(series_var[0] - series_var[1]);
}
}
}

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

Re: VariableObject - why use it?

Postby MidKnight » 31 May 2013

That is the best explanation I've seen so far Henry. Thanks for that.

kenadian
Posts: 9
Joined: 14 Jan 2014
Has thanked: 5 times
Been thanked: 1 time

CalcBar() ignores bars

Postby kenadian » 20 Jan 2014

Hello MidKnight,
I encounter an issue running the code provided: the first 6 bars in chart are not taken into consideration by CalcBar(). Please find attached the .CSV file I worked with.

I output the values used in CalcBar(),

Code: Select all

protected override void CalcBar() {
//simple_var.Value = series_var.Value = f_avg[0];
simple_var.Value = series_var.Value = Bars.Close[0];

plot1.Set(0,simple_var[0] - simple_var[1]);
plot2.Set(0,series_var[0] - series_var[1]);

Output.WriteLine(Bars.CurrentBar +" simple_var[0]=" + simple_var[0]+" series_var[0]=" + series_var[0] );
Output.WriteLine(Bars.CurrentBar +" simple_var[1]=" + simple_var[1]+" series_var[1]=" + series_var[1] );
Output.WriteLine(Bars.CurrentBar +" simple_var[2]=" + simple_var[2]+" series_var[2]=" + series_var[2] );
Output.WriteLine(" ");
} // CalcBar()
and I get

1 simple_var[0]=3600.39 series_var[0]=3600.39 This value corresponds to bar #7, not #1
1 simple_var[1]=3600.39 series_var[1]=0
1 simple_var[2]=3600.39 series_var[2]=0

2 simple_var[0]=3579.27 series_var[0]=3579.27 This value corresponds to bar #8, not #2
2 simple_var[1]=3579.27 series_var[1]=3600.39
2 simple_var[2]=3579.27 series_var[2]=0

3 simple_var[0]=3579.6 series_var[0]=3579.6 This value corresponds to bar #9, not #3
3 simple_var[1]=3579.6 series_var[1]=3579.27
3 simple_var[2]=3579.6 series_var[2]=3600.39

. . . . .

113 simple_var[0]=4154.2 series_var[0]=4154.2 This value corresponds to bar #119, not #113
113 simple_var[1]=4154.2 series_var[1]=4156.59
113 simple_var[2]=4154.2 series_var[2]=4167.18

114 simple_var[0]=4176.59 series_var[0]=4176.59 This value corresponds to bar #120, not #114
114 simple_var[1]=4176.59 series_var[1]=4154.2
114 simple_var[2]=4176.59 series_var[2]=4156.59

Why this happens ? Thank you.
Attachments
six.bars.ignored.PNG
(25.45 KiB) Downloaded 2340 times
yahoo.daily.120.csv.txt
(7.84 KiB) Downloaded 587 times

kenadian
Posts: 9
Joined: 14 Jan 2014
Has thanked: 5 times
Been thanked: 1 time

Sorry, my question was for Henry

Postby kenadian » 20 Jan 2014

Sorry, my question was for Henry. Thank you.

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

Re: CalcBar() ignores bars

Postby Henry MultiСharts » 24 Jan 2014

Hello MidKnight,
I encounter an issue running the code provided: the first 6 bars in chart are not taken into consideration by CalcBar(). Please find attached the .CSV file I worked with.
Why this happens ? Thank you.
Please refer to the Barsback section of Functions and Special Variables article.

bobrovartiom
Posts: 8
Joined: 12 Mar 2016
Has thanked: 1 time

Re: VariableObject - why use it?

Postby bobrovartiom » 12 Mar 2016

Henry, I am trying to create the indicator to figure out difference between series and object. Trying to use code you sent below.

Code: Select all

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

namespace PowerLanguage.Indicator{
public class variable_sample : IndicatorObject {
public variable_sample(object _ctx):base(_ctx){}

private IPlotObject plot1, plot2;
private AverageFC f_avg;

VariableObject <double> simple_var;
VariableSeries <double> series_var;

[Input]
public int length { get; set; }

protected override void Create() {
plot1 = AddPlot(new PlotAttributes("1", EPlotShapes.Line, Color.Red));
plot2 = AddPlot(new PlotAttributes("2", EPlotShapes.Line, Color.Yellow));

f_avg = new Function.AverageFC(this);

simple_var = new VariableObject<double>(this);
series_var = new VariableSeries<double>(this);
}

protected override void StartCalc() {
f_avg.length = length;
f_avg.price = Bars.Close;
}

protected override void CalcBar() {
simple_var.Value = series_var.Value = f_avg[0];
plot1.Set(simple_ [0] - simple_var[var 1]);
plot2.Set(series_var[0] - series_var[1]);
}
}
}
Under some reason it does not comple. Though it shows no errors. Could you look into this please?

shumaev
Posts: 2
Joined: 16 Apr 2014

Re: VariableObject - why use it?

Postby shumaev » 14 Mar 2016

bobrovartiom,

replace

plot1.Set(simple_ [0] - simple_var[var 1]);

with

plot1.Set(simple_var [0] - simple_var[1]);

bobrovartiom
Posts: 8
Joined: 12 Mar 2016
Has thanked: 1 time

Re: VariableObject - why use it?

Postby bobrovartiom » 15 Mar 2016

bobrovartiom,

replace

plot1.Set(simple_ [0] - simple_var[var 1]);

with

plot1.Set(simple_var [0] - simple_var[1]);
Thanks.
Tried it, but still got an issue (attached).
Attachments
var_simple.png
(15.51 KiB) Downloaded 2220 times

shumaev
Posts: 2
Joined: 16 Apr 2014

Re: VariableObject - why use it?

Postby shumaev » 15 Mar 2016

change "lenght" input parameter to any number above zero


Return to “MultiCharts .NET”