##### Click Here To Read Video Transcript

Are you looking for help on how to backtest your trading system with different position sizing models in Amibroker? Well if that’s the case, you’re in the right place. My name is Adrian Reid, founder of Enlightened Stock Trading, and in this Amibroker tutorial I’m going to run through the different position sizing model you can use when backtesting your Amibroker trading system, and how you can code them up and use them for yourself. So let’s jump straight into it. For the purposes of this tutorial video, I’ve got a simple breakout trading system that I’ve designed for the Australian stock exchange. This system can be used on any stock market, but I happen to be testing it on the Australian stock market and I know it works quite well there. Now I don’t recommend you trade this system certainly without backtesting and refining and adding your own unique twist to it, but it’s a really good start and certainly serves a good purpose for the demonstration of how to do position sizing in Amibroker.

So whenever I do a trading system, I always have the backtest set up section at the top here, and that just defines the global parameters of the backtest. How much starting equity, what the maximum number of open positions are, the account margin, whether or not using a custom back test and we’re not in this video. The commission payments, I always include commissions in my backtesting to make sure that the backtest is realistic, and then also the trade delays and the buying and sell price parameters. Now I’m not going to get into the detail of all of that. I’ll cover that in other Amibroker tutorial videos. But let’s go down a little further now to the actual guts of the trading system.

I always separate, in my trading systems, parameters and rules, because if I want to change a number, change an input parameter, I want to make sure that it’s easy to see where I should change that, that I can track it, that it doesn’t get lost. Similarly, if I change the rules, I want to make sure that the rules are easy to understand, written in plain English without numbers and math all over the place. I always have this block of parameters where I specify the value for each of the input parameters. Now if you’re going to do an optimization, then these would be optimized statements or they could also be parameter statements, but right now for the purpose of this tutorial video, I’m just using a single input number because we’re not going to do any optimization in this video.

So here we’ve defined the minimum turnover, which is part of our liquidity filter, the moving average bars, a 200 bar moving average. The number of breakout bars, it’s a 500 bar breakout. The initial stock multiple, it’s a six ATR initial stop-loss. And the default position size is half a percent of risk per trade. So what are the rules of the system? Well, the training system rules are down here. We’ve got a slow moving average, which is a 200 bar exponential moving average. You can see this EMA function refers up to MA bars, which is 200, I’ve also got a minimum turnover requirement because we can only trade liquid stocks. I want to filter out anything that doesn’t have a sufficiently high trading volume. And the entry rule is a highest high breakout, so when today’s close is the highest close of the last 200 days, then… Actually no, sorry, the last 500 days, then we’re going to enter the stock and we’re going to hold the stock and sell it when the price crosses below the slow moving average or the 200 day moving average. And you can see the buy and sell lines pull all that together here.

I’ve also got a position score model to rank the trades, and I’ve gotten a volatility based stop-loss. I’m not going to go through the code of the stop-loss in this video. I’ll cover the applystop Amibroker function in a separate tutorial video. So now let’s talk about position sizing. There’s a number of different position sizing models you could use in your backtest, and most of these are covered in the literature, particularly by Van Tharp in Trade Your Way to Financial Freedom or the Definitive Guide to Position Sizing. But I want to cover today the position sizing methods you can use specifically in Amibroker. And first we’ll cover the non-compounding method and then we’ll cover the compounding method. Non-compounding position sizing models are position sizing models where the position size is constant for the life of the test, and compounding models are position sizing models where, as the account equity grows, the position size also grows and so you get that nice exponential growth in your equity curve, which is what you’re looking for in real time trading.

So first of all non-compounding models. The first model you can use in Amibroker is the number of shares per trade model, and the way we define position sizing in Amibroker is using this argument here or this statement here called set position size. Set position size is a function Amibroker understands and only has two input values. The first input is a value, that’s the number that Amibroker is going to use to determine position size, and the second is the model, the position size model that you’re telling Amibroker to use. So in order to trade a certain number of shares or contracts per trade, we’re going to use SPS shares. This is the whole statement to do your position sizing, set position size, open brackets, then a number, and in this case 100. This represents 100 shares. And we’re going to tell Amibroker it’s shares because we’re using SPS shares, and then we finish with a bracket and a semi-colon. So remember all lines of Amibroker code must end with a semi-colon because it tells Amibroker that line is finished and to move on to the next one.

