In this example, sector performance is measured using the formula EMA(50, 0, GetSeries("...") / EMA(200, 0, GetSeries("..."), where the series used by GetSeries is the SPDR Exchange Traded Fund (ETF) that best represents the sector. The Financial Services ETF (Symbol: XLFS) will represent the Financial sector as it is a better representation of Financials ex-Real Estate.
Note: Real Estate was recently moved out from the Financial sector and into its own sector. The ETF representing Real Estate is XLRE.
The Telecom sector will not be supported in this example as there aren't enough telecom companies to bother with trying to control the weighting.
Before getting started on the simulation, some custom formulas are defined to make the rules easier to follow: The custom formulas are shown below.
The general setup for the simulation is pretty straight forward. The rebalance frequency is set to weeklyand sold holdings are allowed to be rebought at current rebalance. The ideal weight for new positions is set to 2%, with a maximum weight deviation of 10%. The general setup and position sizing are shown below.
The Buy rules consist of three distinct parts:
- Buy1-Buy10: The performance of the sector ETFs are mapped into variables. The variable names are kept short by using the last character of the ETF symbol.
- Buy11: The performance of the sector for the stock currently being processed is mapped into the variable @Weight.
- Buy12: @Weight is compared against all of the sector performance variables @Y, @K, @I, ... If @Weight is the highest of all ten sector performance variables then the sector that the stock resides in is the highest performing sector. Thus the top ten ranked stocks from that sector will be allowed into the portfolio. Otherwise, only the top 5 stocks will be bought.
The final rule Sector != Telecom blocks stocks from the telecom sector from being bought.
Set the Sell rule to TRUE. This is a big simplification to avoid having to include logic similar to what is in the Buy rules. i.e. checking the sector weights and selling stocks in sectors with too high of a weight. The simplification comes at a price however. as there will be many buy/sell differences.
When the simulation backtest is run, the current sector allocation shows a Technology weight approximately double that of the other sectors.
Please note that there is an issue relating to the recent Global Industry Classification System (GICS). With Real Estate becoming the eleventh sector, the number of sectors are eleven instead of ten. In all likelihood, you won't get a backtest representative of what you could get going forward, unless you block financials and real estate from being purchased.