Many traders like to refer to multiple timeframes in their trading systems. For example, a daily trading system may refer to dat from the weekly chart as a “Higher Timeframe” for the trend filter or other indicators. This can be done using the Amibroker TimeFrameSet and TimeFrameRestore functions.

These functions are very powerful and when you learn Amibroker AFL you will find there is huge flexibility to backtest your trading system ideas quickly and easily. If there are any functions you really want to learn more about, leave me a comment below and I will add them to my Ultimate Free Amibroker Tutorial Guide.

 Amibroker Code From This Video:

/* switch to weekly time frame /
TimeFrameSet( inWeekly );
WeeklyMacdHist = MACD( 20, 30) – Signal( 20, 30, 5 ); WeeklyMacdHistTrend = ROC( WeeklyMacdHist, 1 );
// weekly trend – one week change of weekly macd histogram TimeFrameRestore();
// expand calculated MACD to daily so we can use it with daily signals
WeeklyMacdHistTrend = TimeFrameExpand( WeeklyMacdHistTrend, inWeekly );
// This can now be used in your Buy = …; or Sell = …; lines
// Continue with your daily trading system code


Show Full Transcript

Have you ever wanted to back to use weekly data and indicators when you are backtesting an end of day stock trading system in Amibroker?

If you have a daily trading system, but you want to pull in data from the weekly chart, like maybe you want to look at a weekly moving average or a weekly MACD histogram or something like that as part of your trading rule in your daily system.

Well you can do that very, very easily when you are backtesting in Amibroker, and in this video I’m going to show you exactly how to do that with a simple example using the MACD indicator.

So let me just share my screen here and I’ll show you how it’s done. There’s a couple of commands you’ve got to include in your system, which you don’t normally use. I’m not going to go through this whole system because the rest of the daily system isn’t really relevant to the discussion. But above here I’ve got the entry rules for the system, which are with daily chart. But what I want to do is add a weekly entry signal. So a weekly rule rather that um, in addition to the daily signal, sir, what I’ve done here is after all of the daily signals, they’re above line 60 on this Amibroker chart.

What we want to do is switch timeframes to the weekly chart. And the way you do that in Amibroker is you use this ton prime set function and then you specify in weekly. So what you’re doing is as Amibroker reads the code, it gets this line and this is telling him, okay, now switch to the weekly timeframe and calculate everything below this line until I restore the timeframes on the weekly chart. So we’re setting a timeframe to weekly. And then what you do under here is you put the Amibroker indicators that you want to calculate from the weekly chart. So in this case I’m calculating a MACD Histogram. So the MACD histogram is the MACD minus the signal line. And I’ve just chosen to use a MACD with the settings of 20 and 30 in the signal line with a smoothing of 5.

And then I calculate the change in the histogram over one bar are basically using a rate of change indicator of the MACD histogram over one bar. And what I’m looking for here is just for the weekly MACD histogram to be positive to be ticking up. That’s going to be my entry rule. That’s all I want to calculate in this particular example in the weekly chart. Then what I’m going to do is I’m going to restore the timeframes back to what it was. So we use this Amibroker AFL function called timeframerestore. And then once you’ve done that, you need to then expand the weekly indicated you calculated onto the daily chart because on the weekly chart it’s only got one value per week. But when we, when we restore the timeframes to the daily chart in Amibroker, you’re going to miss data, right?

So that that weekly indicator only has a one bar each week. But on the daily chart we need 5 bars per week. So the way we do that is we take the indicator the way calculated on the weekly chart and then we redefine it using this timeframeexpand function. So I’m going to refer to this weekly MACD histogram trend parameter, which I created, but I’m going to refer to it on the daily chart. So I redefine it here. I say that weekly MACD histogram trend equals timeframe expand. And then I specify the indicator I’m expanding and where I’m expanding it from, I’m expanding it from the weekly chart. This will expand it to the default time frame, which in this case is daily. And what it does in practice is take that weekly value and insert it on every single day, every single bar of the week.

So when you go to the daily chart, it’s gonna repeat the same value five times. That way when you refer to it in your backtest or you know, AFL code, it’s going to reference the right value. Doesn’t matter what day of the week it is, can refer to the last weekly. So we, we’ve expanded this weekly MACD histogram, onto daily bars. Now we’re free to use it in our entry rules. So you can see here I’ve got my buy line and I’ve just put and weekly MACD histogram trend of greater than zero as an entry rule. Now I’ve used weekly MACD Histogram as an example, but you could use anything you want. It could be an indicator, it could be a breakout, it could be a moving average as whenever, whenever you want to do.

You’re going to reset the timeframe to weekly calculate what you need to do on the weekly chart, restore the timeframe to daily, and then expand using the timeframeexpand() function, expand that weekly value onto the daily chart. This is very important because if you don’t do this, the back test will be wrong because you’ll have bars missing. And so your code will refer to the wrong days for the weekly chart and basically we get bogus results. So you must have the timeframeset() function to set it to weekly, must have the timeframerestore() function to get it back to daily. And you must have the timeframeexpand() function to take that weekly indicator and slot it into each bar on the daily chart. Then you can use it in your buy or sell lines. So I hope that’s helped.

My name’s Adrian Reid, this is Enlightened Stock Trading and that is how you use Amibroker to back test a daily trading system with weekly indicators included in the system.

This is really powerful. The things like Elder’s triple screen system, or other systems that use the weekly chart as a trend filter or a momentum filter, and remember, don’t just stick to my one example of MACD histogram. Try it out for yourself, try different indicators. And this really expands the variety of options you’ve got for the trading signals. You can take entries and exits off the weekly chart or the daily chart. You can use weekly filters on your daily system is a really powerful way to do it. And what’s even better is in Amibroker, this backtests super quickly.

So try it out for yourself and let me know how you go. Send me a comment below this video and let me know what you think and what else you want to learn about to help you with your Amibroker backtesting. My name is Adrian Reid, this is Enlightened Stock Trading, and I will see you next video.

Pin It on Pinterest

Share This

Share This

Share this post with your friends!