What Happens To Stocks When Bonds Go Up? (Amibroker And Tradestation Code)

The article can be found here.

The logic in plain English:

1. Buy at the close when the close of TLT closes above the 15-day moving average.

2. Sell at the close when the close of TLT closes below the 15-day moving average.

Amibroker code:

Fremmed=Foreign(“TLT”,”close”,fixup=1);

oddis=Optimize(“N-day moving average”,15,5,100,5);

Buy= fremmed<MA(fremmed,oddis);
buyPrice=Close;
Sell= fremmed>MA(fremmed,oddis);
SellPrice=Close;

TradeStation Code:

{
Strategy 108 - What happens to stocks when bonds go up?
The logic in plain English:
1. Buy at the close when the close of TLT closes above the 15-day moving average.
2. Sell at the close when the close of TLT closes below the 15-day moving average.

Add symbol TLT as the second data chart:
Activate a Chart Analysis window. From the menu, click Data > Add Symbol, type-in symbol name and click Plot
The first (default) symbol should be SPY or your preffered stock ETF
}

Inputs:
	Lookback(15);
	
Vars:
	TLTClose(0),
	AvgTLTClose(0);

Once begin
  if (Getsymbolname of Data2) <> "TLT" then Print("The second symbol should be TLT");
end;

TLTClose = Close of Data2;
AvgTLTClose = Average(TLTClose, Lookback);

if TLTClose < AvgTLTClose then buy this bar on close;
if TLTClose > AvgTLTClose then sell this bar on close;