SPY / TLT system

Hello, I'm an amibroker formula beginner. I want to create a formula / system with the following rules:

a) there are just two symbols: TLT + SPY
b) periodcity: monthly
c) at the end of the month:
c1) if SPY > Moving Average (SPY, 6 months) then buy 70% SPY and 30% TLT
c2) if SPY <= Moving Average (SPY, 6 months) then buy 100% TLT

Could you please give me an advice regarding the AFL code.

@ami_ben, before developing your own strategy, if you have not yet done so, I suggest you to take a look at this well-documented example:

1 Like

Hello I am also a formula beginner and interested in coding a similar rotationale strategy:
if the Ratio of SPY/TLT > 200 SMA, buy SPY, otherwise buy TLT.
I don't know how to buy one asset which is different from the "Apply to: Current"

@cpz you could use a rotational trading strategy, that rotates between the two ETF's. There are many examples here on the forum, just do a search. Create a Watch List with your two assets and then apply your strategy to that WL. And the basic info in the User Guide,

http://www.amibroker.com/guide/afl/enablerotationaltrading.html

// rotational trading mode - equivalent of EnableRotationalTrading() call 
SetBacktestMode( backtestRotational );
1 Like

Thanks for quick Answer !

I have read many of your suggested example, this is what I have done:

SetBacktestMode( backtestRotational );

SetOption("MaxOpenPositions",1);

SetOption("WorstRankHeld",1);

SetPositionSize( 100, spsPercentOfEquity );

SPY=Foreign("SPY","C",True);

TLT=Foreign("TLT","C",True);

R=SPY/TLT;

MAR=MA(R,200);

PositionScore = IIf(R>MAR,SPY,TLT);

I applied the backtest to a watchlist of SPY, TLT, the result I get is an OPEN LONG SPY since 2003.....

@cpz I doubt that you have read "many" of my suggestions. There must be a hundred posts on this forum reviewing rotational trading strategies and you seem to have missed the point of most of them. Your system logic and your calculations don't make sense to me. Look into what your variables are calculating and you will see why you are getting those results.

Also please follow the rules of the forum and properly insert code with the "tags" (easily done with three back ticks at the start and end of your code).
image

PositionScore is an AmiBroker variable for calculating ranking and probably should not be the price of one of your selected stocks.
https://www.amibroker.com/guide/h_ranking.html

An example of your calculations, I changed some variable names as I think it's not a great idea to use ticker symbols as variable names.

sp500 = Foreign( "SPY", "C", True );

Bonds = Foreign( "TLT", "C", True );

Ratio = sp500 / Bonds;

aveRatio = MA( Ratio, 200 );

Score = IIf( Ratio > aveRatio, sp500, Bonds );

// look at what you are calculating //
Filter = 1;
AddColumn( sp500, "SPY Close" );
AddColumn( Bonds, "TLT Close" );
AddColumn( Ratio, "Ratio" );
AddColumn( aveRatio, "aveRatio" );

Producing this type of Exploration
image

Using the raw ratio between two very differently priced securities is probably a flawed method of comparing relative strength (if that is what you are trying to do). Perhaps you need to review the logic of your trading strategy (SPY is around 285 and TLT is around 126 currently).

Those are just suggestions, keep reading and coding and you will eventually get there. Good luck.

3 Likes

I tried to implement a very simple SPY/TLT strategy. If the SPY is greater than or equal to the 200 day MA, trade the SPY, otherwise trade the TLT. The watchlist contains just the two symbols (SPY, TLT).

Here is the code.

SetBacktestMode(backtestRotational);

MA1 = MA(C, 200);

if (Name() == "TLT") PositionScore = 0.00001;
else PositionScore = IIf(C >= MA1, 1, 0);

As you can see it is very simple - force TLT to always have a small positive positionscore, and alter any other symbol's positionscore between 0 and 1 based on the close being above or below the MA.

This should trade long only because positionscores are always 0 or > 0, and should trade the SPY as long as it is above the MA(200). Otherwise trade the TLT. But intsead of the trade changing between the two based on the MA of the SPY it only enters one trade on the TLT.

Symbol	Trade	Date	Price	Ex. date	Ex. Price	% chg	Profit	% Profit	Shares	Position value	Cum. Profit	# bars	Profit/bar	MAE	MFE	Scale In/Out	
TLT	Open Long	1/2/2009	85.08471	4/30/2019	123.3922	45.02%	45005.50	45.01%	1175.216	99992.95	45005.50	2599	17.32	-23.63%	56.98%	0/0

What I am I missing? Why doesn't this work?

And the weird thing is if I add more symbols I get the same results until I reach 5 symbols. Four or less symbols gives me the above results. With 5 symbols (SPY,TLT,QQQ,IWM,DIA) I get multiple trades.

