Ok, next step in my tutoring... :)
Errors generated:
Again, I am obviously missing something in my understanding. What is it? Is that there are separate Classes for both Indicator and TimeSeries and certain actions, such as PlotIndicator, only take input from a particular Class?
This is how I defined them.
And this was the Initialization step:
And if it is important, here is the whole module:
Thanks!
V
CODE:
public override void Execute(BarHistory bars, int idx) { double rawPrice = bars.Close[idx]; double filteredPrice = filter.Filter(rawPrice, bars.DateTimes[idx].ToOADate(), bars.DateTimes[idx - 1].ToOADate()); Close = bars.Close; filteredSeries[idx] = filteredPrice; // Plot Original Price Series <strong> PlotIndicator(Close, WLColor.Blue, PlotStyle.Line);</strong> // Plot Filtered Price Series <strong> PlotIndicator(filteredSeries, WLColor.Red, PlotStyle.Line);</strong> if (filteredPrice < bars.Close[idx]) // Trend-following trade execution { PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } }
Errors generated:
QUOTE:
Compiled at 4/6/2025 08:42:04
80: Argument 1: cannot convert from 'WealthLab.Core.TimeSeries' to 'WealthLab.Indicators.IndicatorBase'
83: Argument 1: cannot convert from 'WealthLab.Core.TimeSeries' to 'WealthLab.Indicators.IndicatorBase'
Again, I am obviously missing something in my understanding. What is it? Is that there are separate Classes for both Indicator and TimeSeries and certain actions, such as PlotIndicator, only take input from a particular Class?
This is how I defined them.
QUOTE:
private TimeSeries filteredSeries;
private TimeSeries Close;
And this was the Initialization step:
CODE:
public override void Initialize(BarHistory bars) { filter = new Filter(1.0, 0.02, 1.0); filteredSeries = new TimeSeries(); Close = new TimeSeries(); }
And if it is important, here is the whole module:
CODE:
// WealthLab Strategy Using Filter with Plotting public class WealthLabStrategy : UserStrategyBase { private Filter filter; private TimeSeries filteredSeries; private TimeSeries Close; public override void Initialize(BarHistory bars) { filter = new Filter(1.0, 0.02, 1.0); filteredSeries = new TimeSeries(); Close = new TimeSeries(); } public override void Execute(BarHistory bars, int idx) { double rawPrice = bars.Close[idx]; double filteredPrice = filter.Filter(rawPrice, bars.DateTimes[idx].ToOADate(), bars.DateTimes[idx - 1].ToOADate()); Close = bars.Close; filteredSeries[idx] = filteredPrice; // Plot Original Price Series PlotIndicator(Close, WLColor.Blue, PlotStyle.Line); // Plot Filtered Price Series PlotIndicator(filteredSeries, WLColor.Red, PlotStyle.Line); if (filteredPrice < bars.Close[idx]) // Trend-following trade execution { PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } }
Thanks!
V
Rename
Use PlotTimeSeries instead of PlotIndicator đź‘Ť
Is it really that simple? Wow! Thanks Glitch!
V
V
Next issue...
Current code:
Getting the error with this line:
And the error is:
The Settings are:

Any ideas? Thanks!
V
Current code:
CODE:
// WealthLab Strategy Using Filter with Plotting public class WealthLabStrategy : UserStrategyBase { private Filter filter; private TimeSeries filteredSeries; private TimeSeries Close; public override void Initialize(BarHistory bars) { filter = new Filter(1.0, 0.02, 1.0); filteredSeries = new TimeSeries(); Close = new TimeSeries(); } public override void Execute(BarHistory bars, int idx) { double rawPrice = bars.Close[idx]; double filteredPrice = filter.Filter(rawPrice, bars.DateTimes[idx].ToOADate(), bars.DateTimes[idx - 1].ToOADate()); Close = bars.Close; filteredSeries[idx] = filteredPrice; // Plot Original Price Series PlotTimeSeries(Close, "Close", "Price", WLColor.Blue, PlotStyle.Line); // Plot Filtered Price Series PlotTimeSeries(filteredSeries, "Filtered", "Price", WLColor.Red, PlotStyle.Line); if (filteredPrice < bars.Close[idx]) // Trend-following trade execution { PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } }
Getting the error with this line:
CODE:
double rawPrice = bars.Close[idx];
And the error is:
QUOTE:
Execute Exception (SPY,0) Line 73 - Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at WealthLabStrategy.Execute(BarHistory bars, Int32 idx) in :line 73
at WealthLab.Backtest.UserStrategyExecutor.PushWatcher(List`1 lst, DateTime dt)
The Settings are:
Any ideas? Thanks!
V
The exception is noted at the first line of the method (statement block), but it is likely occurring here...
If idx is 0 then bars.DateTimes[idx - 1] is causing the exception because idx - 1 is negative one.
CODE:
double filteredPrice = filter.Filter(rawPrice, bars.DateTimes[idx].ToOADate(), bars.DateTimes[idx - 1].ToOADate());
If idx is 0 then bars.DateTimes[idx - 1] is causing the exception because idx - 1 is negative one.
Assign StartIndex = 1 (at least).
Ok, I am still doing something very wrong...
Here is the stripped down code:
And the runtime error:
What am I doing wrong?
V
Here is the stripped down code:
CODE:
using WealthLab.Backtest; using WealthLab.Core; using System; using System.Collections.Generic; using ScottPlot.Plottable; using WealthLab.Indicators; public class FilterStrategy : UserStrategyBase { private TimeSeries Close; private TimeSeries smoothedPrices; private double alpha = 0.1; public override void Initialize(BarHistory bars) { Close = bars.Close; StartIndex = 100; for (int i = 1; i < bars.Count; i++) { smoothedPrices[i] = ExponentialSmoothing(alpha, Close[i], Close[i-1]); } } private double ExponentialSmoothing(double alpha, double x, double xPrev) { return alpha * x + (1 - alpha) * xPrev; } public override void Execute(BarHistory bars, int idx) { PlotTimeSeries(smoothedPrices, "Price", "Smoothed Price", WLColor.Blue); PlotTimeSeries(Close, "Price", "Original Price", WLColor.Black); } }
And the runtime error:
QUOTE:
Initialize Exception (SPY) Line 21 - Object reference not set to an instance of an object.
at FilterStrategy.Initialize(BarHistory bars) in :line 21
at WealthLab.Backtest.UserStrategyExecutor.CancelWatcher(List`1 symbols)
What am I doing wrong?
V
You declared a variable called smoothPrices but you never instantiate it in your code. Thus isn’t WL related here, it’s basic C# programming. Before being able to use an object you have to create the instance of it.
After the line Close = bars.Close you can create the instance like this:
smoothPrices = new TimeSeries(bars.DateTimes);
smoothPrices = new TimeSeries(bars.DateTimes);
Thanks Glitch for your help!
Unfortunately I am an old C programmer and I never needed to code after that, so I am "new" to some of this. :(
V
QUOTE:
it’s basic C# programming
Unfortunately I am an old C programmer and I never needed to code after that, so I am "new" to some of this. :(
V
No need to apologize, happy to help!
Your Response
Post
Edit Post
Login is required