Files
OpenBB/openbb_terminal/stocks/databento_model.py
teh_coderer 6a9308c3e7 Feature/sdk v4 pydantic v2 (#5458)
* init

* Update provider_interface.py

* docstring

* pydantic v2

* Update __init__.py

* build package v2 support

* `alises` to `aliases`

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>

* `alises` to `aliases`

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>

* `alises` to `aliases`

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>

* updates

* Update registry.py

* validate_arguments -> validate_call

* update terminal classes

* response_model_by_alias = False

* query_params by alias

* Update major_indices_constituents.py

* add alias to Query in `provider_interface`

* yfinance

* build package, add param defaults and correct `OBBject` results type

* fix cpi validation errors

* Update risk_premium.py

* fix `forex_pairs` active query param annotation, set default when only 1 provider

* fix and refactor intrinio `options_chains`

* Update poetry.lock

* fix benzinga news, build package, add intrinio fred_historical fetcher

* Update extension_map.json

* ruff

* Update unit_test_generator.py

* updates

* add `default=None` on Optional fields

* `parse_obj` -> `model_validate`

* add alias to dup fields on providers with `__alias_dict__`, fix `test_generate_model_docstring`

* Update package_builder.py

* fix validation errors, fix tests

* tests http

* `fred_historical` record

* Update test_intrinio_fetchers.py

* Update test_intrinio_fetchers.py

* Update test_intrinio_fetchers.py

* Update available_indices.py

* Update stock_historical.py

* build package

* build package without optional

* change `SystemSettings` -> `MockSystemSettings` in `test_system_settings.py`

* `openbb_figure.py`/`backend.py` terminal changes  , update deps/lock files

* `linearmodels>4.26`  auto installs `setuptools-scm` which cause error messages

* clean up

* `dev_install.py` for local development

* ruff

* dup `# noqa`

* build

* Update bitquery_model.py

* records

* rec

* f

* worky?

* update `integration_test_generator.py` and `unit_test_generator.py`

* renamed test to tests in file

* fix import due to change in file name

---------

Co-authored-by: Pratyush Shukla <ps4534@nyu.edu>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
2023-09-27 09:40:07 +01:00

134 lines
4.3 KiB
Python

__docformat__ = "numpy"
# IMPORTATION STANDARD
import logging
from datetime import datetime, timedelta
from io import StringIO
# IMPORTATION THIRDPARTY
import pandas as pd
import requests
from pydantic import BaseModel, ConfigDict
# IMPORTATION INTERNAL
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.decorators import check_api_key, log_start_end
from openbb_terminal.helper_funcs import request
from openbb_terminal.rich_config import console
logger = logging.getLogger(__name__)
# pylint:disable=too-few-public-methods
class DataBento(BaseModel):
symbol: str
exchange: str = "XNAS.ITCH"
stype: str = "raw_symbol"
start: str = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
end: str = datetime.now().strftime("%Y-%m-%d")
model_config = ConfigDict(arbitrary_types_allowed=True)
def get_historical_stock(self):
"""Gets historical EOD stock data from DataBento. Currently, just NASDAQ is supported.
Note that only nonadjusted data is available."""
self.exchange = "XNAS.ITCH"
self.stype = "raw_symbol"
return self.get_historical()
def get_historical_futures(self):
"""Gets historical EODfutures data from DataBento. Currently, just CME is supported.
Note this gets the highest volume contract each day"""
self.exchange = "GLBX.MDP3"
self.stype = "continuous"
return self.get_historical()
@log_start_end(log=logger)
@check_api_key(["API_DATABENTO_KEY"])
def get_historical(self) -> pd.DataFrame:
"""Prepares the request to be made. I am using their https interface instead of their python client.
This updated to .C.0"""
base_url = "https://hist.databento.com/v0/timeseries.get_range"
symbol = self.symbol + ".C.0" if self.exchange == "GLBX.MDP3" else self.symbol
params = {
"dataset": self.exchange,
"symbols": symbol,
"schema": "ohlcv-1d",
"start": self.start,
"end": self.end,
"stype_in": self.stype,
"encoding": "csv",
}
auth = requests.auth.HTTPBasicAuth(
get_current_user().credentials.API_DATABENTO_KEY, ""
)
data = self.process_request(base_url, params, auth)
if data.empty:
return pd.DataFrame()
return data
def process_request(self, base_url, params, auth) -> pd.DataFrame:
"""Takes the request and returns the adjusted dataframe"""
r = request(base_url, params=params, auth=auth)
if r.status_code == 422 and "Unprocessable" in r.text:
console.print(r.json()["detail"])
return pd.DataFrame()
if r.status_code != 200:
console.print(f"Error: Status Code {r.status_code}")
return pd.DataFrame()
df = pd.read_csv(StringIO(r.text))
df["time"] = pd.to_datetime(df.ts_event, unit="ns")
df[["open", "high", "low", "close"]] /= 1_000_000_000
df = df.drop(columns=["ts_event", "publisher_id"]).set_index("time")
df = df.rename(
columns={
"open": "Open",
"high": "High",
"low": "Low",
"close": "Close",
"volume": "Volume",
}
)
return df
@log_start_end(log=logger)
@check_api_key(["API_DATABENTO_KEY"])
def get_historical_stock(symbol: str, start_date: str, end_date: str) -> pd.DataFrame:
"""Gets historical EOD data from DataBento. Currently, just NASDAQ is supported.
Parameters
----------
symbol : str
Symbol to get data for
start_date : str
Start date of data
end_date : str
End date to get data for
"""
db = DataBento(symbol=symbol, start=start_date, end=end_date)
return db.get_historical_stock()
@log_start_end(log=logger)
@check_api_key(["API_DATABENTO_KEY"])
def get_historical_futures(symbol: str, start_date: str, end_date: str) -> pd.DataFrame:
"""Gets historical EODfutures data from DataBento. Currently, just CME is supported.
Parameters
----------
symbol : str
Symbol to get data for
start_date : str
Start date of data
end_date : str
End date to get data for
"""
db = DataBento(symbol=symbol, start=start_date, end=end_date)
return db.get_historical_futures()