We are soliciting feedback for a proposed algorithm framework to sit on top of the QCAlgorithm class. This would be a pluggable system which would improve / extend the current API and give algorithms suggested separations - essentially helping divide a large algorithm into discrete pieces which you could copy & reuse between projects.
Most well formed algorithmic trading strategies have 4 distinct large components;
1. Asset Selection (hand picked, coarse universe or fine fundamentals).
2. Signal (indicator, filter, alpha etc).
3. Risk Control - Portfolio risk or Trade risk (often with volatility models).
4. Execution (slippage, fee models included).
We think it would help the community make great strategies if we provide a robust framework to plug these different classes together like lego blocks. We imagine you could make, reuse and share an execution plugin - completely separate to other parts of your strategy. This would let you test different plugins interchangably; or for example; plugin a better execution algorithm as you manage more capital on your algorithm.
By building from QCAlgorithm base class, all existing algorithms would continue to work, and all API methods would still be available in the Framework class. We are brain storming a rough architecture like this:
To ensure we cover all the possible plugin designs we'd like to get your ideas of different plugins and the data they might need; or the data they might output. This way we can design an ideal suite of plugin interfaces which will support 99% of use cases. To kick off this brainstorming we've made this flow chart below which seeds some ideas for the models.
We'd really welcome your feedback! This will enable awesome interchangability in your algorithms and let you experiment without re-coding or redesigning to each strategy.
Jared Broad
The images got down sampled in creating the post: this is the block diagram, and this is the code flow chart.
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.
Petter Hansson
I've made quite a few variants of these (obviously, much less feature complete) so I'll describe some common concerns of mine:
The Lean API would definitely become even more productive with additional standard "lego bricks" so to speak.
JayJayD
Maybe I'm missing something, but I cannot see the advantage repect to what Lean offers today. I mean, today you can "...divide a large algorithm into discrete pieces which you could copy & reuse between projects." by using classes for each one of the modules (alpha, Risk, etc.) and those classes (if they are SOLID) can be reused by other algorithms.
Jared Broad
Thanks guys for posts:
@Petter - I appreciate that a multi-algorithm setup is tricky today. Ideally that would be another class above the algorithm IMHO -- "MultiAlgorithmFramework()" which takes QCAlgorithms as inputs and handles the cash allocations and routes orders out the bottom. That way each could have its own stack described above and be managed / plugged independently?
"but there needs to be risk management interfering here" - like global / fund level risk management?
Potentially the multi-algorithm-framework could also have a risk control plugin at the bottom before it gets routed to the brokerage.
Now that we've finished the bulk of options and futures we're fixing bugs and will start adding depth everywhere across LEAN. This includes fill models and frameworks like the one above to make LEAN better.
@JayJayD - The key difference would be the plugins would be sharable. If I make a better execution model and share it to the community others could instantly use it with one line of code. This also allows us to make a GUI as all the models will be pluggable and follow defined interfaces.
ExecutionModel = new JayJayDBollingerExecution();
Also in the long term we'd like to make a market place of these components; so people could sell proprietary modules.
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.
JayJayD
" Also in the long term we'd like to make a market place of these components; so people could sell proprietary modules."
THAT will be awesome!
Jeffrey Margolis
Big supporter of the concept of a pluggable trading framework.
if done right, could allow for great levels of code reuse across the community, leading to increased robustness and the ability to potentially "mix and match" subcomponents.
Biggest challenge i see is clearly defining to interfaces between components. Primarily between the Volatility Model and the Signal Model. As this is effectively the link between a a suite of indicators to logic that interprets them.
In general, I would like to think about such a system as a graph of components. Many simple algorithms can likely overlook this construct, however, it can be a very powerful addition.
Also, when considering such a system, I would like to ensure support for both manually defined/tuned algorithms, as well as machine learning based components.
I am specifically interested in a component that could provide online scoring of a Signaling Model. This could then in turn be used as either feedback to the signaling model for online or batch based learning purposes, or as input to a Risk model evaluating multiple parallel Signaling Models.
Happy to elaborate or discuss these ideas in more detail if there is shared interest from the community.
WilliamN
@Petter Hansson
Robert Carver's book systematic trading answers 1&2 while rolling in kelly criterion. His modular framework written in python is on:
Jared Broad
Thanks @WilliamNhongo; we'll take some inspiration and make it better in LEAN.
@Jeffery; "clearly defining to interfaces between components." -- Agree. I tried to define those in the sketch above. The signal model currently defines the output as expected return for an asset and variance of that outcome. This should be generic enough for genetic and literally implemented strategies.
"input to a Risk model evaluating multiple parallel Signaling Models" -- interesting; that could be a good way of allowing an algorithm of algorithms.
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.
Petter Hansson
@WilliamNhongo: Interesting, I will have a look. It's definitely something that must be a very common problem with several open source solutions like this one.
@Jared: If I could somehow run several QCAlgorithms simulanteously (with some coordination layer) that would definitely be a quite clean solution.
By risk management, I was hinting at smart capital allocation that can e.g. limit how much capital is invested in a single security, even if all my sub-algorithms individually indicate that security should be invested into. Sorry if I'm misusing the term.
Just to reiterate on what I'm looking for: The ideal for me as an independent trader is if I can spend most of my time on alpha generation and not have to bother building my own execution and risk management code. Alpha generation is a difficult enough topic as it is.
Petter Hansson
On a side note, my most recent framework looks like this:
MyAlgorithm: Subclass of QCAlgorithm, "Server provider"/god class. Can be regarded a singleton at present.
Tradable: Wraps a Security since actions on a Security must be coordinated anyway.
Strategy: There are 1+ strategies per Tradable, currently having a strict prioritization scheme where only one strategy is enabled per Tradable only. It would obviously be better to allow multiple strategies.
A strategy requests a volatility adjusted capital allocation, determined by MyAlgorithm depending already allocated capital and some other factors.
Having multiple QCAlgorithm subclasses/instances instead of Strategy would be way cleaner I think because there is a large risk of strategies colliding when they are observing and manipulating their Tradable/Security (they might need to observe it even when disabled).
Dean Gutierrez
good idea
If you can make it like Lego Mindstorms visual programming, that would appeal to a very wide audience, esp those without coding experience. Basically building a strategy using a flow-chart interface and building with drag and drop elements.
Keith K
Seems like you are biting off more than you can chew.
One thing that I would *love* to see is the following extensions added to the existing Portfolio class:
bool canSell = Portfolio.AvoidWashSale(symbol); if ( canSell ) canSell = Portfolio.ObeyBloodySEC_T3_rules(symbol); if ( !canSell ) return;
For most new users, capital allocation (position sizing) and trade execution are cumbersome and boring to write.
They are also highly dependent on your strategy.
For example, my primary focus is a variation of a pairs trading strategy, which involves quite a lot of capital allocation.
Here's some of my pseudo-ish code
decimal cashToUse = swap.reason.cashAllocation - 100 - cashOnSideline; // limit to maxPortfolio percent. decimal currHoldings = Portfolio.Securities[swap.buyme.Symbol].Holdings.AbsoluteHoldingsValue; decimal clanLimit = swap.reason.maxPortfolio * swap.reason.TotalPortfolioValue; if (cashToUse > clanLimit - currHoldings) { cashToUse = clanLimit - currHoldings; if (cashToUse < 0) cashToUse = 0; } int quantity = (int)Math.Floor(cashToUse / this.rndup(swap.buyme.Close * swapLimitBuy));// 1.001m quantity -= quantity % 100; bool needBuy = (quantity >= 100); if (needBuy) { if (swap.reason.preferCash != PreferCash.No) needBuy = false; if (swap.buyme.T3_Countdown > 0) needBuy = false; // obey SEC 3 day rule! #warning TODO needBuy = AvoidWashSale(); #warning TODO needBuy = TaxLossHarvesting(); if (!needBuy) { swap.buyPrice = 0; //LogInfo(@"stay in cash " + rnd(Portfolio.Cash)); // + ", don't buy " + swap.buyme.Symbol ); if (swap.buyTicket != null) CancelBuySynchronously(swap); } } if ( needBuy ) { // begin trade execution phase }
bool canSell = Portfolio.AvoidWashSale(symbol); if ( canSell ) canSell = Portfolio.ObeyBloodySEC_T3_rules(symbol); if ( !canSell ) return;
bool canSell = Portfolio.AvoidWashSale(symbol); if ( canSell ) canSell = Portfolio.ObeyBloodySEC_T3_rules(symbol); if ( !canSell ) return;
Keith K
Just thinking about this 'plugin' concept some more.
The Lean codebase is an excellent example of a well thought out, well implemented framework.
What I've written on top of the Lean framework, even though it is a sensible group of classes, it is a chaotic mess of code that I would be embarrassed to share.
strategy: a QCAlgorith subclass, a TradeScore class
a RiskManagement subclass that roughly encapsulates position sizing & portfolio risk management
trade execution: SwapReason, SwapPair
My subclasses include absolutely *no* data hiding, and here's why:
1) At any point in the path from strategy through trade execution through to exiting a position, I need to be able to collect stats about my strategy so that I can log them later.
If a trade actually does make its way through the strategy => RiskManagement => Execution gauntlet, I then capture my own statistics about what strategy went into that trade. For example, RSI indicator value, Macd values, etc. etc.
Later on (after deciding to exit a position or after a certain holding period), I capture the stats into a record to be logged.
This process includes a *complex* reverse engineering of QC's TradeBuilder.ClosedTrades, especially Trade.ProfitLoss.
In my case, this is complex because I typically am rebalancing a portfolio of stocks using limit orders that are being filled drip-by-drip.
Once the algorithm is finished, I log each of these records stats in .csv format, cut and paste the log into a .csv file, and map the trades against their ProfitLoss in excel using scatter plots.
2) This is a fun project... Ain't no one got time for data hiding!
In the real world, trading using limit orders sucks, because a limit order from some retail schmuck like me tends to scare away the little fish, and attract sharks. At this point, writing a competitive trade execution phase seems harder than writing a self-driving car. You're trading against real-world pros who are out to get you.In the real world, trading using limit orders sucks, because a limit order from some retail schmuck like me tends to scare away the little fish, and attract sharks. At this point, writing a competitive trade execution phase seems harder than writing a self-driving car. You're trading against real-world pros who are out to get you.
By the way, my scatter plots revealed ghostly patterns in the noise.... subsequent adjustments = wildly huge Sharpe ratios! Curve fitting is easy. In the real world, my algorithm performed worse than SPY over the past 3 months. Oh wells, glad I wasn't using leverage!)(By the way, scatter plots reveal ghostly patterns in the noise.... subsequent adjustments = wildly huge Sharpe ratios! Curve fitting is easy. In the real world, my algorithm performed worse than SPY over the past 3 months. Oh wells, glad I wasn't using leverage!
Keith K
When I cut/paste, it pastes twice.When I cut/paste, it pastes twice.
Ian Worthington
I'm guessing this will extend the University's: Strategy Example: Complete Algorithm Framework?
Jared Broad
Ian we're thinking to design it from scratch with a more wholistic approach. There are a few fundamental differences between the QC University design and this one. We're trying to abstract away the different types of strategies into their core so you could use the same framework for a portfolio based strategy or a single stock scalping one.
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.
Sohail
I'd love to see the use cases you've got that is driving you to this design. For example:
Jared Broad
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!