**theoretical option backtesting framework**which I was working on. This should help, or otherwise speed up, the development of the formal inclusion of options in QuantConnect. Specifically if actual option data is included in QuantConnect, my code could help to derive the option's theoretical properties, such as the greeks. This because I've already implemented all such theoretical formulas in my code (with help from this forum thread).

As the title states, I've tried to create a theoretical option backtesting framework, in which options are calculated using the price of the underlying and the famous Black-Scholes model. Clearly, the greeks do not work if you don't have the

*actual*price of the option, but I've included them anyway (as to help the development of the formal option framework).

In order to use my framework, one must specify how many options you want to use in the algorithm. That is, one must specify how many in-the-money and out-of-money options the algorithm should calculate (these are based on CALLS !!!). Basically, this specifies the depth of options that one will use at any particular moment in the maturity of an option.

*Note: the depth is based on the current price.*Likewise, one must also specify how many maturities in the future the framework should calculate. In a similar manner, one should specify whether the algorithm should use "MONTHLY" options or "WEEKLY" options (yes, both exist!).

`Resolution _res = Resolution.Daily;`

string[] options = {"CALL", "PUT"};

// type of option maturities (also: "WEEKLY")

string _maturityType = "MONTHLY";

// number of option strikes that you want to include

int _nof_strikes = 3;

// number of option maturities that you want to include

int _nof_maturities = 3;

To give you an example, if you specify "

**MONTHLY**",

**_nof_strikes**= 3,

**_nof_maturities**= 3, the framework will, at each maturity date, calculate the values of 3 in-the-money options and 3 out-of-money options, each for 3 maturities (months!) in the future. So these will be the only options that you can then use in your algorithm.

But how to use any given option? Simple:

`Order("SPY.CALL.1.2", 80*100);`

This means:

**buy 80 CALL option contracts on SPY, with the nearest in-the-money strike and the second nearest maturity**.

Easy right? In the same manner,

**Order("SPY.CALL.-1.1", 80*100);**buys the nearest out-the-money strike with the nearest maturity.

So, what would a bull spread using CALLS look like?

`// buy one call with a low strike (at some maturity)`

Order("SPY.CALL.-1.2", 80*100);

// sell (also called "write") calls with a higher strike (at same maturiy)

Order("SPY.CALL.1.2", -80*100);

So, quite straightforward.

I've also included the possibility to make the algorithm download interest rates from QUANDL, to make the theoretical prices more accurate. Likewise, it is also possible to specify a

*dividend yield*for a given stock, as to correct the theoretical prices for that. This goes as follows:

`// Second parameter: interest rate. Third parameter: dividend yield`

OptionSettings.Initialize(this, 0.05M, 0.01M);

My framework, however, is

*far from perfect*. Actually, I wasn't finished yet but after reading the announcement today I've tried to finish a rough version of it as to help the development of the actual framework. One problem that still exists is that I'm still getting

**asset price is $0. If using custom data make sure you've set the 'Value' property.**errors, although I specifically make sure that my option price is always larger or equal than 0.01. This seems to appear especially when the data 'runs through' periods before the sample period. Apparently the LEAN framework always goes through all data before arriving at the sample period of the algorithm. Clearly, this yields a problem when the asset is theoretical and calculated based on the prices

*in*the sample period. I can't get this error away. However,

**it still works fine!**. The errors appear, but the framework seems to work fine nevertheless.

I hope you guys enjoy. At least, it might help out Jared & team to prep the formal option backtesting framework. Enjoy :)