I was trying to find the right syntax for allocating holdings within a rebalancing strategy.
class ModulatedMultidimensionalContainmentField(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2009, 6, 20) # 2013/12/01 Earliest start date for all ETFs in universe 2/1/10
self.SetEndDate(2020, 6, 20)
self.SetCash(250000000)
self.SetBenchmark("ABC")
self.SetWarmup(400)
self.AddEquity("ABC", Resolution.Minute)
self.AddEquity("XYZ", Resolution.Minute)
self.AddEquity("SAM", Resolution.Minute)
self.Strategies = [self.Strategy_1, self.Strategy_2, self.Strategy_3, self.Strategy_4]
self.Strategy_1_Size = self.Portfolio[self.Strategy_1]/self.Portfolio.TotalPortfolioValue
self.Strategy_2_Size = self.Portfolio[self.Strategy_2]/self.Portfolio.TotalPortfolioValue
self.Strategy_3_Size = self.Portfolio[self.Strategy_3]/self.Portfolio.TotalPortfolioValue
self.Strategy_4_Size = self.Portfolio[self.Strategy_4]/self.Portfolio.TotalPortfolioValue
#This is where I am confused. How can I find how much of the portfolio is dedicated to a particular strategy at any given time?
self.strategy_1_portion = 0.25
self.strategy_2_portion = 0.25
self.strategy_3_portion = 0.25
self.strategy_4_portion = 0.1
#would like to do something like this if possible:
self.Schedule.On(
self.DateRules.MonthStart("ABC"),
self.TimeRules.AfterMarketOpen("ABC", 120),
self.AllocationStation
)
self.Schedule.On(
self.DateRules.EveryDay("ABC"),
self.TimeRules.AfterMarketOpen("ABC", 150),
self.Strategies
)
def AllocationStation(self):
self.SetHoldings(self.Strategy_1, self.strategy_1_portion)
self.SetHoldings(self.Strategy_2, self.strategy_2_portion)
self.SetHoldings(self.Strategy_3, self.strategy_3_portion)
self.SetHoldings(self.Strategy_4, self.strategy_4_portion)
def Strategy_1(self):
{Random Strategy}
if something happens, then:
self.SetHoldings("ABC", 0.4*(self.Strategy_1_Size))
self.SetHoldings("XYZ", 0.6*(self.Strategy_1_Size))
elif something happens:
self.SetHoldings("ABC", 0.25*(self.Strategy_1_Size))
self.SetHoldings("XYZ", 0.75*(self.Strategy_1_Size))
else:
self.SetHoldings("ABC", 0*(self.Strategy_1_Size))
self.SetHoldings("XYZ", 0*(self.Strategy_1_Size))
def Strategy_2(self):
{Random Strategy}
if something happens, then:
self.SetHoldings("ABC", 0.1*(self.Strategy_1_Size))
self.SetHoldings("XYZ", 0.9*(self.Strategy_1_Size))
def Strategy_3(self):
{Random Strategy}
if something happens, then:
self.SetHoldings("ABC", 0.2*(self.Strategy_1_Size))
self.SetHoldings("SAM", 0.7*(self.Strategy_1_Size))
def Strategy_4(self):
{Random Strategy}
if something happens, then:
self.SetHoldings("SAM", 0.9*(self.Strategy_1_Size))
self.SetHoldings("XYZ", 0.1*(self.Strategy_1_Size))
#I want to set a regular rebalancing between multiple strategies referencing the same portfolio
#If this is possible I would assume that my strategies would have to be rewritten to reference
#the respective strategy's portion of the portfolio itself, rather than simply SetHoldings()
Shile Wen
Hi Samuel,
We cannot set the holdings on a particular strategy, only to a particular security. So to put half (50%) of our portfolio to AAPL (the stock of Apple), we would do self.SetHoldings(‘AAPL, .5). On the same vein, because we can only allocate percentages to specific securities, we will not be able to determine our allocation to a specific strategy.
Best,
Shile Wen
Adam W
As Shile mentioned, there are no built-in methods in the API for that. However you could try defining some dictionary of dictionaries like
from collections import defaultdict # Mapping of Strategy:Symbol:Holdings holdingsByStrategy = defaultdict(lambda: defaultdict(int))
in Initialize(), then when each strategy fires, you update the value with the quantity of holdings. You would want to manually calculate order sizing though based on current portfolio value and asset price instead of SetHoldings, You can then plot/log this dictionary for inference.
Samuel Schoening
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!