diff --git a/trade_report.py b/trade_report.py index 735b1bb..566a871 100644 --- a/trade_report.py +++ b/trade_report.py @@ -5,66 +5,73 @@ from database.trades import trades symbol = 'SPX' trade_date = most_recent_trade_date() -print(f'Trade Date: {trade_date}') +print(f'# {trade_date.strftime("%B %d, %Y")}\n') trade_data = trades(trade_date) strategy = trade_data['Strategy'].iloc[0] traded_entry_times = set() -trade_open_prices = {} -backtest_open_prices = {} +trade_info = {} +backtest_info = {} for _, trade in trade_data.iterrows(): - entry_time = str(trade["Entry Time"])[-8:] - print(f'Entry Time: {entry_time}') + entry_time = str(trade['Entry Time'])[-8:] traded_entry_times.add(entry_time) spreads = trade['Spreads'] for spread in spreads: - open_price = spread['Open'] spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT' - trade_open_prices[(entry_time, spread_type)] = open_price - - # Assuming the first leg is always short and the second is long. - legs = spread['Legs'] - short_leg = legs[0] - long_leg = legs[1] - - print(f' Short Strike: {short_leg["Strike"]}, Type: {short_leg["Type"]}') - print(f' Long Strike: {long_leg["Strike"]}, Type: {long_leg["Type"]}') - print(f' Opening Price: {open_price}') + short_strike = int(spread['Legs'][0]['Strike']) + long_strike = int(spread['Legs'][1]['Strike']) + open_price = round(spread['Open'], 2) + close_price = round(spread['Close'], 2) if 'Close' in spread else 0.0 + entry_slippage = spread['Entry Slippage'] + exit_slippage = spread['Exit Slippage'] if 'Exit Slippage' in spread else None + trade_info[(entry_time, spread_type)] = (short_strike, long_strike, open_price, close_price, entry_slippage, exit_slippage) backtest_data = backtest_results(symbol, strategy, trade_date) for _, backtest in backtest_data.iterrows(): - entry_time = str(backtest["Entry Time"])[-8:] + entry_time = str(backtest['Entry Time'])[-8:] if entry_time in traded_entry_times: - print(f'Backtest Entry Time: {entry_time}') - spreads = backtest['Spreads'] for spread in spreads: - open_price = spread['Open'] spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT' - backtest_open_prices[(entry_time, spread_type)] = open_price + short_strike = int(spread['Legs'][0]['Strike']) + long_strike = int(spread['Legs'][1]['Strike']) + open_price = round(spread['Open'], 2) + close_price = round(spread['Close'], 2) + backtest_info[(entry_time, spread_type)] = (short_strike, long_strike, open_price, close_price) - legs = spread['Legs'] - short_leg = legs[0] - long_leg = legs[1] +print(f'**Backtest Profit**: {round(backtest_profit(trade_date), 2)}\n') - print(f' Backtest Short Strike: {short_leg["Strike"]}, Type: {short_leg["Type"]}') - print(f' Backtest Long Strike: {long_leg["Strike"]}, Type: {long_leg["Type"]}') - print(f' Backtest Opening Price: {open_price}') +open_differences = [backtest_info[key][2] - trade_info[key][2] for key in trade_info] +average_open_difference = sum(open_differences) / len(open_differences) if open_differences else 0 +print(f'**Average Open Difference**: {round(average_open_difference, 2)}') -print(f'Backtest Profit: {backtest_profit(trade_date)}') +entry_slippages = [trade_info[key][4] for key in trade_info] +average_entry_slippage = sum(entry_slippages) / len(entry_slippages) +print(f'**Average Entry Slippage**: {round(average_entry_slippage, 2)}') -price_differences = [] +exit_slippages = [trade_info[key][5] for key in trade_info if trade_info[key][5] is not None] +if exit_slippages: + average_exit_slippage = sum(exit_slippages) / len(exit_slippages) + print(f'**Average Exit Slippage**: {round(average_exit_slippage, 2)}') -for (entry_time, spread_type), backtest_open_price in backtest_open_prices.items(): - trade_open_price = trade_open_prices[(entry_time, spread_type)] - price_difference = backtest_open_price - trade_open_price - price_differences.append(price_difference) - print(f'{entry_time} {spread_type} Spread Difference: {round(price_difference, 2)}') +print() -average_price_difference = sum(price_differences) / len(price_differences) -print(f'Average Spread Difference: {round(average_price_difference, 2)}') \ No newline at end of file +print('| Entry Time | Spread Type | Live Trading Result | Backtest Result |') +print('|------------|-------------|---------------------|-----------------|') + +for entry_time in sorted(traded_entry_times): + for option_type in ['CALL', 'PUT']: + live_trade = trade_info.get((entry_time, option_type)) + live_trade_result = f'Short Strike: {live_trade[0]}
Long Strike: {live_trade[1]}
Open Price: {live_trade[2]:.2f}
Close Price: {live_trade[3]:.2f}' + + backtest_trade = backtest_info.get((entry_time, option_type)) + backtest_trade_result = f'Short Strike: {backtest_trade[0]}
Long Strike: {backtest_trade[1]}
Open Price: {backtest_trade[2]:.2f}
Close Price: {backtest_trade[3]:.2f}' + + print(f"| {entry_time} | {option_type} | {live_trade_result} | {backtest_trade_result} |") + +print('\n---') \ No newline at end of file