19th Ave New York, NY 95822, USA

How To Hedge A Portfolio In Amibroker : The Quick And Easy Way


In this article, I look at a simple way to add ETFs to your trading system so you can hedge a portfolio without having to use the custom back-test interface. 

How to hedge a portfolio in Amibroker

Lately, I have been testing simple buy-and-hold portfolios on stocks but I have run into the problem of trying to include ETFs into the portfolio as well, when wanting to diversify the holdings.

The main difficulty is that when you run a trading system on a watch-list of securities, Amibroker treats each security the same and there isn’t an option that allows automatic hedging – like there is in Portfolio123 for example.

So let’s say that you want to hold a portfolio consisting of eight stocks and two ETFs. Well, how do you tell Amibroker?

Of course, this can be achieved through the custom back-test interface but there is also a quick and easy way, by utilising the Amibroker ‘PositionScore‘ function.

Step 1 – create a watchlist for your ETFs

The first thing you need to do is create a watch-list for the symbols or ETFs you want to use as a hedge.

Using the database from Norgate Premium Data this task is very simple. I simply locate the ETFs I want to use and add them to a new watchlist. For the purposes of this post, I am going to use the symbols $IEF and $TLT and add them to a new watchlist called ‘Bond ETFs’.

IEF is the iShares 7-10 Year Treasury Bond ETF and TLT is the iShares 20+ Year Treasury Bond ETF. Both of these bond ETFs are good ones to use because they are low-cost trackers and should be relatively uncorrelated to stocks.

hedge a portfolio in Amibroker with PositionScore

Step 2 – create a watchlist for your stocks and ETFs

The first watchlist should contain the ETFs that you want to buy.

The second watchlist should contain both the stocks you want to buy plus the ETFs that you want to buy (the ones you also put in watchlist one).

For the purposes of this post, I just copy all stocks in the S&P 1500 Historical Constituents universe and paste them into a new watchlist called ‘S&P 1500 Plus Bond ETFs’. I then add the two bond ETFs as well, IEF and TLT.

Step 3 – create the trading system

For the purposes of this post, I am going to create a simple buy-and-hold trading system.

SetOption("Maxopenpositions", 10);
PositionSize = -100/10;
PositionScore = RSI(14);
Buy = 1;
Sell = 0;

The system buys 10 stocks from the S&P 1500 with the highest RSI(14) and never sells. Starting capital is set at $10,000 (equal weighting) and commission is set at $0.01 per share.

Running this simple system between 1/1/2005 and 1/1/2015 returns the following results and trades:

Annualised return: 5.33%
Maximum drawdown: -59.37%
CAR/MDD: 0.09

Equity curve and trade list:

buy and hold without bonds test one

buy hold trade list one

Step four – add the hedge using PositionScore (THE IMPORTANT PART)

Now we have seen how the simple system performs we can add our hedge in Amibroker, using the PositionScore formula. The PositionScore formula is used to rank securities so that we can choose between trading signals. And the important code to use here is the InWatchlistName function.

Thus, if we assign a higher ranking score to the symbols in a specific watchlist, Amibroker will buy those symbols first. The code can be set as follows:

PosScore = RSI(14);
PositionScore = IIf( Inwatchlistname("Bond ETFs"), posScore + 1000, posScore);

In other words, Amibroker will cycle through all of the symbols and if it comes across a symbol in the ‘Bond ETFs’ watchlist, Amibroker will add a score of 1000 to it’s ranking. Otherwise, it will use the original PositionScore.

In this way, symbols in the ‘Bond ETFs’ watchlist will be given a much larger ranking score and they will always be bought first. Thus, Amibroker first purchases the two Bond ETFs. It then proceeds to buy the next 8 stocks with the highest RSI(14).

Running the system again, this time using the hedge in place returns the following results:

Annualised return: 6.67%
Maximum drawdown: -53.05%
CAR/MDD: 0.13

Equity curve and trade list:

buy and hold test two with bonds

buy and hold with bonds trade list

As you can see from the results, TLT and IEF were the first symbols to be purchased. By including these two bond ETFs into the overall portfolio mix, the drawdown was reduced and the annualised return was increased.

There are lots of creative ways to use PositionScore and this is just one method for including certain symbols into the trading system.

If you have any more creative ideas for using PositionScore, let us know in the comments!

You may also like:

– Amibroker AFL Collection
– Where to get historical data for Amibroker
30 + Trading Strategies For Stocks

Comments (3)

Great post. Creative and helpful.

Hi we are looking to partner with active amibroker users for conducting training programs for the software ( Full-time/ Part – time )

payment structure is flexible according to the classes and support

looking forward for your response

Sahil Manchanda

Leave a comment