Anticipation or Prediction?

Posted in User Tips by tharnett on March 22, 2011

It may seem like semantics, but I invite you to consider the not so subtle differences between the two words in the title. Prediction is about a belief that something WILL happen. Anticipation is being READY for what might possibly happen and having a plan for what to do. This shift in attitude can be difficult to accept because prediction doesn’t require much work, while anticipation does.

Traders should be very familiar with these terms. Each day we are faced with many opportunities, but they are only opportunities if we are prepared for them. Simply trying to predict the outcome once price arrives at a particular level is meaningless. The Footprint chart provides way to anticipate when a market may pause or reverse based on some simple patterns that only the Footprint can show. This helps anticipate market moves because it is focuses not on price alone, but on volume and order flow which drive and support price moves. It is this interplay between price, volume, and order flow which the Footprint visualizes so well.

Two helpful resources for learning more are the FAQ showing some “Meaningful Footprint Patterns” and our PDF Guide to Reading the Footprint Chart.

“Initial Balance” Rotation Strategy: Part 9 – Optimization

Posted in RTL by astoeckley on March 18, 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.)

What is truly the best profit target to wait for?

  • Do you get out after 3 points of profit or hold on for 6?

What is the best stop-loss level to maximize long-term profits?

  • Should you exit after a 2-point loss or give your strategy much more breathing room?

These are the type of questions that Optimization can answer.

Whereas “Backtesting” is taking a strategy and testing it against historical data, “Optimization” is tweaking a strategy against historical data to see how you could have increased profits, minimized losses and changed nearly any quantifiable aspect of the strategy itself to make it as successful as possible… in the past.

An optimization is basically a large set of multiple similar backtests. The software plugs in values for any variable you wish, such as the profit target, the stop-loss level, or other components of the strategy, and runs a backtest. When that test completes, it runs it again with slightly different values for these variables. It can run this hundreds or even thousands of times, called iterations, and then it reports on which combination of values offered the best rewards.

Today we will show you how to take our existing Initial Balance Rotational Strategy and optimize it to find the best profit targets and stop-loss values for an all-in, all-out trading style with this strategy.

Note: It is important to understand that optimization is, by definition, curve-fitting. It alters a strategy based on what would have worked in the past. This does not at all necessarily mean that the most ideal settings for previous periods in time will also be the most ideal settings for the present or future. But the entire goal behind backtesting and optimization is to develop ideas, and for that this can be useful information.

We currently have 9 rules in our system:

  1. The Main Rule limits the strategy to 1 trade per day
  2. The Long entry rule buys at the 2xIB Low
  3. The Short entry rule shorts at the 2xIB High
  4. The Long exit rule; after a specific target is met
  5. The Long stop-loss rule; after a maximum loss occurs
  6. The Short exit rule
  7. The Short stop-loss rule
  8. End-of-day exit for Long positions, if profit and stop targets are not met
  9. End-of-day exit for Short positions

We will not add any more rules to this system today. Instead, we will tweak these rules to make them ripe for optimization.

We want to optimize for the best profit target and stop-loss parameters. Currently, these numbers are hard-coded into the rules. For example, the Long exit rule is:

