From 2ea84e8dcddf08d1e506461fa62fac8bb80e2468 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Mon, 4 Mar 2024 15:59:06 -0800 Subject: [PATCH] Correctly update the closing price of each spread --- backtesting/backtest_iron_condor.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/backtesting/backtest_iron_condor.py b/backtesting/backtest_iron_condor.py index ba4e1a9..1a0a927 100644 --- a/backtesting/backtest_iron_condor.py +++ b/backtesting/backtest_iron_condor.py @@ -124,18 +124,18 @@ def _backtest_iron_condor( "Legs": [{"Action": "SELL", "Strike": call_spread_entry['strike_short_strike'], "Type": "CALL"}, {"Action": "BUY", "Strike": call_spread_entry['strike_long_strike'], "Type": "CALL"}], "Open": original_call_spread_price, - "High": None, - "Low": None, - "Close": None + "High": float('-inf'), + "Low": float('inf'), + "Close": 0.0 } put_spread_details = { "Legs": [{"Action": "SELL", "Strike": put_spread_entry['strike_short_strike'], "Type": "PUT"}, {"Action": "BUY", "Strike": put_spread_entry['strike_long_strike'], "Type": "PUT"}], "Open": original_put_spread_price, - "High": None, - "Low": None, - "Close": None + "High": float('-inf'), + "Low": float('inf'), + "Close": 0.0 } trades_entered = False @@ -172,13 +172,11 @@ def _backtest_iron_condor( else: current_put_spread_price = ((put_spread['ask_short_strike'] + put_spread['bid_short_strike']) / 2.0) - ((put_spread['ask_long_strike'] + put_spread['bid_long_strike']) / 2.0) - call_spread_details['High'] = max(call_spread_details['High'] or float('-inf'), current_call_spread_price) - call_spread_details['Low'] = min(call_spread_details['Low'] or float('inf'), current_call_spread_price) - call_spread_details['Close'] = current_call_spread_price + call_spread_details['High'] = max(call_spread_details['High'], current_call_spread_price) + call_spread_details['Low'] = min(call_spread_details['Low'], current_call_spread_price) - put_spread_details['High'] = max(put_spread_details['High'] or float('-inf'), current_put_spread_price) - put_spread_details['Low'] = min(put_spread_details['Low'] or float('inf'), current_put_spread_price) - put_spread_details['Close'] = current_put_spread_price + put_spread_details['High'] = max(put_spread_details['High'], current_put_spread_price) + put_spread_details['Low'] = min(put_spread_details['Low'], current_put_spread_price) if not call_spread_stopped_out: if current_call_spread_price >= ((call_spread_strategy.stop_loss_multiple + 1) * original_call_spread_price): @@ -231,9 +229,11 @@ def _backtest_iron_condor( if not call_spread_stopped_out and current_call_spread_price > 0.05: premium_received -= current_call_spread_price + call_spread_details['Close'] = current_call_spread_price if not put_spread_stopped_out and current_put_spread_price > 0.05: premium_received -= current_put_spread_price + put_spread_details['Close'] = current_put_spread_price number_of_contracts = call_spread_strategy.number_of_contracts stop_out_fees = 0.0 # It costs money to get stopped out.