I want to explain using the example of the One Percent a Week Strategy. On Friday, there was a situation where a buy order was executed, but according to the strategy code, the position should have been closed on Friday at market close. That’s exactly how it worked in the backtest. I noticed that in reality, it didn’t.
In the strategy monitor, I had At-Close Signaling disabled. I enabled it, i guess this feature was designed for exactly such situations. But it doesn’t work as expected.
When I enable it, my Next Run changes to 3:59:00 PM (60 seconds before close as specified), even though it should be the set run time. And even if I run the strategy manually, no signals are generated. With it disabled, everything works fine.
In the strategy monitor, I had At-Close Signaling disabled. I enabled it, i guess this feature was designed for exactly such situations. But it doesn’t work as expected.
When I enable it, my Next Run changes to 3:59:00 PM (60 seconds before close as specified), even though it should be the set run time. And even if I run the strategy manually, no signals are generated. With it disabled, everything works fine.
Rename
The public versions of the "One Percent" strategy are not programmed to work with At-Close Signaling. The strategy already "knows" on Thursday that it will exit any position on Friday at the Close.
If the position was open on Thursday, you'd have to cancel the Sell at Limit order and place the MOC at the end of the day on Friday (before 3:55 PM for a MOC order) manually. Alternatively, sell At Market some seconds before the close.
Maybe Glitch can help you with an automated version of the program that would work for At-Close Signaling.
If the position was open on Thursday, you'd have to cancel the Sell at Limit order and place the MOC at the end of the day on Friday (before 3:55 PM for a MOC order) manually. Alternatively, sell At Market some seconds before the close.
Maybe Glitch can help you with an automated version of the program that would work for At-Close Signaling.
Well heck, since you made me think about it, here's my shot at it. Depending on which version of the strategy you're using, you'll have exit/entry logic something like this...
.. which can be simplified to this -
After watching Glitch's video (https://www.youtube.com/watch?v=Z39A55OEudM) for the nth time, I think I've got my brain wrapped around the indexing...
It all comes down to this:
The trick is to place the AtClose order on the *penultimate* bar when processing Friday's partial bar with At-Close Signaling. Assuming the edit to the strategy above, *normally* this should work for the One Percent system because on Thursday's bar, the strategy places the MarketClose order.
But here's the kicker - this note in the user guide:
% Important!% At-Close Signaling is always based on the hypothetical backtest and does not use Live Positions even when Use Live Position is enabled. To exit a broker position with the At-Close Signaling feature, the backtest must have an active (or NSF) Position on the last daily bar.
Last week, the One Percent strategy (the original, not GuyRFleury's version) entered the trade on Friday. Therefore the strategy didn't have a position on Thursday to Sell MOC on Friday. For this special case, I don't think there's a way to make it work when the entry is on the same bar. Glitch?
CODE:
if (HasOpenPosition(bars, PositionType.Long)) { double target = LastOpenPosition.EntryPrice; if (LastOpenPosition.ProfitPctAsOf(idx) > -0.5) target = target * 1.01; Backtester.CancelationCode = 642; PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target); Backtester.CancelationCode = 642; if (idx < bars.Count - 1) { if (NextBarIsLastDayOfWeek) PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } else { //last bar: if today is the last trading day of the week, signal MOC for the open position if (bars.TomorrowIsLastTradingDayOfWeek(idx)) PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } } else { if (!Double.IsNaN(mondayOpen) && !tradedThisWeek) { double mult = 0.99; PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, mondayOpen * mult); if (NextBarIsLastDayOfWeek) { Backtester.CancelationCode = 642; PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } } }
.. which can be simplified to this -
CODE:
if (HasOpenPosition(bars, PositionType.Long)) { double target = LastOpenPosition.EntryPrice; if (LastOpenPosition.ProfitPctAsOf(idx) > -0.5) target = target * 1.01; Backtester.CancelationCode = 642; PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target); Backtester.CancelationCode = 642; //last bar: if today is the last trading day of the week, signal MOC for the open position if (bars.TomorrowIsLastTradingDayOfWeek(idx)) PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } else { if (!Double.IsNaN(mondayOpen) && !tradedThisWeek) { double mult = 0.99; PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, mondayOpen * mult); if (bars.TomorrowIsLastTradingDayOfWeek(idx)) { Backtester.CancelationCode = 642; // here's a same-bar order if the position is entered on Friday PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } } }
After watching Glitch's video (https://www.youtube.com/watch?v=Z39A55OEudM) for the nth time, I think I've got my brain wrapped around the indexing...
It all comes down to this:
The trick is to place the AtClose order on the *penultimate* bar when processing Friday's partial bar with At-Close Signaling. Assuming the edit to the strategy above, *normally* this should work for the One Percent system because on Thursday's bar, the strategy places the MarketClose order.
But here's the kicker - this note in the user guide:
% Important!% At-Close Signaling is always based on the hypothetical backtest and does not use Live Positions even when Use Live Position is enabled. To exit a broker position with the At-Close Signaling feature, the backtest must have an active (or NSF) Position on the last daily bar.
Last week, the One Percent strategy (the original, not GuyRFleury's version) entered the trade on Friday. Therefore the strategy didn't have a position on Thursday to Sell MOC on Friday. For this special case, I don't think there's a way to make it work when the entry is on the same bar. Glitch?
QUOTE:I am using Patreon version.
The public versions of the "One Percent" strategy are not programmed to work with At-Close Signaling.
QUOTE:
For this special case, I don't think there's a way to make it work when the entry is on the same bar.
I assume that the extra At-Close run is meant exactly for such cases, at least that’s how I understood this functionality. In my case, it looks like the strategy monitor doesn’t add an extra run but replaces the current one. As for this particular strategy, removing Friday entries doesn’t really affect the overall profit much. Let’s see what Glitch says.
I'll run it in the SM this Friday and report back.
I just tested it with the script below (after manipulating the Market hours for TQQQ) with and without entering on the same bar, and it worked fine. Since the backtest gets the partial bar it's able to determine that the entry for "today" put on a position, so it's able to process the MOC order for that position. The code is similar to the way "One Percent" enters and exits at MOC.
Test Script assumes today is Wedesday for an exit At Close today.
Bonus!
Run this strategy (Parameter = 0) with 100% equity sizing on TQQQ since 2010 to buy on Monday and sell Wednesday at the close. You get approximately the same return as the original "One Percent" system.
Test Script assumes today is Wedesday for an exit At Close today.
CODE:
using System; using WealthLab.Backtest; using WealthLab.Core; namespace WealthScript9 { public class AtCloseSignalingTest : UserStrategyBase { Parameter _sameday; public AtCloseSignalingTest() { _sameday = AddParameter("Same Day Trade = 1", ParameterType.Int32, 0, 0, 1); } public override void Initialize(BarHistory bars) { StartIndex = 0; } public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { DateTime nextTradingDay = bars.GetNextTradingDate(bars.DateTimes[idx]); if (_sameday.AsInt != 1) { // buy on Monday if (nextTradingDay.DayOfWeek < bars.DateTimes[idx].DayOfWeek) { Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } else { // same day trade (assumes last complete bar was Tuesday_ if (TomorrowIsWednesday(bars, idx)) { Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market); PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } } } else // Sell on Thursday close { Position p = LastPosition; if (TomorrowIsWednesday(bars, idx)) ClosePosition(p, OrderType.MarketClose); } } bool TomorrowIsWednesday(BarHistory bars, int bar) { DateTime tomorrow = bars.DateTimes[bar].AddDays(1); return tomorrow.DayOfWeek == DayOfWeek.Wednesday; } } }
Bonus!
Run this strategy (Parameter = 0) with 100% equity sizing on TQQQ since 2010 to buy on Monday and sell Wednesday at the close. You get approximately the same return as the original "One Percent" system.
Your Response
Post
Edit Post
Login is required