So set position size 100, SPS shares will force your backtest to take 100 shares of each stock every time a trade is triggered. So every trade will be 100 shares. Now in reality, you’re never going to trade like that. That just doesn’t make sense at all because you really need to link your position size to your account size and also to the specific stock you’re trading. But this can have some useful applications, this number of shares position sizing model, in backtesting and in checking the validity, stability of your position sizing model. Let’s say, for instance, you are trading ETFs and you wanted to check whether the edge in your trading system was stable over time. You might tell Amibroker to buy one ETF every single time it gets a trading signal, and that way you can look at the backtest, the equity curve, and see the profit over time from a single contract. The idea is you want to make sure that that was fairly linear profit over time. But in reality, like I said, the the number of shares per trade model is not one that I actively would recommend you use.

The second model that you can use is the value of positions. The value of positions is the dollar value of the trades. Again, this is a non-compounding model, so we’re going to define a certain dollar value for every single trade in our backtest. I do use this in Amibroker backtesting for checking stability of my system over time, instability of the edge. Say, for example, I’m trading system just like this one, a breakout system on regular stocks and I want to make sure that the edge has remained constant over time. I might force Amibroker to take $10,000 of each stock every time it takes a trade and then look at that resulting equity curve. What I’d like to see is a nice linear equity curve from bottom left to top right. And if it’s linear and the angle is constant over the life of the back test, that means the system is stable. But if the equity curve is falling away or it turns over and starts losing money dramatically, then you know that the edge of the system has to decay.

Now again, the value based position sizing model, you force Amibroker to use that by saying SPS value in your set position sizing code here. You’re not going to us that for real time trading most of the time, but it is a really useful tool for backtesting and to make sure that your system is stable. Now I’m going to cover the three main compounding position size models. The first one is percent of equity. This is the simplest. The way percent of equity position sizing model works, is Amibroker, whenever you get a buy signal for a trade, it’ll go and look at the amount of equity you’ve got in your backtest on day, multiply it by some set percentage, and that’s going to give you the dollar amount, dollar value of the position that Amibroker’s going to take in the backtest.

So as an example, let’s say you’ve got, in your backtest, you get a buy signal and there’s $100,00 in the account and you’re using 5% of equity, then Amibroker’s going to put $5,000 into that trade. And then if your account over time grows and goes up to, say, $200,000, and you’re still using the 5% of equity model, then now the position is going to be twice as big and it’s going to be $10,000 per trade. That’s how the percent of equity model works. So how do we code it in the Amibroker formula language? Well again, you’re just using set position size with these two standard arguments. If you’re using 5% of equity, you type in five here, and then SPS percent of equity. Now SPS stands for set position size, just in case you’re wondering. And this number here, this 5%, it’s written as a whole number. So 5 represents 5%. 1 represents 1%. 0.5 represents 0.5%. and you can make this an input parameter and optimize it and have Amibroker vary it automatically if you want to.

Now the next compounding position size model you can use is percent risk. Now, this was popularized by Van Tharp and other authors, but I particularly learned this from Trade Your Way to Financial Freedom, and percent risk-based position sizing model requires that your system has a stop-loss. The way it works is you have your entry price and you have your stop-loss level and this distance between your entry price and you’ll stop-loss level, that is your risk on that trade under normal circumstances. What the position sizing model does, the risk-based position sizing model, is it equates that risk to a certain percentage of your account. So let’s say you wanted to risk half a percent of your account per trade, then you’re going to get Amibroker to calculate the number of shares for you to buy in that trade so that this distance between your entry price and a stop-loss equates to half a percent of your account.

So how do we do that? Well, the way we do it is we modify the percent of equity position sizing formula we used before. So first of all, like I said, we need a stop-loss to use risk-based position sizing. And we happen to have a stop-loss in this system, and the width of the stop-loss is defined by this line here, risk per share, and it’s an ATR-based stop-loss. If you remember up here, we had an initial stop multiple of six, so this stop-loss is six ATRs wide, six times the average true range, which is quite a wide stop-loss. So we’re going to enter and if we’re rolling this trade and it falls, it’s going to fall six times the average true range before it hits the stop-loss and we get out.

So how do we calculate the number of shares to buy? Well, the position sizing model set position size requires two parameters. The percent risk of your account and the percent of equity using this percent of equity model. So this percent size number, we want this to equate to half a percent of our equity. So the present size is calculated by saying what is the percent risk on that trade? Now up here we’re defining percent risk as 0.5%, like I said. 0.5% risk, not percent of equity. So if we lose on the trade, the loss should be 0.5 of a percent of our account. We need to now convert that to how much total exposure we need on that trade. We’re going to say 0.5% risk per trade. We’re going to multiply by the buy price and then divided by the risk per share. That’s going to tell us the percentage of our account to put into this trade.

