options-automation/trade_report.py

86 lines
3.9 KiB
Python
Raw Permalink Normal View History

from database.backtest import backtest_results
from database.procedures import backtest_profit, most_recent_trade_date
from database.trades import trades
symbol = 'SPX'
fees_and_commissions = 3.19 # IBKR fees and commissions for a single spread trade.
trade_date = most_recent_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_info = {}
backtest_info = {}
for _, trade in trade_data.iterrows():
entry_time = str(trade['Entry Time'])[-8:]
traded_entry_times.add(entry_time)
spreads = trade['Spreads']
for spread in spreads:
spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT'
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:]
if entry_time in traded_entry_times:
spreads = backtest['Spreads']
for spread in spreads:
spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT'
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)
trade_profits = [100 * (info[2] - info[3]) - fees_and_commissions for info in trade_info.values()]
stopped_out_spreads = sum(1 for info in trade_info.values() if info[5] is not None)
profit = sum(trade_profits) - (stopped_out_spreads * fees_and_commissions)
print(f'**Profit**: {round(profit, 2)}')
profit_backtest = backtest_profit(trade_date)
print(f'**Backtest Profit**: {round(profit_backtest, 2)}')
print(f'**Difference**: {round(profit - profit_backtest, 2)}\n')
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)}')
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)}')
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)}')
print()
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]}<br>Long Strike: {live_trade[1]}<br>Open Price: {live_trade[2]:.2f}<br>Close Price: {live_trade[3]:.2f}'
backtest_trade = backtest_info.get((entry_time, option_type))
backtest_trade_result = f'Short Strike: {backtest_trade[0]}<br>Long Strike: {backtest_trade[1]}<br>Open Price: {backtest_trade[2]:.2f}<br>Close Price: {backtest_trade[3]:.2f}'
print(f"| {entry_time} | {option_type} | {live_trade_result} | {backtest_trade_result} |")
print('\n---')