In the .NET programming guide there is the following example:
double old_power;
protected override void StartCalc() {
old_power = 0;
}
protected override void CalcBar(){
double afast = AvgVal(fastLength);
double aslow = AvgVal(slowLength);
double power = Math.Abs(100*(afast - aslow)/Bars.Close[0]);
if ( (power >= strongLevel)&&(old_power < strongLevel) ){
switch(TrendDir()){
case -1:
trend_SE.Send();
break;
case 1:
trend_LE.Send();
break;
}
}
if ((CurrentPosition.Side != EMarketPositionSide.Flat)
13
&&(old_power >= strongLevel)
&&(power < strongLevel))
{
trend_LX.Send();
trend_SX.Send();
}
old_power = power;
}
This appears to assume that old_power will be persisted bar to bar such that the previous value is still available and not wiped out the next time CalcBar() is called. However I cannot get this type of functionality to work even if I declare the primitive/array within the constructor. I find I must declare a VariableSeries or VariableObject. Is this the only way in the .NET version to persist the data from one bar to the next?
In addition the reason I would like to use a primitive/array in this case is because I need to persist an array of values calculated for the current bar which would be available for the next bars calculation. If there was an ability to create an array of VariableObjects that would suit my needs, but I have not seen an example.
Adivse?
Persistence of primitives if declared within constructor?
-
- Posts: 9
- Joined: 24 Apr 2013
- Has thanked: 2 times
-
- Posts: 81
- Joined: 29 Apr 2013
- Location: www.threefoldmarkets.com
- Has thanked: 12 times
- Been thanked: 16 times
- Contact:
Re: Persistence of primitives if declared within constructor
"old_power" is a class variable. Not sure what "persistence of primitives" means? This should be working just fine though, if you want to see a value across calls to CalcBar. You are not showing all of the code, however. Where is the declaration of "SignalObject"? Can't really help with too little code showing, or if you just cut this from the programming guide, you don't have a complete signal defined here.... oh now I see, looks like you didn't post your code at all.
Post your code so we can help you.
Here is an example from my code:
Post your code so we can help you.
Here is an example from my code:
Code: Select all
public class TFractal : SignalObject {
private VariableSeries<Double> m_cfb;
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Persistence of primitives if declared within constructor
Hello MarketPrep,
The code you posted allows to remember values of variables not between bars, but between script calculation (there can be multiple script calculation per bar). If you want to remember values of variables between bars, you should use VariableSeries type, you can find examples in standard prebuilt scripts.
The code you posted allows to remember values of variables not between bars, but between script calculation (there can be multiple script calculation per bar). If you want to remember values of variables between bars, you should use VariableSeries type, you can find examples in standard prebuilt scripts.
-
- Posts: 9
- Joined: 24 Apr 2013
- Has thanked: 2 times
Re: Persistence of primitives if declared within constructor
Thanks Andrew,
I figured that between bars VariableSeries or VariableObject were needed, however what i need is an array of VariableObjects to be available from bar to bar calculation.
Is there an example of how to do this?
Pseudo code below (example)
private VariableObject<Double>[] R2;
private VariableObject<Double>[] R1;
protected override void Create(){
VariableObject<Double>[] R2 = new VariableObject<Double>(this)[48];
VariableObject<Double>[] R1 = new VariableObject<Double>(this)[48];
protected override void CalcBar(){
for (int i = 0; i< 48; i++){
R2.SetValue(R1,i); // syntax does not work
R1.SetValue(R2 + 0.2*i); // syntax does not work
}
I figured that between bars VariableSeries or VariableObject were needed, however what i need is an array of VariableObjects to be available from bar to bar calculation.
Is there an example of how to do this?
Pseudo code below (example)
private VariableObject<Double>[] R2;
private VariableObject<Double>[] R1;
protected override void Create(){
VariableObject<Double>[] R2 = new VariableObject<Double>(this)[48];
VariableObject<Double>[] R1 = new VariableObject<Double>(this)[48];
protected override void CalcBar(){
for (int i = 0; i< 48; i++){
R2.SetValue(R1,i); // syntax does not work
R1.SetValue(R2 + 0.2*i); // syntax does not work
}
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2957 times
Re: Persistence of primitives if declared within constructor
MarketPrep, our programmer has corrected your code:
Code: Select all
private VariableObject<Double>[] R2;
private VariableObject<Double>[] R1;
const int _Count = 48;
protected override void Create()
{
R2 = new VariableObject<Double>[_Count];
R1 = new VariableObject<Double>[_Count];
for (int i = 0; i < _Count; i++)
{
R1[i] = new VariableObject<Double>(this);
R2[i] = new VariableObject<Double>(this);
}
}
protected override void CalcBar()
{
for (int i = 0; i < _Count; i++)
{
double _R1Val = R1[i].Value;
R2.SetValue(_R1Val, i);
double _R2Val = R2[i].Value;
R1[i].Value = _R2Val + 0.2 * i;
}
}