OK - I actually made a mistake in the formulas but I’ll get to that later…
I assume you are running a sim and not a screener backtest.
Ticker(“SPY,SH”) can be a rule in a custom universe or it can be a buy rule. If it is a buy rule then you would select “All ETFs” for the universe. This rule limits positions to SPY and SH; no other ETFs can be bought.
Now - what is the condition you want for buying SPY? Let us say you want a buy to occur when the SPY is above its 200 day moving average. The rule would be Close(0)>SMA(200). But this rule unto itself doesn’t specify the ETF. With this rule as is you could buy SPY or SH. But you only want this rule when the ticker is SPY, not SH. So you need to add a qualifier:
Close(0)>SMA(200) AND Ticker(“SPY”)
This can also be expressed as:
Close(0)>SMA(200) & Ticker(“SPY”)
The above rule is almost right but not quite. It would be correct if you only ever wanted to buy SPY. The result will never be TRUE when the ticker is SH. So you have to add an additional term to the formula:
Close(0)>SMA(200) AND Ticker(“SPY”) OR Ticker(“SH”)
This makes the rule always TRUE for SH. Thus a second buy rule has to be added to take care of the buy formula for SH. For the sake of simplicity lets use the same formula:
Close(0)>SMA(200) AND Ticker(“SH”) OR Ticker(“SPY”)
So the three rules so far are:
(1) Ticker(“SPY,SH”)
(2) Close(0)>SMA(200) AND Ticker(“SPY”) OR Ticker(“SH”)
(3) Close(0)>SMA(200) AND Ticker(“SH”) OR Ticker(“SPY”)
Each buy rule is AND’d with all of the other buy rules. An easier way is to combine rules (2) and (3) into one rule:
(1) Ticker(“SPY,SH”)
(2) Close(0)>SMA(200) AND Ticker(“SPY”) OR Close(0)>SMA(200) AND Ticker(“SH”)
What if you want to get a little more complicated? Perhaps the price should be above the 200 day moving average but the short term relative strength is low… lets use RSI(14)<40 for SPY and RSI(9)<20. The buy rules would be:
(1) Ticker(“SPY,SH”)
(2) Close(0)>SMA(200) AND RSI(14)<40 AND Ticker(“SPY”) OR Close(0)>SMA(200) AND RSI(9)<20 AND Ticker(“SH”)
As more conditions are added the above formulas get more and more complicated. So lets make the code easier to follow by defining variables then apply the boolean logic to the variables:
(1) Ticker(“SPY,SH”)
(2) SetVar(@SPY1,Close(0)>SMA(200)
(3) SetVar(@SPY2,RSI(14)<40)
(4) SetVar(@SPY,@SPY1 AND @SPY2)
(5) SetVar(@SH1,Close(0)>SMA(200)
(6) SetVar(@SH2,RSI(9)<20)
(7) SetVar(@SH,@SH1 AND @SH2)
(8) @SPY AND Ticker(“SPY”) OR @SH AND Ticker(“SH”)
Rule (8) can be simplified by using an Eval statement (same as an if statement):
(8) Eval(Ticker(“SPY”),@SPY,@SH)
This takes care of the buy rules. You have to do something similar with sell rules except you don’t need the first rule Ticker(“SPY,SH”). Also, the sell rules are OR’d together as opposed to the buy rules which are AND’d together. Note that the variables cannot be carried over from buy to sell side. You have to define the variables again.
If you have troubles after working through this example then send me an EMAIL.
Steve