optionlab 1.3.2__tar.gz → 1.4.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.1
2
+ Name: optionlab
3
+ Version: 1.4.2
4
+ Summary: Python library for evaluating options trading strategies
5
+ Author: Roberto Gomes, PhD
6
+ Author-email: roberto.veiga@ufabc.edu.br
7
+ Requires-Python: >=3.10,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Requires-Dist: holidays (>=0.44,<0.45)
12
+ Requires-Dist: jupyter (>=1.0.0,<2.0.0)
13
+ Requires-Dist: matplotlib (>=3.8.3,<4.0.0)
14
+ Requires-Dist: pandas (>=2.2.1,<3.0.0)
15
+ Requires-Dist: pydantic (>=2.9,<3.0)
16
+ Requires-Dist: scipy (>=1.12.0,<2.0.0)
17
+ Description-Content-Type: text/markdown
18
+
19
+ # OptionLab
20
+
21
+ This package is a lightweight library written entirely in Python, designed to provide
22
+ quick evaluation of option strategies.
23
+
24
+ The code produces various outputs, including the profit/loss profile of the strategy on
25
+ a user-defined target date, the range of stock prices for which the strategy is
26
+ profitable (i.e., generating a return greater than \$0.01), the Greeks associated with
27
+ each leg of the strategy using the Black-Sholes model, the resulting debit or credit on the
28
+ trading account, the maximum and minimum returns within a specified lower and higher price
29
+ range of the underlying asset, and an estimate of the strategy's probability of profit.
30
+
31
+ The probability of profit (PoP) on the user-defined target date for the strategy is calculated
32
+ by default using the Black-Scholes model. Alternatively, the user can provide an array of
33
+ underlying asset prices following a distribution other than the normal (e.g. Laplace) or
34
+ model other than the Black-Scholes model (e.g. Heston model) that will be used in the calculations.
35
+
36
+ Despite the code having been developed with option strategies in mind, it can also be
37
+ used for strategies that combine options with stocks and/or take into account the
38
+ profits or losses of closed trades.
39
+
40
+ If you have any questions, corrections, comments or suggestions, just
41
+ [drop a message](mailto:roberto.veiga@ufabc.edu.br).
42
+
43
+ You can also reach me on [Linkedin](https://www.linkedin.com/in/roberto-gomes-phd-8a718317b/) or
44
+ follow me on [X](https://x.com/rgaveiga). When I have some free time, which is rare, I publish articles
45
+ on [Medium](https://medium.com/@rgaveiga).
46
+
47
+ ## Installation
48
+
49
+ The easiest way to install **OptionLab** is using **pip**:
50
+
51
+ ```
52
+ pip install optionlab
53
+ ```
54
+
55
+ ## Basic usage
56
+
57
+ The evaluation of a strategy is done by calling the `run_strategy` function provided by
58
+ the library. This function receives the input data either as a Python dictionary or an
59
+ `Inputs` object. For example, let's say we wanted to calculate the probability of profit
60
+ for naked calls on Apple stocks with maturity on December 17, 2021. The strategy setup
61
+ consisted of selling 100 175.00 strike calls for 1.15 each on November 22, 2021.
62
+
63
+ ```python
64
+ input_data = {
65
+ "stock_price": 164.04,
66
+ "start_date": "2021-11-22",
67
+ "target_date": "2021-12-17",
68
+ "volatility": 0.272,
69
+ "interest_rate": 0.0002,
70
+ "min_stock": 120,
71
+ "max_stock": 200,
72
+ "strategy": [
73
+ {
74
+ "type": "call",
75
+ "strike": 175.0,
76
+ "premium": 1.15,
77
+ "n": 100,
78
+ "action":"sell"
79
+ }
80
+ ],
81
+ }
82
+ ```
83
+
84
+ After defining the input data as a Python dictionary, we pass it to the `run_strategy`
85
+ function as shown below:
86
+
87
+ ```python
88
+ from optionlab import run_strategy, plot_pl
89
+
90
+ out = run_strategy(input_data)
91
+
92
+ print(out)
93
+
94
+ plot_pl(out)
95
+ ```
96
+
97
+ The variable `out` is an `Outputs` object that contains the results from the
98
+ calculations. By calling `print` with `out` as an argument, these results are
99
+ displayed on screen. The `plot_pl` function, in turn, takes an `Outputs` object as
100
+ its argument and plots the profit/loss diagram for the strategy.
101
+
102
+ Usage examples for a number of popular options trading strategies can be found in the
103
+ Jupyter notebooks in the **examples** directory.
104
+
105
+ ## Contributions
106
+
107
+ Contributions are definitely welcome. However, it should be mentioned that this
108
+ repository uses [poetry](https://python-poetry.org/) as a package manager and
109
+ [git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) with
110
+ [pre-commit](https://pre-commit.com/) to customize actions on the repository. Source
111
+ code must be formatted using [black](https://github.com/psf/black).
112
+
113
+ ## Disclaimer
114
+
115
+ This is free software and is provided as is. The author makes no guarantee that its
116
+ results are accurate and is not responsible for any losses caused by the use of the
117
+ code.
118
+
119
+ Options are very risky derivatives and, like any other type of financial vehicle,
120
+ trading options requires due diligence. This code is provided for educational and
121
+ research purposes only.
122
+
123
+ Bugs can be reported as issues.
124
+
@@ -0,0 +1,105 @@
1
+ # OptionLab
2
+
3
+ This package is a lightweight library written entirely in Python, designed to provide
4
+ quick evaluation of option strategies.
5
+
6
+ The code produces various outputs, including the profit/loss profile of the strategy on
7
+ a user-defined target date, the range of stock prices for which the strategy is
8
+ profitable (i.e., generating a return greater than \$0.01), the Greeks associated with
9
+ each leg of the strategy using the Black-Sholes model, the resulting debit or credit on the
10
+ trading account, the maximum and minimum returns within a specified lower and higher price
11
+ range of the underlying asset, and an estimate of the strategy's probability of profit.
12
+
13
+ The probability of profit (PoP) on the user-defined target date for the strategy is calculated
14
+ by default using the Black-Scholes model. Alternatively, the user can provide an array of
15
+ underlying asset prices following a distribution other than the normal (e.g. Laplace) or
16
+ model other than the Black-Scholes model (e.g. Heston model) that will be used in the calculations.
17
+
18
+ Despite the code having been developed with option strategies in mind, it can also be
19
+ used for strategies that combine options with stocks and/or take into account the
20
+ profits or losses of closed trades.
21
+
22
+ If you have any questions, corrections, comments or suggestions, just
23
+ [drop a message](mailto:roberto.veiga@ufabc.edu.br).
24
+
25
+ You can also reach me on [Linkedin](https://www.linkedin.com/in/roberto-gomes-phd-8a718317b/) or
26
+ follow me on [X](https://x.com/rgaveiga). When I have some free time, which is rare, I publish articles
27
+ on [Medium](https://medium.com/@rgaveiga).
28
+
29
+ ## Installation
30
+
31
+ The easiest way to install **OptionLab** is using **pip**:
32
+
33
+ ```
34
+ pip install optionlab
35
+ ```
36
+
37
+ ## Basic usage
38
+
39
+ The evaluation of a strategy is done by calling the `run_strategy` function provided by
40
+ the library. This function receives the input data either as a Python dictionary or an
41
+ `Inputs` object. For example, let's say we wanted to calculate the probability of profit
42
+ for naked calls on Apple stocks with maturity on December 17, 2021. The strategy setup
43
+ consisted of selling 100 175.00 strike calls for 1.15 each on November 22, 2021.
44
+
45
+ ```python
46
+ input_data = {
47
+ "stock_price": 164.04,
48
+ "start_date": "2021-11-22",
49
+ "target_date": "2021-12-17",
50
+ "volatility": 0.272,
51
+ "interest_rate": 0.0002,
52
+ "min_stock": 120,
53
+ "max_stock": 200,
54
+ "strategy": [
55
+ {
56
+ "type": "call",
57
+ "strike": 175.0,
58
+ "premium": 1.15,
59
+ "n": 100,
60
+ "action":"sell"
61
+ }
62
+ ],
63
+ }
64
+ ```
65
+
66
+ After defining the input data as a Python dictionary, we pass it to the `run_strategy`
67
+ function as shown below:
68
+
69
+ ```python
70
+ from optionlab import run_strategy, plot_pl
71
+
72
+ out = run_strategy(input_data)
73
+
74
+ print(out)
75
+
76
+ plot_pl(out)
77
+ ```
78
+
79
+ The variable `out` is an `Outputs` object that contains the results from the
80
+ calculations. By calling `print` with `out` as an argument, these results are
81
+ displayed on screen. The `plot_pl` function, in turn, takes an `Outputs` object as
82
+ its argument and plots the profit/loss diagram for the strategy.
83
+
84
+ Usage examples for a number of popular options trading strategies can be found in the
85
+ Jupyter notebooks in the **examples** directory.
86
+
87
+ ## Contributions
88
+
89
+ Contributions are definitely welcome. However, it should be mentioned that this
90
+ repository uses [poetry](https://python-poetry.org/) as a package manager and
91
+ [git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) with
92
+ [pre-commit](https://pre-commit.com/) to customize actions on the repository. Source
93
+ code must be formatted using [black](https://github.com/psf/black).
94
+
95
+ ## Disclaimer
96
+
97
+ This is free software and is provided as is. The author makes no guarantee that its
98
+ results are accurate and is not responsible for any losses caused by the use of the
99
+ code.
100
+
101
+ Options are very risky derivatives and, like any other type of financial vehicle,
102
+ trading options requires due diligence. This code is provided for educational and
103
+ research purposes only.
104
+
105
+ Bugs can be reported as issues.
@@ -0,0 +1,2 @@
1
+ __pycache__
2
+ old
@@ -1,7 +1,7 @@
1
1
  import typing
2
2
 
3
3
 
4
- VERSION = "1.3.2"
4
+ VERSION = "1.4.2"
5
5
 
6
6
 
7
7
  if typing.TYPE_CHECKING:
@@ -10,60 +10,68 @@ if typing.TYPE_CHECKING:
10
10
  from .models import (
11
11
  Inputs,
12
12
  OptionType,
13
- OptionInfo,
14
13
  Option,
15
14
  Outputs,
16
15
  ClosedPosition,
17
- ProbabilityOfProfitArrayInputs,
18
- ProbabilityOfProfitInputs,
16
+ ArrayInputs,
17
+ TheoreticalModelInputs,
18
+ BlackScholesModelInputs,
19
+ LaplaceInputs,
19
20
  BlackScholesInfo,
20
- Distribution,
21
+ TheoreticalModel,
22
+ FloatOrNdarray,
21
23
  StrategyLeg,
22
24
  StrategyType,
23
25
  Stock,
24
- Country,
25
26
  Action,
26
27
  )
27
28
  from .black_scholes import (
28
29
  get_itm_probability,
29
30
  get_implied_vol,
30
31
  get_option_price,
31
- get_d1_d2,
32
+ get_d1,
33
+ get_d2,
32
34
  get_bs_info,
33
35
  get_vega,
34
36
  get_delta,
35
37
  get_gamma,
36
38
  get_theta,
39
+ get_rho,
37
40
  )
38
41
  from .engine import run_strategy
39
42
  from .plot import plot_pl
43
+ from .price_array import create_price_array
40
44
  from .support import (
41
45
  get_pl_profile,
42
46
  get_pl_profile_stock,
43
47
  get_pl_profile_bs,
44
48
  create_price_seq,
45
- create_price_samples,
46
- get_profit_range,
47
49
  get_pop,
48
50
  )
51
+ from .utils import (
52
+ get_nonbusiness_days,
53
+ get_pl,
54
+ pl_to_csv,
55
+ )
49
56
 
50
57
  __version__ = VERSION
51
58
  __all__ = (
52
59
  # models
53
60
  "Inputs",
54
61
  "OptionType",
55
- "OptionInfo",
56
62
  "Option",
57
63
  "Outputs",
58
64
  "ClosedPosition",
59
- "ProbabilityOfProfitArrayInputs",
60
- "ProbabilityOfProfitInputs",
65
+ "ArrayInputs",
66
+ "TheoreticalModelInputs",
67
+ "BlackScholesModelInputs",
68
+ "LaplaceInputs",
61
69
  "BlackScholesInfo",
62
- "Distribution",
70
+ "TheoreticalModel",
71
+ "FloatOrNdarray",
63
72
  "StrategyLeg",
64
73
  "StrategyType",
65
74
  "Stock",
66
- "Country",
67
75
  "Action",
68
76
  # engine
69
77
  "run_strategy",
@@ -72,11 +80,10 @@ __all__ = (
72
80
  "get_pl_profile_stock",
73
81
  "get_pl_profile_bs",
74
82
  "create_price_seq",
75
- "create_price_samples",
76
- "get_profit_range",
77
83
  "get_pop",
78
84
  # black_scholes
79
- "get_d1_d2",
85
+ "get_d1",
86
+ "get_d2",
80
87
  "get_option_price",
81
88
  "get_itm_probability",
82
89
  "get_implied_vol",
@@ -85,8 +92,15 @@ __all__ = (
85
92
  "get_delta",
86
93
  "get_gamma",
87
94
  "get_theta",
95
+ "get_rho",
88
96
  # plot
89
97
  "plot_pl",
98
+ # price_array
99
+ "create_price_array",
100
+ # utils
101
+ "get_nonbusiness_days",
102
+ "get_pl",
103
+ "pl_to_csv",
90
104
  )
91
105
 
92
106
  # A mapping of {<member name>: (package, <module name>)} defining dynamic imports
@@ -95,17 +109,18 @@ _dynamic_imports: "dict[str, tuple[str, str]]" = {
95
109
  "Inputs": (__package__, ".models"),
96
110
  "Outputs": (__package__, ".models"),
97
111
  "OptionType": (__package__, ".models"),
98
- "OptionInfo": (__package__, ".models"),
99
112
  "Option": (__package__, ".models"),
100
113
  "ClosedPosition": (__package__, ".models"),
101
- "ProbabilityOfProfitArrayInputs": (__package__, ".models"),
102
- "ProbabilityOfProfitInputs": (__package__, ".models"),
114
+ "ArrayInputs": (__package__, ".models"),
115
+ "TheoreticalModelInputs": (__package__, ".models"),
116
+ "BlackScholesModelInputs": (__package__, ".models"),
117
+ "LaplaceInputs": (__package__, ".models"),
103
118
  "BlackScholesInfo": (__package__, ".models"),
104
- "Distribution": (__package__, ".models"),
119
+ "TheoreticalModel": (__package__, ".models"),
120
+ "FloatOrNdarray": (__package__, ".models"),
105
121
  "StrategyLeg": (__package__, ".models"),
106
122
  "StrategyType": (__package__, ".models"),
107
123
  "Stock": (__package__, ".models"),
108
- "Country": (__package__, ".models"),
109
124
  "Action": (__package__, ".models"),
110
125
  # engine
111
126
  "run_strategy": (__package__, ".engine"),
@@ -114,11 +129,10 @@ _dynamic_imports: "dict[str, tuple[str, str]]" = {
114
129
  "get_pl_profile_stock": (__package__, ".support"),
115
130
  "get_pl_profile_bs": (__package__, ".support"),
116
131
  "create_price_seq": (__package__, ".support"),
117
- "create_price_samples": (__package__, ".support"),
118
- "get_profit_range": (__package__, ".support"),
119
132
  "get_pop": (__package__, ".support"),
120
133
  # black_scholes
121
- "get_d1_d2": (__package__, ".black_scholes"),
134
+ "get_d1": (__package__, ".black_scholes"),
135
+ "get_d2": (__package__, ".black_scholes"),
122
136
  "get_option_price": (__package__, ".black_scholes"),
123
137
  "get_itm_probability": (__package__, ".black_scholes"),
124
138
  "get_implied_vol": (__package__, ".black_scholes"),
@@ -127,8 +141,15 @@ _dynamic_imports: "dict[str, tuple[str, str]]" = {
127
141
  "get_delta": (__package__, ".black_scholes"),
128
142
  "get_gamma": (__package__, ".black_scholes"),
129
143
  "get_theta": (__package__, ".black_scholes"),
144
+ "get_rho": (__package__, ".black_scholes"),
130
145
  # plot
131
146
  "plot_pl": (__package__, ".plot"),
147
+ # price_array
148
+ "create_price_array": (__package__, ".price_array"),
149
+ # utils
150
+ "get_nonbusiness_days": (__package__, ".utils"),
151
+ "get_pl": (__package__, ".utils"),
152
+ "pl_to_csv": (__package__, ".utils"),
132
153
  }
133
154
 
134
155