QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.04k stars 3.15k forks source link

Options Support #166

Closed jaredbroad closed 7 years ago

jaredbroad commented 8 years ago

Support for options trading. Broadly speaking this will have several pieces. Lets scope out the depth of the changes needed here:

PHASE 1: LEAN

Proposed User Facing

ToolBox:

PHASE 2: Brokerage and live trading support. TBD.

mchandschuh commented 8 years ago

@AlexCatarino - Also, how important is running daily volume for options price models?

lurium commented 8 years ago

Not very, although if you have Open Interest, that's somewhat useful -- not to derive theoretical values or greeks, but to understand the supply and demand and dealer inventory considerations

On Thu, Feb 25, 2016 at 3:57 PM, Michael notifications@github.com wrote:

@AlexCatarino https://github.com/AlexCatarino - Also, how important is running daily volume for options price models?

— Reply to this email directly or view it on GitHub https://github.com/QuantConnect/Lean/issues/166#issuecomment-188982812.

rbohac commented 8 years ago

I agree that open interest is particularly useful along with the bid/ask spread for liquidity

glyphard commented 8 years ago

IOptionPricingModel Approach makes sense to me as well. the mechanics of where the actual data inputs for the vol/dividend would come from are a little less clear since they are time varying at different scales. For instance depending on which submodel you choose, dividends can be discrete events, and vol changes tick by tick. OR dividends are steady/annual or quarterly rate based, and vol is updated daily.

In any event the choices for which approach submodel to use generally are a function of how what the consumer of the pricing data wants to do. In practice what some people do is use something that's 'wrong all the time' like the BSM model and just make a super simplifying assumption like all non observable inputs except implied vol are zero. Then use a numerical scheme to back out implied vol from those zero'd input assumptions with observed option pricecs. This approach has a name that I don't recall off hand, but it works fairly well for market data anytime withing the last 10 years or so, is quite common, and is simple to implement.

My suggestion is that you consider implementing this first, as a 'base' model, then extend the api and allow 'others' to add on the more complex things. This should help make the options implementation more tractable. If you need a reasonably fast BSM implementation, and don't have one, let me know, I'm happy to write one in c#... it's about 20 minutes worth of work, and another 15 minutes for unit tests, just make an issue and point me at it... happy to help if this is the way you decide to go.

On Thu, Feb 25, 2016 at 4:21 PM, Ray Bohac notifications@github.com wrote:

I agree that open interest is particularly useful along with the bid/ask spread for liquidity

— Reply to this email directly or view it on GitHub https://github.com/QuantConnect/Lean/issues/166#issuecomment-188993935.

mchandschuh commented 8 years ago

Here's some initial implementation for options!

AlexCatarino commented 8 years ago

I've been thinking about what a 'default' IVolatilityModel would look like. Say that the 'default' would calculate Implied Volatility using Black-Scholes Model. The volatility model would start with a seed value for sigma (volatility), then a Compute/Estimate/Fit method would call the BSM method (input parameter: double sigma, returns decimal optionPrice) until the value of sigma is close enough to the one that returns the current option price. I hope that answers what you asked.

I think we could take @glyphard's implementation, since he says it already done. I would translate into C# the Visual Basic code we find at Espen Haug's "The Complete Guide to Option Pricing Formulas".

lurium commented 8 years ago

I might have missed some of this thread but:

Most of the options we will likely deal with on QC will be American exercise, so BSM isn't the best choice.

What you want is a Binomial Tree for stock options and the Whaley Model for most American commodity futures options.

