Adding A Stop-Loss And Fixing Compile Errors
In this second and final part of the tutorial, you’ll learn how to:
- Employ best practice methods for evolving a trading system
- Create a variant of the Trend Trader system with a stop-loss rule added
- Use chart and Indicator Panel overlays to see what’s going on in your trading system
- Solve problems when they occur
- Batch-Test multiple variants of your trading system
- Archive old versions of your system
- Deploy your system for live trading
Test, Evaluate, Repeat
There are many ways to use RuleTrader. If you’re using it to data-mine market data for tradable anomalies, then this section will be of less relevance to you. If that is the case, then please feel free to skip to the next section.
However, if you wish to systematically refine an investment system for ever greater profits, then please read-on. RuleTrader was designed to help you in this through a simple 5-step process:
- Test your system
- Review the results
- Generate ideas for improvement
- Repeat from step 1 until you are happy
- Then deploy your system for live trading
Obviously you’ll need an idea to begin with, of the type of investment strategies you want to use and the markets you want to focus on. There are many good books on different approaches to investment that provide a wealth of trading ideas. What RuleTrader allows you to do is to test these ideas, so you can combine what works and eliminate what doesn’t.
This was the method used to develop the Example Investment System (included with RuleTrader), at the beginning of 2022. Today it’s still delivering double digit returns, while out-performing comparable UK funds. Yet it’s not a terribly sophisticated system and we’re definitely not rocket-scientists; instead we take a very pragmatic view on investing.
The real trick is to be methodical and these four golden rules will help you in that:
- Keep records
- Change one thing at a time
- Ensure enhancements make sense (very important)
- Test enough shares, over a long enough period, for the law of large numbers to kick in
We’ll employ these rules as we create a new variant of the Trend Trader system. The one exception is rule 4. Though Trend Trader’s 100 share selection is reasonable enough, testing it over only 8 months is not – tests ought to be a lot longer. The bigger the product of the number of shares tested, multiplied by the length of time tested, the more reliable, and predictive, your test results ought to be.
However, we’ve a lot of ground to cover in this tutorial and no time to wait for long tests to end, so we’ll stick with 8 months for now. Of course, if you want to test the system over longer periods, then please do.
Steps (i) – (iii) of the refining process were performed in the first part of the Tutorial, by testing Trend Trader v1.00, reviewing the results, and coming up with the idea that we should add a Stop-Loss rule with a threshold between 10% – 50%. We don’t want to leave too much money on the table, so how about we go with 15% for now? We’ll then test variants on the threshold later, using RuleTrader’s batch-test facility.
Adding A Stop-Loss Rule And Finding And Fixing Errors
Normally you won’t come across errors in your Trading Spec, as RuleTrader works hard to ensure you can only select options that make sense. When errors do happen, it will be because one definition A references the output of a second definition B and that second definition B is changed in a way that it no longer ‘fits’ in the first definition A. There are two ways this can happen:
- The referenced definition B’s currency requirement has changed, so that while it’s output didn’t need a currency specifier in definition A previously, it needs one now, or vice-versa. For example, if B was defined as a share’s volume, then it won’t require a currency to be defined. But if it’s subsequently changed to a share’s price, then it will.
- The referenced definition B’s output type has changed, so that while the previous type ‘fitted’ in definition A, the new type doesn’t. The data types are: numeric value, percentage value, condition (i.e. true or false), security (share), security (instrument), and security-list. So, for example:
- a numeric value can’t be replaced by a percentage value, and vice-versa
- conditions, securities, security lists and values are not inter-changeable between types
- data that may be available for a share, may not be available for an instrument e.g. bonds and exchange-rates obviously don’t have associated profit and turnover values
So you can learn how to recognise, locate and fix these errors, we’ll deliberately induce one in the definition of the stop-loss rule, next. It requires a convoluted approach to creating the definition, in order to beat RuleTrader’s guards against these sort of errors, so please bear with it:
- Open the Test Dashboard, select Trend Trader v1.00 in the Specification drop-down, then click the Copy button and call the new variant Trend Trader v1.01. Using version numbers means we can always refer back to earlier versions, if we have to, which conforms to rule A: Keeping records. And because we’re only making one change – adding a Stop-Loss rule – we’re conforming to rule B: Change one thing at a time. We’re also sticking to the very important rule C: Ensure enhancements make sense.
- Click the Edit button with the new variant selected, which opens the Trading Specification dialog.
- Navigate to the Strategy Rules tab and create a new trading rule (click here if you’d like a reminder how). The rule we’ll create and the entries we need to make are as follows (see if you can spot the two deliberate errors):
‘Stop Loss’ trading-rule signals ‘Stop Loss’ in the evaluated security, when: ‘Pooled Return’ is less than or equal to ‘Stop Loss Threshold’; where:
- ‘Pooled Return’ = the evaluated security’s pooled % return from its current position
- ‘Stop Loss Threshold’ = 15
For the ‘Pooled Return’ Private Term enter:
“Pooled Return” → value → security’s value → evaluated security → trading position data → pooled % return from → end expression
We’ll now create the the main ‘Stop Loss’ Trading Rule definition, before we create the ‘Stop Loss Threshold’ term, so we can insert the error. To do this, we’ll use a place-holder term in lieu of the ‘Stop Loss Threshold’ term. Notice as you create the definition that RuleTrader requires you to use a percentage place-holder, because it is being compared to the ‘Pooled Return’, which you can see is a percentage value. So create the rule by entering the following:
“Stop Loss” → signals a trade → Stop Loss → don’t add attributes → true → value expression is → named value term → Pooled Return → end expression → less than or equal to a value → named percentage term → <percentage 3> → end expression → end condition
Then enter the second ‘Stop Loss Threshold’ Private Term by editing the <percentage 3> place-holder definition that is created for you, as follows. We’ll induce an error by defining the term as a decimal number, rather than a percentage:
“Stop Loss Threshold” → value → number → 15 → end expression
- Now you’re done click Exit Wizard, then Back To Shared, then Exit and RuleTrader will start compiling the updated specification. This is when it will detect the error and display an error message dialog, along with descriptive text in the Console, telling you that the error is in the ‘Stop Loss’ trading rule.
- Click Ok in the error message dialog and you’ll be taken back to the Trading Spec dialog. Don’t click Cancel or you’ll lose the changes you just made to the Trading Specification and RuleTrader will then revert to the last good version of the Spec, which was the copy we made of Trend Trader v1.00.
- Go to the definition identified in the Console and open it for editing. In this case it’s the ‘Stop Loss’ trading rule, so click Edit next to it, which will take you back to the Wizard, with the trading rule ready to be edited. Assuming we didn’t know where in the definition that the error was, the way to find it is to keep clicking Next (don’t use the Finish Editing button). While you’re doing this, watch the evolution of the ‘NOW’ sentence that describes the rule, as it is edited, and the ‘WAS’ sentence, which describes how the rule was originally. One of three things will happen, either:
- The ‘WAS’ sentence will disappear and you’ll be left with a partially completed sentence, as the Wizard switches from edit mode to create mode. This happens because something has changed in the definition that meant the flow of entries has changed, so this is where the error has occurred.
- Or you’ll get to the end of the definition and it will successfully complete. This happens when the error doesn’t affect the flow of the definition, but nevertheless it was there in the background and you’ll have fixed it without realising it. You can now exit and recompile your Spec.
- Or you’ll get to a location where the option you had previously selected can’t be selected now. This is what you’ll see with the ‘Stop-Loss’ rule, when you reach the point where the place-holder term name was replaced with a reference to the ‘Stop Loss Threshold’ definition. At this point, RuleTrader is no longer offering ‘Stop Loss Threshold’ as an allowable option, because it’s a decimal number but the rule requires a percentage to compare with the percentage from the ‘Pooled Return’ term.
- So how do we fix it? Clearly, the problem is with the ‘Stop Loss Threshold’ term, rather than the ‘Stop Loss’ trading rule. So all we need to do is redefine ‘Stop Loss Threshold’ as a percentage. So press the Escape key on your keyboard to exit the editing of the trading rule definition. It will now revert back to what it was before.
- Now click Edit next to the ‘Stop Loss Threshold’ definition and then click Next until you reach the point where you selected number as the value type for the threshold and change that to percentage. When you click Next again, you’ll see that the ‘WAS’ and ‘NOW’ sentences have disappeared, as the Wizard has switched from edit mode to create mode. The downside of this is that you lose everything in the definition that comes after the point where you made the change. Fortunately, this is just the percentage threshold value, so just enter 15 (spot the next deliberate mistake?) and click Next to finish.
- If you now click Exit Wizard, then Back To Shared, then Exit, RuleTrader will again compile your Spec, successfully this time.
On the odd times you come across errors like this, you can refresh your memory on how to deal with them, with our Help article on fixing errors.
Time now, to look at how we can use Chart and Indicator Panel Overlays, and Rule Highlights to give us more information about the operation of our Trading Spec