- ago
I had hoped to continue to use WL6 to run my models to supply signals to my clients for a few more years, but since WL6 will be going away in a few months I guess I will finally have to learn to code in WL8 and migrate the models. Oh well, learning new things helps keep the mind working well! ;)

I am starting simple, and have run into an error that I cannot seem to solve myself.
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using System.Drawing; public class SMACrossover : UserStrategyBase {    public SMACrossover()    {       // Add parameters to allow flexibility in the strategy       AddParameter("Fast SMA Period", ParameterType.Int32, 10, 1, 100, 1);       AddParameter("Slow SMA Period", ParameterType.Int32, 20, 1, 200, 1); //      Name = "SMA Crossover Strategy (WL8)";    }    public override void Initialize(BarHistory bars)    {       // Plot the Fast SMA and Slow SMA on the chart       fastSMA = new SMA(bars.Close, Parameters[0].AsInt);       slowSMA = new SMA(bars.Close, Parameters[1].AsInt);       PlotIndicator(slowSMA, WLColor.Blue);       PlotIndicator(fastSMA, WLColor.Green);    }    protected override void Execute(BarHistory bars, int idx)    {       // Ensure there is enough data to calculate both SMAs       if (idx < Math.Max(Parameters[0].AsInt, Parameters[1].AsInt))          return;       // Check for a crossover condition       // Buy signal: Fast SMA crosses above Slow SMA       if (fastSMA[idx - 1] <= slowSMA[idx - 1] && fastSMA[idx] > slowSMA[idx])       {          PlaceTrade(bars, TransactionType.Buy, OrderType.Market);       }       // Sell signal: Fast SMA crosses below Slow SMA       else if (fastSMA[idx - 1] >= slowSMA[idx - 1] && fastSMA[idx] < slowSMA[idx])       {          PlaceTrade(bars, TransactionType.Sell, OrderType.Market);       }    }    // Declare parameters for fast and slow SMA periods    private SMA fastSMA;    private SMA slowSMA; }


My error is:
QUOTE:

30: 'SMACrossover.Execute(BarHistory, int)': cannot change access modifiers when overriding 'public' inherited member 'UserStrategyBase.Execute(BarHistory, int)'


What am I doing wrong, and why? What misconception do I have? Thanks!

Vince
0
68
Solved
7 Replies

Reply

Bookmark

Sort
Cone8
 ( 0.70% )
- ago
#1
Execute() is public, not protected like it was in WL6.
0
Cone8
 ( 0.70% )
- ago
#2
Also, instead of this -
CODE:
      // Ensure there is enough data to calculate both SMAs       if (idx < Math.Max(Parameters[0].AsInt, Parameters[1].AsInt))          return;

Put this at the end of Initialize(). This also sets the start of the Benchmark backtest.
CODE:
StartIndex = Math.Max(Parameters[0].AsInt, Parameters[1].AsInt);
0
Cone8
 ( 0.70% )
- ago
#3
And for the Cadillac version,
1. modify conditions to use CrossesOver/Under.
2. create Parameter variables instead of the Parameters[] index. It's okay for a script with a couple variables, but when you have more than 3 or 4, it gets confusing quickly.
3. Finally, if you're at all interest in doing WFO, get in the habit of adding the NewWFOInterval method and calling it from Initialize().

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core;    using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using System.Drawing; public class SMACrossover : UserStrategyBase {    public SMACrossover()    {       // Add parameters to allow flexibility in the strategy       _fast = AddParameter("Fast SMA Period", ParameterType.Int32, 10, 1, 100, 1);       _slow = AddParameter("Slow SMA Period", ParameterType.Int32, 20, 1, 200, 1);    }    public override void Initialize(BarHistory bars)    {       NewWFOInterval(bars);              // Plot the Fast SMA and Slow SMA on the chart       PlotIndicator(slowSMA, WLColor.Blue);       PlotIndicator(fastSMA, WLColor.Green);             StartIndex = Math.Max(_fast.AsInt, _slow.AsInt);    }    public override void NewWFOInterval(BarHistory bars)    {       //recreate the indicators for each WFO interval here       fastSMA = new SMA(bars.Close, _fast.AsInt);       slowSMA = new SMA(bars.Close, _slow.AsInt);    }    public override void Execute(BarHistory bars, int idx)    {       // Check for a crossover condition       // Buy signal: Fast SMA crosses above Slow SMA       if (fastSMA.CrossesOver(slowSMA, idx))       {          PlaceTrade(bars, TransactionType.Buy, OrderType.Market);       }       // Sell signal: Fast SMA crosses below Slow SMA       else if (fastSMA.CrossesUnder(slowSMA, idx))       {          PlaceTrade(bars, TransactionType.Sell, OrderType.Market);       }    } // Declare variables for fast and slow SMA private SMA fastSMA;    private SMA slowSMA;    Parameter _fast;    Parameter _slow;    }
You also should modify the Parameter ranges to make better sense and maybe increment in steps of 2 or 5.
0
- ago
#4
QUOTE:
Execute() is public, not protected like it was in WL6.


Thanks for this info, but what did I do to attempt to override the inherited member? That is what I am having trouble understanding. Where did that occur?
0
Glitch8
 ( 1.18% )
- ago
#5
You specified protected instead of public.
0
Best Answer
- ago
#6
Subtle error on my part but absolutely important. Thanks Glitch!
0
- ago
#7
QUOTE:
Put this at the end of Initialize(). This also sets the start of the Benchmark backtest.
CODE:
StartIndex = Math.Max(Parameters[0].AsInt, Parameters[1].AsInt);


Cone, thanks for this bit of info. Need to start learning these details.

V
1

Reply

Bookmark

Sort