What am I doing wrong?
The script is compiled, but the execution of the script gives an error on any dataset:
Execute Exception (AABA.20191002.A,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (NLOK,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (AAPL,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (NTAP,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (TLAB.20131203.A,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (ADBE,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (ADSK,0) Line 24 - Object reference not set to an instance of an object.
etc .......................
...........................
..........................
at WealthScript3.MyStrategy.Execute(BarHistory bars, Int32 idx) in :line 24
at WealthLab.Backtest.UserStrategyExecutor.PatchReg(List`1 lst, DateTime dt)
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript3 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { TimeSeries weeklyClose = TimeSeriesCompressor.ToWeekly(bars.Close); RSI rsiWeekly = RSI.Series(weeklyClose, 2); TimeSeries expandedRSI = TimeSeriesSynchronizer.Synchronize(rsiWeekly, bars); PlotTimeSeries(expandedRSI, "RSI(2-week)", "RSI", Color.Blue); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { if (expandedRSI[idx] < 20) PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } //declare private variables below private IndicatorBase expandedRSI; } }
The script is compiled, but the execution of the script gives an error on any dataset:
Execute Exception (AABA.20191002.A,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (NLOK,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (AAPL,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (NTAP,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (TLAB.20131203.A,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (ADBE,0) Line 24 - Object reference not set to an instance of an object.
Execute Exception (ADSK,0) Line 24 - Object reference not set to an instance of an object.
etc .......................
...........................
..........................
at WealthScript3.MyStrategy.Execute(BarHistory bars, Int32 idx) in :line 24
at WealthLab.Backtest.UserStrategyExecutor.PatchReg(List`1 lst, DateTime dt)
Rename
This should fix it:
CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript3 { public class MyStrategy : UserStrategyBase { TimeSeries expandedRSI; public override void Initialize(BarHistory bars) { TimeSeries weeklyClose = TimeSeriesCompressor.ToWeekly(bars.Close); RSI rsiWeekly = RSI.Series(weeklyClose, 2); expandedRSI = TimeSeriesSynchronizer.Synchronize(rsiWeekly, bars); PlotTimeSeries(expandedRSI, "RSI(2-week)", "RSI", Color.Blue); } public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { if (expandedRSI[idx] < 20) PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } } }
Many thanks. Now OK
Your Response
Post
Edit Post
Login is required