HI >= ENTRY+5 AND SET(V#1,ENTRY+5)

This means that the system exits the trade when prices reach our Entry price plus 5 points; thus, we get out at a 5-point profit.

The Long stop-loss rule is:

LO <= ENTRY-2 AND SET(V#1,ENTRY-2)

If we are long and the price drops 2 points below our entry, we exit. This is also hard-coded into the RTL itself.

The Short rules are just mirror images of these two Long rules.

The first step is to remove these hard-coded values and replace them with user variables. This will allow the optimization engine to substitute different values for these levels as it process multiple iterations.

In every instance where you see a specific value that you wish to optimize, change it to a corresponding V# number. For example, profit targets could be V#1 and stop loss amounts could be V#2. You do not want to use V# numbers you already use elsewhere in the system, such as V#1 which is used for the actual entry rule and V#2 which is used in the logic to limit the trades to once per day, or use any V# numbers that you use in your charts or elsewhere in the program.

We will use V#3 for our profit targets and V#4 for stop-loss values.

The Long exit rule will now be:

HI >= ENTRY+V#3 AND SET(V#1,ENTRY+V#3)

And the Long stop-loss rule is:

LO <= ENTRY-V#4 AND SET(V#1,ENTRY-V#4)

The Short rules are also similar.

Be sure to press Save on each RTL window after you edit the code.

If you were to run this Backtest with these modified rules, you would not get meaningful results because the Backtest itself would have no values to insert for these variables. The Optimization engine will insert values, but not a regular Backtest. So we must add new RTL for the “Main” rule that sets these variables for a regular backtest.

Our Main rule currently says:

IF (POS=1) THEN (SET(V#2,0));
IF (POS_SIZE != 0) THEN (SET(V#2,1));

We will add one line to this:

IF (POS=1) THEN (SET(V#2,0));
IF (POS_SIZE != 0) THEN (SET(V#2,1));
IF (CTX != CTX_OPTI) THEN (SET(V#3,5) AND SET(V#4,2));

This line uses the RTL Token “CTX” which means “System Context.” We know from last time that “!=” means “not equal.”

In plain English, this RTL statement means,

“If the system context is not optimization, then set V#3 to 5 and set V#4 to 2.”

This means that if we run a backtest, which is not an optimization, it will plug these values into these user variables so we have the same results as we did before when these values were hard-coded into the rules themselves.

It is possible for this statement to contain many many V# variables if you are optimizing many different parts of a strategy. Thus, you may find it useful to add notes in this Main rule that identify which variables stand for which values. You could add this:

IF (POS=1) THEN (SET(V#2,0));
IF (POS_SIZE != 0) THEN (SET(V#2,1));
IF (CTX != CTX_OPTI) THEN (SET(V#3,5) AND SET(V#4,2)); /* V#3=profit, V#4=stop-loss */

In RTL, you can start a comment line with /* and end it with the mirror of this, */ — everything between these two symbols is entirely ignored by the program and can be in any format you wish.

Save the rule, then save the system itself, by pressing Save at the top of the Trading System window.

If you press “Backtest” now, you should see the exact same identical results as we had before we made all these changes. But now the system is ready for Optimization.

Before you run any optimization or backtest, make sure you have enough historical data on file to cover the settings in the “Setup Backtest” area. If you do not, you must download data before running either a backtest or optimization. You will generally have better luck getting many years of data if your system uses minute history rather than tick history.

Press the Optimize button on this window, and you will see the Optimization screen. In this new window, you specify the variables you want to optimize. In this case, they are V#3 and V#4. Then you set the testing range for each variable, and the increments to test within this range. So we might test all combinations of profit targets and stop-losses from 1 to 10 points, in 1-point increments. As you make these changes you will see the total number of iterations building at the top of the screen. You can then adjust what type of information will appear in your optimization report and, most importantly, how you want to prioritize the sorting of these different tested combinations. For most people, sorting by either “Net Profit” or “Average Profit Per Share” will be the most useful. Note that the Net Profit option includes the commission fees set in the backtesting settings themselves, as described in an earlier part of this series.


(Click image for a better view.)

When ready, press Optimize.

The time it takes for the optimization to complete depends on these factors:

  1. How many days of data you are testing. This is set in the backtesting setup.
  2. The periodicity you are testing, as this affects the number of actual bars the system will study. A 1-minute periodicity, as set in the system screen, will take much longer than a 10-minute periodicity.
  3. The number of iterations; this is based on the total number of V# variables you are testing and the number of data values in each V#. Increasing your increment (step) amount will reduce the number of iterations, but give you less comprehensive data for the test.

Complex backtests over many years of data with small periodicities and large iterations in the thousands can take hours to run.

As the test runs, it alerts you to its progress and how much time is remaining, as well as other information:

Our results are below:

Last week, we noted that adding a 2-point stop-loss cut our 5-point profit target backtest’s net profit in half, from approximately $5,000 to around $2,500. Today we see that a wider stop of 9 points and a profit target of 3 points has provided the best results so far, nearly $7,000. However, it is worth noting that other combinations below it in the list, such as 6 point stops with 4 point profits, also perform well, so there is a good mix here of different targets and stop-loss values to accommodate different trading styles.

You could then take these values for V#3 and V#4 and use them in the Main rule instead of the previous values of 5 and 2. If you ran a regular backtest after doing this, you should see the same results as this optimization report shows, but then you could study the individual trades as well.

If you modify any rules in your system and wish to re-run the optimization, remember to always Save the system first.

Printing in MarketDelta

Posted in User Tips by rmular on March 17, 2011

Currently, Printing in MarketDelta is limited to legacy printers and will not work with the modern printers of today. If you are unable to print from MarketDelta with your printer, you will need to install a PDF printer.  Fortunately, most PDF printers are free and are rather easy to get setup. This article contains a walkthrough on installing a PDF printer called CutePDF and how to successfully print from MarketDelta using this software. CutePDF is freeware software and is no cost to the end user. Although there are many other possible PDF printers that can perform the same functions as CutePDF, we have chosen this version because of it’s tested compatibility with MarketDelta.

Step 1: Download CutePDF (Freeware) at www.cutepdf.com



Step 2: Double-Click the CuteWriter.exe install file

Step 3: Press Next

Step 4: Accept the Agreement, Click Next

Step 5: Uncheck all options to prevent advertising applications from being installed (unless you prefer those option(s) checked)

Step 6: Click Install

Step 7: When prompted to install the PS2PDF converter, Select Yes.

Step 8: After the PS2PDF converter is download, the install will automatically continue

Step 9: Once the Installer has disappeared, the installation is complete

Setting Up CutePDF in MarketDelta

Step 10: Open MarketDelta

Step 11: Click File > Page Setup

Step 12: Click dropdown to select CutePDF printer

Step 13: Select “Portrait” or Landscape” depending on how you want to view the orientation. You will be required to open the “Page Setup” menu to change back and forth between each orientation

Step 14: Click on a chart that you want to print

Step 15: Click File > Print or ALT + P to print or Right-Click on a chart and select “Print Chart”

Step 16: After the CutePDF “Save As” box pops up, save to a folder on your PC to open for printing

Further Considerations:

A PDF reader will be required to open the PDF’s that are generated from the CutePDF printer when printing from MarketDelta. We recommend that you download Adobe Reader (www.adobe.com, click on Adobe Reader link) to download for best compatibility.

Additionally, Printing of charts in MarketDelta even when using CutePDF may not produce a perfect print. Circumstances may vary based on how many elements are present on a chart when printing. A large amount of technical indicators and other chart elements may cause the prints to look pushed together. When printing a chart, resize the chart so that the window takes up as much of the screen as possible.

“Initial Balance” Rotation Strategy: Part 8 – Stop Loss

Posted in RTL by astoeckley on March 9, 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.)

Our trading system currently has 7 rules:

  • 2 entry rules; one for long, one for short
  • 2 profit target rules, for each long or short
  • 2 end-of-day exits, for each long or short
  • 1 “Main” rule that currently limits the number of trades per day to just 1

You can see all seven rules in our Trading Rules list for this system, which we have already setup:


Once a system enters a trade, long or short, it ignores all other entry rules (except in the case of multiple entries, which are a special case that we will discuss soon). The system only considers the exit rules once a position is active. If a profit target rule is met, the system exits at that price, for a profit. If this target is never met, then our system waits until the end of the day and exits then.

Most traders add additional exit criteria to their trading, “stop losses,” which limit the amount that a position can move against them before exiting and accepting a loss.

Considerations

It is important to understand the reality of this type of exit rule. While it may seem to positively benefit a trading system, by reducing the size of your losses, it can also whipsaw you out of potentially winning trades. For example, a trade might move against you by 3 points before reversing and leading you to profit. If, however, your stop loss takes you out at a 2-point loss, this trade is a loser instead of a winner.

On the flip side, a trade that moves against you by several points could have been a much larger loser without a stop loss.

There is a very delicate balance that traders must carefully consider when choosing stops that work for them.

Today, we will build the stop loss rules into our system. During our optimization phase, which is our next article in this series, we will analyze what are truly the best profit and stop loss levels for our strategy.

The Rules

An exit for a specific loss amount is not unlike an exit for a specific profit amount.

You can either create new signals from scratch, or just open the existing profit target rules and Save As with basic modifications.

For example, we previously built this rule for exiting a long at a 5-point profit:

HI >= ENTRY+5 AND SET(V#1,ENTRY+5)

We set V#1 as the Rule Price so we got out right at that price, even if it was intrabar.

Here is the code for a similar long stop-loss rule:

LO <= ENTRY-2 AND SET(V#1,ENTRY-2)

This provides a 2-point stop level. The stop loss for a short position would be:

HI >= ENTRY+2 AND SET(V#1,ENTRY+2)

When you add the stop signals to the rule list, you may want to use a quantity of “All”. This way, if you later create multiple exits, entries, etc, the stop will always exit whatever position you have, regardless of its amount:

Very Important: As always, the order of the rules in your trading system is crucial to its behavior and backtest results. Remember, the rules are evaluated from the top rule to the last rule for each bar on the chart. If you place your new stop loss rules after the entry rules, it is conceivable that you could enter and get stopped out all on the same bar. If instead your entry rules are the last rules in your list, then your exits, whatever they may be – targets, stops or end-of-day – will always be on at least the bar following your entry.

Backtest Results

Here is a sample trade that shows the stop working:

If you do a lot of backtesting, you will see that stops very often reduce long-term profitability. Here is same 250-day backtest we’ve performed before, using 2-point stops we’ve created here:

This is about half the profit we had before adding stop loss rules for 2 points.

If you want to test the strategy by removing the stops, one way to do so without erasing all your RTL code we’ve created is to simply change the stop to something particularly large, like 50 points. Since it will usually never get triggered, you can compare a more meaningful stop to results without stops, but still keep the stop rules in place.

Avoiding Re-Entry after a Stop Exit

While I won’t go into all the different programming possibilities for different scenarios, here are some issues to consider as you code stop rules.

If you get stopped out, the conditions for an entry may still exit. If you do not limit your trades, as described in our last article, our current backtest system will immediately get you back into a trade, because the entry rules we created simply require that the low has reached or extended beyond the 2X IB level. Thus, when the stop is triggered, the entry rule is still valid and the system gets you back in.

As a more advanced technique, you could allow for multiple entries except when a stop order is triggered. You could add an IF/THEN statement to the stop loss rule that sets a V# to a number, similar to what we did last week, and then use this V# as the condition to limit new entries. It would be similar to the code used in the Main rule last week, but placed inside the stop rules instead of a Main rule, using the condition of the stop rule as the IF condition.

Alternately, and without an IF/THEN statement, you could instead change the entry rules to only enter when prices are, for example, within a 1-point range extending beyond the 2XIB. Currently our entry rules are not this specific. This way, if the stop rule was beyond this range, it would not re-enter immediately after the stop exit, but it could re-enter if prices came back to the 2XIB.

There are  many different ways to code a system like this, and it is open to individual creativity and requirements.

“Initial Balance” Rotation Strategy: Part 7 – Trade Frequency

Posted in RTL by astoeckley on March 2, 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.)

When you consider the system we have built thus far, it is apparent that once a position exits for a profit target, the system may still enter the same trade again on the same day, as we have not created any RTL code that would suggest otherwise. Just because you get your profit does not mean the market will not return to your entry level. Currently, the system enters the trade again. Many traders may not wish to enter the trade again, although in some cases it might actually be effective in overall backtests. You may wish to test both scenarios.

Consider this trade, created from the backtest. Here I have changed the profit to just 1 point to make it easier to identify:

So, how to limit the system from only taking one trade per day?

Backtests often require rules that do not actually take actions, but instead set a framework for managing the system. This will be an important part of optimization, which is the subject of a future article.

We can create a rule at the beginning of our trading rules list that is processed at the beginning of every bar, before any other rules are studied. This introduces some new RTL techniques and tokens:

  1. POS_SIZE – This token is a variable that equals your current position quantity. It is positive if you are long and negative if you are short. If no position is active, it equals zero.
  2. IF/THEN – This is a standard technique in nearly all programming languages, and is called a condition. It lets you run an algorithm or process only if specific criteria are met.

Here is our “Main” Rule:

Note that a semicolon is required to separate multiple lines inside a single trading rule.

The symbol “!=” means “not equal.”

Now, we have to alter our entry rules to consider the current value of V#2 before taking the trade:

All you have to do is add “AND V#2=0″ to the end of each entry rule so it will not execute unless no other trade has occurred during the day, as per our Main Rule.

Here is our long rule now:

SET(V#1,(SESST_LOW  – (SESST – SESST_LOW))) AND LO <=V#1  AND TIME >1030 AND TIME < 1500 AND V#2=0

Once you make these changes to the long and short entries, and add the Main Rule, you’ll see that the same chart signal shown above now looks like this:

If you run a backtest for the same 250-day period using this tweak of limiting the trade once per day, you will see that we have further improved our net profits over this time, by limiting trades to once per day, and taking a 5-point profit target or end-of-day exit:

As we will discover next time, you need to consider these limits in strategies that incorporate a stop loss; after all, you don’t want to get in immediately after you are stopped out!

We will discuss stop loss rules in the next article.