Hi all,I would like to ask if someone has been worked on that or point out where is my mistake, here is the situation.We want to give allocations to some assets based on the current regime, for example:At the end of every day, we want to do a daily asset allocation with no leverageif the market is in regime A, we want to hold ½ SPY and ½ QQQ.if the market is in regime B, we want to hold ⅓ GLD, ⅓ SPY and ⅓TLTif the market is in regime C, we want to hold ⅓ VXX, ⅓ IWM and ⅓ GLDhence, my code is something like this:def order_excution(self): if self.regime == ‘A’: self.SetHoldings([PortfolioTarget("SPY", ½), PortfolioTarget("QQQ", ½)])
elif self.regime == ‘B’:
self.SetHoldings([PortfolioTarget("GLD", ⅓), PortfolioTarget("SPY", ⅓), PortfolioTarget("TLT", ⅓) ])
else: # regime == C self.SetHoldings([PortfolioTarget("VXX", ⅓), PortfolioTarget("IWM", ⅓), PortfolioTarget("GLD", ⅓)])def OnSecuritiesChanged(self, changes): '''Liquidate symbols that are removed from the dynamic universe ''' for security in changes.RemovedSecurities: if security.Invested: self.Liquidate(security.Symbol, 'Removed from universe') However, some orders can't be filled because the leverage > 2, what's wrong with this code.. thank you!
Arthur Chen
The formatting has some issues, here is the tidy one
Hi all, I would like to ask if someone has been worked on that or point out where is my mistake, here is the situation. We want to give allocations to some assets based on the current regime,
For example: At the end of every day, we want to do a daily asset allocation without leverage
1. if the market is in regime A, we want to hold ½ SPY and ½ QQQ
2 .if the market is in regime B, we want to hold ⅓ GLD, ⅓ SPY and ⅓TLT.
3. if the market is in regime C, we want to hold ⅓ VXX, ⅓ IWM and ⅓ GLD
Hence, my code is something like this:
def order_excution(self):
if self.regime == ‘A’:
self.SetHoldings([PortfolioTarget("SPY", ½), PortfolioTarget("QQQ", ½)])
elif self.regime == ‘B’:
self.SetHoldings([PortfolioTarget("GLD", ⅓), PortfolioTarget("SPY", ⅓), PortfolioTarget("TLT", ⅓) ])
else: # regime == C
self.SetHoldings([PortfolioTarget("VXX", ⅓), PortfolioTarget("IWM", ⅓), PortfolioTarget("GLD", ⅓)])
def OnSecuritiesChanged(self, changes):
'''Liquidate symbols that are removed from the dynamic universe'''
for security in changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol, 'Removed from universe')
However, some orders can't be filled because the leverage > 2, what's wrong with this code.. thank you!
Yuri Lopukhov
I think the problem is that you do not close existing positions, try adding all symbols you are using to all SetHoldings targets, but use 0 for ones you don’t need, so that their positions could be liquidated.
Arthur Chen
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!