Amibroker Tutorial Video - Using The Case Switch Function In Amibroker Afl

Adrian Reid shows how to use Amibroker's case/switch function to test multiple trading rules at once. Set up separate cases for each entry rule, then turn the selector into an optimization variable so Amibroker runs each one and compares the results.
Chapters
So the first example of a amibroker function I want to go through is the case switch function. Now, I use this in a bunch of my trading system development work, and I know some of you may have seen this before, but I want to make sure that you know how to use this because it's really, really powerful, particularly when you're testing lots of different ideas for a certain component of your trading system. Let me give you an example of where this might be useful. Let's say you're, you're design a system for a new market and you don't know what sort of entry trigger would be useful for that market. So you brainstorm a whole list of possible entry triggers and you convert each of those individual entry triggers into code. And instead of, I mean, the traditional way would be to put each of those entry triggers into your system one at a time, back test the system, see if it works, and then move on to the next one. But that's really slow and cumbersome. And the case switch function allows you to test really quickly a whole bunch of different rules, using essentially an optimization to switch between the different rules. So let me show you the structure of it, make sure that it's clear how it all works. There's a couple of components, so bear with me while I explain them all. Firstly, we've got the switch case syntax here. So what it consists of is the switch function and you've got a, a parameter input that defines which switch you're going to use.
So way it works is if the switch function has a value of 1, it uses the first case, if the switch function has a value of 2, it uses the second case, if it has a value of 3, the third case, and so on. So what we can do is we can define a switch function that has many different cases and within each case we define a rule that we want to try. So here what I've done is I've defined an entry rule. And for case one I've said the entry rule equals the close is exactly equal to the highest high value of the close over the last 200 days. And it's been more than 200 days since the last highest 200 day highest close. The second entry rule I defined is the entry is true when the close crosses above the 200 day moving average. The third one entry rule is today's close is the highest close that it's ever been. And the number of bars since the previous highest close ever is more than 200. And these are just random examples that I made up These entry rules, but, so I'll just go through them real quick. The fourth case is today's 20 day moving average is the highest high value of the 20 day moving average over the last 200 bars. And the last one is today's low is less than the 200 day exponential moving average and the close is greater than the exponential 200 day exponential moving average. You could put whatever you want in there. So let's say you want to, you want to design a new trading system and you've got 20 different entry signal ideas. You would have a case, a switch function with 20 cases in there with entry equals 1, entry equals 2, entry equals 3, and so on. All the different rules you brainstorm and you record them all like this. So we've got switch with a optimization variable which I'll show you shortly, and then curly brackets and whoops.
And case one colon and then the case, the rule followed by a semicolon and then a break with a semicolon. And then you go into case two, case three, case four and so on. And you can have as many of these as you want. And the way you make Amy Broker switch between them is you turn this input into an optimization variable. So this input here, this entry opt that I've defined tells the switch function which case to use. So what we can do is we can define this entry opt as an optimization variable. So we say entryopt equals optimize. And I've just given a name optimizer for entry rule. And we're going to optimize it from one to five in steps of one. So when we run optimization in amibroker and it runs the first optimization, it's going to have a value of one which is going to feed one into here, which is going to backtest this rule. Then when the optimization takes the next step, it goes to a value of two. It's going to put a two in here and it's going to take the case value of two and so on until the optimization is complete. So here's what it looks like. I'm going to just push that to the analysis window and then I'm just going to run an optimization on the ASX just for fun and let's see what happens. So it's testing rule one, now it's testing rule two, now it's testing rule three, rule four and rule five. And so we can see that it's got quite different performance values or profit values for each of those different rules because it substituted those rules in one at a time. Run the whole system and given us the performance report. And so if you use this for system design, for instance, you would know that this rule makes a lot more money than the other rules. This rule is pretty much useless. And you can look at the performance stats and decide which rule you might want to dig into further and really investigate. And in this case, Rule 4 might be interesting, and Rule 1 might be interesting. Rule 1 makes a lot less money. But the car of the MDD has some potential. It's not great, but it has some potential. So that's how you use the case switch function to test lots of different rules or rule ideas for a particular part of your trading system. And you can have one of these for your entries, for your trend filters, for your exits, for whatever you want. And it really saves you from having to.
Manually change the rules and brainstorm different ideas. You do all the brainstorming at once up front. You put them into a case switch function, you run the optimization and you get the results and you move on. You don't need to keep going through this cycle of brainstorm, test, brainstorm test, brainstorm test. You do all the brainstorming batch together.
It.











