A Pair Trade In SPY And TLT (Pairs Trading Strategies) (Amibroker Code)

The strategy in plain English:

 

  1. The S&P 500 (SPY) must fall at least 1.25 times more than the 50 day average of the H-L (high minus low) from yesterday’s close to today’s close.
  2. Opposite, the 20-year Treasury bonds (TLT) must rise 1.25 times more than the 50-day average of the H-L (high minus low) from yesterday’s close to today’s close.
  3. If both 1 and 2 are try, go long SPY at the close and sell short TLT at the close.
  4. Close both positions at the close x-days later.

 

Click here for the original article.

 

Amibroker code:

 

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

Fremmed1=Foreign(“TLT”,”open”,fixup=1);

Fremmed2=Foreign(“TLT”,”high”,fixup=1);

Fremmed3=Foreign(“TLT”,”low”,fixup=1);

FremmedTo=Foreign(“tlt”,”low”,fixup=1);

rangefremmed=MA(Fremmed2-Fremmed3,50);

 

Fremmedto=Foreign(“SPY”,”close”,fixup=1);

Fremmedto1=Foreign(“SPY”,”open”,fixup=1);

Fremmedto2=Foreign(“SPY”,”high”,fixup=1);

Fremmedto3=Foreign(“SPY”,”low”,fixup=1);

rangefremmedto=MA(Fremmed2-Fremmed3,50);

 

if( Name() == “SPY” )

{

Buy=( ( ref(fremmedto,-1) – fremmedto) > rangefremmedto*1.25 ) AND ( ( fremmed – Ref(fremmed,-1) ) > rangefremmed*1.25 );

buyPrice=Close;

Sell= RSI(5)>99 ; //unlikely to be triggered, thus the applystop gets relevant all the time

sellPrice=Close ;

ApplyStop(stopTypeNBar,stopModeBars,1,1) ;

}

if( Name() == “TLT” )

{

Short=( ( fremmed – Ref(fremmed,-1) ) > rangefremmed*1.25 ) AND ( ( Ref(fremmedto,-1) – fremmedto ) > rangefremmedto*1.25 );

shortPrice=Close;

Cover= RSI(5)<1 ; //unlikely to be triggered, thus the applystop gets relevant all the time

coverPrice=Close ;

ApplyStop(stopTypeNBar,stopModeBars,1,1) ;

 

 

Tradestation code:

 

Not ready yet, coming soon.

 

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.