Converting Calendar Days to Bars

I’ve been looking into figuring out a way to implement “Pyramiding”.

Where I’m currently stuck is that I’m using a method of having a “Core” portfolio where all the rules are set and a secondary portfolio with the buy rules:

Portfolio(“ABC”) - Where I determine if a stock is within the portfolio
And making use of PortfolioOpen(“ABC”) to determine, if the stock in interest has gained since being bought in the “core” portfolio. For example, Buy when stock has gone up 10% since “core” portfolio has bought.

The problem I have is that PortfolioOpen returns Calendar days, whereas the functions such as Close(x) require the use of Bars. The issue then comes to that if I use something like Close(PortfolioOpen(“ABC”)) I would go too far back in time and the results would be off.

Would anyone know of a way where I can convert Calendar Days to Bars, or perhaps a better method to determine if a stock has gained X amount and hence triggers the secondary buy?

Perhaps you can do this in a custom series? And define the series to have an additional value of 2 for every 5 trading days?

I’m trying the use of something like this: Trunc((PortfolioOpen(“ABC”)/7)*4.8).

I got 4.8 as there is approx 251 bars in a year so I calculated 251/52 - seems to work okay, though it appears to leak through some that I don’t want

It’s unfortunate that PortfolioOpen returns days. Where would days be helpful outside of calculating annualized returns or meeting some kind of tax holding period?

If you really need this function, consider submitting a feature request for PortfolioOpenBars().

Walter

That’s a good idea - I definitely think it would be very helpful as there isn’t a good way to add to existing positions other than creating an additional portfolio and creating a book, or to constantly rebalance which can sometimes cause more problems than good.

I’ve added PortfolioOpenBar and PortfolioCloseBar, available for immediate use. Please note that PortfolioCloseBar can return -1, a case which should be handled specifically.

Thank You, Aaron!

Amazing - Thank you :slight_smile: