- ago
Is this happening to anyone else that runs this code?

I am slowly converting a WL6 code to WL7. I'm having to piecemeal the code from the 5 example strategies (I can't wait until the crossref has more WL7 code examples!).

If I just do buy and sell and ignore the positions it can finish but when trying to get ready to deal with multiple positions with the position loop it gets stuck.

Any ideas please?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript13 { public class MyStrategy : UserStrategyBase {       public MyStrategy() : base()       {          AddParameter("Days", ParameterTypes.Int32, 2, 2, 5, 1);       }              //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars) {       } //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 (LastPosition == null)             PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, "Initial");          /*                   if (OpenPositions.Count > 0)                   {                      for (int i = OpenPositions.Count - 1; i >= 0; i--)                      {                         Position pos = OpenPositions[i];                         if (pos != null)                            ClosePosition(pos, OrderType.Limit, pos.EntryPrice * _rise, _rise.ToString());                      }                   }          */          foreach (Position p in OpenPositions) {             //cop e your sell conditions here             if (p.DaysInPosition(idx, false) > Parameters[0].AsInt)                ClosePosition(p, OrderType.Market, 0, "Exit Day");             //            PlaceTrade(bars, TransactionType.Sell, OrderType.Market);          } } //declare private variables below } }
0
592
Solved
4 Replies

Reply

Bookmark

Sort
- ago
#1
I'd say you're running the backtest on Daily data. DaysInPosition should be applied to intraday data only. On Daily the backtest never ends. Creating an issue to investigate.

For daily, rewrite this line...
CODE:
if (p.DaysInPosition(idx, false) > Parameters[0].AsInt)

...like this:
CODE:
if (idx + 1 - p.EntryBar > Parameters[0].AsInt)
0
Best Answer
- ago
#3
That worked, thank you.
0
- ago
#4
Good to know. Also we fixed this for Build 7 so DaysInPosition won't get stuck forever on Daily.
0

Reply

Bookmark

Sort