Hi There,
I've found other forum posts describing how to use SetHoldings for rebalancing a portfolio on a given date, weekly, monthly, etc. However, I am having trouble finding an example using the Algorithm Framework, only executing trades/rebalancing on a monthly basis when implementing a custom PortfolioConstructionModel. Do I need to keep track of the timespan somehow in my main algorithm file in Initialize()? Is there someway to add this logic to the PortfolioConstructionModel itself? Thank you!
Gurumeher Sawhney
The PortfolioConstructionModel is meant to receive Insight objects from the alpha model and create PortfolioTargets. If the algorithm needs to trade/rebalance on a monthly basis, try emitting insights once a month and then the PortfolioConstructionModel will create the targets/rebalance on a monthly basis. One possibility could be using a boolean that stops any trading decisions from being made and change the boolean to True once a month,
Filib Uster
What triggers the Update method in Alpha Model? And why isn't it possible to user-define the frequency with which it is called?Â
Jack Simonson
Hi Filib,
The Update method gets called as frequently as the smallest resolution data in your Universe (i.e. Hour, Minute, etc.). You can force it to ignore data slices and only run the code within at certain times, and I've added an example of this to your post here.
Grimreaper
Jack Simonson Could you please post your code/backtest here?
Daniel Chen
Hello Grimreaper,
Thanks for your support! You could find Jack's code and backtest, the accepted answer, in this link by clicking buttons of the chart, statistics, and code Â
If there's a problem, please let me know. Hope it helps!
Eugene
Hi There,
i am facing the same issues.
In the w/o framework environment the frequency of the universe construction can be easily controlled by boolean variables combined with the time object.Â
Accordingly, the rebalancing can be easily controlled within the OnSecirityChanged method and is automatically synchronized with the universe construction.Â
Now, within the frameworkÂ
are called in the resolution of the algorithm indipendently from each other.
E.g. the PortfolioConstructionModel is called even though there was no change in the AlphaModel, and rebalances the portfolio based on the price changes.Â
Question1: What is the best way to have a rebalancing e.g. once a month or twice a year synchronized with the method OnSecurityChange.
Question2: What is the best way to calculate the alpha period time-span-value if it should be just valid until the next universe construction. e.g. reconstruction/rebalancing on the 1st Jan. and on the 1st July.
Cu Eugene
Martin Molinero
Hi Eugene,
I'm attaching a backtest which just rebalances on security changes using the EqualWeightingPortfolioConstructionModel.
Also, it might be interesting for you to take a look at this rebalancing method PortfolioRebalanceOnCustomFuncRegressionAlgorithm which rebalances based on the total deviation of securities from their target. There are other examples too, for specific date rules you can see PortfolioRebalanceOnDateRulesRegressionAlgorithm which rebalances every Wednesday.
Hope it helps!
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.
Eugene
Hi Martin,
thx. that helped!Â
Eugene
Hi,
further questions regarding the framework.
Question1:
What is the best way to calculate the alpha period time-span-value if it should be just valid until the next universe construction. e.g. reconstruction/rebalancing on the 1st Jan., 1st Feb., 1st Match,... and so on.?
Right now I just set it to 1 day. This is possible because the rebalancing is done OnSecurityChange
Settings.RebalancePortfolioOnInsightChanges = false; Settings.RebalancePortfolioOnSecurityChanges = true; SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel(time => null));
But I cant imagine this is the way it should be, or a good algorithm style should look like.
I tried also to calculate the amount of trading days between rebalancing events:
var tradingDaysTillNextSelection = Time.EachTradeableDay(algorithm.Securities[algorithm.Symbol("SPY")].Exchange.Hours, rebDate, nextRebDate).Count();
This doesnt work out, because of some overlapping days resulting in unnecessary trades in the overlapping time.
Question2:Â
What is the best way the generate order tags in the framework environment?
e.g. How to add an indicator value which was generated in the alphamodel to an order tag? Whats the best way to do it?Â
Thx again
Jared Broad
Hey Eugene!Â
1. A few weeks ago we added an explicit function to allow you to control the rebalancing as accurately as you needed. You can see more on this from the author Martin in this discussion thread.
2. Orders and insights are disconnected in the framework environment. There is a clear separation of concerns from the alpha model which simply generates the predictions to the portfolio construction who generates targets, and finally execution who fills those objectives. There is no way to tag an order with the insight[s] which "influenced its creation".
2.1 You might be able to manually override the Portfolio Construction model to assign a custom portfolio target object and include a string with it. This is definitely in your algorithm-land though! If you'd like Insight-Order control I'd recommend just using the classic style.Â
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.
P Chen
Sorry for bumping an old thread but I'm attempting to do exactly what Jared has described in 2.1 above. Any suggestion about how we could go about "assigning a custom portfolio target object"? Would it be something like:
# in PCM for insight in insights: target = PortfolioTarget.Percent(algorithm, insight.Symbol, quantity) target.Tag = f"Emitted from {insight.SourceModel}" # in Execution model for target in targets: # calculate remaining quantity to be ordered quantity = OrderSizing.GetUnorderedQuantity(algorithm, target) if quantity != 0: algorithm.MarketOrder(target.Symbol, quantity, target.Tag) #
Â
Shile Wen
Hi P Chen,
This is actually not possible as the PortfolioTarget class doesn't have a Tag field, and it's not possible to create a custom portfolio target object at the moment.
Best,
Shile WenÂ
Taylor Jensen
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!