Article Contents
Print

Creating The Trading Rules Using AutoTrend

Now we’ve selected the shares we may want to trade, all that remains is to define the Trading Rules that decide when and if a share should be traded. Like List Selectors, Trading Rules consist of two parts:

  • the first part specifies the type of rule (whether it buys or sells), the trade-size and, optionally, some attributes for the rule.
  • the second part defines the condition (i.e. criteria), that triggers the rule. When the condition is met, the rule is triggered. In back-tests this results in the trade being executed. In live sessions, it results in a signal being displayed for the share in the RuleTrader Live layout’s share list window.

When you have a moment, please review the Help article on How Trading Rules Are Evaluated. In the meantime, here are some important notes on Trading Rules:

  • Rules are evaluated in the order they are listed in the Strategy Rules tab of the Trading Specification dialog
  • In back-tests (only) rules that sell are evaluated before rules that buy, so capital from any sale is available for any buy
  • Once triggered, no further trading rules are evaluated again that day, for that share. This prevents sells triggering immediately after a buy, or vice-versa, for the same share
  • If the trade size for a triggered buy rule exceeds available funds, then it will not be traded in Test sessions, or signalled in Live sessions. This behaviour can be modified by the System SettingsShow buy trade-signals, even if there is insufficient cash for the trade‘ (Live sessions only) and ‘Actual trade-size must be at least __ % of the target size
  • Only long positions are supported. RuleTrader may allow short positions in the near future – if this is something you’d like to see please drop us a line at suggestions@ruletrader.co.uk

AutoTrend Does It For You

For the Trend Trader system, we’ll use RuleTrader’s proprietary AutoTrend feature. This automatically tracks micro, short, medium and long term price-trends that typically last less than a week, a few weeks, a few months, or 6 months or longer, respectively. Our algorithms automatically detect when trends start and finish for you, so you don’t have to spend your time drawing and reviewing trends in charts.

AutoTrend frames the trend with a price-channel consisting of a best-fit trend-line with two parallel confidence lines at 2 standard deviations on either side of the trend-line. This frame provides a simple way to automatically monitor various statistics about the trend including: growth rate, length, standard deviation and the price’s position in the channel. We’ll be using these to enter a position when the price is at a relative low and to exit when the trend breaks downwards.

AutoTrend’s named bands, illustrated below, will help us to locate the price relative to the trend-line (you can use the Channel Styles settings to change how price-channels are displayed). We’ll also use AutoTrend’s ability to track the lowest low price and highest high price in the channel, to determine when a low has been hit

Named Bands provide a simple way to determine the price's position in the trend-channel

The Buy Rule

The buy trading rule we’ll create uses sub-terms that look for a short term bounce back from a low price (relative to the trend’s channel), in a long term rising trend, provided the bounce back hasn’t gone so far that we’ve missed the opportunity:

‘Buy Rising Trend’ trading-rule signals ‘Open Position’ in the evaluated security, for the nominal trade-size, when: the ‘Long Channel Rising’ term is true, AND the ‘Short Channel Rising’ term is true, AND the ‘Was A Low Price’ term is true, AND the ‘Still A Low Price’ term is true; where:

  • ‘Long Channel Rising’ = a condition that is true if the evaluated security’s long channel’s trend today, is rising
  • ‘Short Channel Rising’ = a condition that is true if the evaluated security’s short channel’s trend today, is rising
  • ‘Was A Low Price’ = a condition that is true if the evaluated security’s lowest low price, up to today, has previously been in or below the long channel’s Lower Turn-Band (outer)
  • ‘Still A Low Price’ = a condition that is true if the evaluated security’s open price, as of today, is in or below the long channel’s Lower Channel (outer)