So again, the percentage of our account to put into this trade is equal to the percent risk on the trade times the buy price divided by the risk per share. And then this percent size number gets fed into the position sizing formula on the next line. So this is how you do percent risk-based position sizing model, and the percent risk, if we’re wrong, we’re going to lose 0.5% on this trade. You can set that as optimization variables as well, so you can vary it from, say, 0.1 to 1% in steps of 0.1 and see how your system performs. Pardon me, this is the percent risk based position sizing model.

Now the other position sizing model that Van Tharp talks about is the percent of volatility position sizing model. So what is the percent volatility position sizing model? Well, the percent volatility model aims to equate the volatility of your trade, the daily volatility of each position to a certain percentage of your account. So maybe you want to say, for instance, equate one ATR, one average true range, to 1% of equity. So the daily movement of each trade is roughly equivalent to 1% of equity or half a percent of equity. That’s what the percent volatility model does. And again, we’re going to calculate that in Amibroker using a variation on the percent of equity position sizing model. There’s a couple of different ways to do this, but this is the way that I do it. This is the way that I like.

So first of all, we’re going to define the volatility unit. The unit of measure or the indicator that we use to define volatility, and I tend to use average true range for this. I’m using yesterday’s average true range because if you’re placing your orders at the open, you don’t yet know today’s average true range, so you’re putting a yesterdays average true range. And what percent volatility do I want to equate the average true range to? I’ve just picked a number and said 0.2. You’ve got to be careful using the percent volatility model that you don’t end up having positions which are too big or risking too much, but that’s a parameter for you to optimize and vary in your system testing. So we’ve defined the volatility units, average true range, we’ve defined the percent volatility that we want to equate the average true range to, I’m saying 0.2%. So let’s convert it now to the percent size input that we need for this position sizing formula. Again, we’re using the position sizing, set position size, percent size, SPS percent of equity.

But we need to calculate the percent size so that it equates to a certain volatility amount, and that equates the volatility to a certain percentage of our account. We do that here on line 83. So the percent size is equal to the percent volatility per trade that we want, times the buy price divided by the volatility units. So in this case it’s the 2.2% volatility times the opening price, divided by the average true range. Now you’ll notice this formula is very similar to the percent risk-based formula here. It’s just that the numerator and the denominator values aren’t changed. If you use this formula in your Amibroker AFL code for your backtest, that will equate the ATR of each trade that you take on the day of entry 2.2% of your account. So the bigger the volatility, the smaller the trade will be. The smaller the volatility, the bigger the trade will be.

This is quite useful. It’s a useful alternative to allow you to normalize volatility between your different trades if you’re not using a stop-loss, because you can measure the ATR and for a really volatile stock you’d take a small position and for a really quiet low volatility stock, you take a bigger position, so the average daily movement of each of your positions tends to be the same and that way no one position in your portfolio wags the portfolio around. A lot of people will divert to percent of equity position sizing models when you don’t have a stop-loss, but you don’t need to do that. You can still normalize all your positions based on volatility.

Now, which one of these trading position sizing models is best? Well for backtesting, you’re probably to want to use the value of the position model and also one or all of these. For real time trading, you’re going to have to test each of these compounding models and find the one that works best with your system. Every single time I develop a trading system, I’ll test each of these different position sizing models and find the one that happens to work best, gives the most stable, smoothest returns for that system. So I have some systems that work on percent of equity, some that work on percent risk, and some that work on percent volatility. There’s no one right answer. You need to test it for yourself.

I hope you found this tutorial on position sizing in the Amibroker useful. Now if you want to get this code to save you typing it all out for yourself and figuring out the syntax and so on, click the link below and download the code from this video so that you can just follow along, edit the code and change it and use it in your own trading systems. Again, my name’s Adrian Reid, this is Enlightened Stock Trading, and I’ll see you in the next Amibroker tutorial video. Bye for now.

The compounding 2 and 3 strategies are identical. I could not find any difference between the two.Can you please ealoborare the diff.Both are equating the ATR risk in to % of equity to be lost per trade.

Depending on the trading system and the settings you use for position size the different position size models can compound the same or differently. The key is to understand what the different position sizing models are and how to use them so that you can test them for yourself in each of your stock trading systems and find the one which works best for you… Happy Backtesting!!!

Adrian