Amibroker Tutorial Video - Using The Foreign And Setforeign Amibroker Afl Functions

Adrian Reid shows how to use Amibroker's foreign() and setforeign() functions to filter trades based on index conditions. foreign() pulls a single data series like closing price from another ticker, while setforeign() switches context to calculate full indicators like RSI or ATR on the index. Example: only trade stocks when the index close exceeds its 200-day EMA plus 8 ATR.
Chapters
What I wanted to do is share with you.
How to use the foreign function versus the set foreign function. One of the common rules that we've got in a lot of our systems is an index filter. An index filter is often something like the value of the index today is greater than the 200 day moving average of the index. That's really easy because it only requires one.
Data series from the index, the closing price. So the way we define that is usually index equals foreign and then we say the value the ticker of the index and.
Then specify the closing value. So let me show you that on the screen.
So this is a typical way to create a simple index filter. The index is a foreign function. Foreign function just means it is not the ticker that we're back testing right now. Okay, so which function are we referring to? We're referring to the dot aord, which is the meta stock symbol for the Australian ordinaries index. And we're going to use the closing price and then the index trend is just equal to the index is greater than the exponential moving average of the index over the last 200 days. Nice and simple. You don't have to do anything fancy because you can define it all in one line. We've created this.
Parameter here called index or this variable here called index, which just contains the closing prices of the all ordinaries index for every single day in the all ords index. And we can then do calculations on the closing price. Now that's, but that's, that's the brilliance of it because it's super simple. But the limitation is you can only use the closing price. You can't do any fancy indicators that don't use the close.
So if you want to calculate like the RSI or the volatility of the index, you can't because all you're doing is calling in one foreign data series, which in this case is the closing price of the index. So how do you do calculate a more complex formula on a foreign function? That's where you use set foreign instead of just foreign. So what amibroker does when you're running the backtest is it comes down the code and applies all of the formulas to the ticker that it's backtesting in that moment and then when it moves on to the next ticker, it applies all of the formulas again to the next ticker and so on. If you want to calculate a formula based on a different ticker other than the one that is currently testing, you have to tell Amy Broker, stop using this ticker. Go use this foreign ticker, calculate all of this and then come back. Okay, so that's what the set foreign function does. And here's the way you do it. You have.
A set foreign statement and all you do is you write set foreign and then in inverted commas the ticker that you're going to use and then you put a semicolon and when amibroker comes down here and reads all of the code, when it gets to this, it goes, ah, okay, I'm going to switch across to the all ordinaries ticker and everything. After this point, I'm going to calculate on the all ordinary sticker. So all of these lines, it calculates on the all ordinary sticker. And then you use this restore price arrays.
Function to tell it to come back to the original ticker that you're actually backtesting. So what this allows us to do is put more complex formulas that use any of the indicators that are built into Amy Broker or any functions you like.
But apply them to the all ordinaries or whatever foreign function you want, calculate the values and then come back to the stock that we're back testing and apply those rules to that were calculated based on the foreign function, the foreign ticker, and apply them to the system on that stock. So here's what we're doing as the example. We're saying set foreign to the all ordinaries index. Now calculate the index filter as being the closing price must be greater than the exponential moving average of the close plus eight times the average true range. So this means basically the closing price of the index must be 8 ATRs above the moving average of the index, which means the index is moving up substantially. Okay, now I don't know if this is a good rule. I just made it up for the example of this, this demonstration. But you can't do this if you are just using a foreign function.
So if you want to do something like this, use the ATR index indicator or use any other indicator that requires more than just a closing price of the index, you've got to use set foreign restore price arrays. So now what we can do is take this index filter and use it in our buy rules down here. And so for every stock that Amy Burke is back testing, it's going to say the liquidity rule has to be true and the index filter has to be true. But index filter is defined using the foreign ticker that we've specified earlier and the entry trigger has to be true. So in here you might say, okay, I only want to trade if the index is going up and the volatility is low and the index is is not at an all time high, and this and that, and whatever other conditions you like. So you can sort of box your system in a little bit based on what the market is doing using this sort of function, and then apply your normal trading rules to the stock, provided all of those index conditions are right. So that's using the set foreign and restore price arrays function, which gives you a lot more flexibility in just using the foreign function, like we do in a lot of our simple systems.
It.











