“Initial Balance” Strategy: Part 12 – Alternate Methods

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

Today, we demonstrate the flexibility of RTL and show an entirely different method you could have used to code this system, yet have the same results. RTL is a basic programming language, and as such it can take any form desired by the programmer. The programmer’s creativity determines the flow of the system’s logic, and different programmers may approach the same task in different ways.

To demonstrate this, we will alter how the system triggers all the exits: the stops, scales and final targets. You may find that today’s methodology is more elegant than the prior methods introduced in this article series. No method is better than the other; they are simply different approaches.

Here is the primary change we will make to the system:

Instead of calculating the exit levels within each exit rule, we will instead determine all our exit points up front at the moment we enter a trade, as part of the entry rules. This will make our separate exit rules very simple and easy to understand. It also lets us adjust the stop levels to breakeven without actually adjusting the stop exit rules. Before, we had a total of 4 stop-loss exit rules. 2 for the long trades and 2 for the short trades. One stop level was the initial stop set at the time of entry. Another stop rule set the breakeven level for when the trade exited at its first profit scale. But now we will effectively combine these two stop levels into one single rule and adjust the stop-loss level on the fly, letting us have fewer rules in our system.

The Main Rule

We are going to introduce 3 additional V# variables. At this point, it is a good idea to define all your user variables in the Main rule of the system as a comment. This makes it easier to troubleshoot your system later, or for other programmers to understand how you coded the system. Our new Main rule shall look like this:

We have not actually made any change to the logic of this Main rule. But we have added numerous comments to the end that serve as a form of documentation for our system. Most notably, we have declared that our subsequent rules will use three new user variables, V#6, V#7 and V#8, and each of these will hold the actual price level of our exit targets.

Delete the prior Breakeven Stop Rules

As mentioned, we will not need the two old breakeven stop rules, so you can delete them now. We will still keep the two other general stop rules we created.

Special Note: Because our new stop rules are going to cover all stop scenarios, you might want to use this method for complex stop scenarios where you have multiple adjustments to stops, such as with trailing stop strategies. This would greatly reduce the number of rules you might need in your system since you won’t have to code multiple stops as individually separate trading rules. This will be more obvious below.

The New Entry Rules

As part of our Entry rules, we will set all the exits in advance, and assign them to our three new user variables:

Short Rule:

SET(V#1,(SESST + (SESST – SESST_LOW))) AND HI >= V#1  AND TIME >930 AND TIME < 1400 AND V#2=0 AND SET(V#6,V#1-V#5) AND SET(V#7,V#1-V#3) AND SET(V#8,V#1+V#4)

Long Rule:

SET(V#1,(SESST_LOW  – (SESST – SESST_LOW))) AND LO <=V#1  AND TIME >930 AND TIME < 1400 AND V#2=0 AND SET(V#6,V#1+V#5) AND SET(V#7,V#1+V#3) AND SET(V#8,V#1-V#4)

The underlined portion represents the new parts of this rule. Note that the math used for each of the three user variable levels is the same math we previously used in our individual exit rules.

The Stop Rules and Final Exit Rules

Since our exit rules no longer need to contain logic for the actual math needed to calculate the price level, we can remove that logic from those rules and make them particularly simple rules. Consider our new Long Stop and Short Stop rules:

Stop rule for long -

Stop rule for Short -

Pretty clear and concise!

The final exit rules are equally as basic:

Long exit -

HI >= V#7

Short exit -

LO <= V#7

Rules don’t get much more basic than this!

Rule Prices

We have another efficiency that is now built-in to this system: except for our entry rules, we no longer need to calculate and assign the Rule Price to V#1 using the SET token because each of these rules already has a user variable calculated for the price level, something that didn’t exist before. So change the Rule Price for these rules to match the user variable of the rule itself, as follows:

You can use the “Modify Rule” button to do this.

The Scale Out Rules

Since we are adjusting stop-loss levels on the fly, our scale out rules need to accomplish two things:

  1. They need to obviously contain the logic to exit at the first profit target, and do so only when we have not taken any exits before (this is already in these rules).
  2. They now need to adjust the stop-loss level for V#8 so our single, combined stop rules will function as expected in all cases

We will use the IF…THEN logic to reset the V#8 variable. Here is the new first-exit rule for a long trade:

The second line in this code should be obvious; like our other exit rules, it is simplified for V#6 but must still include the POS_SIZE token from previously; and the Rule Price is set to V#6.

The first line basically repeats this logic as a condition; IF the condition is true, THEN it resets our stop-loss level, V#8. Note the semicolon at the end of this line; this is required in RTL (and most programming languages) to signify the end of one statement before beginning another.

Here is the short scale-out rule:


That’s it! We now have a new RTL environment for the exact same strategy. Here is all that it accomplished:

  1. It removed 2 rules from the system.
  2. It significantly simplified our exit rules and let nearly all the parameters of the strategy be laid out in just two rules, the two entry rules.
  3. It makes the program more flexible should you choose to move stops around even further; no new stop rules will be necessary.
  4. It documents the user variables in the Main rule.

Finally, press the Backtest button, and………..

Same exact backtesting results as in Part 10 of this series, which uses different RTL for most of the rules.

In Conclusion

This blog series has demonstrated the power of RTL and introduced a wide range of techniques for many different scenarios. We hope you have enjoyed it and will continue to experiment with RTL for your own ideas now that its possibilities are made obvious. Your strategy may be entirely different than the IB Rotational Strategy discussed in this series, but the programming concepts and techniques are the same.

“Initial Balance” Rotation Strategy: Part 11 – Realization

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

Realization is a simulation of real trading. It is not just backtesting, it is the actual trading process re-enacted for the most accurate picture of a strategy’s past results.

We now have a trading system based on the Market Profile’s Initial Balance range that has backtested well in many different scenarios. Our current incarnation allows for two exits to our trade, so we can “scale out” and move our stop-loss levels to break-even (the entry price) after the first profit is taken.

In our last article, we performed the backtest for the same 250-day period as in all other tests and, after $5 round-turn commissions, found a net profit of $3,970 for 2 contracts when using a 2-point stop, 5-point total profit, and a 2-point scale-out.

Using optimization, we can see that we would have increased profit substantially if we instead went for 15 point profits, a 9-point stop and a first scale of 4 points.

Here’s the problem

It is fun to know today what would have worked in the past. But last year, if we had started trading this system, we would not have had the luxury of optimizing it from price action that would occur in the future. We only know what has already happened, not what we would have done with knowledge we didn’t yet have. Just because we know now that the prior year worked well with 15 point profit targets, does not mean that we would have actually used this in our strategy at that time — because we didn’t know that this was going to be optimal in the months that followed.

It is for this reason that optimization cannot show you potential profits for the future, even if it can help you structure a strategy and give you ideas.

Realization

However, MarketDelta does offer a featured called “Realization” that accurately captures the real results you would have enjoyed in the past, if you optimized, traded off that optimization, then optimized again later, and continued to adjust your strategy based on recent market conditions. In this way, you can see the effects of optimization on real-time performance.

Realization works as follows… You set an “Optimization period” — this is the amount of time to include in each optimization. Then you set a “Realization period” — this is how often you optimize based on prior data and alter your strategy based on the results of that optimization.

MarketDelta runs an optimization, then performs a simple backtest using these optimization results for the period following the optimization. Then at the end of that period (the realization period), it runs another optimization and uses those results for the next backtest. This process continues until all the data is tested.

It effectively captures how you might have traded a system and only uses optimization results for past data when calculating the actual backtests on future data.

To set it up, just press the Realize button in the Optimization window.

Make your settings, then press Realize. For these settings, we get these results:

This is a real result that could have occurred without knowing the prices of the future. Still profitable, but the reality is obvious: not as profitable as a straight-up optimization for the entire period. If your system can perform well in the Realization phase, you have many reasons to be more confident.

If we alter our realization for different trading behavior, we would alter our profits over the 250-day period:

These settings include more data in each optimization along the way, and then re-optimize every 30 days. If you traded this system, you would not change your profit targets and stops as often, and these levels would be based on more price history. Here are the results for this realization:

Somewhat comparable.

Now let’s try a test for more aggressive changes over time to the strategy. Suppose we optimize every 10 days based on the most recent 10 days, and then use those results in trading for the following 10 days, before repeating this procedure:

Keep in mind that your strategy is likely to change much more dramatically every 10 days since it is using optimal results from a short testing period for each change.

Here are the results:

Here are our best Realization results yet; so, at least for this strategy, frequent optimization and strategy changes proved profitable over the long term.

This would not necessarily be the case with other strategies.

“Initial Balance” Rotation Strategy: Part 10 – Scale-outs

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

So far in this series, we have built a trading system based on an all-in, all-out trading style. That is, we enter all at once, and then exit the position all at once. However, many traders use a “scale-out” method whereby you gradually liquidate a position as it becomes more and more profitable. This allows you to lock in small partial profits and hold on for larger profits all in a single trade. Of course, it requires that you trade more than a single contract so you have “bullets in the gun,” so to speak, as the market moves more and more in your favor.

Today we will add to our existing system by creating rules and changing signals so we can take advantage of this method. Our system is already built to buy and sell 2 contracts. We will add one scale-out so each contract exits at a different time.

Another important benefit: After the first scale-out, most traders move their stop-loss to a break-even level (the original entry) so that losses are not possible on the trade once the first profit is locked in.

We shall create the rules that add these new scale-outs for long and short trades, and additional break-even stop-loss rules for each, for a total of 4 new rules.

Here are the steps to pull this off:

1. Change all existing rules, except for the entry rules, to a rule quantity of “All.” Since we have either partial or full positions now, this is the easiest way to make sure that, for example, the final (second) scale out isn’t set to liquidate 2 contracts when only 1 contract remains. We will keep the current exit rules as our second scale-out rules. To modify each of the affected rules, click on it in the Trading Rules list, change the quantity in the upper right, then click the Modify Rule button.

2. Change the Main rule to accommodate a new user variable should you choose to optimize this system, as per our last article in this series. In this example, I’m assigning V#5 to the profit target for the first scale-out (V#3 is already set as the main profit target, for our second scale-out):

Here I am keeping 5 points as the main profit target, and using 2 points as the first scale-out, with a 2-point stop-loss.

3.  Create the two new scale-out rules: one for long trades, one for short trades. The easiest way to do this is to open the existing SELL rule (for exiting a long) and immediately use the Save As button to save as a variation, such as SELL_SCALE. Then we use the POS_SIZE token (“position size”) as part of the signal, so this rule only triggers when you are carrying the full position. This allows the system to trade past this level after a scale-out and return to this level without exiting again before the second scale is reached. Change the user variable to V#5 instead of the V#3 in the second scale signal. The POS_SIZE token is always the value of your current trade size. It is 2 when you are long 2 contracts, zero when you are flat, and -2 if you are short two contracts. Here our our two new scale-out rules:

When you create rules from these new signals, use a rule quantity of 1 since you are only exiting part of the position.

4. Create two new stop-loss rules, where the stop loss is set to the ENTRY with a rule quantity of ALL. Also, use a POS_SIZE of 1 or -1 (depending on long or short trade) so the stop loss only triggers after the first scale out is completed:

Our 13 trading rules for this system after making these changes:

Last time we tested this all-in, all-out strategy using 5-points profits or 2-point losses (or end-of-day exit). The result from that backtest was a 250-day profit of $2420. Adding these new scale-out rules, we see a significant increase in net profit as losses are trimmed and profits locked in:

It’s worth noting, however, that the system is still not as profitable as it was prior to introducing stop-loss rules at all, as we noted in Part 7.