- ago
AddParameter() returns Parameter object. But I can’t just write:

rollingWindow = AddParameter("rollingWindow", ParameterTypes.Int32, 125, 5, 750, 5);
in the constructor.

I have to add rollingWindow = Parameters.FindName("rollingWindow").AsInt;
in Initialize() method.

To shorten this would be good. As AddPrameter() returns a Parameter object, it looks like you wanted it to work like one line instead of two.
1
172
10 Replies

Reply

Bookmark

Sort
- ago
#1
How come you can't use it?
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using WealthLab.ChartWPF; using System.Drawing; using System.Collections.Generic; namespace WealthLab {    public class MyStrategy : UserStrategyBase    {       Parameter p;              public MyStrategy() : base()       {          p = AddParameter("Test", ParameterTypes.Int32, 0, 0, 1, 1);       }       public override void Initialize(BarHistory bars)       {          WriteToDebugLog(p.ToString());       }       public override void Execute(BarHistory bars, int idx)       {       }    } }
0
- ago
#2
I added two params this way and no matter what value I set using UI controls (I don't know how this conrol is called in English :) ) - the strategy always uses default values for every param.
0
Glitch8
 ( 11.81% )
- ago
#3
Here is a minimal example. When you change the parameter via the slider and run the backtest, the SMA period changes.
CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript1 { public class MyStrategy : UserStrategyBase {       //constructor       public MyStrategy()       {          p = AddParameter("Period", ParameterTypes.Int32, 10, 2, 30, 2);       }        //Initialize public override void Initialize(BarHistory bars) {          int period = p.AsInt;          sma = SMA.Series(bars.Close, period);          PlotIndicator(sma); } //Execute public override void Execute(BarHistory bars, int idx) { }       //private members       Parameter p;       SMA sma; } }


0
- ago
#4
Sorry, I meant:
rollingWindow = AddParameter("rollingWindow", ParameterTypes.Int32, 125, 5, 750, 5).AsInt
in constructor didn’t work

So you have to add three lines to add a single param.

The best would be something like one-line:
Var rollingWindow = AddParameter("rollingWindow", ParameterTypes.Int32, 125, 5, 750, 5).AsInt;
In “private members” area, still not sure if something like this is possible.
0
Glitch8
 ( 11.81% )
- ago
#5
No, something like that won’t work, you will need 3 lines for each parameter.
0
- ago
#6
Sad).

The idea was: the operation is quite frequent, and you have to add not only three lines but in three different parts of your code)), it slows down the strategy developing process, but not a real problem of course).
1
Glitch8
 ( 11.81% )
- ago
#7
You could reduce it to 2 lines by not saving the Parameter to a variable, but instead accessing using

Parameters[0].AsInt

But you need to access the value after the constructor, because WL7 assigns the slider value to the Parameter after it is constructed. There’s no other way 🤷🏼‍♂️
1
- ago
#8
looks like still three lines?

- declaring a variable (int e.g.) in "private members"
- AddParameter() in constructor (can't be done in Initialize())
- setting the value of the variable (int e.g.) in Initialize() (can't be done in costructor)
0
Cone8
 ( 25.44% )
- ago
#9
Not required:
- declaring a variable (int e.g.) in "private members"

If an extra variable is too much, then access it like Glitch said:
Parameters[0].AsInt

The setup is 1 line of code. It can't be simplified more than that!
CODE:
//constructor public MyStrategy() { //it's not required to assign a functions to a variable AddParameter("Period", ParameterTypes.Int32, 10, 2, 30, 2); }
0
- ago
#10
Oh, I got it, you mean to get the value directly without putting it to the variable.

Ok, thanks.
0

Reply

Bookmark

Sort