optstrat 0.1.0__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.
optstrat-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matías Roldán
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.4
2
+ Name: optstrat
3
+ Version: 0.1.0
4
+ Summary: A small Python library for building options strategies
5
+ Author: Matías Roldán
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/roldan/optstrat
8
+ Project-URL: Issues, https://github.com/roldan/optstrat/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Topic :: Office/Business :: Financial
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: numpy
21
+ Dynamic: license-file
22
+
23
+ # optstrat
24
+
25
+ A small Python library for building options strategies.
26
+
27
+ It lets you define a strategy leg by leg, calculate the payoff at expiration, and compute max profit and max loss.
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ pip install optstrat
33
+ ```
34
+
35
+ For plotting examples:
36
+
37
+ ```bash
38
+ pip install matplotlib
39
+ ```
40
+
41
+ ## Usage example
42
+
43
+ Plot payoff using `matplotlib`:
44
+
45
+ ```python
46
+ import matplotlib.pyplot as plt
47
+
48
+ from optstrat import OptionStrategy
49
+
50
+ strategy = OptionStrategy("Covered Call", 100, contract_size=100)
51
+
52
+ # 100 shares + short 1 call contract
53
+ strategy.long_underlying(100, quantity=100)
54
+ strategy.short_call(110, 2.5, quantity=1)
55
+
56
+ max_profit, max_loss = strategy.max_profit_and_loss()
57
+ net_cost = strategy.net_cost()
58
+
59
+ print(f"Max Profit: {max_profit}")
60
+ print(f"Max Loss: {max_loss}")
61
+ print(f"Cost of entering position ${net_cost}")
62
+ for leg in strategy.legs:
63
+ print(leg)
64
+
65
+ plt.plot(strategy.underlying_prices, strategy.payoffs)
66
+ plt.title(strategy.name)
67
+ plt.fill_between(
68
+ strategy.underlying_prices,
69
+ strategy.payoffs,
70
+ where=(strategy.payoffs > 0),
71
+ facecolor='g',
72
+ alpha=0.4,
73
+ )
74
+ plt.fill_between(
75
+ strategy.underlying_prices,
76
+ strategy.payoffs,
77
+ where=(strategy.payoffs < 0),
78
+ facecolor='r',
79
+ alpha=0.4,
80
+ )
81
+ plt.xlabel(r'$S_T$')
82
+ plt.ylabel('P&L')
83
+ plt.show()
84
+ ```
85
+
86
+ ## Notes
87
+
88
+ - `underlying_prices` represents possible underlying prices at expiration.
89
+ - `quantity` is the number of contracts for option legs and the number of shares for underlying legs.
90
+ - `contract_size` applies only to option legs.
91
+ - `strategy.net_cost()` computes the net cost of entering the position.
92
+ - `strategy.max_profit_and_loss()` computes max profit and max loss at expiration.
@@ -0,0 +1,70 @@
1
+ # optstrat
2
+
3
+ A small Python library for building options strategies.
4
+
5
+ It lets you define a strategy leg by leg, calculate the payoff at expiration, and compute max profit and max loss.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install optstrat
11
+ ```
12
+
13
+ For plotting examples:
14
+
15
+ ```bash
16
+ pip install matplotlib
17
+ ```
18
+
19
+ ## Usage example
20
+
21
+ Plot payoff using `matplotlib`:
22
+
23
+ ```python
24
+ import matplotlib.pyplot as plt
25
+
26
+ from optstrat import OptionStrategy
27
+
28
+ strategy = OptionStrategy("Covered Call", 100, contract_size=100)
29
+
30
+ # 100 shares + short 1 call contract
31
+ strategy.long_underlying(100, quantity=100)
32
+ strategy.short_call(110, 2.5, quantity=1)
33
+
34
+ max_profit, max_loss = strategy.max_profit_and_loss()
35
+ net_cost = strategy.net_cost()
36
+
37
+ print(f"Max Profit: {max_profit}")
38
+ print(f"Max Loss: {max_loss}")
39
+ print(f"Cost of entering position ${net_cost}")
40
+ for leg in strategy.legs:
41
+ print(leg)
42
+
43
+ plt.plot(strategy.underlying_prices, strategy.payoffs)
44
+ plt.title(strategy.name)
45
+ plt.fill_between(
46
+ strategy.underlying_prices,
47
+ strategy.payoffs,
48
+ where=(strategy.payoffs > 0),
49
+ facecolor='g',
50
+ alpha=0.4,
51
+ )
52
+ plt.fill_between(
53
+ strategy.underlying_prices,
54
+ strategy.payoffs,
55
+ where=(strategy.payoffs < 0),
56
+ facecolor='r',
57
+ alpha=0.4,
58
+ )
59
+ plt.xlabel(r'$S_T$')
60
+ plt.ylabel('P&L')
61
+ plt.show()
62
+ ```
63
+
64
+ ## Notes
65
+
66
+ - `underlying_prices` represents possible underlying prices at expiration.
67
+ - `quantity` is the number of contracts for option legs and the number of shares for underlying legs.
68
+ - `contract_size` applies only to option legs.
69
+ - `strategy.net_cost()` computes the net cost of entering the position.
70
+ - `strategy.max_profit_and_loss()` computes max profit and max loss at expiration.
@@ -0,0 +1,35 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "optstrat"
7
+ version = "0.1.0"
8
+ description = "A small Python library for building options strategies"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = "MIT"
12
+ dependencies = ["numpy"]
13
+ authors = [
14
+ { name = "Matías Roldán" }
15
+ ]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3 :: Only",
19
+ "Programming Language :: Python :: 3.9",
20
+ "Programming Language :: Python :: 3.10",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Intended Audience :: Developers",
24
+ "Topic :: Office/Business :: Financial",
25
+ ]
26
+
27
+ [project.urls]
28
+ Homepage = "https://github.com/roldan/optstrat"
29
+ Issues = "https://github.com/roldan/optstrat/issues"
30
+
31
+ [tool.setuptools]
32
+ package-dir = {"" = "src"}
33
+
34
+ [tool.setuptools.packages.find]
35
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ from .option_strategy import Kind, Leg, OptionStrategy, Side
2
+
3
+ __all__ = ["Kind", "Leg", "OptionStrategy", "Side"]
@@ -0,0 +1,164 @@
1
+ from enum import Enum
2
+
3
+ import numpy as np
4
+
5
+ class Kind(Enum):
6
+ CALL = 'call'
7
+ PUT = 'put'
8
+ UNDERLYING = 'underlying'
9
+
10
+ class Side(Enum):
11
+ LONG = 1
12
+ SHORT = -1
13
+
14
+ class Leg:
15
+
16
+ def __init__(self, kind: Kind, side: Side, entry_price, quantity=1, K=None):
17
+ self.kind = kind
18
+ self.side = side
19
+ self.entry_price = entry_price
20
+ self.quantity = quantity
21
+ self.K = K
22
+
23
+ def __repr__(self):
24
+ side = self.side.name
25
+ qty = f' x{self.quantity}' if self.quantity != 1 else ''
26
+ if self.kind == Kind.UNDERLYING:
27
+ return f'{side} {self.kind.value} @ {self.entry_price}{qty}'
28
+ return f'{side} {self.kind.value} {self.K} @ {self.entry_price}{qty}'
29
+
30
+ class OptionStrategy:
31
+ """Build an option strategy leg by leg and compute payoff at expiration.
32
+
33
+ Quantity semantics:
34
+ - option legs use `quantity` as number of contracts
35
+ - underlying legs use `quantity` as number of shares
36
+
37
+ `contract_size` applies only to option legs.
38
+ """
39
+
40
+ def __init__(self, name, S0, price_range_ratio=0.5, step=1, contract_size=100):
41
+ self.name = name
42
+ self.S0 = S0
43
+ self.contract_size = contract_size
44
+ self.underlying_prices = np.arange(S0 * price_range_ratio, S0 * (1 + price_range_ratio), step)
45
+ self.payoffs = np.zeros_like(self.underlying_prices, dtype=float)
46
+ self.legs = []
47
+
48
+ def _apply(self, kind: Kind, side: Side, entry_price, quantity=1, K=None):
49
+ if kind == Kind.CALL:
50
+ intrinsic = np.maximum(self.underlying_prices - K, 0)
51
+ payoffs = (side.value * intrinsic - side.value * entry_price) * quantity * self.contract_size
52
+ elif kind == Kind.PUT:
53
+ intrinsic = np.maximum(K - self.underlying_prices, 0)
54
+ payoffs = (side.value * intrinsic - side.value * entry_price) * quantity * self.contract_size
55
+ elif kind == Kind.UNDERLYING:
56
+ payoffs = side.value * (self.underlying_prices - entry_price) * quantity
57
+
58
+ self.payoffs += payoffs
59
+ self.legs.append(Leg(kind, side, entry_price, quantity, K=K))
60
+
61
+ def long_call(self, K, entry_price, quantity=1):
62
+ """Add a long call.
63
+
64
+ Parameters:
65
+ K: Strike price.
66
+ entry_price: Premium paid per contract.
67
+ quantity: Number of option contracts.
68
+ """
69
+ self._apply(Kind.CALL, Side.LONG, entry_price, quantity, K=K)
70
+
71
+ def short_call(self, K, entry_price, quantity=1):
72
+ """Add a short call.
73
+
74
+ Parameters:
75
+ K: Strike price.
76
+ entry_price: Premium received per contract.
77
+ quantity: Number of option contracts.
78
+ """
79
+ self._apply(Kind.CALL, Side.SHORT, entry_price, quantity, K=K)
80
+
81
+ def long_put(self, K, entry_price, quantity=1):
82
+ """Add a long put.
83
+
84
+ Parameters:
85
+ K: Strike price.
86
+ entry_price: Premium paid per contract.
87
+ quantity: Number of option contracts.
88
+ """
89
+ self._apply(Kind.PUT, Side.LONG, entry_price, quantity, K=K)
90
+
91
+ def short_put(self, K, entry_price, quantity=1):
92
+ """Add a short put.
93
+
94
+ Parameters:
95
+ K: Strike price.
96
+ entry_price: Premium received per contract.
97
+ quantity: Number of option contracts.
98
+ """
99
+ self._apply(Kind.PUT, Side.SHORT, entry_price, quantity, K=K)
100
+
101
+ def long_underlying(self, entry_price, quantity=1):
102
+ """Add a long underlying position.
103
+
104
+ Parameters:
105
+ entry_price: Entry price per share.
106
+ quantity: Number of shares.
107
+ """
108
+ self._apply(Kind.UNDERLYING, Side.LONG, entry_price, quantity)
109
+
110
+ def short_underlying(self, entry_price, quantity=1):
111
+ """Add a short underlying position.
112
+
113
+ Parameters:
114
+ entry_price: Entry price per share.
115
+ quantity: Number of shares.
116
+ """
117
+ self._apply(Kind.UNDERLYING, Side.SHORT, entry_price, quantity)
118
+
119
+ def net_cost(self):
120
+ """Return the net cost of entering the strategy."""
121
+ return sum(
122
+ leg.entry_price * leg.quantity * leg.side.value * self.contract_size
123
+ if leg.kind in (Kind.CALL, Kind.PUT)
124
+ else leg.entry_price * leg.quantity * leg.side.value
125
+ for leg in self.legs
126
+ )
127
+
128
+ def max_profit_and_loss(self):
129
+ """Return max profit and max loss at expiration."""
130
+ prices = {0}
131
+ for leg in self.legs:
132
+ if leg.K is not None:
133
+ prices.add(leg.K)
134
+
135
+ def payoff_at(price):
136
+ payoff = 0
137
+ for leg in self.legs:
138
+ if leg.kind == Kind.CALL:
139
+ intrinsic = max(price - leg.K, 0)
140
+ payoff += (leg.side.value * intrinsic - leg.side.value * leg.entry_price) * leg.quantity * self.contract_size
141
+ elif leg.kind == Kind.PUT:
142
+ intrinsic = max(leg.K - price, 0)
143
+ payoff += (leg.side.value * intrinsic - leg.side.value * leg.entry_price) * leg.quantity * self.contract_size
144
+ elif leg.kind == Kind.UNDERLYING:
145
+ payoff += leg.side.value * (price - leg.entry_price) * leg.quantity
146
+ return payoff
147
+
148
+ slope = 0
149
+ for leg in self.legs:
150
+ if leg.kind == Kind.CALL:
151
+ slope += leg.side.value * leg.quantity * self.contract_size
152
+ elif leg.kind == Kind.UNDERLYING:
153
+ slope += leg.side.value * leg.quantity
154
+
155
+ payoffs = [payoff_at(price) for price in sorted(prices)]
156
+ max_profit = max(payoffs)
157
+ max_loss = min(payoffs)
158
+
159
+ if slope > 0:
160
+ max_profit = np.inf
161
+ elif slope < 0:
162
+ max_loss = -np.inf
163
+
164
+ return max_profit, max_loss
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.4
2
+ Name: optstrat
3
+ Version: 0.1.0
4
+ Summary: A small Python library for building options strategies
5
+ Author: Matías Roldán
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/roldan/optstrat
8
+ Project-URL: Issues, https://github.com/roldan/optstrat/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Topic :: Office/Business :: Financial
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: numpy
21
+ Dynamic: license-file
22
+
23
+ # optstrat
24
+
25
+ A small Python library for building options strategies.
26
+
27
+ It lets you define a strategy leg by leg, calculate the payoff at expiration, and compute max profit and max loss.
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ pip install optstrat
33
+ ```
34
+
35
+ For plotting examples:
36
+
37
+ ```bash
38
+ pip install matplotlib
39
+ ```
40
+
41
+ ## Usage example
42
+
43
+ Plot payoff using `matplotlib`:
44
+
45
+ ```python
46
+ import matplotlib.pyplot as plt
47
+
48
+ from optstrat import OptionStrategy
49
+
50
+ strategy = OptionStrategy("Covered Call", 100, contract_size=100)
51
+
52
+ # 100 shares + short 1 call contract
53
+ strategy.long_underlying(100, quantity=100)
54
+ strategy.short_call(110, 2.5, quantity=1)
55
+
56
+ max_profit, max_loss = strategy.max_profit_and_loss()
57
+ net_cost = strategy.net_cost()
58
+
59
+ print(f"Max Profit: {max_profit}")
60
+ print(f"Max Loss: {max_loss}")
61
+ print(f"Cost of entering position ${net_cost}")
62
+ for leg in strategy.legs:
63
+ print(leg)
64
+
65
+ plt.plot(strategy.underlying_prices, strategy.payoffs)
66
+ plt.title(strategy.name)
67
+ plt.fill_between(
68
+ strategy.underlying_prices,
69
+ strategy.payoffs,
70
+ where=(strategy.payoffs > 0),
71
+ facecolor='g',
72
+ alpha=0.4,
73
+ )
74
+ plt.fill_between(
75
+ strategy.underlying_prices,
76
+ strategy.payoffs,
77
+ where=(strategy.payoffs < 0),
78
+ facecolor='r',
79
+ alpha=0.4,
80
+ )
81
+ plt.xlabel(r'$S_T$')
82
+ plt.ylabel('P&L')
83
+ plt.show()
84
+ ```
85
+
86
+ ## Notes
87
+
88
+ - `underlying_prices` represents possible underlying prices at expiration.
89
+ - `quantity` is the number of contracts for option legs and the number of shares for underlying legs.
90
+ - `contract_size` applies only to option legs.
91
+ - `strategy.net_cost()` computes the net cost of entering the position.
92
+ - `strategy.max_profit_and_loss()` computes max profit and max loss at expiration.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/optstrat/__init__.py
5
+ src/optstrat/option_strategy.py
6
+ src/optstrat.egg-info/PKG-INFO
7
+ src/optstrat.egg-info/SOURCES.txt
8
+ src/optstrat.egg-info/dependency_links.txt
9
+ src/optstrat.egg-info/requires.txt
10
+ src/optstrat.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ numpy
@@ -0,0 +1 @@
1
+ optstrat