Amibroker Tutorial Video - Changing Amibroker Afl Trading System Rules Depending On The Marketid

Adrian Reid shows how to use Amibroker's MarketID function to run one trading system across multiple markets with different rules. Detect which market a stock trades in, then swap parameters like the index benchmark and liquidity thresholds. A A$1,000,000 turnover requirement becomes roughly HK$5.7M for Hong Kong stocks.
Chapters
The next amibroker function that I wanted to talk about is the market id. Now this is one that's probably most useful for people who are going to trade in lots of different markets and where you might be applying one system to different markets. Now, let me just give you an example of how this can be important and then I'll show you the, the way you implement it. So let's say you're thinking about applying a trading system to the Australian market and the Hong Kong market, just for argument's sake. Now, one Australian dollar and one Hong Kong dollar are not equal.
In fact, one, one Australian dollar is about.
Five or so Hong Kong dollars, I think almost six. Right? So if you have a liquidity filter, like let's say you require a million Australian dollars of turnover in a stock per day in order to, for you to buy that stock. If you put a million dollars, a million, as the turnover filter for Hong Kong, you're going to be in trouble because that's really much less liquid than the Australian stock with the same system applied to it. So if you ran a system on Australia, you would use a value of a million. If you ran a system on Hong Kong, you'd probably use a value of 6 million.
But if you're running one system over both markets simultaneously, you need a way for the system to detect which market is this stock in and therefore which value should I use.
Make sense? So here's how you do it, because amibroker can actually look at what market the stock is in, and based on that, you can then do different calculations. So here's the, here's the example.
I was working on a system a little while ago where I was applying the same rules to many markets. First, for several reasons, I was looking to see could I get better results just by diversifying over basically the whole tradable world in my broker, or can I find a market that this particular set of rules performs really well in and therefore go and refine those rules for that market?
So several different ways I was thinking of using this, but here's the code to detect which market the stock is in and therefore what. Change the. Change the way the system calculates in order to make sure that you have the right rule for the right market. Now this is particularly an example about liquidity. Okay, so actually, let's do, let's. Rather than liquidity, let's do the index rule first. So what I want to do is if the stock is an Australian stock, I want to have an index rule that refers to the Australian. All Ordinaries index. If the stock is a Hong Kong stock, I want to.
Index rule that refers to the Hong Kong index and so on for each of the different markets. And I want to do it all in the one system so that I can apply this into a backtest on a global system. So here's how we would do it. We're going to create a variable called index and I'm just going to initialize that to zero and then what I've got here is this series of logic statements and you can do this several different ways. So don't get hung up on the, you know, I would do that a different way. The key is how do we use the market ID function to achieve this? So what I've done here, as I've said, if IIf the market ID is equal to 1, then index is going to be equal to foreign.
Using the referring to the all odds index.
Otherwise I've just used the previous value of the index and then on the next line I've said if the market ID is equal to 2 then use this one that the Hong Kong index otherwise use the previous value of the index and so on all the way down. So this way what? Let's see where the market ID comes into this. So if we go to the Amy broker window and in the symbols pane, if you look at the markets and you expand out markets, if you hover over the first market you can see how it's got a one and then a colon and then name of the market, the second one has a two and then a colon and the name of the market and so on. So this is market ID1, this is market ID2, this is market ID3 and so on. So in our formula we we can say if the market ID is 1, then do this, if the market ID is 2, then do that. And each time whenever the AMY broker is running the backtest, what it does is it picks up a particular stock, applies the rules to those to that stock, finds all the entries and exits in that stock and then moves on to the next stock. So when it picks up a stock that's in the Australian market it'll say oh market ID is one and therefore apply the rules.
For the Australian market and then it picks up the next stock. And if that's a Hong Kong stock it'll say oh market ID is 2 and then it'll apply the rules for market ID too. So coming back to here, we can have a different foreign statement depending on what the market ID is. Similarly on the liquidity Example, the currency values are quite different depending on which market we're in. So what I'm saying here is I've created this variable called currency multiplier and I've said If the market ID is 1, then give currency multiplier value of 1. If the market ID is 2, then give the market ID, sorry, give the currency multiplier value of 5.7. So this is the Australian market because I'm basing my tobacco and Australian dollars, so I'm just using a value of 1. This is Hong Kong. So if it's a, it's a Hong Kong stock, I want to multiply my Australian liquidity requirement by 5.7 so that it's relevant in the Hong Kong market and I get sufficiently liquid stocks. And I've done the same thing for all of the different market IDs here.
So then in my index filter, sorry in my liquidity filter I've got the exponential moving average of close times volume over the last 20 days, which is the liquidity or one definition of it must be greater than my minimum turnover, which I've called a million Australian dollars in this backtest times the currency multiplier.
So no matter which current, which, which market the stock that's being back tested is in, the liquidity will be adjusted back to an equivalent of a million Australian dollars per day turnover.
That way I don't get unrealistic backtests because in Hong Kong the currency is, you know, worth one sixth the amount of the Australian dollar. And so therefore I'm getting all these really tiny illiquid stocks in my backtest that have massive moves. But I could never possibly move any volume in and out of that stock because it's too illiquid. So this eliminates that problem because it's adjusting for the currency on each of the different.
Stops those backtesting. So this function market ID returns a value and the value returns is the number, the number of the sequence of that market. So is it Market 1, Market to Market 3, Market 4, Market 5. And each stock is assigned to a market in Amibroker. And so therefore Amy Broker knows which market ID that stock is attached to. So we can change our rules depending on the market id.
Now you can use this for lots of different things. I've used it to apply different entry rules per market. You can see here I've got different index foreign functions per market or to change liquidity requirements by market. Lots of different ways you could use it. But knowing which market a stock comes from allows you to combine multiple markets into the one system and just tweak things a little bit, market by market, to make sure that your system does what you need it to do.
So that's how to use market ID to change your rules. Depending on what market stock is allocated in.











