optopsy

by michaelchu

michaelchu / optopsy

A nimble options backtesting library for Python

255 Stars 70 Forks Last release: 18 days ago (v2.0.0b2) GNU General Public License v3.0 148 Commits 7 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Downloads CircleCI Test Coverage Black

Optopsy

Optopsy is a nimble backtesting and statistics library for option strategies, it is designed to answer questions like "How do vertical spreads perform on the SPX?" or "Which strikes and/or expiration dates should I choose to make the most potential profit?"

This library is currently being rewritten and is in beta state, use at your own risk

Supported Option Strategies

  • Calls/Puts
  • Straddles/Strangles
  • Butterflies/Iron Condors (WIP)
  • Many more to follow

Documentation

Please see the wiki for API reference.

Usage

Use Your Data

  • Use data from any source, just provide a Pandas dataframe with the required columns when calling optopsy functions.

Dependencies

You will need Python 3.6 or newer and Pandas 0.23.1 or newer and Numpy 1.14.3 or newer.

Installation

pip install optopsy==2.0.0b2

Example

Let's see how long calls perform on the SPX on a small demo dataset on the SPX: Download the following data sample from DeltaNeutral: http://www.deltaneutral.com/files/SampleSPX20151001to20151030.csv

This dataset is for the month of October in 2015, lets load it into Optopsy. First create a small helper function that returns a file path to our file. We will store it under a folder named 'data', in the same directory as the working python file.

def filepath():
    curr_file = os.path.abspath(os.path.dirname(__file__))
    return os.path.join(curr_file, "./data/Sample_SPX_20151001_to_20151030.csv")

Next lets use this function to pass in the file path string into Optopsy's

csv_data()
function, we will map the column indices using the defined function parameters. We are omitting the
start_date
and
end_date
parameters in this call because we want to include the entire dataset. The numeric values represent the column number as found in the sample file, the numbers are 0-indexed: ``` import optopsy as op

spxdata = op.csvdata( filepath(), underlyingsymbol=0, underlyingprice=1, optiontype=5, expiration=6, quotedate=7, strike=8, bid=10, ask=11, ) ``

The
csvdata()
function is a convenience function. Under the hood it uses Panda's
read
csv()` function to do the import. There are other parameters that can help with loading the csv data, consult the code/future documentation to see how to use them.

Optopsy is a small simple library that offloads the heavy work of backtesting option strategies, the API is designed to be simple and easy to implement into your regular Panda's data analysis workflow. As such, we just need to call the

single_calls()
function to have Optopsy generate all combinations of a simple long call strategy for the specified time period and return a DataFrame. Here we also use Panda's
round()
function to return data within two decimal places.
long_single_calls = op.singles_calls(spx_data, strike_dist_pct_interval=0.05, side="long").round(2)

The function will returned a Pandas DataFrame containing the statistics of running long calls in all valid combinations on the SPX (The example below is truncated):

