I have created an asset allocation system with a couple of equity ETFs and a lot of non-equity ETFs. The backtest is below.
What I want to do is use the Portfolio function inside a separate stock strategy to see if this ETF strategy holds SPY or USMV (a lot of the time it won’t). If it does, then my stock strategy will hold stocks. If not, then my stock strategy will hedge.
I’ve tried using eval(portfolio(1234)=ticker(““),1,0) and eval(portfolio(1234)=getseries(““),1,0) but it doesn’t work.
Any ideas? Can you query a portfolio for a specific ticker? And if so, can you reference ETF tickers within a stock strategy?
Thanks Marco. Would I need to use API functions or something to get time-series holdings for the strategy? I’m trying to figure out an easy way to get a historical output when the SPY is in the portfolio.
I don't think that's what he wants. See definition of function below. He wants to check that a SPY is being held in a different portfolio, which is an interesting approach.
Portfolio() : Return TRUE if stock being analyzed is an open position.
Sorry I wasn’t very clear how I wanted it to work.
Portfolio A = ETF
Portfolio B = stocks
As a buy rule in Portfolio B I want to reference Portfolio A to see if it holds the SPY. If it does, then Portfolio B is allowed to buy. If not, it cannot buy.
The solution given thus far would instead try to tell me if the current portfolio holds SPY, which I don’t want and cannot do as it is a stock portfolio.
There is only one way to make this work for an ETF model A and a stock model B, and it’s not automatic.
first define formula $port = Eval((TICKER("SPY")&Portfolio(A number))=TRUE,1,0)
then run an ETF model named TEST which goes to SPY when $port = 1
with buy rule: eval($port <1,ticker("0"),ticker("spy"))
then download performance to excel and remove all columns except date and #positions.
Since there is only one ticker in the model you will get 1 and 0 for positions.
Then make a Data Series with name say : BUY and fill it with the dates and positions from TEST.
Then in your stock model B go to Hedge module and set it to go to 100% cash using hedge rules:
enter Hedge : close(0,getseries("BUY"))=0 is true
and exit Hedge when : close(0,getseries("BUY"))=0 is false
That will give the performance of your stock model B which only buys when ETF model A holds SPY.
I just tested this on a model that holds all the cap weighted stocks of the S&P500 and it works 100% correct.
NOTE: The downloaded position numbers 1 and 0 are indicated for the first trading day of the week, usually Monday. You have to subtract 3 days from each date in the series so that the getseries rule has that signal on Friday to do the trade on Monday, otherwise the trade is delayed by one week.
I have done something similar to geov’s strategy, except instead of using a series as the gating function, creating an “exposure list,” then specifying that list in the simulation under the “period and restrictions” section. Either way, a bit time consuming.