Hi Everyone,
I'm currently working on an allocation strategy (and framework) between algorithms. Just to illustrate this: Imagine you have one strategy for ranging markets and one for trending markets and some logic/idea on how to switch between those based on how they perform, how do you do that without too much spaghetti code.
Has anyone done that already? Did I miss something in the Lean framework that would allow me to observe the performance of substrategies and switch their weight?
My problem:
I have several QCAlgorithm developed independently, but I would like to switch my cash allocation between those automatically from 0 to 100%. Currently, I do this manually between different IB accounts but I am therefore limited to a handful of strategies for practical reasons.
My goal:
Basically, my allocation model treats each individual algorithm as an asset and then trades/allocate each of those algorithms (long obviously ?). This is more or less a momentum model applied on Portfolios, depending on market regimes some strategies perform better than others.
The issue with the insight model is that while we can have many in a single QCAlgorithm, there is no performance record (that I know of) that can be linked to an Alpha and observed through time to make a decision on the weight to use on the insight.
My ideas/solution/work in progress!
I'm working on a Algo class that implements IAlgorithm and mimics QCAlgorithm so that porting/converting code from a standalone strategy to a multi-strategy is trivial.
I wish we could have multiple QCalgorithm within a project but it is not allowed.
I have a main QCalgorithm that create the many individual Algo classes and dispatches messages to them (OnData mostly) and treats each Strategy as external data with AddData which allows me to treat them as blackbox assets.
I've created a DummyPortfolio that is a much-simplified version of PortfolioSecurityManager and replace the portfolio objection the IAlgorihm implementation. The SetHoldings/Order methods then place trades in this DummyPortfolio recording the algorithm perfomance. There is an additional method that allows for the main QCAlgorithm to query trades that have been made by the strategy. (The DummyPortfolio also deals - or will deal! - with splits and dividends)
The main QCAlgo then decides which weight to use for each Algos and queries trades made by each algo and make convert them in the "real/main" Portfolio.
-----
There are many limitations to this way of doing things (I may not have thought of all of them), what annoys me is the need to duplicate/reimplement some of the Lean code... which may be due to my lack of knowledge/understanding of its inner workings!
I hope this is clear enough. My code is not yet at a stage where I can post anything, but before I commit too much time, I wanted to see if anyone had done something similar successfully before or if anyone can think of a better/simpler way to do it?
Any comments or ideas are appreciated!
Shile Wen
Hi AlphAngel,
In our InternFund posts we demonstrate how to have multiple strategies with different allocations. To have the allocations be dynamic, we can simply add logic to change the allocation ratios, e.g. IF condition (i.e. 200-50 SMA cross) THEN set new allocation ratio (i.e. allocate more to the leveraged ETF strategy).
Best,
Shile Wen
AlphAngel
Hi Thanks Shile,
Interesting post, however this is not exactly what I want, I would want to change the allocation ratios based on the individual performance of each underlying strategy,
As an example, Let's say that the 60:40 strategy performance has crossed above its MA50 (or any performance related logic), then allocate 0.5 of portfolio, Between MA50 and MA200 then 0.3 below MA200 allocation is 0% but I still would want to "monitor" its theoretical recovery to reallocate to 0.3/0.5. Does that make sense?
Then the TT_AR strategy would follow a similar pattern independently...
Shile Wen
Hi AlphAngel,
We would have to manually update SMA's with the performance. The performance values for a specific strategy would need to be manually calculated using the performance of the securities bought and sold by the strategy.
Best,
Shile Wen
AlphAngel
Yes that's what I'm doing but I find myself having to rewrite a version of the PortfolioSecurityManager, dealing with fees, splits, etc... ... and a wrapper for Alphas if I want to use them.
Samuel Slusher
AlphAngel I think this old thread will give some good insight.
If you treat each strategy as a class, it can maintain an internal state (current trades, past trades, etc.) which you can pass data into and return new trades/positions. Then just sum up the new positions and place them as such (that way you are not going short and long the same asset from different strategies and incurring additional fees rather than just a net trade amount). This allows more flexability without having to re-write a portfolio constructor. Also, this would allow you to maintain a running performance curve within that class that you can use for meta-allocation. That is a huge point, as you can use different models or criteria to determine allocation weights and which strategies get what amount of capital.
AlphAngel
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!