|
|
“Initial Balance” Rotation Strategy: Part 5 – Backtesting 101(This is a continuation in our blog series on creating a trading system from start to finish. Want to see more? Click here for our main RTL support page, which links to all the articles in this series and many more tutorials. Questions? Click here for the RTL Community Forum where you can get help on your programming.) Backtesting is the process of taking a trading idea and applying it to historical price data for the purpose of testing the method’s long-term profitability. It is fairly straightforward to imagine a potentially rewarding trading strategy, but how do you build confidence that it works over time? Backtesting investigates how it would have performed if you had traded it every day over the course of a few months or years, whatever your preference.
Today we kickstart a multi-part series for backtesting in MarketDelta. We use as the strategy the 200% Initial Balance rotation pattern identified in Parts 1 thru 4 of this series. The RTL code used for the backtesting expands upon the custom indicators and signals already discussed in the earlier parts of this series. If you have not read these, see the “Case Study” on this page. It is very important to outline some notable drawbacks to backtesting:
However, despite this caution, backtesting grows in popularity and can effectively contribute to your work — as long as you maintain this perspective. Exit SignalsSo far in this series, we have focused only on coding the trading signals that accurately determine when to enter a trade based on our strategy. As we will see, a backtest is more involved with the exit signals. How you close a position has dramatic influence on whether the entry signal worked. Consider the following ways you could exit a trade:
Additionally, you may have multiple entries in combination with multiple exits. In reality, most traders naturally do a combination of all these on every trade. This makes backtesting complicated. In this series, we will discuss all these scenarios. Today, we look only at a very simple closing strategy: the end-of-day exit. This strategy is important for all future techniques as well; if a trade has not stopped out for a loss, but has also not met a profit target, you would likely still exit at the end of the day. In today’s introduction to backtesting, we simply enter once and hold until the last minute of the trading session, and then exit. We will investigate what happens if you enter a short at the Double IB High or a long at the Double IB Low and hold it until the end of the day. Create a Trading SystemTo start, use the RTL button, the File > New menu, or the Open > Trading System menu to open the Trading System window: If you have not already, hit the New button to create a new system. Create Backtest Entry SignalsWe already created 2 trading signals in this blog series: the long and short signals at the Double IB High and Low. While these signals were good for charting, they need some modification for the system rules. In backtesting, each signal typically involves more than just entry/exit criteria; it also sets variables used to manage the system’s actions. Consider this RTL code for one of our signals: LO <= (SESST_LOW – (SESST_HIGH – SESST_LOW)) AND TIME >1030 AND TIME < 1500 (The times, 1030 and 1500 listed, are for Eastern. Adjust accordingly for your time zone.) We want our backtest to presume that we entered right at this 2X IB Low, so the “Rule Price” for this action will be a user variable, V#1, that is set to be equal to the 2x IB Low. We set the user variable as part of the signal using the SET token. When you specify a specific entry price as we do here, the backtest will enter at, or nearest, this price on the signaling bar. You could choose other Rule Price options, such as the Close of the current bar, the Open of the next bar, or other variations. But by using SET and entering at the exact level then our backtest accurately emulates real-time trading behavior. To set a variable, the syntax is as follows. We want to set variable V#1 to the value of the 2x IB Low. SET(V#1,(SESST_LOW – (SESST_HIGH – SESST_LOW))) Now, we combine this with our existing signal, and we get: SET(V#1,(SESST_LOW – (SESST_HIGH – SESST_LOW))) AND LO <= (SESST_LOW – (SESST_HIGH – SESST_LOW)) AND TIME >1030 AND TIME < 1500 However, there is no need to duplicate the math, so we can simplify this as: SET(V#1,(SESST_LOW – (SESST_HIGH – SESST_LOW))) AND LO <=V#1 AND TIME >1030 AND TIME < 1500 Note how the LO <= V#1 is a replacement for the full arithmetic, since this math is included in the SET statement. Thus, the signal we use for the backtest is not the same as the signal we used in charting. Create this new signal for backtesting in one of two ways:
The signal will appear in the signal list after it is created. Note: You can filter the signal list by typing the first few characters of signal names into the “Find” box below the Signal list. If you name all your signals for a system with the same prefix, such as IBx2_long, IBx2_short, etc, then you can filter for “IBx2″ and just see the signals you need for the current backtest project. You will then need to create a similar signal for the Short side of this strategy. Create a Trading RuleOnce the entry signals are in place, we create Actions for these signals. How many to buy? At what price? Click once on an entry signal you created, in the signal list. Choose the appropriate Action from the column to the right. For short entry signals, do not choose “Sell” – rather, choose “Sell Short.” Choose the number of contracts you plan to trade with this Action, and specify “Contracts” or “Shares.” Note: if your strategy is an all-in/all-out system, without multiple scale-outs, then the profit per-share (or per-contract) is going to be the same regardless of how many contracts you choose here. For today’s backtesting, enter any number you want, but make sure you use the same number for each entry and exit signal. For the “Rule Price,” select V#1 since our signals will set this variable directly. For Periodicity, choose “one minute.” Since it is easy to acquire several months, or even years of one-minute data, we recommend this for backtesting signals such as those used in this system. For most strategies, this is sufficient. If you use Tick data, you have limited access to how far back you can backtest since it is difficult to acquire vast amounts of Tick data. Additionally, the backtesting can take much longer as it much look at thousands of times the amount of data. The “Rule Marker” and the “Intra-bar” check box do not apply to backtesting; these settings are for charting the rule after it is created. This optional step lets you verify where the rule would normally trigger. As we’ll see in a moment, you can do this more comprehensively by sending the entire trading system to the chart instead. Press the Add Rule button and your new rule will appear in the Trading Rules list. Do this again with the other entry signals in your system. Create the Exit RuleFor backtesting purposes, we will have our strategy exit at the close of the last bar of the session. First, click “New” above the signal list. Enter the following simple RTL code to specify the last bar of the day: POS = 1 When you save this Signal under a desired name, it will prompt you for the POS token settings. Choose Bars from End of Session. This means that we exit 1 bar from the end of the day. You only need one signal, even though we will create two exit rules from this same signal (one to exit the Long, another to exit the Short). Create the long exit rule in the same way: click the new exit signal in the list, choose the number of contracts, and choose Sell. Use “Close” as the Rule Price, and select 1-minute Periodicity. Repeat this process again for the short exit rule, using “Cover Short” as the Action. The program will offer a warning that you are adding the same signal more than once; this is fine. When you are finished, you will see all the rules: [caption id="attachment_1561" align="aligncenter" width="499" caption="Click image for a larger view."] Trading Rules List OrderThe order of the rules in your trading rules list can be very important. When the backtest runs, it studies each bar in succession, and evaluates all your trading rules in order, from the first rule to the last. If you have an Exit rule above an Entry rule, and the logic of your RTL code permits it, then it is possible for the system to generate an Exit followed by a new Entry all on the same bar, if they are in this order. If, however, you had the Entry above the Exit, this would not happen on a single bar. In this system, both our entry signals and exit signals have a Time attached to them, thus the order of the rules for this case is arbitrary and makes no difference. But for more complex systems, it can be quite important, so keep this in mind. Also, the system considers the current portfolio status of Long or Short when it studies each rule for each bar. If the system is Long at a particular point in time, it will ignore all entry signals and only look for a “Sell” signal that exits the Long position — or, if you have a BuyMore action, or a Reverse action, it will consult those as well. All Short signals or CoverShort rules are ignored while the position is Long. Setup the BacktestOne the system is built, you have to tell the software how to perform the backtest. Press the Setup Backtest button: [caption id="attachment_1568" align="aligncenter" width="500" caption="Click to see a larger view."] An explanation of the numbered items in this window:
Press OK to save this Setup. Run the Backtest
|








