- ago
The code :

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript1 {    public class My : UserStrategyBase    {       private static List<BarHistory> Buys = new List<BarHistory>();       public override void Initialize(BarHistory bars)       {          StartIndex = 101;          _Mom = new Momentum(bars.Close, 100);          bars.Cache["Momentum"] = _Mom;       }       public override void PreExecute(DateTime dt, List<BarHistory> participants)       {          foreach (BarHistory bh in participants)          {             Momentum _M = (Momentum)bh.Cache["Momentum"];             int idx = GetCurrentIndex(bh);             double MomVal = _Mom[idx];             bh.UserData = MomVal;          }          participants.Sort((a, b) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble));          Buys.Clear();          for (int n = 0; n <= 19; n++)          {             if (n >= participants.Count)                break;             Buys.Add(participants[n]);          }       }       public override void Execute(BarHistory bars, int idx)       {          bool inBuyList = Buys.Contains(bars);          if (!HasOpenPosition(bars, PositionType.Long))          {             if (inBuyList)                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          }          else          {             if (!inBuyList)                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);          }       }       private Momentum _Mom;    } }


Strategy Setting Image :



How can i debug this :



Thx.
0
574
5 Replies

Reply

Bookmark

Sort
- ago
#1
A. Most likely, you can find more details of the exception in the Log Viewer (Tools menu).
B. Since the strategy appears to work, wrapping the offending line in a try/catch block could help catch errors and output to the Debug log.
C. WriteToDebugLog is the built-in way to print out the various debug info.
0
- ago
#2
QUOTE:
C. WriteToDebugLog is the built-in way to print out the various debug info.
Yes, I would add some WriteToDebugLog lines in there.

What concerns me is the idx value for _Mom[idx] below?
CODE:
int idx = GetCurrentIndex(bh); double MomVal = _Mom[idx];
Is that idx value valid? I'm guessing it isn't. But if you print its value out with WriteToDebugLog, you could verify it's wrong. Remember idx values between 0 thru 100 are not valid arguments and _Mom[idx] will return Double.NaN. But WriteToDebugLog can confirm that; otherwise, we are just guessing.
1
- ago
#3
Ok, Thx

And, have Backtest result question

Every execute "Run Backtest", the results vary a lot

I don't edit code.







Thx
0
Glitch8
 ( 7.81% )
- ago
#4
It's answered in the FAQ under Basic Usage, third question down:

https://www.wealth-lab.com/Support/Faq
1
- ago
#5
QUOTE:
It's answered in the FAQ under Basic Usage, third question down:

https://www.wealth-lab.com/Support/Faq


Maybe you could add extra checkbox when downloading WL7? – “I confirm that I know why backtesting results may differ even if I changed nothing”.

Btw data scientist use random seed for this. You set it and it lets you reproduce the same result even if using randomization.
But I like this variety of results – it’s more realistic, one more small step away from curve-fitting.

I guess this is something already in your backlog, you could add links to the exact part of the page – something with # symbol, I guess (I don’t know how it works).
0

Reply

Bookmark

Sort