Add the ability to query OHLC data
This commit is contained in:
parent
867891dbe3
commit
36a592c628
1
database/ohlc/__init__.py
Normal file
1
database/ohlc/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .ohlc import ohlc
|
12
database/ohlc/create_table.sql
Normal file
12
database/ohlc/create_table.sql
Normal file
@ -0,0 +1,12 @@
|
||||
CREATE TABLE OHLC (
|
||||
"Symbol" VARCHAR(10) NOT NULL,
|
||||
"Date" DATE NOT NULL,
|
||||
"Timeframe" VARCHAR(10) NOT NULL,
|
||||
"Timestamp" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
|
||||
"Open" NUMERIC NOT NULL,
|
||||
"High" NUMERIC NOT NULL,
|
||||
"Low" NUMERIC NOT NULL,
|
||||
"Close" NUMERIC NOT NULL,
|
||||
"Volume" NUMERIC,
|
||||
PRIMARY KEY ("Symbol", "Date", "Timeframe", "Timestamp")
|
||||
);
|
52
database/ohlc/ohlc.py
Normal file
52
database/ohlc/ohlc.py
Normal file
@ -0,0 +1,52 @@
|
||||
import pandas as pd
|
||||
|
||||
from datetime import datetime
|
||||
from dotenv import load_dotenv
|
||||
from os import getenv
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
load_dotenv()
|
||||
|
||||
def ohlc(symbol: str, timeframe: str, date: datetime = datetime.now,
|
||||
start_date: datetime = None, end_date: datetime = None):
|
||||
"""
|
||||
Retrieve OHLC data for the specified symbol, time frame, and date(s).
|
||||
|
||||
Example Usage:
|
||||
|
||||
data = ohlc('SPY', '5m', datetime(2023, 9, 20)) # For a single day.
|
||||
|
||||
data = ohlc('SPY', '5m', datetime(2023, 9, 20), datetime(2023, 9, 25)) # For a range of dates.
|
||||
|
||||
:param symbol: The stock, etf, etc. symbol.
|
||||
:param timeframe: The timeframe (e.g., '5m', '1h', etc.).
|
||||
:param date: The date for which data is being requested.
|
||||
:param start_date: The start date (optional).
|
||||
:param end_date: The end date (optional).
|
||||
:return: A pandas DataFrame containing the OHLC data.
|
||||
"""
|
||||
|
||||
database_url = (
|
||||
f"postgresql+psycopg2://{getenv('DATABASE_USER')}:{getenv('DATABASE_PASSWORD')}"
|
||||
f"@{getenv('DATABASE_HOST')}:{getenv('DATABASE_PORT')}/{getenv('DATABASE_NAME')}"
|
||||
)
|
||||
engine = create_engine(database_url)
|
||||
|
||||
if start_date and end_date:
|
||||
query = f"""
|
||||
SELECT "Timestamp", "Open", "High", "Low", "Close", "Volume"
|
||||
FROM "OHLC"
|
||||
WHERE "Symbol" = %s AND "Timeframe" = %s AND "Date" BETWEEN %s AND %s
|
||||
ORDER BY "Timestamp";
|
||||
"""
|
||||
params = (symbol, timeframe, start_date, end_date)
|
||||
else:
|
||||
query = f"""
|
||||
SELECT "Timestamp", "Open", "High", "Low", "Close", "Volume"
|
||||
FROM "OHLC"
|
||||
WHERE "Symbol" = %s AND "Timeframe" = %s AND "Date" = %s
|
||||
ORDER BY "Timestamp";
|
||||
"""
|
||||
params = (symbol, timeframe, date)
|
||||
|
||||
return pd.read_sql_query(query, engine, params=params)
|
5
query_ohlc_example.py
Normal file
5
query_ohlc_example.py
Normal file
@ -0,0 +1,5 @@
|
||||
from database.ohlc import ohlc
|
||||
from datetime import datetime
|
||||
|
||||
data = ohlc('SPY', '5m', datetime(2023, 9, 22))
|
||||
print(data)
|
Loading…
Reference in New Issue
Block a user