Hi guys, I hope you're doing great.
I would like to know if there is a function that would help me get the current value of an individual asset on my Portfolio. Basically the purpose of this would be to Liquidate the asset if it has reached my Take Profit or my Stop Loss. I found a way to do this but affecting the entire Portfolio. Meaning that if at least one asset made my Portfolio loss 1% it would Liquidate all of the transactions.
class VerticalParticleRegulators(QCAlgorithm):
def Initialize(self):
"""
Initialization Code
"""
#Initial Portfolio Value
self.MaxPortfolioValue = self.Portfolio.TotalPortfolioValue
def OnData(self, data):
#Real Time take profit and trailing Stop.
currentPortfolioValue = self.Portfolio.TotalPortfolioValue
#Determine Take Profit
if currentPortfolioValue > self.MaxPortfolioValue:
if (currentPortfolioValue - self.MaxPortfolioValue)/self.MaxPortfolioValue > 0.02:
self.Liquidate()
#Specify the new Maximum portfolio value
self.MaxPortfolioValue = currentPortfolioValue
#Determine Stop Loss
else:
if (self.MaxPortfolioValue - currentPortfolioValue)/self.MaxPortfolioValue > 0.01:
self.Liquidate()
#Specify the new Maximum portfolio value
self.MaxPortfolioValue = currentPortfolioValue
I tried doing the following:
currentPortfolioValue = {}
currentPortfolioValue["EURUSD"] = self.Portfolio["EURUSD"].TotalPortfolioValue
However I'm getting the following error:
During the algorithm initialization, the following exception has occurred: AttributeError : 'ForexHolding' object has no attribute 'TotalPortfolioValue' at Initialize in main.py:line 83 :: self.MaxPortfolioValue[symbol] = self.Portfolio[symbol].TotalPortfolioValue AttributeError : 'ForexHolding' object has no attribute 'TotalPortfolioValue'Which makes sense as I shouldn't be requesting the Total Porfolio Value from a single asset.
Rodrigo Xavier G
Never mind, I got it. In case someone needs it, I'm attaching the resolution to the problem.
#RiskManagementRealTime for symbol in self.symbols: self.MaxPortfolioValue[symbol] = self.Portfolio.TotalPortfolioValue / len(self.symbols) def OnData(self, data): #self.Debug("Portfolio Invested: "+str(self.Portfolio["EURUSD"].Invested)) currentPortfolioValue = {} for symbol in self.symbols: currentPortfolioValue[symbol] = self.Portfolio[symbol].Price if currentPortfolioValue[symbol] > self.MaxPortfolioValue[symbol]: if (currentPortfolioValue[symbol] - self.MaxPortfolioValue[symbol])/self.MaxPortfolioValue[symbol] > 0.03: self.Liquidate(symbol) self.MaxPortfolioValue[symbol] = currentPortfolioValue[symbol] #self.Log("*****Take profit hit") else: if (self.MaxPortfolioValue[symbol] - currentPortfolioValue[symbol])/self.MaxPortfolioValue[symbol] > 0.01: self.Liquidate(symbol) self.MaxPortfolioValue[symbol] = currentPortfolioValue[symbol] #self.Log("*****Stop Loss hit")
Rodrigo Xavier G
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!