- ago
DataSets :



Backtest :



Debug Log :



Ex.

look backtest image on 2020/8/10
AAPL and TSLA signal but full cash,
the phenomenon for some time, why?

Thx
0
558
10 Replies

Reply

Bookmark

Sort
- ago
#1
Cannot tell without seeing the code and Strategy settings.
0
- ago
#2
Strategy Settings :



Code :

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 {    public class s : 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 _Mom = (Momentum)bh.Cache["Momentum"];             int idx = GetCurrentIndex(bh);             double MomVal = _Mom[idx];             bh.UserData = MomVal;             if (_Mom[idx].ToString() == "NaN")                return;          }          participants.Sort((a, b) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble));          Buys.Clear();          for (int n = 0; n <= 1; 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;    } }
0
- ago
#3
How many NSF trades did the strategy produce in Metrics Report? 38 in my test. That's the key. When using 100% Equity, try changing the Basis Price to "Market Open next Bar" and bumping the Margin Factor up a bit.
0
- ago
#4
NSF : 0

0
- ago
#5
QUOTE:
NSF : 0

OK so you choose to not Retain them. With this preference activated, both Basis Price options work just fine.

0
- ago
#6
How do I know where NSF happens on date
0
- ago
#7
You don't have NSFs. Try "Market open next bar".
0
Cone8
 ( 26.90% )
- ago
#8
With "Retain NSF Positions" disabled in the Advanced Strategy Settings, NSF Position Count will always be zero.
0
- ago
#9
In the code below, you are setting the sort key to Double.NaN some of the time. Is that intentional or a big mistake? Why are you sorting on NaN in the first place?
CODE:
foreach (BarHistory bh in participants) { Momentum _Mom = (Momentum)bh.Cache["Momentum"]; int idx = GetCurrentIndex(bh); double MomVal = _Mom[idx]; bh.UserData = MomVal; if (_Mom[idx].ToString() == "NaN") return; }
Something like this -10000 solution might make a more predicable sort key.
CODE:
foreach (BarHistory bh in participants) { Momentum _Mom = (Momentum)bh.Cache["Momentum"]; int idx = GetCurrentIndex(bh); bh.UserData = (_Mom[idx]==Double.NaN) ? -10000.0 : _Mom[idx]; }

The other thought would be to avoid the Double.NaN cases altogether with a FOR loop (instead of a foreach loop) which begins at _Mom.FirstValidIndex. If the Momentum indicator is failing to set the FirstValidIndex correctly (Is that a problem?), then I would force that setting so it is corrected.
1
- ago
#10
Hi, superticker

thx for ur suggested.

I was debugging the "NaN".
0

Reply

Bookmark

Sort