Codewise the you can just iterate as you mentioned, but a nice optimization is to use the vega (even the BSM vega as it's cheap to calculate) to do a smarter, Newton-Raphson type iteration.

You can use BSM for European options, but if you use it for American options, you'll often get different volatilities for calls and puts of the same strike.

On Fri, Feb 26, 2016 at 1:39 PM, Alexandre Catarino < notifications@github.com> wrote:

I've been thinking about what a 'default' IVolatilityModel would look like. Say that the 'default' would calculate Implied Volatility using Black-Scholes Model. The volatility model would start with a seed value for sigma (volatility), then a Compute/Estimate/Fit method would call the BSM method (input parameter: double sigma, returns decimal optionPrice) until the value of sigma is close enough to the one that returns the current option price. I hope that answers what you asked.

I think we could take @glyphard https://github.com/glyphard's implementation, since he says it already done. I would translate into C# the Visual Basic code we find at Espen Haug's "The Complete Guide to Option Pricing Formulas".

— Reply to this email directly or view it on GitHub https://github.com/QuantConnect/Lean/issues/166#issuecomment-189417882.

jaredbroad commented 8 years ago

@lurium - The point of the thread is mostly to seek feedback on architecture plans / designs - to ensure we fully support the various ways people want to use options. This also applies the API / how people want to access the option data.

e.g. Discussing various model implementations is important so we understand the data required to calculate these models. This way we can make sure LEAN supports as many models as possible (is as generic and extendible as possible). We also want to support international markets and all security types.

lurium commented 8 years ago

Ok, then you want something like this.

Black-Scholes: European Exercise -- Indexes SPX, VIX, weeklies at the CME Whaley: American Exercise -- Indexes with Continuous Divs or Commodities (Gold, Oil at the CME, etc) Binomial-Tree: American Exercise -- Stock options with discrete divs (MSFT, SPY, etc)

Essentially you have to gather or somehow discover information about both the underlying contract and the terms of the option contract to pick the proper model.

You could simplify this a little, but at the price of performance -- A Special Binomial Tree will price all of the above, it's just really slow.

lurium commented 8 years ago

I should also note that there are quite a few other option models, especially for fixed income and spot-forex, but these Big 3 will get you a long way. The inputs are very similar. I could probably point the thread to some decent implementations if you guys are interested.

jaredbroad commented 8 years ago

Just an update; options backtesting is done, we're working on getting options trading support in live mode as well and then can publish it live.

rmagen commented 7 years ago

Great news.

  1. Does that include support for futures? (On master they don't seem to be supported yet..)
  2. When can we expect to have support for live options trading?
  3. I'd be happy to help move this feature along. Anything I can do?
jaredbroad commented 7 years ago

Hi @rmagen - futures aren't done yet. @quant1729 is leading the push to finish off options & futures.

We'd welcome your help! A good PR/issue would be updating the IQFeed toolbox project to subscribe to options contracts and push the BaseData packets into the IDataQueueHandler. It is one of the steps on the path to live options trading and helps LEAN support options locally.

@AnObfuscator has created a new AlgoSeekOptionConverter and we'll be crunching the data this week! We have 400TB to chew through so it might take some time :)

AnObfuscator commented 7 years ago

The code is about as good as it's going to get. I just need to finish some testing today, and I'll fix any issues and submit a pull request.

jaredbroad commented 7 years ago

Its embarrassing this has taken a whole year but we're 99% complete thanks to efforts of @quant1729 on the options and futures branch. We should have this merged into LEAN soon :)

tedpenner commented 7 years ago

Thank you. this is great news. I understand from my developer that there is no bid or ask data at all. Is that accurate? Where do you guys stand on that?

On Sep 29, 2016 6:08 PM, "Jared" notifications@github.com wrote:

Its embarrassing this has taken a whole year but we're 99% complete thanks to efforts of @quant1729 https://github.com/quant1729 on the options and futures branch. We should have this merged into LEAN soon :)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/QuantConnect/Lean/issues/166#issuecomment-250617586, or mute the thread https://github.com/notifications/unsubscribe-auth/AAMeA3s3vBPxPolAHxY7bNVI3H5-uj9Bks5qvEUDgaJpZM4F-ApW .

jaredbroad commented 7 years ago

Since options are not liquid for options we have made QuoteBars which have enough information for accurate fills. https://github.com/QuantConnect/Lean/tree/options-and-futures

bobobo1618 commented 7 years ago

So options support seems to be in and working (hurray!) but how does one add data for it? I couldn't figure out the format.

jaredbroad commented 7 years ago

Options data has tradebars, quotebars and open interest files. We provide a converter for data purchased from AlgoSeek (https://github.com/QuantConnect/Lean/tree/master/ToolBox/AlgoSeekOptionsConverter). If you have other raw options data files please contribute a converter for your format! :)

jaredbroad commented 7 years ago

Closing as feature implementation completed; and we're now in bug fixes mode.