voly 0.0.234__py3-none-any.whl → 0.0.236__py3-none-any.whl

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.
voly/core/data.py CHANGED
@@ -307,12 +307,12 @@ def process_option_chain(df: pd.DataFrame, currency: str) -> pd.DataFrame:
307
307
  df['bid_depth'] = df['bids']
308
308
  df['ask_depth'] = df['asks']
309
309
 
310
- df['bs'] = voly.bs(s, df['strikes'], 0, df['mark_iv'], df['t'])
311
- df['delta'] = voly.delta(s, df['strikes'], 0, df['mark_iv'], df['t'])
312
- df['gamma'] = voly.gamma(s, df['strikes'], 0, df['mark_iv'], df['t']) * 10000
313
- df['vega'] = voly.vega(s, df['strikes'], 0, df['mark_iv'], df['t'])
314
- df['theta'] = voly.theta(s, df['strikes'], 0, df['mark_iv'], df['t'])
315
- df['rho'] = voly.rho(s, df['strikes'], 0, df['mark_iv'], df['t'])
310
+ df['bs'] = bs(df['spot_price'], df['strikes'], df['interest_rate'], df['mark_iv'], df['t'], df['flag'])
311
+ df['delta'] = delta(df['spot_price'], df['strikes'], df['interest_rate'], df['mark_iv'], df['t'], df['flag'])
312
+ df['gamma'] = gamma(df['spot_price'], df['strikes'], df['interest_rate'], df['mark_iv'], df['t'], df['flag']) * 10000
313
+ df['vega'] = vega(df['spot_price'], df['strikes'], df['interest_rate'], df['mark_iv'], df['t'], df['flag'])
314
+ df['theta'] = theta(df['spot_price'], df['strikes'], df['interest_rate'], df['mark_iv'], df['t'], df['flag'])
315
+ df['rho'] = rho(df['spot_price'], df['strikes'], df['interest_rate'], df['mark_iv'], df['t'], df['flag'])
316
316
 
317
317
  df['open_interest'] = df['open_interest'] * s
318
318
  df['volume'] = df['volume'] * s
voly/formulas.py CHANGED
@@ -8,50 +8,50 @@ from scipy.stats import norm
8
8
  from py_vollib.black_scholes.implied_volatility import implied_volatility
9
9
  from typing import Tuple, Dict, Union, List, Optional
10
10
  from voly.utils.logger import catch_exception
11
- from typing import Tuple
11
+ from voly.exceptions import VolyError
12
+ from functools import wraps
12
13
 
13
14
 
14
15
  @catch_exception
15
16
  def vectorize_inputs(func):
