From 3526d5d5642a808d99093fd38bef548f4e95bbf8 Mon Sep 17 00:00:00 2001 From: Nelson Torres Date: Sun, 8 Dec 2024 13:38:23 -0300 Subject: [PATCH] minor refactor cleaning the code --- max_pain.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/max_pain.py b/max_pain.py index aef67944..468b1d3d 100644 --- a/max_pain.py +++ b/max_pain.py @@ -57,28 +57,25 @@ async def max_pain_daemon( # an amount greater than zero. total_intrinsic_value: Decimal = Decimal('Infinity') max_pain: Decimal = Decimal(0) - + call_cash: Decimal = Decimal(0) + put_cash: Decimal = Decimal(0) intrinsic_values: dict[str, dict[str, Decimal]] = {} - closes: list[str] = sorted(oi_by_strikes.keys()) - for strike in oi_by_strikes: - s: Decimal = Decimal(f'{strike}') - call_cash: Decimal = Decimal(0) - put_cash: Decimal = Decimal(0) - for close in closes: - c: Decimal = Decimal(f'{close}') - call_cash += max(0, (s - c) * oi_by_strikes[f'{close}']['C']) - put_cash += max(0, (c - s) * oi_by_strikes[f'{close}']['P']) - + closes: list = sorted(Decimal(close) for close in oi_by_strikes) + + for strike, oi in oi_by_strikes.items(): + s = Decimal(strike) + call_cash = sum(max(0, (s - c) * oi_by_strikes[str(c)]['C']) for c in closes) + put_cash = sum(max(0, (c - s) * oi_by_strikes[str(c)]['P']) for c in closes) + intrinsic_values[strike] = { 'C': call_cash, 'P': put_cash, 'total': call_cash + put_cash, } - for strike in intrinsic_values: - if intrinsic_values[f'{strike}']['total'] < total_intrinsic_value: - total_intrinsic_value = intrinsic_values[f'{strike}']['total'] - max_pain = strike\ + if intrinsic_values[strike]['total'] < total_intrinsic_value: + total_intrinsic_value = intrinsic_values[strike]['total'] + max_pain = s return { 'timestamp': timestamp, @@ -87,7 +84,6 @@ async def max_pain_daemon( 'max_pain': max_pain, } - async with get_client( ) as client: instruments = await client.get_instruments(