Pair trading with Foreign symbol, was: Start Difficulties

Hello everybody,
I started a few times with understanding and creating AFL code but it always turned out to be quite difficult. Now I want to give it another try. The following easy strategy I'd like to program in order to backtest it:

"Switch between SPY and TLT depending on a CROSS-function"

So the behaviour should be similar to this:
if Cross( MA(FOREIGN_C,p1) , MA(FOREIGN_C,p2) ) BUY SPY (and close position TLT)
if Cross( MA(FOREIGN_C,p2) , MA(FOREIGN_C,p1) ) BUY TLT (and close position SPY)

  1. Should I realize such a system as backtestRegular or backtestRotational?
  2. If backtestRotational should be used, how do I set the positionscore so that in the one case SPY should be ranked higher and in the other case TLT should be ranked higher?

Thank you so much in advance & best regards

The answer to your question is covered in the Knowledge Base:

In short you have to run the backtest on watchlist of TWO symbols (SPY and TLT) and use the code like this:

Buy = ... your original buy rule (for SPY)
Sell = ... your original sell rule (for SPY)

if( Name() == "TLT" )
      // swap the rules 
      Tmp = Sell;
      Sell = Buy;
      Buy = Tmp;

There are plenty of other posts on this forum covering that already, see:

Last but not least, your user account isn't marked with License verified badge. Did you purchase AmiBroker license?

Only users with License verified badge are allowed to post on this forum.

So before posting, make sure you verify your license as explained here.

1 Like