16
- """
17
- Decorator to vectorize Black-Scholes functions to handle both scalar and array inputs.
18
- For invalid inputs (K <= 0, o <= 0, or t <= 0), returns np.nan instead of trying to compute.
19
- """
20
-
21
- def wrapper(s, K, r, o, t, flag='call'):
22
- # Check if inputs are scalar
23
- K_scalar = np.isscalar(K)
24
- o_scalar = np.isscalar(o)
25
- t_scalar = np.isscalar(t)
26
-
27
- # Convert pandas Series to numpy arrays if needed
28
- if isinstance(K, pd.Series):
29
- K = K.values
30
- if isinstance(o, pd.Series):
31
- o = o.values
32
- if isinstance(t, pd.Series):
33
- t = t.values
34
-
35
- # If all inputs are scalar, use the original function directly
36
- if K_scalar and o_scalar and t_scalar:
37
- return func(s, K, r, o, t, flag)
38
-
39
- # For arrays, we need to apply the function element-wise
40
- # Instead of using np.vectorize directly, we'll create a custom vectorized function
41
- # that handles the conditions properly
42
- def safe_vectorized_func(K_val, o_val, t_val):
43
- # Check for invalid strike price, volatility, or time values
44
- if K_val <= 0 or o_val <= 0 or t_val <= 0:
45
- return np.nan # Return NaN for invalid inputs
46
- else:
47
- # Only call the function if inputs are valid
48
- return func(s, K_val, r, o_val, t_val, flag)
49
-
50
- # Now use numpy's vectorize on our safe function
51
- vectorized_func = np.vectorize(safe_vectorized_func)
52
-
53
- # Call the vectorized function with the inputs
54
- return vectorized_func(K, o, t)
17
+ @wraps(func)
18
+ def wrapper(s, *args, **kwargs):
19
+ # s is always scalar
20
+ all_args = list(args)
21
+ input_names = func.__code__.co_varnames[1:len(all_args)+1]
22
+ input_dict = dict(zip(input_names, all_args))
23
+ input_dict.update(kwargs)
24
+
25
+ # Identify vectorized vs scalar inputs
26
+ vector_lengths = {
27
+ k: len(v) for k, v in input_dict.items()
28
+ if isinstance(v, (list, np.ndarray, pd.Series))
29
+ }
30
+
31
+ is_vectorized = len(vector_lengths) > 0
32
+
33
+ if is_vectorized:
34
+ # Check if all are of same length
35
+ lengths = set(vector_lengths.values())
36
+ if len(lengths) != 1:
37
+ raise VolyError("All vectorized inputs must have the same length.")
38
+
39
+ if any(not isinstance(v, (list, np.ndarray, pd.Series)) for v in input_dict.values()):
40
+ raise VolyError("Cannot mix scalar and vector inputs. Pass all as vectors or all as scalars.")
41
+
42
+ # Proceed with vectorized call
43
+ max_len = next(iter(lengths))
44
+ return [
45
+ func(
46
+ s,
47
+ *(input_dict[name][i] for name in input_names if name in input_dict),
48
+ **{k: input_dict[k][i] for k in kwargs if k in input_dict}
49
+ )
50
+ for i in range(max_len)
51
+ ]
52
+ else:
53
+ # All scalar inputs, call directly
54
+ return func(s, *args, **kwargs)
55
55
 
56
56
  return wrapper
57
57
 
@@ -73,13 +73,6 @@ def d2(s: float, K: float, r: float, o: float, t: float, flag: str = 'call') ->
73
73
  @catch_exception
74
74
  @vectorize_inputs
75
75
  def bs(s: float, K: float, r: float, o: float, t: float, flag: str = 'call') -> float:
76
- if o <= 0 or t <= 0:
77
- # Intrinsic value at expiry
78
- if flag.lower() in ["call", "c"]:
79
- return max(0, s - K)
80
- else:
81
- return max(0, K - s)
82
-
83
76
  d1_val = d1(s, K, r, o, t)
84
77
  d2_val = d2(s, K, r, o, t)
85
78
 
@@ -92,13 +85,6 @@ def bs(s: float, K: float, r: float, o: float, t: float, flag: str = 'call') ->
92
85
  @catch_exception
93
86
  @vectorize_inputs
94
87
  def delta(s: float, K: float, r: float, o: float, t: float, flag: str = 'call') -> float:
95
- if o <= 0 or t <= 0:
96
- # At expiry, delta is either 0 or 1 for call, 0 or -1 for put
97
- if flag.lower() in ["call", "c"]:
98
- return 1.0 if s > K else 0.0
99
- else:
100
- return -1.0 if s < K else 0.0
101
-
102
88
  d1_val = d1(s, K, r, o, t)
103
89
 
104
90
  if flag.lower() in ["call", "c"]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: voly
3
- Version: 0.0.234
3
+ Version: 0.0.236
4
4
  Summary: Options & volatility research package
5
5
  Author-email: Manu de Cara <manu.de.cara@gmail.com>
6
6
  License: MIT
@@ -1,11 +1,11 @@
1
1
  voly/__init__.py,sha256=8xyDk7rFCn_MOD5hxuv5cxxKZvBVRiSIM7TgaMPpwpw,211
2
2
  voly/client.py,sha256=H4BY5n1isHOqMYX-Koe8-WKBKE6zxbdDYopDF58BPBI,14533
