“Initial Balance” Rotation Strategy: Part 5 – Backtesting 101

Posted in RTL by astoeckley on February 3, 2011

(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:

  1. Markets are constantly changing, and their flux alters volatility, participant’s attitudes and the way in which they behave. Backtesting cannot study the future; it focuses only on events that already occurred. A system that performed well in a previous year will not necessarily perform well in the following year. But backtesting does provide a meaningful clue on the overall success of a trading concept.
  2. Backtesting assumes that you traded your system rigidly, with great discipline, following the exact conditions of the trading rules without error or improvisation. In reality, discretionary traders rarely react this way, unless they strictly buy and sell from computer-generated signals.
  3. Market “slippage” prevents you from consistently entering or exiting at precisely the specific levels included in backtesting. You must consider this when studying the profit reports from a backtest. Real results, even if followed rigidly, are usually a bit different than a backtest suggests.

However, despite this caution, backtesting grows in popularity and can effectively contribute to your work — as long as you maintain this perspective.

Exit Signals

So 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:

  1. Close the entire position at a specific profit target
  2. Close the entire position at a specific price level or event determined by a signal, regardless of the trade’s profit at that time
  3. Scale out, by closing parts of the position at different times, as it becomes more and more profitable
  4. Stop-loss: closing a position after a maximum loss threshold is reached
  5. Time target: close a position at a specific time (such as end-of-day)

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 System

To 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 Signals

We 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:

  1. If a similar chart signal already exists, find it in the signal list, double-click to open, then press Save As right away and give it a new name. Make your edits with the above SET statements and Save again.
  2. If creating the signal from scratch, press the New button next to Signal (not the New button at the top of the window, which is for starting a whole new system).

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 Rule

Once 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 Rule

For 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."][/caption]

Trading Rules List Order

The 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 Backtest

One 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."][/caption]

An explanation of the numbered items in this window:

  1. You can backtest on a single symbol, or an entire quote page. For example, you could test against the entire Dow 30 stocks, allowing for multiple entries per day. Here, we are testing the E-Mini.
  2. If your strategy allows for multiple purchases throughout the session, you can specify the absolute max size of exposure. This does not apply to our current backtest.
  3. Commissions are a very significant part of trading results. There are two ways to approach commission costs in a backtest:
    1. Leave them at zero, and then subtract them yourself when you look at the “Average Profit Per Share” of the backtest report. If your average profit is $20 per contract, and your round-turn commission per contract is $5, then you know your actual profit results are $15 per contract. This is the easiest solution.
    2. If you wish to have the commissions automatically calculated into the backtest, you have to break it down in the following ways: Are you paying a flat rate for the trade, regardless of its size, or are you paying a per-share/per-contract commission? Are you paying a single commission for the entire round-trip, or do you pay a commission for both entry and exit? For futures traders, you typically pay a single round-turn commission for each contract. These Commissions fields are flat rate, unless you enter a comma. If you are not on a flat rate commission, your Entry is 0 plus a comma plus your per-contract fee. Then your Exit is also zero. So “0,5” would be a $5 commission per round-turn. Or you could enter “0,2.5″ for both Entry and Exit, to break it up equally.
  4. How far back do you want to test? You can specify an exact start and end date for the backtest, or go back a set number of days from the current day. You must have price data downloaded into MarketDelta for the time period you are studying. Use the Download button on the main MarketDelta toolbar and specify 1-minute data and as many days as you require.
  5. Our strategy depends on regular trading hours (RTH), so select Session 2. Remember, our exit is 1 bar from the close of the day, which would mean at 4:14 pm Eastern Time. You could optionally select Session 0 if you want to exit at 3:59 pm, when the stock market closes.
  6. Choose which backtest reports interest you. The “Average Profit Per Share” and “Total Net Profit” are on the Day by Day report. Where it says “Report Round Trips As”… This is important! Most day traders think of an entry and an exit as a single contract transaction. Choose “1″ if this applies to you. Otherwise, the report will show half your net profit per share, since it breaks it up between the entry and the exit.
  7. If you already ran a backtest and just want to see the reports again, click Display Latest Reports so you don’t have to wait for another backtest to complete (which can sometimes take a long time).

Press OK to save this Setup.

Run the Backtest

Finally, press the Backtest button to run the backtest.

When completed, the reports pop up. According to the Day by Day report, a 250-day backtest for Session 2 of this strategy reveals that a basic end-of-day exit for only 2 contracts yields $3070 of net profit when accounting for $5 round trip commissions:

The report shows a positive net profit, including commissions. Now, considering our strategy, it is fairly clear: markets are rotational indeed.

We will have much more to discuss on backtesting in coming articles.

Note: All backtesting examples are for illustrative purposes only and not a recommendation to trade these signals.

Visualize the Trades

Open any chart, and set it to the same session as your backtest. Then, in the backtesting window, press the Send to Chart button at the bottom. The TSYS indicator is added to the chart. It shows every trade in the backtest, when you entered and when you exited. Colors denote profits and losses during the trade:

Or, you can add the Trading System indicator to any chart you wish, and select your new system.

0 Responses to “Initial Balance” Rotation Strategy: Part 5 – Backtesting 101

No comments.


Add a Comment