From e0d17837157eba5342e8ee04ae102b3b664458cb Mon Sep 17 00:00:00 2001 From: moshferatu Date: Fri, 2 Feb 2024 10:43:37 -0800 Subject: [PATCH] Add a method to retrieve the best performing entry times over the specified period of time --- backtesting/__init__.py | 1 + backtesting/best_entry_times.py | 14 ++++++++++++++ test/best_entry_times_test.py | 5 +++++ 3 files changed, 20 insertions(+) create mode 100644 backtesting/best_entry_times.py create mode 100644 test/best_entry_times_test.py diff --git a/backtesting/__init__.py b/backtesting/__init__.py index 4a4c3a3..415352d 100644 --- a/backtesting/__init__.py +++ b/backtesting/__init__.py @@ -1,5 +1,6 @@ from .available_entry_times import available_entry_times from .backtest_iron_condor import backtest_iron_condor +from .best_entry_times import best_entry_times from .credit_target_strategy import CreditTargetStrategy from .delta_target_strategy import DeltaTargetStrategy from .option_spread_strategy import OptionSpreadStrategy diff --git a/backtesting/best_entry_times.py b/backtesting/best_entry_times.py new file mode 100644 index 0000000..d3aacf2 --- /dev/null +++ b/backtesting/best_entry_times.py @@ -0,0 +1,14 @@ +from database.backtest import backtest_results +from datetime import datetime, timedelta + +def best_entry_times(lookback_period: int = 10): + end_date = datetime.now().replace(hour = 0, minute = 0, second = 0, microsecond = 0) + start_date = end_date - timedelta(days = lookback_period) + + backtest = backtest_results('SPX', '10 Delta Iron Condor', start_date = start_date, end_date = end_date) + backtest['Entry Time'] = backtest['Entry Time'].dt.time + + profit_by_entry_time = backtest.groupby('Entry Time')['Profit'].sum().reset_index() + + best_entry_times = profit_by_entry_time.sort_values(by='Profit', ascending=False).head(10) + return [str(entry_time) for entry_time in best_entry_times['Entry Time'].tolist()] \ No newline at end of file diff --git a/test/best_entry_times_test.py b/test/best_entry_times_test.py new file mode 100644 index 0000000..a7f5e5d --- /dev/null +++ b/test/best_entry_times_test.py @@ -0,0 +1,5 @@ +from backtesting import best_entry_times + +print(best_entry_times()) + +print(best_entry_times(lookback_period = 30)) # Days. \ No newline at end of file