Hi, I just want to get updated (rolling) portfolio value of each security holding. Please see below backtest that I made for a simple example. I just want to see something like this:
current_spy_value : 50,000
current_iwm_value : 51,000
Total_current_value : 101,000
.......
I tried 'HoldingsValue', where I see easily holding value of longed stock (SPY in this example), but it's hard to get holding value of shorted stock (IWM in this example). Can anyone help on this? Thanks :)
Alexandre Catarino
I don't know if I follow your question: "it's hard to get holding value of shorted stock".
What about SecurityHoldings.AbsoluteHoldingsValue?
HanByul P
Alexandre Catarino, Please see the attached backtest. I just simplified this algo. Short 'SPY' for 5 days and no covering it, which means that we only see market value of our holding position and unrealized pnl. As you can see in the backtest, we see the ending value of equity, 103,937. I need to get this value from each stocks holding. However, if we use 'HoldingsValue' or 'AbsoluteHoldingsValue' as below, we cannot get the ending value of 103,937. Instead, we get -97,397.03 or 97,397.03.
2016-01-04 09:45:00 SPY_HoldVal : -99876.83
2016-01-04 09:45:00 SPY_AbsHoldVal : 99876.83
2016-01-04 09:45:00 SPY_pnl : -2.59
2016-01-05 09:45:00 SPY_HoldVal : -100826.75
2016-01-05 09:45:00 SPY_AbsHoldVal : 100826.75
2016-01-05 09:45:00 SPY_pnl : -952.51
2016-01-06 09:45:00 SPY_HoldVal : -99471.86
2016-01-06 09:45:00 SPY_AbsHoldVal : 99471.86
2016-01-06 09:45:00 SPY_pnl : 402.38
2016-01-07 09:45:00 SPY_HoldVal : -97974.48
2016-01-07 09:45:00 SPY_AbsHoldVal : 97974.48
2016-01-07 09:45:00 SPY_pnl : 1899.75
2016-01-08 09:45:00 SPY_HoldVal : -97397.03
2016-01-08 09:45:00 SPY_AbsHoldVal : 97397.03
2016-01-08 09:45:00 SPY_pnl : 2477.21
What I want is the rolling equity value of each stock in open position, which is just like the equity value of each bar in the backtest screen. Do I need some calculation method to get this value especially for shorted stocks (e.g. initial value + pnl1 + pnl2 + ...) ? Thanks :)
HanByul P
Alexandre Catarino and all, I think I got this as below.
spy_abs_cost = round(self.Portfolio["SPY"].AbsoluteHoldingsCost, 2) spy_pnl = round(self.Portfolio["SPY"].UnrealizedProfit, 2) curr_spy_val = spy_abs_cost + spy_pnl
2016-01-04 09:45:00 SPY_HoldVal : -99876.83 2016-01-04 09:45:00 SPY_AbsCost : 99876.83 2016-01-04 09:45:00 SPY_pnl : -2.59 2016-01-04 09:45:00 SPY_Curr_Val : 99874.24 2016-01-05 09:45:00 SPY_HoldVal : -100826.75 2016-01-05 09:45:00 SPY_AbsCost : 99876.83 2016-01-05 09:45:00 SPY_pnl : -952.51 2016-01-05 09:45:00 SPY_Curr_Val : 98924.32 2016-01-06 09:45:00 SPY_HoldVal : -99471.86 2016-01-06 09:45:00 SPY_AbsCost : 99876.83 2016-01-06 09:45:00 SPY_pnl : 402.38 2016-01-06 09:45:00 SPY_Curr_Val : 100279.21 2016-01-07 09:45:00 SPY_HoldVal : -97974.48 2016-01-07 09:45:00 SPY_AbsCost : 99876.83 2016-01-07 09:45:00 SPY_pnl : 1899.75 2016-01-07 09:45:00 SPY_Curr_Val : 101776.58 2016-01-08 09:45:00 SPY_HoldVal : -97397.03 2016-01-08 09:45:00 SPY_AbsCost : 99876.83 2016-01-08 09:45:00 SPY_pnl : 2477.21 2016-01-08 09:45:00 SPY_Curr_Val : 102354.04
The 'AbsoluteHoldingsCost' gets us the cost basis value of stocks invested, and the 'UnrealizedProfit' gets us rolling basis unrealized profit of stocks invested. So the sum of these two is the rolling current value of stocks in open positions. There should be other ways to get this but so far this is the easiest way I've found. Any comments are welcome. Thanks :)
Apollos Hill
thanks for sharing this. it helped me.
Apollos Hill
actually i'm geting syntax errors for this now. i'm sure there is a better way to code this. any suggestions from anyone? i'm trying to have my algo search for buying/shorting opportunities in whichever pair is not currently invested.
if self.Portfolio.["EURUSD"].HoldingsValue < 0 or self.Portfolio.["GBPNZD"].HoldingsValue < 0:
Initiate search for entry price
Alexandre Catarino
You have an extra dot after Portfolio:
# WRONG if self.Portfolio.["EURUSD"].HoldingsValue < 0 or self.Portfolio.["GBPNZD"].HoldingsValue < 0: pass # RIGHT if self.Portfolio["EURUSD"].HoldingsValue < 0 or self.Portfolio["GBPNZD"].HoldingsValue < 0: pass
HanByul P
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!