Question about with Portfolio() function

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?

No way to do this automatically. We would need a new portfolio function like

PortfolioHolding("ticker",portfolio(s)...)

Best you can do is upload a new Imported Factor, like $$HasSPY and $$HasUSMV and set it to 1 or 0 accordingly.

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 think you could use this to tell you if the Portfolio holds SPY or USMV:

Ticker("SPY") & (Portfolio(xxxxxxx)

Ticker("USMV") & (Portfolio(xxxxxxx)

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.

I believe this does accomplish what Kurtis wanted. These do return 1 and 0 depending on whether or not SPY or USMV is in the portfolio.

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.

define formula $port = Eval((TICKER("SPY")&Portfolio(A number))=TRUE,1,0)

in buy rule: eval($port<1, rank>98, 1)

This will not comletely stop buying stocks, but will limit buying to the highest ranked stocks if Portfolio A does not hold SPY.

No, this does not work, it justs sets the buy rule to rank>98.

Building on Geov’s idea. Try adding ($port = 1) to each existing buy rule. Like

($port = 1) AND (existing buy rule)

My membership level does not allow me to test this, but the logic should work.

Cheers,

Rich

It doesn’t work. As Marco pointed out you cannot reference a specific holding in another 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.

ETF models and Stock models don’t speak to each other on P123. Your rules will not work.

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.