Simplify backtest result table accessor to only allow for retrieving backtests for a single date or date range

This commit is contained in:
moshferatu 2024-02-02 09:41:18 -08:00
parent aa08acb083
commit 5bcd8135a4
2 changed files with 30 additions and 48 deletions

View File

@ -8,32 +8,26 @@ from sqlalchemy import create_engine
load_dotenv()
def backtest_results(
symbol: str = None,
strategy: str = None,
date: datetime = None,
entry_time: datetime = None,
start_date: datetime = None,
symbol: str,
strategy: str,
start_date: datetime,
end_date: datetime = None
) -> pd.DataFrame:
"""
Retrieve records from the 'Backtest' table for the specified symbol, strategy, date, and entry time.
Retrieve records from the 'Backtest' table for the specified symbol, strategy, and date or date range.
Example Usage:
Example usage:
For a single day, all entry times:
data = backtest_results('SPX', '10 Delta Iron Condor', datetime(2024, 1, 1))
data = backtest_records('SPX', '10 Delta Iron Condor', datetime(2024, 1, 1))
For a range of dates:
data = backtest_results('SPX', '10 Delta Iron Condor', datetime(2024, 1, 1), datetime(2024, 1, 31))
For a single day, single entry time:
data = backtest_records('SPX', '10 Delta Iron Condor', datetime(2024, 1, 1), datetime(2024, 1, 1, 9, 30))
:param symbol: The instrument symbol.
:param strategy: The trading strategy name.
:param date: The date for which data is being requested.
:param entry_time: The trade entry time.
:param start_date: The start date (optional).
:param end_date: The end date (optional).
:param start_date: The start date for which data is being requested, or the specific date if end_date is None.
:param end_date: The end date for which data is being requested (optional).
:return: A pandas DataFrame containing the backtest results.
"""
@ -43,39 +37,21 @@ def backtest_results(
)
engine = create_engine(database_url)
# TODO: This doesn't cover all cases, but it's all I need for now.
if start_date and end_date:
query = f"""
if end_date is None:
query = """
SELECT *
FROM "Backtest"
WHERE "Symbol" = %s AND "Strategy" = %s AND "Date" = %s
ORDER BY "Entry Time";
"""
params = (symbol, strategy, start_date)
else:
query = """
SELECT *
FROM "Backtest"
WHERE "Symbol" = %s AND "Strategy" = %s AND "Date" BETWEEN %s AND %s
ORDER BY "Entry Time";
"""
params = (symbol, strategy, start_date, end_date)
elif entry_time:
query = f"""
SELECT *
FROM "Backtest"
WHERE "Symbol" = %s AND "Strategy" = %s AND "Date" = %s
AND "Entry Time" = %s
ORDER BY "Entry Time";
"""
params = (symbol, strategy, date, entry_time)
elif date and (not symbol) and (not strategy):
query = f"""
SELECT *
FROM "Backtest"
WHERE "Date" = %s
ORDER BY "Entry Time";
"""
params = (date,)
else:
query = f"""
SELECT *
FROM "Backtest"
WHERE "Symbol" = %s AND "Strategy" = %s AND "Date" = %s
ORDER BY "Entry Time";
"""
params = (symbol, strategy, date)
return pd.read_sql_query(query, engine, params = params)
return pd.read_sql_query(query, engine, params = params)

View File

@ -1,5 +1,11 @@
from database.backtest import backtest_results
from datetime import datetime
data = backtest_results('SPX', '10 Delta Iron Condor @ 09:35:00', datetime(2024, 1, 2))
print(data)
single_date = backtest_results('SPX', '10 Delta Iron Condor', datetime(2024, 1, 2))
print('Single Date:')
print(single_date)
date_range = backtest_results('SPX', '10 Delta Iron Condor',
start_date = datetime(2024, 1, 1), end_date = datetime(2024, 1, 31))
print('Date Range:')
print(date_range)