Symbol	Trade	Date	Price	Ex. date	Ex. Price	% chg	Profit	% Profit	Shares	Position value	Cum. Profit	# bars	Profit/bar	MAE	MFE	Scale In/Out	
TLT	Long	1/2/2009	85.08471	6/1/2009	67.59661	-20.55%	-20566.41	-20.57%	1175.216	99992.95	-20566.41	103	-199.67	-21.57%	0.49%	0/0
DIA	Long	6/1/2009	68.81526	6/22/2009	65.85782	-4.30%	-3427.40	-4.32%	1154.201	79426.67	-23993.81	16	-214.21	-4.30%	1.91%	0/0
IWM	Long	6/22/2009	42.94965	6/7/2010	54.41132	26.69%	20259.17	26.66%	1769.411	75995.58	-3734.64	242	83.72	-4.14%	52.75%	0/0
TLT	Long	6/7/2010	76.12376	6/15/2010	74.55555	-2.06%	-1998.27	-2.08%	1264.491	96257.77	-5732.91	7	-285.47	-2.49%	0.00%	0/0
DIA	Long	6/15/2010	84.32099	6/24/2010	82.35715	-2.33%	-2208.70	-2.34%	1117.876	94260.38	-7941.61	8	-276.09	-2.33%	1.90%	0/0
QQQ	Long	6/24/2010	41.33675	6/29/2010	39.53197	-4.37%	-4045.45	-4.40%	2226.712	92045.02	-11987.06	4	-1011.36	-4.37%	0.68%	0/0
TLT	Long	6/29/2010	78.07822	7/26/2010	77.18478	-1.14%	-1020.53	-1.16%	1127.154	88006.18	-13007.58	19	-53.71	-2.56%	1.88%	0/0
DIA	Long	7/26/2010	85.43659	8/12/2010	83.96747	-1.72%	-1507.97	-1.73%	1018.139	86986.31	-14515.55	14	-107.71	-2.29%	2.01%	0/0
TLT	Long	8/12/2010	78.54229	9/13/2010	80.18076	2.09%	1770.13	2.07%	1088.304	85477.92	-12745.42	22	80.46	0.00%	8.23%	0/0
DIA	Long	9/13/2010	86.09174	8/2/2011	98.72684	14.68%	12730.76	14.66%	1008.529	86826.01	-14.66	225	56.58	-0.65%	23.78%	0/0
QQQ	Long	8/2/2011	51.70029	8/4/2011	49.77083	-3.73%	-3754.23	-3.76%	1933.717	99973.74	-3768.89	3	-1251.41	-3.73%	1.08%	0/0
TLT	Long	8/4/2011	85.46342	1/10/2012	97.29039	13.84%	13302.66	13.82%	1125.913	96224.35	9533.77	110	120.93	-2.92%	19.31%	0/0
DIA	Long	1/10/2012	104.9844	6/1/2012	103.2067	-1.69%	-1867.15	-1.70%	1043.275	109527.50	7666.62	100	-18.67	-1.69%	7.79%	0/0
QQQ	Long	6/1/2012	55.86279	11/7/2012	59.64531	6.77%	7266.30	6.75%	1927.134	107655.05	14932.91	110	66.06	-0.61%	17.37%	0/0
DIA	Long	11/7/2012	111.2842	11/8/2012	110.1993	-0.97%	-1132.80	-0.99%	1032.732	114926.72	13800.11	2	-566.40	-0.97%	0.00%	0/0
SPY	Long	11/8/2012	121.1083	11/14/2012	119.257	-1.53%	-1750.77	-1.54%	939.6091	113794.47	12049.34	5	-350.15	-1.53%	1.01%	0/0
TLT	Long	11/14/2012	106.5655	11/28/2012	105.2033	-1.28%	-1444.83	-1.29%	1051.4	112043.02	10604.50	10	-144.48	-2.14%	0.36%	0/0
DIA	Long	11/28/2012	111.9069	10/13/2014	146.6281	31.03%	34303.41	31.02%	988.3088	110598.57	44907.91	472	72.68	-0.54%	39.38%	0/0
QQQ	Long	10/13/2014	88.92429	8/21/2015	98.77031	11.07%	16024.06	11.06%	1629.455	144898.13	60931.97	217	73.84	-2.93%	24.08%	0/0
TLT	Long	8/21/2015	114.9479	4/13/2016	121.4702	5.67%	9114.22	5.66%	1399.969	160923.56	70046.19	162	56.26	-6.05%	8.34%	0/0
DIA	Long	4/13/2016	166.773	10/24/2018	242.2968	45.29%	76724.25	45.28%	1016.056	169450.78	146770.44	640	119.88	-4.18%	59.17%	0/0
TLT	Long	10/24/2018	112.981	2/20/2019	121.1555	7.24%	17827.39	7.22%	2184.06	246757.34	164597.83	80	222.84	-2.27%	8.68%	0/0
DIA	Open Long	2/20/2019	258.1546	4/30/2019	265.1271	2.70%	7134.06	2.70%	1024.935	264591.66	171731.89	50	142.68	-2.71%	3.11%	0/0

Can anyone explain what is wrong with this very simple code?

Thanks

There are far more settings that exist in the Analysis Settings window that you seem to ignore and must share so that members can understand the complete scenario.
For eg. what is the Max-open positions in your case ? etc etc

The results are not weird, they are the output of what you have set.

You are correct in that by adding a line to set the worst rank held solved the problem, but I did not see a setting for this in Backtester Settings window. Is it there?

I had kept all the settings pretty basic using defaults with Max Open Positions set to 1, but it needed the option set for worst rank held to work.

SetBacktestMode(backtestRotational);
SetOption("WorstRankHeld", 1);

MA1 = MA(C, 200);

if (Name() == "TLT") PositionScore = 0.00001;
else PositionScore = IIf(C >= MA1, 1, 0);

Thanks for your suggestions!

Portfoliobuilder, The logic of the ratio strategy is to follow a DUAL signal: When stocks outperform bonds AND that outperformance is above a moving average (sort of momentum in that outperformance) you want to be long stocks and vice versa.
Thanks to all of you I was able to compare my ratio strategy with Trader500 more simple long SPY if above or the 200 SMA, long TLT if below;
Since 2002, CAR is slightly inferior ( 4.52% for the ratio versus 5.31% for the SMA) but MDD and volatiltity are significanlty reduced (32% versus 25% MDD) and more then halved the MDD of the buy and hold SPY.
Overall the ratio strategy is more defensive and will switch you out of stocks more quickly like 2008 and this month again (early sell stock signal first week of may).