System always buys in 2 assets

Hello forum,
Some time ago I try to program a system but there is a part that I do not know how to do it. I want you to do this:

-If close price of SPY > daily MA (C, 200) then sell TLT and buy SPY
-Sell SPY if close price of SPY <daily MA (C, 200) and buy TLT.

I know that for buying and selling SPY according to the rules I have to put:

SP500 = Foreign ("SPY", "C");
Buy = C > MA (SP500,200);
Sell ​​= C < MA (SP500,200);

How do I buy and sell TLT according to the criteria?

Best regards

@ricardcomas trade the TLT with your rules but use either the Foreign or SetForeign functions for the SPY signal.

http://www.amibroker.com/kb/2014/09/20/broad-market-timing-in-system-formulas/

Or if you are rotating back and forth between the two, then code up a rotational trading strategy (there are many examples on this forum).

1 Like

@ricardcomas,
This looks like a pair trade; holding SPY or TLT. If so, search in the forum for pair trade strategies. Alternatively, go to Google and enter "site:www.amibroker.com pair trade". That should give you some material to study.

1 Like