options-automation/trade_report.py

70 lines
2.6 KiB
Python
Raw 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'
trade_date = most_recent_trade_date()
print(f'Trade Date: {trade_date}')
trade_data = trades(trade_date)
strategy = trade_data['Strategy'].iloc[0]
traded_entry_times = set()
trade_open_prices = {}
backtest_open_prices = {}
for _, trade in trade_data.iterrows():
entry_time = str(trade["Entry Time"])[-8:]
print(f'Entry Time: {entry_time}')
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}')
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:
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
legs = spread['Legs']
short_leg = legs[0]
long_leg = legs[1]
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}')
print(f'Backtest Profit: {backtest_profit(trade_date)}')
price_differences = []
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)}')
average_price_difference = sum(price_differences) / len(price_differences)
print(f'Average Spread Difference: {round(average_price_difference, 2)}')