Remove plotting logic and instead depend on local plotting module
This commit is contained in:
parent
f223238bd5
commit
ab3f28e1e4
@ -1,5 +1,6 @@
|
|||||||
from backtesting import backtest_iron_condor, DeltaTargetStrategy, OptionType
|
from backtesting import backtest_iron_condor, DeltaTargetStrategy, OptionType
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from plotting import BacktestChart, plot
|
||||||
|
|
||||||
def create_strategies(entry_time: str, number_of_contracts: int = 1):
|
def create_strategies(entry_time: str, number_of_contracts: int = 1):
|
||||||
call_spread_strategy = DeltaTargetStrategy(
|
call_spread_strategy = DeltaTargetStrategy(
|
||||||
@ -34,5 +35,9 @@ if __name__ == '__main__':
|
|||||||
end_date
|
end_date
|
||||||
)
|
)
|
||||||
print(backtest_result)
|
print(backtest_result)
|
||||||
# TODO: Move plot() to plotting module.
|
|
||||||
# plot(backtest_result, title = 'Iron Condor Backtest Results')
|
plot(BacktestChart(
|
||||||
|
dates = backtest_result['Date'],
|
||||||
|
profit = backtest_result['Cumulative Profit'],
|
||||||
|
title = f'Iron Condor @ {call_spread_strategy.trade_entry_time}'
|
||||||
|
))
|
@ -1,5 +1,4 @@
|
|||||||
import logging
|
import logging
|
||||||
import numpy as np
|
|
||||||
import os
|
import os
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import plotly.express as px
|
import plotly.express as px
|
||||||
@ -44,42 +43,6 @@ max_profits = []
|
|||||||
wins = []
|
wins = []
|
||||||
exit_times = []
|
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:
|
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]
|
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))]
|
opening_quotes = historical_option_data[(historical_option_data['quote_datetime'] == (current_date + ' ' + option_strat.trade_entry_time))]
|
||||||
|
Loading…
Reference in New Issue
Block a user