opt

optopsy

A nimble options backtesting library for Python

Showing:

Popularity

Downloads/wk

0

GitHub Stars

468

Maintenance

Last Commit

4mos ago

Contributors

5

Package

Dependencies

2

License

GPL-3.0-or-later

Categories

Readme

Test Coverage Maintainability CircleCI Downloads Black

Optopsy

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

Use cases for Optopsy:

  • Generate option strategies from raw option chain datasets for your own analysis
  • Discover performance statistics on percentage change for various options strategies on a given stock
  • Run backtests on option strategies based on entry conditions generated from Optopsy (Planned)

I made this little tool for free, but you can still make a donation if you want to show your support :)

Donate with Ethereum

Supported Option Strategies

  • Calls/Puts
  • Straddles/Strangles
  • Vertical Call/Put Spreads
  • Butterflies (Planned)
  • Iron Condors (Planned)
  • 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.1

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/Sample_SPX_20151001_to_20151030.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

spx_data = op.csv_data(
    filepath(),
    underlying_symbol=0,
    underlying_price=1,
    option_type=5,
    expiration=6,
    quote_date=7,
    strike=8,
    bid=10,
    ask=11,
)

The csv_data() 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 long_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 afterwards to return statistics within two decimal places.

long_calls_spx_pct_chgs = op.long_calls(spx_data).round(2)

The function will returned a Pandas DataFrame containing the statistics on the percentange changes of running long calls in all valid combinations on the SPX:

dte_rangeotm_pct_rangecountmeanstdmin25%50%75%max
0(0, 7](-0.5, -0.45]1550.030.02-0.020.010.020.040.11
1(0, 7](-0.45, -0.4]2010.040.03-0.020.010.030.060.12
2(0, 7](-0.4, -0.35]2470.040.03-0.020.020.040.070.13
3(0, 7](-0.35, -0.3]2960.050.04-0.020.020.040.080.15
4(0, 7](-0.3, -0.25]3290.050.05-0.030.020.050.090.17
5(0, 7](-0.25, -0.2]3520.060.05-0.030.020.050.10.2
6(0, 7](-0.2, -0.15]3830.080.07-0.040.030.070.130.26
7(0, 7](-0.15, -0.1]4170.110.09-0.060.040.090.170.37
8(0, 7](-0.1, -0.05]4610.180.16-0.120.070.150.280.69
9(0, 7](-0.05, -0.0]5050.641.03-10.140.370.877.62
10(0, 7](-0.0, 0.05]2692.348.65-1-1-0.891.1668
11(0, 7](0.05, 0.1]2-10-1-1-1-1-1
12(7, 14](-0.5, -0.45]700.060.030.020.030.070.080.12
13(7, 14](-0.45, -0.4]1650.090.040.020.060.080.10.17
14(7, 14](-0.4, -0.35]1970.090.040.020.070.090.120.19
15(7, 14](-0.35, -0.3]2350.110.040.020.090.10.130.21
16(7, 14](-0.3, -0.25]2650.130.050.030.10.120.150.25
17(7, 14](-0.25, -0.2]2800.150.060.030.110.140.180.3
18(7, 14](-0.2, -0.15]3070.180.080.040.140.180.230.38
19(7, 14](-0.15, -0.1]3320.250.110.050.180.240.310.54
20(7, 14](-0.1, -0.05]3700.40.180.070.290.390.520.97
21(7, 14](-0.05, -0.0]4041.020.68-0.460.580.861.324.4
22(7, 14](-0.0, 0.05]3881.524.45-1-0.99-0.732.6532
23(7, 14](0.05, 0.1]36-0.930.06-1-1-0.94-0.87-0.83
24(14, 21](-0.5, -0.45]60.10.010.090.090.10.10.1
25(14, 21](-0.45, -0.4]660.140.040.090.110.140.170.23
26(14, 21](-0.4, -0.35]910.160.040.10.120.160.20.25
27(14, 21](-0.35, -0.3]1350.180.050.110.130.170.210.28
28(14, 21](-0.3, -0.25]1490.20.050.120.150.20.250.33
29(14, 21](-0.25, -0.2]1600.240.060.140.180.230.290.4
30(14, 21](-0.2, -0.15]1740.30.080.170.230.290.350.51
31(14, 21](-0.15, -0.1]1870.40.110.220.30.380.480.7
32(14, 21](-0.1, -0.05]2110.630.190.320.470.60.751.16
33(14, 21](-0.05, -0.0]2291.390.530.5811.31.733.1
34(14, 21](-0.0, 0.05]2522.582.92-1-12.724.5610.1
35(14, 21](0.05, 0.1]93-0.820.92-1-1-1-16.39
36(21, 28](-0.5, -0.45]10.11nan0.110.110.110.110.11
37(21, 28](-0.45, -0.4]210.150.030.110.120.150.170.23
38(21, 28](-0.4, -0.35]390.20.060.120.160.180.240.32
39(21, 28](-0.35, -0.3]610.210.060.130.170.20.260.35
40(21, 28](-0.3, -0.25]750.250.080.140.20.240.310.41
41(21, 28](-0.25, -0.2]790.30.090.170.230.270.370.49
42(21, 28](-0.2, -0.15]870.370.110.20.290.340.450.62
43(21, 28](-0.15, -0.1]930.480.150.260.370.460.580.85
44(21, 28](-0.1, -0.05]1050.740.240.360.560.710.891.39
45(21, 28](-0.05, -0.0]1141.450.540.621.051.341.733.28
46(21, 28](-0.0, 0.05]1252.973.38-11.292.584.2117.15
47(21, 28](0.05, 0.1]850.825.3-1-1-1-119.5
48(28, 35](-0.4, -0.35]50.310.010.30.30.310.320.32
49(28, 35](-0.35, -0.3]70.340.010.320.330.350.350.36
50(28, 35](-0.3, -0.25]120.390.020.360.370.390.40.42
51(28, 35](-0.25, -0.2]130.460.020.420.440.450.470.49
52(28, 35](-0.2, -0.15]140.550.040.50.530.550.580.62
53(28, 35](-0.15, -0.1]150.730.070.630.670.720.770.84
54(28, 35](-0.1, -0.05]171.060.140.860.941.051.171.32
55(28, 35](-0.05, -0.0]191.950.441.361.581.872.262.79
56(28, 35](-0.0, 0.05]205.722.232.943.855.237.339.97
57(28, 35](0.05, 0.1]213.535.47-1-1-110.3811.32

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.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100