- ago
I would like to make an overnight strategy by using the daily candles of Norgate.
I check if the close price is above the close price of the previous bar, then I create a long position. At next morning at market openning I would like to sell it.

However: PlaceTrade is executed the next day inside the method "Execute" and not the same day. Unfortunately I have only daily data of Norgate, I can't use 1m data. And performance would be really great if it would work with daily bar data.
Does someone knows how I could solve this problem?

CODE:
using WealthLab.Backtest; using WealthLab.Core; namespace WealthScript6 {    public class MyStrategy : UserStrategyBase    {       private TimeSeries close;       public MyStrategy() : base()       {          StartIndex = 1;          AddParameter("Min. Tagesanstieg [%]", ParameterType.Double, 0.0, 0.0, 5.0, 0.1);       }       public override void Initialize(BarHistory bars)       {          close = bars.Close;       }       public override void Execute(BarHistory bars, int idx)       {          double thresholdParam = Parameters[0].AsDouble;          double threshold = thresholdParam / 100.0;          if (idx < bars.Count - 1)          {             if (close[idx] > close[idx - 1] * (1 + threshold))             { // FIXME: Tell him in some way, to execute the trade today, not at next session                PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose, 0, 0, "Buy at Close: " + close[idx]); // Execute next day at morning                PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, 0, "Sell at Next Open");             }          }       }    } }
0
114
Solved
7 Replies

Reply

Bookmark

Sort
ConeA
 ( 3.80% )
- ago
#1
See: At-Close Signaling
https://youtu.be/Z39A55OEudM

Once you get it programmed, to trade it live, you need a provider (not Norgate) that can give you a partial daily bar for the Strategy Monitor's At-Close Signaling function.

For that snippet, this should do it -
CODE:
      public override void Execute(BarHistory bars, int idx)       {          double thresholdParam = Parameters[0].AsDouble;          double threshold = thresholdParam / 100.0;          if (idx < bars.Count - 1)          {             if (close[idx + 1] > close[idx] * (1 + threshold))             {                // FIXME: Tell him in some way, to execute the trade today, not at next session                PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose, 0, 0, "Buy at Close: " + close[idx]);             }          }          // Execute next day at morning          PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, 0, "Sell at Next Open");       }
0
Best Answer
- ago
#2
And is there a possibility to backtest it?
0
Glitch8
 ( 6.16% )
- ago
#3
Yes you can backtest it of course.
0
- ago
#4
Thanks, I just saw the change: close[idx + 1]
Hmm should it then not be if (idx < bars.Count - 2) instead of if (idx < bars.Count - 1)?
It works great with futures like &FDAX, but &VX seems to be broken:


Current Code:
CODE:
using WealthLab.Backtest; using WealthLab.Core; namespace WealthScript1 {    public class MyStrategy : UserStrategyBase    {       private TimeSeries close;       public MyStrategy() : base()       {          StartIndex = 1;          AddParameter("Min. Tagesanstieg [%]", ParameterType.Double, 0.0, 0.0, 5.0, 0.1);       }       public override void Initialize(BarHistory bars)       {          close = bars.Close;       }       public override void Execute(BarHistory bars, int idx)       {          double thresholdParam = Parameters[0].AsDouble;          double threshold = thresholdParam / 100.0;          if (idx < bars.Count - 1)          {             if (close[idx + 1] >= close[idx] * (1 + threshold))             {                PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose, 0, 0, "Buy at Close: " + close[idx]);             }          }          // Execute next day at morning          PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, 0, "Sell at Next Open");       }    } }
0
ConeA
 ( 3.80% )
- ago
#5
if (idx < bars.Count - 1) lets the backtest process all bars except the final bar in the chart, whose bar number is bar.Count - 1. This is required for your scenario because we need to peek at the close of the next bar to determine if we want to buy at that close.

Watch the video linked in Post #1 and see the Help > Strategy Monitor for At-Close Signaling (item 4b) for details.
0
- ago
#6
And do you have an idea why backtests aborts after 15 July 2022 in the VIX Future (&VX)? It should normally continue.

0
ConeA
 ( 3.80% )
- ago
#7
That's off topic and there's no way to determine it with those pictures.

Buy my guess is that you shouldn't use 100% sizing without any margin because the result is likely to lead to many NSF positions, which might appear to you like the backtest stopped trading.
0

Reply

Bookmark

Sort