- ago
CODE:
         X = ATR.Series(bars, 20).LastValue;          NumATRs = 1.2;          WriteToDebugLog(X*NumATRs);          double Shift = NumATRs * X;


this code compiles ok.

But at runtime, it fails at the last line (double Shift = NumATRs * X) with the following error:

Execute Exception (arry1min,20) Line 48 - Unable to cast object of type 'WealthLab.Core.TimeSeries' to type 'System.IConvertible'.
at System.Convert.ToDouble(Object value)
at WealthScript1.MyStrategy.Execute(BarHistory bars, Int32 idx) in :line 48
at WealthLab.Backtest.UserStrategyExecutor.ComputeService(List`1 lst, DateTime dt)

When I check the debug window, it shows the value for X*NumATR 0.047182568945888306, so its printing a double, not sure why its breaking at run time.
Has anyone encountered this ?
0
360
Solved
4 Replies

Reply

Bookmark

Sort
Glitch8
 ( 13.81% )
- ago
#1
You only provided some partial code. Your bug must be somewhere else in your script, because when I run the complete strategy code here, I get the expected output.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 { 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) {          double X = ATR.Series(bars, 20).LastValue;          double NumATRs = 1.2;          WriteToDebugLog(X *NumATRs);          double Shift = NumATRs * X; } //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)) { //code your buy conditions here } else { //code your sell conditions here } } //declare private variables below } } [code] Output: [code] ---Symbol by Symbol Debug Logs--- ---SPY--- 8.13948694454827
1
Best Answer
- ago
#2
It ran fine for me. Considering its a runtime exception, I would check neighboring code for the culprit. It might not necessarily be line 48. I've run into runtime exceptions where the problem was related to a line inside a block scope, and it wasn't precisely at the line indicated in the exception.
1
- ago
#3
QUOTE:
Execute Exception (arry1min,20)

Check the OHLC and ATR of your "arry1min" symbol at idx 20. What are they?
1
- ago
#4
Thanks all, it turned out to be somewhere else as you suspected. Now i know going forward. Appreciate all your help ! Have a great weekend.
0

Reply

Bookmark

Sort