Let’s break these definitions down for you:

  • The ‘Open Position’ rule type indicates it’s a buy rule:
  • Available Trading Rule types are Open Position, Increase Position, Decrease Position, Take Profit, Close Position and Stop-Loss. Decrease Position and Take Profit are the same, as are Close Position and Stop-Loss. The different names are there to allow you to distinguish the intent of the trades in Trading Performance Reports
  • Open Position rules are only evaluated for shares without a position. Increase Position works with shares with or without a position, while all other types are only evaluated for shares with a position, as you’d expect. It is not something you need to test for.
  • The rule decides whether to buy the evaluated security. This is an important concept. The way RuleTrader works is that each day a back-test is run, or each time the Live session receives new data, RuleTrader evaluates the Trading Rules separately for each security in each Strategy‘s Trading List. For this purpose, each such security becomes the evaluated security. So wherever the ‘evaluated security’ is referenced in the Trading Rules, it is that security’s data that is used and it is that security that the Trading Rule is deciding whether to trade or not.
  • If the rule’s condition is met (i.e. the rule is triggered) then a signal is raised to buy the evaluated security in an amount equal to the nominal trade size, which is defined in the Trade Sizing tab of RuleTrader. During back-tests, this signal is then automatically executed, provided there are sufficient funds in the Trading Account(s). NB: Use of the nominal trade-size is optional; you can instead calculate a trade-size in a rule definition.
  • The rule’s condition is simplified by breaking it into four sub-conditions using the rule’s Private Terms, each of which must be met for the rule to trigger.
Creating The Buy Rule
  1. Navigate to the Strategy Rules tab in the Strategy level of the Trading Specification dialog (if you’re at the Shared level, go to the Strategy Selection tab and click Edit next to the ‘Trendy’ trading strategy to move down to the Strategy level)
  2. Select Create and click Do It! in the Strategy Rules tab to open the DEfT Wizard in create mode.
  3. Rather than creating the rule’s Private Terms first, as we did with the strategy’s List Selector definition (see here), we’ll do it the other way around this time, so we can show you how useful place-holders are. So enter the following to create the first part of the Trading Rule:

“Buy Rising Trend” → signals a tradeOpen Positionthe nominal trade-sizedon’t add attributestrue

  1. You should now be at the beginning of the second part of the trading rule, where its trigger condition is defined. As discussed, we’ll be breaking this down into a number of named terms, so select named condition term is. On the next dialog, you’ll then be asked which named term you want to use, which is problematic, as the only defined term in the drop-down is ‘Growth Shares’. This would allow you to test if the Growth Shares List Selector selected any shares today, which isn’t what we want.
Place-holders can be used instead of Named Term names that have not yet been defined

However, you’ll notice that <undefined condition 1> is also available, so pick this now (don’t worry if the number is different for you; it’s only there to make the name unique and is not important). As we noted earlier, the <…> delimiters indicate this is a place-holder term. It allows you to reference a term that doesn’t yet exist.

  1. On the next Wizard page you’ll see that <undefined condition 1> has turned into <condition 1>, in the sentence you’re building, indicating the place-holder is now reserved. We want this condition to be true, so pick that. We also want this and some other conditions to be true for the rule to trigger, so pick AND to start defining the next sub-term condition, which is referenced by picking named condition term is again. On the next dialog page the available options are now:
The undefined place-holder term has now turned into reserved place-holder, denoted as <condition 1>

So you can see that we can reference the previous <condition 1> place-holder that we created, or we can reference a new <undefined condition 2> place-holder. As we want 4 separate sub-terms, you should pick the new <undefined condition 2> place-holder.

  1. Repeat step 5 for the next two sub-terms. The sequence of entries you need to make are as follows. Try entering them using only your keyboard’s tab-key, down arrow and Enter key – you’ll find it much quicker than using your mouse.

true AND named condition term is <undefined condition 3> true AND named condition term is <undefined condition 4> true end condition

  1. After you choose end condition the definition will be complete and voila! RuleTrader automatically creates 4 place-holder definitions for you, in the Private Terms area of the Trading Spec dialog:
Default definitions are automatically created for each place-holder that has been reserved
  1. All you need to do now is click Edit next to each of those private terms and enter their definitions. As you do this, please note the following:
    • When you click Edit, the definition defaults to value. This needs to be changed to condition to conform with the type of sub-term the ‘Buy Rising Trend’ trading rule expects. Ideally RuleTrader would have done this for you. We’re working on it.
    • The basic structure of these conditions is to specify the thing you’re testing (a security’s value, condition or characteristic), which security’s data you’re testing (the evaluated security), the specific data you want to test (channel data), and the thing you want to test (either the channel’s trend-direction or the price’s position in the channel)

