OPEN-SOURCE SCRIPT
📊 Cumulative Portfolio Tracker

Hi all, first time poster here
I just figured I'd share a script that I wrote for portfolio buy and hold backtesting purposes.
Basically what it does is compares the performance of a group of stocks combined as a portfolio vs a benchmark. In this case I have a portfolio of 20 companies vs SPY set as the default but it's pretty easy to change them in the code. Also it starts in January 2022 because one of my chosen companies only started trading at that date. Again, easy to change.
Alright, so how do we interpret the data?
The script tracks the two values to be compared with a base value of 100 and then that number rises and falls from there showing their relative performance. The orange will be the user defined portfolio and the blue will be the benchmark.
Some caveats - the user defined portfolio will start exactly when the newest ticker began trading unless specified to start past that date. This means for example if you compare SPY to SPYI, SPY will be showing years of compounding compared to SPYI's performance.
I'm a pretty novice coder for Pinecode as I usually use Python for my projects but anyway.
Potential future features -
- Swapping the baseline 100 for a % gain or $ gain from the starting point(giving the option to choose which one). This one would probably be pretty easy to do. To be honest this code isn't exactly complex. I really do not know Pinescript that well.
- Adding in returns from dividends. I don't know if this one is possible. Will need to look into it.
- The ability to overlay indicators on both the portfolio and the benchmark. This might already be possible with this code, I haven't tried because I just finished managing to get it to compile and I'm frankly tired.
-Custom weighting.
As of now it's all equal weight.
Questions I assume you will be asking -
- "Can we display the user profile as candlesticks instead of a line?"
I really don't think so. I have to assume it's hardcoded. My first idea was to just have a blank chart with no ticker and then do some fiddling to use the user profile's combined high/low/open/close display on the chart but couldn't figure that one out.
-Doesn't Portfoliovisualizer already do this but better?
It sure does. However the features I include here are going to be more flexible compared to their free version if you can't be bothered to pay for it(number of tickers, length of time, etc.).
-The benchmark line doesn't perfectly match the ticker I have on screen!
You're right it doesn't. I've turned off candles in my screenshot for that exact purpose. This goes back to what I mentioned about how you have to carefully pick the timespan that you're going to be looking at. At least I think that's what causes it. Further research needed. For now it was just easier to use a couple of lines only.
- *other scripter* already did this idea but better!
It's not exactly that complex of an idea so I wouldn't doubt it but I didn't look. I wanted to make a tool that would both be useful to me and also help me get better with Pinescript. That's pretty much the whole thing.
-How many tickers can I add?
Not a clue. 100? 1000? That would be tedious to test. 20 seemed like a good baseline.
Anyway, if anyone has feedback on what to add or anything I'm all ears. This is just the stuff that came to me over the last few hours while I was working my way through the documentation. If you find this useful, awesome! If not, no hard feelings. Still new!
Thanks folks,
Steve

I just figured I'd share a script that I wrote for portfolio buy and hold backtesting purposes.
Basically what it does is compares the performance of a group of stocks combined as a portfolio vs a benchmark. In this case I have a portfolio of 20 companies vs SPY set as the default but it's pretty easy to change them in the code. Also it starts in January 2022 because one of my chosen companies only started trading at that date. Again, easy to change.
Alright, so how do we interpret the data?
The script tracks the two values to be compared with a base value of 100 and then that number rises and falls from there showing their relative performance. The orange will be the user defined portfolio and the blue will be the benchmark.
Some caveats - the user defined portfolio will start exactly when the newest ticker began trading unless specified to start past that date. This means for example if you compare SPY to SPYI, SPY will be showing years of compounding compared to SPYI's performance.
I'm a pretty novice coder for Pinecode as I usually use Python for my projects but anyway.
Potential future features -
- Swapping the baseline 100 for a % gain or $ gain from the starting point(giving the option to choose which one). This one would probably be pretty easy to do. To be honest this code isn't exactly complex. I really do not know Pinescript that well.
- Adding in returns from dividends. I don't know if this one is possible. Will need to look into it.
- The ability to overlay indicators on both the portfolio and the benchmark. This might already be possible with this code, I haven't tried because I just finished managing to get it to compile and I'm frankly tired.
-Custom weighting.
As of now it's all equal weight.
Questions I assume you will be asking -
- "Can we display the user profile as candlesticks instead of a line?"
I really don't think so. I have to assume it's hardcoded. My first idea was to just have a blank chart with no ticker and then do some fiddling to use the user profile's combined high/low/open/close display on the chart but couldn't figure that one out.
-Doesn't Portfoliovisualizer already do this but better?
It sure does. However the features I include here are going to be more flexible compared to their free version if you can't be bothered to pay for it(number of tickers, length of time, etc.).
-The benchmark line doesn't perfectly match the ticker I have on screen!
You're right it doesn't. I've turned off candles in my screenshot for that exact purpose. This goes back to what I mentioned about how you have to carefully pick the timespan that you're going to be looking at. At least I think that's what causes it. Further research needed. For now it was just easier to use a couple of lines only.
- *other scripter* already did this idea but better!
It's not exactly that complex of an idea so I wouldn't doubt it but I didn't look. I wanted to make a tool that would both be useful to me and also help me get better with Pinescript. That's pretty much the whole thing.
-How many tickers can I add?
Not a clue. 100? 1000? That would be tedious to test. 20 seemed like a good baseline.
Anyway, if anyone has feedback on what to add or anything I'm all ears. This is just the stuff that came to me over the last few hours while I was working my way through the documentation. If you find this useful, awesome! If not, no hard feelings. Still new!
Thanks folks,
Steve
開源腳本
本著TradingView的真正精神,此腳本的創建者將其開源,以便交易者可以查看和驗證其功能。向作者致敬!雖然您可以免費使用它,但請記住,重新發佈程式碼必須遵守我們的網站規則。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。
開源腳本
本著TradingView的真正精神,此腳本的創建者將其開源,以便交易者可以查看和驗證其功能。向作者致敬!雖然您可以免費使用它,但請記住,重新發佈程式碼必須遵守我們的網站規則。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。