Hi Folks,
I am a beginner here. Have been running some simple algos to familarize myself. However i have run into an issue i dont seem to be quite "getting it". My Idea is a simple one, buy equal priced "SPY" and "BND". and if one of them goes up in price, sell a fraction and buy the other to bring both of them to an equal level. I am unable to figure out a method to convey this in an Alpha model. As i do not seem to know any idicator that already does this. My code thusfar. Appreciate any help or any pointers. Thanks
from QuantConnect import Market, Resolution, SecurityType, Symbol
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Orders.Fees import ConstantFeeModel
from QuantConnect.Algorithm.Framework.Selection import ManualUniverseSelectionModel
from QuantConnect.Algorithm.Framework.Portfolio import EqualWeightingPortfolioConstructionModel
from QuantConnect.Algorithm.Framework.Execution import ImmediateExecutionModel
from QuantConnect.Algorithm.Framework.Risk import MaximumDrawdownPercentPortfolio, MaximumDrawdownPercentPerSecurity
from QuantConnect.Algorithm.Framework.Alphas import AlphaModel, Insight, InsightDirection
from datetime import timedelta
class MyAlphaModel(AlphaModel):
def OnSecuritiesChanged(self, algorithm, changes):
def Update(self, algorithm, data):
class EqualPortfolio(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2016, 1, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
_symbols = ["SPY", "BND"]
#self.symbols = [ self.AddEquity(s, Resolution.Day).Symbols for s in _symbols ]
#self.spy, self.tlt, self.gld = self.symbols[0], self.symbols[1], self.symbols[2]
self.UniverseSettings.Resolution = Resolution.Daily
symbols = [ Symbol.Create(s, SecurityType.Equity, Market.USA) for s in _symbols ]
self.SetSecurityInitializer(lambda security: security.SetFeeModel(ConstantFeeModel(0)))
self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
self.SetAlpha(MyAlphaModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetRiskManagement(MaximumDrawdownPercentPortfolio(0.02))
self.SetExecution(ImmediateExecutionModel())
Int 0x80
class MyAlphaModel(AlphaModel): def __init__(self): self.symbols = [] def OnSecuritiesChanged(self, algorithm, changes): for security in changes.AddedSecurities: symbol = security.Symbol if symbol not in self.symbols: self.symbols.append(symbol) for security in changes.RemovedSecurities: symbol = security.Symbol if symbol in self.symbols: self.symbols.remove(symbol) def Update(self, algorithm, data): insights = [] for symbol in self.symbols: insights.append(Insight.Price(symbol, timedelta(1), InsightDirection.Up)) return insights
Fatfingered copying my alphamodel. Here is the updated version.
Vladimir
Int 0x80,
Not sure if this is the best way, but at least it work.
Shile Wen
Hi Int 0x80,
The EqualWeightingPortfolioConstructionModel automatically rebalances the portfolio to have equal holdings of the universe.
Best,
Shile Wen
Int 0x80
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!