Adaptrade Software Newsletter Article
Weekly Strategies Using End-of-Week Exits
In the most recent release of Adaptrade Builder (version 3.1), I added an end-of-week order type. I also added an option to the "Exit after" time exit order so that it can be restricted to a specific day of the week, such as Friday. This effectively means the Exit-after order can also be used to exit at the end of the week. I added this functionality partly in response to user requests. Many traders want to end the week flat to avoid holding positions over the weekend when news events can affect their positions. It can be frustrating to be locked into a trade until Monday morning, knowing that it's vulnerable to events that may happen while the market is closed.
To illustrate how these new order types work, this article will demonstrate how to develop a trading strategy that exits at the end of the week using Adaptrade Builder. Trading strategies will be developed for both daily bars and 30-minute bars of the E-mini S&P 500 stock index futures. I'll discuss the settings, how the order types work, and provide the project files for both build projects.
Implementing an End-of-Week Exit
While it might seem that it would be a simple matter to add an end-of-week order type to a strategy, there are several complications. For one thing, the actual end of the week is the closing price on the last day of the week. That means you need an order type that can exit on the close. Of the scripting languages that Builder supports (EasyLanguage, NinjaScript 7, NinjaScript 8, MQL4, and AFL), only EasyLanguage provides a "market on close" order type, and only EasyLanguage and NinjaScript (both 7 and 8) have optional settings for exiting at the end of the session, which work for both daily and intraday bar types. However, the setting in NinjaScript ("IsExitOnSessionCloseStrategy" in NinjaScript 8; "ExitOnClose" in NinjaScript 7) applies to all days; it cannot be restricted to a specific day of the week.
Another complication is that we don't know if the current day is the last day of the week until the next day, at which point the prior day can be checked to see if it was in fact the last day of that week. For most markets, of course, the trading week ends on Friday, and almost all trading weeks end on Friday other than those for which Friday is a holiday. While this is seldom a problem (i.e., it's only a problem for markets that don't trade on Friday, for markets where the trading week doesn't end on Friday, or for trades taking place during a week where Friday is a trading holiday), it's something to keep in mind.
Only EasyLanguage code allows for a true end-of-session order type that can be restricted to Friday ("SetExitOnClose" command). This is enabled simply by checking for the day of the week and sending the SetExitOnClose command if the day of the week is Friday. This works for both daily bars and intraday data. The code for this is as follows:
If DayOfWeek(date) = 5 then begin SetExitOnClose; end;
However, there's a problem here, too. Because the SetExitOnClose command sends a market order at the close, by the time the order is sent, the market may already be closed, so the order is unlikely to be filled. This is not a problem in historical testing, which will show a simulated fill in the back-test results, but for real-time trading, if the order is not filled, a limit order will be placed in the extended session, if available. This limit order may or may not be filled.
A more reliable approach than using the SetExitOnClose command is to send a market order near the end of the session on Friday. This has the advantage of giving the market time to execute the order before the market closes. Also, it can be implemented in any of the languages Builder supports. The disadvantage is that it requires intraday data. In EasyLanguage, this type exit looks like the following:
If Time >= 1500 and DayOfWeek(date) = 5 then Sell next bar at market;
I've implemented both approaches in version 3.1 of Builder. The "Exit End-of-Week" order type uses the SetExitOnClose command in EasyLanguage and is therefore only available for the TS/MC and TS 2000i code types. It's shown below in Fig. 1 in the Order Types menu. The intraday exit option is shown below in Fig. 2 as the "Exit after" option on the Trading Logic Options menu. This option adds the kind of code shown above, which places a market exit order for the next bar when the current bar time is greater than or equal to the specified time. The optional day-of-week restriction, shown as "Apply on" in Fig. 2, only places the order if the day of the week matches the selection made on the "Apply on" drop-down menu.
Figure 1. Order Types menu in Adaptrade Builder version 3.1, showing the Exit End-of-Week order type.
Figure 2. "Exit after" time option in Adaptrade Builder version 3.1 with optional day-of-week restriction, which enables an approximate end-of-week exit order type.
To understand how the "Exit after" option works to create an end-of-week exit, consider the settings shown in Fig. 2. The session for this symbol ends at 3:15 pm, and the bar size happens to be 30 minutes. The last two bars have bar times of 3:00 pm and 3:15 pm, which are the closing times for those bars. The "Exit after" time has been entered as 3:00 pm, and the day-of-week setting ("Apply on") has been selected as Friday. This means that on the 3:00 pm bar on Friday, a market order will be placed to exit the position on the next bar. This order will be filled at the open of the 3:15 pm bar, which opens just after the 3:00 pm bar closes. As a result, the fill should take place just after 3:00 pm on Friday, 15 minutes prior to the close.
If you're wondering how things change if the bar time represents the open of the bar, rather than the close, it's just a matter of adjusting the "Exit after" time to match the different bar time labels. This is how MetaTrader 4 labels their price bars. In the example above, the last bar would be labeled as 3:00 pm (closing at 3:15 pm), and the prior bar would be labeled as 2:30 pm (closing at 3:00 pm). In this case, you would choose 2:30 as the exit-after time, so that the exit would occur at the open of the 3:00 pm bar, which is the last bar in the session. The exit would be at the same time as before: just after 3:00 pm, 15 minutes prior to the close.
Build Settings for an End-of-Week E-mini Strategy
To build strategies based on these new exit types, I created two projects, one based on daily bars of the E-mini S&P 500 stock index futures (symbol ES, Chicago Mercantile Exchange (CME)), and the second one based on 30-minute bars of the same symbol. For both projects, the symbols were back-adjusted, continuous contracts, as provided by IQFeed (symbol @ES#C). Both project files, which can be opened in Builder version 3.1, are available below.
The basic build approach was to guide the population towards strategies that had one trade per week, with winning trades exiting at the close on Friday. For daily bars, I achieved this by adding a condition that winning trades should be at least four bars in length, which helped bias it towards fewer, longer trades, rather than multiple, shorter trades per week. I also eliminated from consideration all market exit order types, so that it would exit either via the end-of-week exit or via a protective (money management) stop. For the project on daily bars, the complete list of settings is here.
The primary different for the project on 30-minute bars was that I had to change the condition for the number of bars in winning trades from four bars to 48 bars. Also, instead of using the "Exit End-of-Week" order type, I selected the option to exit after 3:00 pm on Friday, as shown in Fig. 2. For the project on 30-minute bars, the complete list of settings is here.
To select the final strategy for each project, I entered a set of screening conditions for the so-called Top Strategies population based on net profit, drawdown, and profit factor on the training segment. For the project on 30-minute bars, I also added two conditions on the test segment in order to prevent the Top Strategies population from getting too large. After the build process was complete, I sorted the strategies in the Top Strategies population by the fitness on the test segment and selected the top population member (i.e., the one with highest fitness on the test segment). This preserved the validation segment as out-of-sample data.
The project files referenced above are available here:
Project file on daily bars
Project file on 30-minute bars
These can be opened in Adaptrade Builder version 3.1, which is available as a free trial via the Builder download page for those who want to examine the project file but don't have a licensed copy of the program.
The project files contain all the settings and build results, including the strategy code. For the project on daily bars, the strategies were built for EasyLanguage code. For the project on 30-minute bars, NinjaTrader 8 was selected as the code type, although this can be changed to either NinjaTrader 7 or EasyLanguage code with minimal effect on the results.
The equity curve for the top end-of-week strategy on daily bars of the E-mini S&P 500 futures (ES) is shown below in Fig. 3.
Figure 3. Equity curve for top end-of-week strategy on daily bars (ES).
Some key performance statistics for the results shown in Fig. 3 are given below.
Ave Weekly Profit: $184
Pct Wins: 62.2%
Win/Loss Ratio: 1.08
Prof Fact: 1.78
The equity curve for the top end-of-week strategy on 30-minute bars of the E-mini S&P 500 futures (ES) is shown below in Fig. 4.
Figure 4. Equity curve for top end-of-week strategy on 30-minute bars (ES).
Some key performance statistics for the results shown in Fig. 4 are given below.
Ave Weekly Profit: $344
Pct Wins: 52.3%
Win/Loss Ratio: 1.54
Prof Fact: 1.69
It's worth noting that the validation results in each case are in line with the results on the training and test segments, despite the fact that the strategies were chosen based on only the training and test segments. When comparing the results for the two strategies, keep in mind that the strategy for daily bars was built using a longer period of data — almost 20 years — whereas the strategy for 30-minute bars was built on approximately eight years of data. As a result, while the total net profit from the daily strategy is greater, the average weekly profit from the 30-minute strategy is higher.
Unsurprisingly, the worst-case drawdown for each strategy occurred during the recent sharp market decline in March of this year. For both strategies, this took place during the validation period. The fact that the results held up fairly well during this period, which is out-of-sample, is a good sign that the strategies are fairly robust.
Not everyone wants to day trade the markets. If your favorite holding period is a matter of a few days, it's entirely reasonable to want to end the week flat in order to avoid holding an open position through the weekend. No one wants to wake up Monday morning to find the markets have opened sharply against their position.
The results of this study demonstrate that it's straightforward to develop a trading strategy that exits at or near the end of the week using either the SetExitOnClose command of EasyLanguage or using a simple time-of-day exit on intraday data. The latter approach is more reliable in that it doesn't require sending a market order at the close of the session when it's unlikely to be filled. However, it does require the use of intraday data, and the bar size has to be chosen so that the open of the last bar is not too far from the close of the market. For example, for 30-minute bars of the day session ES, the open of the last bar is 15 minutes from the close of the market.
The strategies developed for this study were designed to have no more than one trade per week in order to focus on the end-of-week exit. There's no reason, though, that an end-of-week exit could not be used with more active strategies that might trade several times per week. For those types of strategies, the end-of-week exit could be treated as a kind of catch-all exit that would apply on Friday if none of the other exit types had already been triggered.
For those who typically hold trades overnight, adding an end-of-week exit to your strategy can be a simple way to avoid an unwanted Monday morning surprise. If you build your trading strategies manually, the code snippets provided above illustrate how simple it is to add these types of exits. If you use Adaptrade Builder to generate your trading strategies, it's even simpler: just select the "Exit after" option with "Friday" as the Apply-on date prior to building.
Good luck with your trading.
This article appeared in the August 2020 issue of the Adaptrade Software newsletter.
HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN INHERENT LIMITATIONS. UNLIKE AN ACTUAL PERFORMANCE RECORD, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES HAVE NOT ACTUALLY BEEN EXECUTED, THE RESULTS MAY HAVE UNDER- OR OVER-COMPENSATED FOR THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS, SUCH AS LACK OF LIQUIDITY. SIMULATED TRADING PROGRAMS IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN.