- ago
Ok, next step in my tutoring... :)

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

0
109
10 Replies

Reply

Bookmark

Sort
Glitch8
 ( 6.30% )
- ago
#1
Use PlotTimeSeries instead of PlotIndicator đź‘Ť
0
- ago
#2
Is it really that simple? Wow! Thanks Glitch!

V
0
- ago
#3
Next issue...
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
0
- ago
#4
The exception is noted at the first line of the method (statement block), but it is likely occurring here...

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.
0
Cone8
 ( 2.06% )
- ago
#5
Assign StartIndex = 1 (at least).
1
- ago
#6
Ok, I am still doing something very wrong...

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
0
Glitch8
 ( 6.30% )
- ago
#7
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.
0
Glitch8
 ( 6.30% )
- ago
#8
After the line Close = bars.Close you can create the instance like this:

smoothPrices = new TimeSeries(bars.DateTimes);
0
- ago
#9
Thanks Glitch for your help!

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
0
Glitch8
 ( 6.30% )
- ago
#10
No need to apologize, happy to help!
0

Reply

Bookmark

Sort