From ab3f28e1e41ffae0d184a1ba7239bcce78c62552 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 30 Jan 2024 11:17:46 -0800 Subject: [PATCH] Remove plotting logic and instead depend on local plotting module --- backtest_iron_condor_example.py | 9 +++++-- backtesting/backtest_iron_condor.py | 37 ----------------------------- 2 files changed, 7 insertions(+), 39 deletions(-) diff --git a/backtest_iron_condor_example.py b/backtest_iron_condor_example.py index e79ea2b..62f5851 100644 --- a/backtest_iron_condor_example.py +++ b/backtest_iron_condor_example.py @@ -1,5 +1,6 @@ from backtesting import backtest_iron_condor, DeltaTargetStrategy, OptionType from datetime import datetime +from plotting import BacktestChart, plot def create_strategies(entry_time: str, number_of_contracts: int = 1): call_spread_strategy = DeltaTargetStrategy( @@ -34,5 +35,9 @@ if __name__ == '__main__': end_date ) print(backtest_result) - # TODO: Move plot() to plotting module. - # plot(backtest_result, title = 'Iron Condor Backtest Results') \ No newline at end of file + + plot(BacktestChart( + dates = backtest_result['Date'], + profit = backtest_result['Cumulative Profit'], + title = f'Iron Condor @ {call_spread_strategy.trade_entry_time}' + )) \ No newline at end of file diff --git a/backtesting/backtest_iron_condor.py b/backtesting/backtest_iron_condor.py index a9041de..cf77abd 100644 --- a/backtesting/backtest_iron_condor.py +++ b/backtesting/backtest_iron_condor.py @@ -1,5 +1,4 @@ import logging -import numpy as np import os import pandas as pd import plotly.express as px @@ -44,42 +43,6 @@ max_profits = [] wins = [] exit_times = [] -def plot(backtest_results: pd.DataFrame, title: str): - backtest_results.drop('Profit', axis = 1, inplace = True) - backtest_results.rename(columns = {'Cumulative Profit' : 'Profit'}, inplace = True) - - # Exclude dates on which the market was closed from being plotted in order to prevent gaps on chart. - start_date = backtest_results['Date'].min() - end_date = backtest_results['Date'].max() - backtest_date_range = pd.date_range(start = start_date, end = end_date).to_list() - backtest_date_range = set([timestamp.strftime('%Y-%m-%d') for timestamp in backtest_date_range]) - backtested_dates = set(backtest_results['Date'].to_list()) - excluded_dates = backtest_date_range - backtested_dates - - backtest_results['Color'] = np.where(backtest_results['Profit'] >= 0, 'limegreen', 'red') - color_sequence = ['limegreen', 'red'] if backtest_results.iloc[0]['Profit'] >= 0 else ['red', 'limegreen'] - - chart = px.bar(backtest_results, x='Date', y='Profit', title=title, color='Color', color_discrete_sequence=color_sequence, hover_data={'Color': False}) - chart.update_layout({ - 'font_color': '#7a7c7d', - 'plot_bgcolor': '#0f0f0f', - 'paper_bgcolor': '#0f0f0f', - 'title_font_color': '#7a7c7d', - 'xaxis': { - 'gridcolor': '#0f0f0f', - 'zerolinecolor': '#0f0f0f' - }, - 'yaxis': { - 'gridcolor': '#0f0f0f', - 'zerolinecolor': '#0f0f0f' - } - }) - chart.update_traces(marker_line_width=0, selector=dict(type='bar')) - chart.update_layout(bargap=0, bargroupgap = 0) - chart.update_layout(showlegend=False) - chart.update_xaxes(rangebreaks=[dict(values=list(excluded_dates))]) - chart.show() - def get_spread_history_credit(historical_option_data: pd.DataFrame, option_strat: CreditTargetStrategy) -> pd.DataFrame: current_date = historical_option_data.iloc[0]['quote_datetime'][:10] opening_quotes = historical_option_data[(historical_option_data['quote_datetime'] == (current_date + ' ' + option_strat.trade_entry_time))]