| | dterange | strikeotmpctrange | longprofitpctmin | longprofitpctmax | longprofitpctmean | longprofitpctmedian | longprofitpctstd | longprofitpctcount | |-----|-------------|------------------------|-----------------------|-----------------------|------------------------|--------------------------|-----------------------|-------------------------| | 0 | (0, 7] | (-1.0, -0.95] | -0.01 | 0.07 | 0.02 | 0.02 | 0.02 | 20 | | 1 | (0, 7] | (-0.95, -0.9] | -0.01 | 0.06 | 0.02 | 0.02 | 0.02 | 21 | | 2 | (0, 7] | (-0.9, -0.85] | 0 | 0.07 | 0.03 | 0.02 | 0.02 | 20 | | 3 | (0, 7] | (-0.85, -0.8] | -0.01 | 0.06 | 0.02 | 0.02 | 0.02 | 27 | | 4 | (0, 7] | (-0.8, -0.75] | 0 | 0.08 | 0.03 | 0.02 | 0.02 | 27 | | 5 | (0, 7] | (-0.75, -0.7] | -0.01 | 0.08 | 0.03 | 0.02 | 0.02 | 33 | | 6 | (0, 7] | (-0.7, -0.65] | -0.01 | 0.09 | 0.03 | 0.02 | 0.03 | 40 | | 7 | (0, 7] | (-0.65, -0.6] | -0.01 | 0.09 | 0.03 | 0.02 | 0.03 | 37 | | 8 | (0, 7] | (-0.6, -0.55] | -0.02 | 0.09 | 0.03 | 0.02 | 0.03 | 73 | | 9 | (0, 7] | (-0.55, -0.5] | -0.02 | 0.1 | 0.03 | 0.03 | 0.03 | 158 | | 10 | (0, 7] | (-0.5, -0.45] | -0.02 | 0.11 | 0.03 | 0.03 | 0.03 | 184 | | 11 | (0, 7] | (-0.45, -0.4] | -0.02 | 0.12 | 0.04 | 0.03 | 0.03 | 201 | | 12 | (0, 7] | (-0.4, -0.35] | -0.02 | 0.13 | 0.04 | 0.04 | 0.04 | 247 | | 13 | (0, 7] | (-0.35, -0.3] | -0.02 | 0.15 | 0.05 | 0.04 | 0.04 | 296 | | 14 | (0, 7] | (-0.3, -0.25] | -0.03 | 0.17 | 0.05 | 0.05 | 0.05 | 329 | | 15 | (0, 7] | (-0.25, -0.2] | -0.03 | 0.2 | 0.06 | 0.05 | 0.05 | 352 | | 16 | (0, 7] | (-0.2, -0.15] | -0.04 | 0.26 | 0.08 | 0.07 | 0.07 | 383 | | 17 | (0, 7] | (-0.15, -0.1] | -0.06 | 0.37 | 0.11 | 0.09 | 0.09 | 417 | | 18 | (0, 7] | (-0.1, -0.05] | -0.12 | 0.69 | 0.18 | 0.15 | 0.16 | 461 | | 19 | (0, 7] | (-0.05, 0.0] | -1 | 7.62 | 0.64 | 0.37 | 1.03 | 505 | | 20 | (0, 7] | (0.0, 0.05] | -1 | 68 | 2.34 | -0.89 | 8.65 | 269 | | 21 | (0, 7] | (0.05, 0.1] | -1 | -1 | -1 | -1 | 0 | 2 | | 22 | (7, 14] | (-1.0, -0.95] | 0.01 | 0.09 | 0.05 | 0.04 | 0.03 | 12 | | 23 | (7, 14] | (-0.95, -0.9] | 0.01 | 0.1 | 0.05 | 0.05 | 0.02 | 22 | | 24 | (7, 14] | (-0.9, -0.85] | 0.01 | 0.09 | 0.05 | 0.05 | 0.02 | 16 | | 25 | (7, 14] | (-0.85, -0.8] | 0.01 | 0.11 | 0.06 | 0.06 | 0.03 | 20 | | 26 | (7, 14] | (-0.8, -0.75] | 0.01 | 0.11 | 0.06 | 0.06 | 0.03 | 25 | | 27 | (7, 14] | (-0.75, -0.7] | 0.01 | 0.12 | 0.06 | 0.06 | 0.03 | 21 | | 28 | (7, 14] | (-0.7, -0.65] | 0.02 | 0.12 | 0.07 | 0.06 | 0.02 | 33 | | 29 | (7, 14] | (-0.65, -0.6] | 0.02 | 0.13 | 0.07 | 0.07 | 0.03 | 27 | | 30 | (7, 14] | (-0.6, -0.55] | 0.02 | 0.14 | 0.07 | 0.07 | 0.03 | 45 | | 31 | (7, 14] | (-0.55, -0.5] | 0.02 | 0.15 | 0.07 | 0.07 | 0.03 | 114 | | 32 | (7, 14] | (-0.5, -0.45] | 0.02 | 0.16 | 0.08 | 0.08 | 0.03 | 153 | | 33 | (7, 14] | (-0.45, -0.4] | 0.02 | 0.17 | 0.09 | 0.08 | 0.04 | 166 | | 34 | (7, 14] | (-0.4, -0.35] | 0.02 | 0.19 | 0.09 | 0.09 | 0.04 | 197 | | 35 | (7, 14] | (-0.35, -0.3] | 0.02 | 0.21 | 0.11 | 0.1 | 0.04 | 235 | | 36 | (7, 14] | (-0.3, -0.25] | 0.03 | 0.25 | 0.13 | 0.12 | 0.05 | 265 | | 37 | (7, 14] | (-0.25, -0.2] | 0.03 | 0.3 | 0.15 | 0.14 | 0.06 | 280 | | 38 | (7, 14] | (-0.2, -0.15] | 0.04 | 0.38 | 0.18 | 0.18 | 0.08 | 307 | | 39 | (7, 14] | (-0.15, -0.1] | 0.05 | 0.54 | 0.25 | 0.24 | 0.11 | 332 | | 40 | (7, 14] | (-0.1, -0.05] | 0.07 | 0.97 | 0.4 | 0.39 | 0.18 | 370 | | 41 | (7, 14] | (-0.05, 0.0] | -0.46 | 4.4 | 1.02 | 0.86 | 0.68 | 404 | | 42 | (7, 14] | (0.0, 0.05] | -1 | 32 | 1.52 | -0.73 | 4.45 | 388 | | 43 | (7, 14] | (0.05, 0.1] | -1 | -0.83 | -0.93 | -0.94 | 0.06 | 36 |

There are more customization options for Optopsy's strategy functions, consult the codebase/future documentation to see how it can be used to adjust the results, such as increasing/decreasing the intervals and other data to be returned.

The library is currently under development, as such expect changes to the API in the future.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.