Skip to content

Commit b3e32f0

Browse files
committed
Create helper function for BS inputs
1 parent b2f08fd commit b3e32f0

1 file changed

Lines changed: 28 additions & 66 deletions

File tree

src/mcp_massive/functions.py

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,30 @@ def _bs_d1d2(
205205
return d1, d2
206206

207207

208+
def _bs_inputs(
209+
inputs: dict[str, Any], n: int
210+
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
211+
"""Resolve the five common BS params (S, K, T, r, sigma) to numpy arrays.
212+
213+
apply_pipeline guarantees these keys are present before the impl runs;
214+
no defensive checks needed here.
215+
"""
216+
return (
217+
_to_numpy(inputs["S"], n),
218+
_to_numpy(inputs["K"], n),
219+
_to_numpy(inputs["T"], n),
220+
_to_numpy(inputs["r"], n),
221+
_to_numpy(inputs["sigma"], n),
222+
)
223+
224+
208225
# ---------------------------------------------------------------------------
209226
# Greeks implementations
210227
# ---------------------------------------------------------------------------
211228

212229

213230
def _impl_bs_price(table: Table, inputs: dict[str, Any]) -> np.ndarray:
214-
n = len(table)
215-
S = _to_numpy(inputs["S"], n)
216-
K = _to_numpy(inputs["K"], n)
217-
T = _to_numpy(inputs["T"], n)
218-
r = _to_numpy(inputs["r"], n)
219-
sigma = _to_numpy(inputs["sigma"], n)
231+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
220232
option_type = _validate_option_type(inputs)
221233

222234
d1, d2 = _bs_d1d2(S, K, T, r, sigma)
@@ -228,12 +240,7 @@ def _impl_bs_price(table: Table, inputs: dict[str, Any]) -> np.ndarray:
228240

229241

230242
def _impl_bs_delta(table: Table, inputs: dict[str, Any]) -> np.ndarray:
231-
n = len(table)
232-
S = _to_numpy(inputs["S"], n)
233-
K = _to_numpy(inputs["K"], n)
234-
T = _to_numpy(inputs["T"], n)
235-
r = _to_numpy(inputs["r"], n)
236-
sigma = _to_numpy(inputs["sigma"], n)
243+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
237244
option_type = _validate_option_type(inputs)
238245

239246
d1, _d2 = _bs_d1d2(S, K, T, r, sigma)
@@ -245,25 +252,15 @@ def _impl_bs_delta(table: Table, inputs: dict[str, Any]) -> np.ndarray:
245252

246253

247254
def _impl_bs_gamma(table: Table, inputs: dict[str, Any]) -> np.ndarray:
248-
n = len(table)
249-
S = _to_numpy(inputs["S"], n)
250-
K = _to_numpy(inputs["K"], n)
251-
T = _to_numpy(inputs["T"], n)
252-
r = _to_numpy(inputs["r"], n)
253-
sigma = _to_numpy(inputs["sigma"], n)
255+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
254256

255257
d1, _d2 = _bs_d1d2(S, K, T, r, sigma)
256258
gamma = _norm_pdf(d1) / (S * sigma * np.sqrt(T))
257259
return gamma
258260

259261

260262
def _impl_bs_theta(table: Table, inputs: dict[str, Any]) -> np.ndarray:
261-
n = len(table)
262-
S = _to_numpy(inputs["S"], n)
263-
K = _to_numpy(inputs["K"], n)
264-
T = _to_numpy(inputs["T"], n)
265-
r = _to_numpy(inputs["r"], n)
266-
sigma = _to_numpy(inputs["sigma"], n)
263+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
267264
option_type = _validate_option_type(inputs)
268265

269266
d1, d2 = _bs_d1d2(S, K, T, r, sigma)
@@ -277,12 +274,7 @@ def _impl_bs_theta(table: Table, inputs: dict[str, Any]) -> np.ndarray:
277274

278275

279276
def _impl_bs_vega(table: Table, inputs: dict[str, Any]) -> np.ndarray:
280-
n = len(table)
281-
S = _to_numpy(inputs["S"], n)
282-
K = _to_numpy(inputs["K"], n)
283-
T = _to_numpy(inputs["T"], n)
284-
r = _to_numpy(inputs["r"], n)
285-
sigma = _to_numpy(inputs["sigma"], n)
277+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
286278

287279
d1, _d2 = _bs_d1d2(S, K, T, r, sigma)
288280
vega = S * _norm_pdf(d1) * np.sqrt(T)
@@ -291,12 +283,7 @@ def _impl_bs_vega(table: Table, inputs: dict[str, Any]) -> np.ndarray:
291283

292284

293285
def _impl_bs_rho(table: Table, inputs: dict[str, Any]) -> np.ndarray:
294-
n = len(table)
295-
S = _to_numpy(inputs["S"], n)
296-
K = _to_numpy(inputs["K"], n)
297-
T = _to_numpy(inputs["T"], n)
298-
r = _to_numpy(inputs["r"], n)
299-
sigma = _to_numpy(inputs["sigma"], n)
286+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
300287
option_type = _validate_option_type(inputs)
301288

302289
_d1, d2 = _bs_d1d2(S, K, T, r, sigma)
@@ -309,12 +296,7 @@ def _impl_bs_rho(table: Table, inputs: dict[str, Any]) -> np.ndarray:
309296

310297

311298
def _impl_bs_vanna(table: Table, inputs: dict[str, Any]) -> np.ndarray:
312-
n = len(table)
313-
S = _to_numpy(inputs["S"], n)
314-
K = _to_numpy(inputs["K"], n)
315-
T = _to_numpy(inputs["T"], n)
316-
r = _to_numpy(inputs["r"], n)
317-
sigma = _to_numpy(inputs["sigma"], n)
299+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
318300

319301
d1, d2 = _bs_d1d2(S, K, T, r, sigma)
320302
vanna = -_norm_pdf(d1) * d2 / sigma
@@ -323,12 +305,7 @@ def _impl_bs_vanna(table: Table, inputs: dict[str, Any]) -> np.ndarray:
323305

324306

325307
def _impl_bs_volga(table: Table, inputs: dict[str, Any]) -> np.ndarray:
326-
n = len(table)
327-
S = _to_numpy(inputs["S"], n)
328-
K = _to_numpy(inputs["K"], n)
329-
T = _to_numpy(inputs["T"], n)
330-
r = _to_numpy(inputs["r"], n)
331-
sigma = _to_numpy(inputs["sigma"], n)
308+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
332309

333310
d1, d2 = _bs_d1d2(S, K, T, r, sigma)
334311
vega = S * _norm_pdf(d1) * np.sqrt(T)
@@ -344,12 +321,7 @@ def _impl_bs_volga(table: Table, inputs: dict[str, Any]) -> np.ndarray:
344321

345322

346323
def _impl_bs_charm(table: Table, inputs: dict[str, Any]) -> np.ndarray:
347-
n = len(table)
348-
S = _to_numpy(inputs["S"], n)
349-
K = _to_numpy(inputs["K"], n)
350-
T = _to_numpy(inputs["T"], n)
351-
r = _to_numpy(inputs["r"], n)
352-
sigma = _to_numpy(inputs["sigma"], n)
324+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
353325

354326
d1, d2 = _bs_d1d2(S, K, T, r, sigma)
355327
sqrt_T = np.sqrt(T)
@@ -361,12 +333,7 @@ def _impl_bs_charm(table: Table, inputs: dict[str, Any]) -> np.ndarray:
361333

362334

363335
def _impl_bs_veta(table: Table, inputs: dict[str, Any]) -> np.ndarray:
364-
n = len(table)
365-
S = _to_numpy(inputs["S"], n)
366-
K = _to_numpy(inputs["K"], n)
367-
T = _to_numpy(inputs["T"], n)
368-
r = _to_numpy(inputs["r"], n)
369-
sigma = _to_numpy(inputs["sigma"], n)
336+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
370337

371338
d1, d2 = _bs_d1d2(S, K, T, r, sigma)
372339
sqrt_T = np.sqrt(T)
@@ -381,12 +348,7 @@ def _impl_bs_veta(table: Table, inputs: dict[str, Any]) -> np.ndarray:
381348

382349

383350
def _impl_bs_color(table: Table, inputs: dict[str, Any]) -> np.ndarray:
384-
n = len(table)
385-
S = _to_numpy(inputs["S"], n)
386-
K = _to_numpy(inputs["K"], n)
387-
T = _to_numpy(inputs["T"], n)
388-
r = _to_numpy(inputs["r"], n)
389-
sigma = _to_numpy(inputs["sigma"], n)
351+
S, K, T, r, sigma = _bs_inputs(inputs, len(table))
390352

391353
d1, d2 = _bs_d1d2(S, K, T, r, sigma)
392354
sqrt_T = np.sqrt(T)

0 commit comments

Comments
 (0)