Note that the construct security’s value, condition or characteristic is, only allows a single term on the left hand side of the test but it allows a full expression on the right hand side i.e. you couldn’t test “2 * security’s profit + 30 > 50”, though you could test “security’s profit > 50 – 30 / 2”, which is the same thing. 

If you do want a test between two multi-term expressions, use the value expression is option (it’s not required for this tutorial)

    • RuleTrader maintains today’s and yesterday’s values for a number of channel data items, which can be useful if you want to see how the values have changed. In these definitions we use the today values

The sequence of entries to define each private term place-holder is:

<condition 1>

“Long Channel Rising” → conditiontrue ifsecurity’s value, condition or characteristic isevaluated security channel data is trend-directionlongtodayrisingend condition

<condition 2>

“Short Channel Rising” → conditiontrue ifsecurity’s value, condition or characteristic isevaluated securitychannel data istrend-directionshorttodayrisingend condition

<condition 3>

“Was A Low Price” → condition true if security’s value, condition or characteristic isevaluated security channel data isprice’s positionnamed band containinglowest lowtoday in or belowlongLower Turn-Band (outer)end condition

<condition 4>

“Still A Low Price”→ conditiontrue ifsecurity’s value, condition or characteristic isevaluated securitychannel data isprice’s positionnamed band containingopen → todayin or belowlongLower Channel (outer)end condition

And that’s it. One buy rule, nailed! Time to look now at our exit strategy.

The Sell Rule

Exiting a position effectively is a lot harder than entering it, in our opinion. But then we’re only simple souls. Having tried many different strategies the most effective we’ve found is to exit when the long trend breaks downwards.

The downside to this is that for the trend to break downwards, the price must have dropped, so it leaves money on the table. The alternative is to try and predict when the trend is going to break and hope to exit on the high side of the price-channel, before that happens. However, every way we’ve tried this (which is by no means exhaustive), it’s tended to leave more money on the table, as a result of leaving a perfectly good trend too early.

Another thing we’ve noticed is that when a price-trend breaks on the downside, the price can often retrace upwards, as it’s become over-sold. So we’ve refined the sell rule to require both the long and short trends to be falling before we exit. You never know; if the price is rising in the short term it may ‘push’ the long term trend back up, obviating the need to exit after all.

Sound familiar? This is of course the opposite of the buy rule’s two Private Terms: ‘Long Channel Rising’ and ‘Short Channel Rising’. So lets save some effort by using these. We just need them to be false instead of true i.e. the long and short channels are not rising, implying they’re flat or falling, which will do.

The snag is that these terms are currently defined as Private Terms of the ‘Buy Rising Trend’ rule and private means they are only available to that rule. Fortunately, this is easily fixed by cutting these terms out of the rule’s Private Terms area and pasting them into the Strategy Terms tab, where they will be available to any rule in our ‘Trendy’ trading strategy (if we were going to create additional strategies and we wanted these terms to also be available to them, then we’d paste them into the Shared Terms tab in the Shared level of the Trading Spec dialog).

So, time to learn how to cut and paste multiple definitions, at the same time:

  1. In the Trading Rules tab, click Edit next to the ‘Buy Rising Trend’ rule to access its Private Terms
  2. Tick the checkmarks next to the ‘Long Channel Rising’ and ‘Short Channel Rising’ terms, ensuring no other Private Terms are ticked. This selects the definitions the operation will work on
  3. In the Operation list pick the Cut selected option and click Do It! The required terms will be removed from the list
  4. Click Exit Wizard to return to the Trading Spec dialog and then navigate to the Strategy Terms button to open the Strategy Terms tab
  5. In its Operation drop-down, pick Paste and click Do It! and the terms will reappear
  6. Now we can access the terms that we’ll want, navigate back to the Trading Rules tab and create the ‘Sell Falling Trend’ rule using the following entries (note that this time we’re selecting the Close Position rule type and that we require the sub-terms to be false):

“Sell Falling Trend” → signals a trade Close Position don’t add attributes true named condition term is Long Channel Rising false AND named condition term is Short Channel Rising false end condition

  1. Finally, when you’re done, click Exit Wizard, then Back To Shared, then Exit and RuleTrader will start compiling your Trading Specification. The compilation process turns your English language specification into computer code, so your system will run at the full speed of your computer, without you having to learn anything about programming.

Now that you’ve created your first automated investment system, all that remains is to back-test it

Scroll to Top