3
3
  voly/exceptions.py,sha256=PBsbn1vNMvKcCJwwJ4lBO6glD85jo1h2qiEmD7ArAjs,92
4
- voly/formulas.py,sha256=Jx2QSTkN3S_X1YWYXN3T0gBcxMKB_VLsqgDPg32ApmM,10833
4
+ voly/formulas.py,sha256=CY4VChzEgfuc9aLkUFt3ipyot8wr_2z8K1G2Zsw4Ov8,10317
5
5
  voly/models.py,sha256=CGJQr13Uie7iwtx2hjViN9lMXeRN_uOqzp4u8NPaTlA,9282
6
6
  voly/core/__init__.py,sha256=bu6fS2I1Pj9fPPnl-zY3L7NqrZSY5Zy6NY2uMUvdhKs,183
7
7
  voly/core/charts.py,sha256=6MSU0z01fPOVSssodxdnFqchzDfupSmXq_e71WwDmVQ,12635
8
- voly/core/data.py,sha256=wal8YlTKjhztt1XCZzLF0kQ1JWxbFIyoHchtP50bxeY,13031
8
+ voly/core/data.py,sha256=H-U4jI1GG4OgXTAeEWCWOBz6T0sLB2wiIKOTzopWGQk,13271
9
9
  voly/core/fit.py,sha256=R3aDgCjvZ30PujoE9pgnGQXPQOTTMbk7_-RQ0ZMx5ig,13918
10
10
  voly/core/hd.py,sha256=dSv197RmSWFWbRRdoBzMrD_poT7ZiJ8hdD_wKE-Li_M,13559
11
11
  voly/core/interpolate.py,sha256=GAkrqaar7A0D6UMipXQUp4vSHRfb44TmCG5Xiv9elXg,5176
@@ -13,8 +13,8 @@ voly/core/rnd.py,sha256=wiZ5OIjPDf1Th5_sQ9CZG5JgAo3EL8f63T_Rj1_VP-0,13214
13
13
  voly/utils/__init__.py,sha256=E05mWatyC-PDOsCxQV1p5Xi1IgpOomxrNURyCx_gB-w,200
14
14
  voly/utils/density.py,sha256=ONpRli-IaJDgOZ2sb27HHFc9_tkkGSATKl94JODd86A,5879
15
15
  voly/utils/logger.py,sha256=4-_2bVJmq17Q0d7Rd2mPg1AeR8gxv6EPvcmBDMFWcSM,1744
16
- voly-0.0.234.dist-info/licenses/LICENSE,sha256=wcHIVbE12jfcBOai_wqBKY6xvNQU5E909xL1zZNq_2Q,1065
17
- voly-0.0.234.dist-info/METADATA,sha256=Wl_C95JNc25vG12aaCN94QjLncRS5Q4OKNBFAguseGE,4115
18
- voly-0.0.234.dist-info/WHEEL,sha256=ck4Vq1_RXyvS4Jt6SI0Vz6fyVs4GWg7AINwpsaGEgPE,91
19
- voly-0.0.234.dist-info/top_level.txt,sha256=ZfLw2sSxF-LrKAkgGjOmeTcw6_gD-30zvtdEY5W4B7c,5
20
- voly-0.0.234.dist-info/RECORD,,
16
+ voly-0.0.236.dist-info/licenses/LICENSE,sha256=wcHIVbE12jfcBOai_wqBKY6xvNQU5E909xL1zZNq_2Q,1065
17
+ voly-0.0.236.dist-info/METADATA,sha256=Du-T3VnsBghrihQ-Cy0NYMmWQ2WetDOPc0YIcLEalzY,4115
18
+ voly-0.0.236.dist-info/WHEEL,sha256=ck4Vq1_RXyvS4Jt6SI0Vz6fyVs4GWg7AINwpsaGEgPE,91
19
+ voly-0.0.236.dist-info/top_level.txt,sha256=ZfLw2sSxF-LrKAkgGjOmeTcw6_gD-30zvtdEY5W4B7c,5
20
+ voly-0.0.236.dist-info/RECORD,,
File without changes