Convert trade report to markdown and include entry and exit slippage information

This commit is contained in:
moshferatu 2024-03-06 06:32:34 -08:00
parent 0bebd31c26
commit 34cb5f3fe9

View File

@ -5,66 +5,73 @@ from database.trades import trades
symbol = 'SPX' symbol = 'SPX'
trade_date = most_recent_trade_date() 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) trade_data = trades(trade_date)
strategy = trade_data['Strategy'].iloc[0] strategy = trade_data['Strategy'].iloc[0]
traded_entry_times = set() traded_entry_times = set()
trade_open_prices = {} trade_info = {}
backtest_open_prices = {} backtest_info = {}
for _, trade in trade_data.iterrows(): for _, trade in trade_data.iterrows():
entry_time = str(trade["Entry Time"])[-8:] entry_time = str(trade['Entry Time'])[-8:]
print(f'Entry Time: {entry_time}')
traded_entry_times.add(entry_time) traded_entry_times.add(entry_time)
spreads = trade['Spreads'] spreads = trade['Spreads']
for spread in spreads: for spread in spreads:
open_price = spread['Open']
spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT' spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT'
trade_open_prices[(entry_time, spread_type)] = open_price short_strike = int(spread['Legs'][0]['Strike'])
long_strike = int(spread['Legs'][1]['Strike'])
# Assuming the first leg is always short and the second is long. open_price = round(spread['Open'], 2)
legs = spread['Legs'] close_price = round(spread['Close'], 2) if 'Close' in spread else 0.0
short_leg = legs[0] entry_slippage = spread['Entry Slippage']
long_leg = legs[1] 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)
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) backtest_data = backtest_results(symbol, strategy, trade_date)
for _, backtest in backtest_data.iterrows(): 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: if entry_time in traded_entry_times:
print(f'Backtest Entry Time: {entry_time}')
spreads = backtest['Spreads'] spreads = backtest['Spreads']
for spread in spreads: for spread in spreads:
open_price = spread['Open']
spread_type = 'CALL' if spread['Legs'][0]['Type'] == 'CALL' else 'PUT' 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'] print(f'**Backtest Profit**: {round(backtest_profit(trade_date), 2)}\n')
short_leg = legs[0]
long_leg = legs[1]
print(f' Backtest Short Strike: {short_leg["Strike"]}, Type: {short_leg["Type"]}') open_differences = [backtest_info[key][2] - trade_info[key][2] for key in trade_info]
print(f' Backtest Long Strike: {long_leg["Strike"]}, Type: {long_leg["Type"]}') average_open_difference = sum(open_differences) / len(open_differences) if open_differences else 0
print(f' Backtest Opening Price: {open_price}') 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(): print()
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('| Entry Time | Spread Type | Live Trading Result | Backtest Result |')
print(f'Average Spread Difference: {round(average_price_difference, 2)}') 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---')