Simple 2 ETF Long Short System

Hi all,

I am fairly new to P123 and recently finished setting up a Value/Momentum Strategy. Next up for me is a simple 2 ETF Long Short System. Based on Market Timing signals, the System should be either invested in SPY or in SH.
I already figured I could generate the Signals for entering SH with the Hedge/Market Timing function.
But I am currently struggling with the signals for buying the SPY. No matter what I do, I always need to use a full universe and a ranking system and can never limit my trades to just the SPY. I tried solving this problem by creating a custom universe with just the SPY in it, but did not succeed at this so far.

Any recommendations how to solve this?

thanks,
Andreas

Hi Andreas - you need to select an ETF simulation, not a stock simulation. You won’t be able to use the hedge module in this case. ou can then set up a custom universe or buy rule with Ticker(“SPY,SH”).
Steve

Hi Steve,

thanks for your response. That makes perfect sense, so if ETF simulations dont have the hedge module, how do I make sure that the system buys the “right” ETF? They buy rules would only be able to trigger either a SPY buy or a SH. Would the solution be to run two sims and combine them in a book?

thanks,
Andreas

No - you can do something like this:

Ticker(“SPY,SH”)
SetVar(@SPY,( … ) // SPY buy condition(s)
SetVar(@SH,( … ) // SH buy condition(s)

@SPY & !Ticker(“SH”)
@SH & !Ticker(“SPY”)

The sell side would be something similar.

Steve

Hi Steve,

thanks so much for your help. I think you lost me here…let me see if I get what you mean:

Ticker(“SPY,SH”)
SetVar(@SPY,( … ) // SPY buy condition(s)
SetVar(@SH,( … ) // SH buy condition(s)

→ These three are essentially three buy rules in the portfolio sim?

@SPY & !Ticker(“SH”)
@SH & !Ticker(“SPY”)

→ But what are these? not sure I understand where I need to put them and what exactly these do.

Thanks,
Andreas

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

Hi Steve,

thanks so much! you really helped me break the problem down. Now I can start playing around with this. Hope I will be able to come up with a good L/S Market Timing System!

thanks,
Andreas