2024-02-02 18:43:37 +00:00
|
|
|
from database.backtest import backtest_results
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2024-02-02 21:07:15 +00:00
|
|
|
def best_entry_times(date: datetime = datetime.now(), days_to_look_back: int = 10):
|
|
|
|
end_date = date.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
|
2024-02-02 21:15:33 +00:00
|
|
|
end_date -= timedelta(days = 1) # Subtract one day in order to prevent lookahead bias.
|
2024-02-02 21:07:15 +00:00
|
|
|
start_date = end_date - timedelta(days = days_to_look_back)
|
2024-02-02 18:43:37 +00:00
|
|
|
|
|
|
|
backtest = backtest_results('SPX', '10 Delta Iron Condor', start_date = start_date, end_date = end_date)
|
2024-02-02 21:15:33 +00:00
|
|
|
print(backtest)
|
2024-02-02 18:43:37 +00:00
|
|
|
backtest['Entry Time'] = backtest['Entry Time'].dt.time
|
|
|
|
|
|
|
|
profit_by_entry_time = backtest.groupby('Entry Time')['Profit'].sum().reset_index()
|
|
|
|
|
2024-02-02 21:07:15 +00:00
|
|
|
best_entry_times = profit_by_entry_time.sort_values(by = 'Profit', ascending = False).head(10)
|
2024-02-02 18:43:37 +00:00
|
|
|
return [str(entry_time) for entry_time in best_entry_times['Entry Time'].tolist()]
|