Monthly Momentum In SPY and TLT (Rotation Strategy S&P 500 and Treasury Bonds) (Amibroker Code)
The strategy in plain English:
Data is monthly and we enter and exit on the open next month. The strategy is long the ETF with the best performance over the last month from close to close. The strategy rotates between TLT and SPY.
Click here to see the original article.
Amibroker code:
SetBacktestMode( backtestRotational );
SetOption(“initialequity”,100000);
SetOption(“MaxOpenPositions”,1);
SetOption(“WorstRankHeld”,1);
SetPositionSize( 100, spsPercentOfEquity );
PositionScore = 1000 +ROC(C,1) ;
The exit and entry are on the open the next day, and this needs to be marked off in the “settings” under the tab “trades”. By using the “backtestrotational” you can only control the exits and entries this way. Use monthly bars in the settings, not daily.
You also need to make a watch list containing those two symbols and use the filter in the Analysis window when you backtest.
Alternative code:
setOption("holdminbars",1);
SetTradeDelays( 0, 0, 0, 0 );
Fremmed=Foreign("SPY","close",fixup=1);
Fremmedto=Foreign("TLT","close",fixup=1);
ChangeSPY = ((fremmed - Ref(fremmed,-1)) / Ref(fremmed,-1)) ;
ChangeTLT = ((fremmedto - Ref(fremmedto,-1)) / Ref(fremmedto,-1)) ;
if( Name() == "SPY" )
{
IIf( Name() == "SPY",setPositionSize(99.75*leverage,spsPercentOfEquity),0);
Buy = ChangeSPY >ChangeTLT ;
BuyPrice=Close;
Sell = C>0 ;
SellPrice = Close;
}
if( Name() == "TLT" )
{
IIf( Name() == "TLT",setPositionSize(99.75*leverage,spsPercentOfEquity),0);
Buy = ChangeTLT >ChangeSPY ;
buyPrice=Close;
Sell = C>0 ;
sellPrice = Close;
}
An alternative strategy is this (slightly changed from the above):
setOption("holdminbars",1);
SetTradeDelays( 0, 0, 0, 0 );
Fremmed=Foreign("SPY","close",fixup=1);
Fremmed1=Foreign("SPY","open",fixup=1);
Fremmed2=Foreign("SPY","high",fixup=1);
Fremmed3=Foreign("SPY","low",fixup=1);
Ibsfremmed=(fremmed-fremmed3)/(fremmed2-fremmed3);
Fremmedto=Foreign("TLT","close",fixup=1);
Fremmedto1=Foreign("TLT","open",fixup=1);
Fremmedto2=Foreign("TLT","high",fixup=1);
Fremmedto3=Foreign("TLT","low",fixup=1);
Ibsfremmedto=(fremmedto-fremmedto3)/(fremmedto2-fremmedto3);
ChangeSPY = ((fremmed - Ref(fremmed,-1)) / Ref(fremmed,-1)) ;
ChangeTLT = ((fremmedto - Ref(fremmedto,-1)) / Ref(fremmedto,-1)) ;
if( Name() == "SPY" )
{
IIf( Name() == "SPY",setPositionSize(99.75*leverage,spsPercentOfEquity),0);
Buy = ibsfremmed<ibsfremmedto ;
BuyPrice=Close;
Sell = C>0 ;
SellPrice = Close;
}
if( Name() == "TLT" )
{
IIf( Name() == "TLT",setPositionSize(99.75*leverage,spsPercentOfEquity),0);
Buy = ChangeTLT >ChangeSPY ;
BuyPrice=Close;
Sell = C>0 ;
SellPrice = Close;
}
Tradestation code:
We have no Tradestation code for this strategy because of the complexity of the strategy.
Disclosure: We are not financial advisors. Please do your own due diligence and investment research or consult a financial professional. All articles are our opinions – they are not suggestions to buy or sell any securities.
