refactored MajorIndicesEOD for economy.index

This commit is contained in:
Pratyush Shukla
2023-07-28 02:39:45 +05:30
parent c287101e64
commit 90db0babd0
3 changed files with 103 additions and 117 deletions

View File

@@ -1,64 +1,58 @@
"""FMP Major Indices end of day fetcher."""
from datetime import date, datetime
from typing import Dict, List, Literal, Optional
from datetime import datetime
from typing import Dict, List, Optional
from openbb_provider.abstract.data import Data, QueryParams
from openbb_provider.abstract.fetcher import Fetcher
from openbb_provider.helpers import data_transformer, get_querystring
from openbb_provider.metadata import DATA_DESCRIPTIONS
from openbb_provider.models.major_indices_eod import (
MajorIndicesEODData,
MajorIndicesEODQueryParams,
)
from pydantic import Field, NonNegativeInt, validator
from openbb_fmp.utils.helpers import get_data_many
from pydantic import Field, NonNegativeInt, validator
class FMPMajorIndicesEODQueryParams(QueryParams):
"""FMP Major Indices end of day query.
Source: https://site.financialmodelingprep.com/developer/docs/#Historical-stock-index-prices
class FMPMajorIndicesEODQueryParams(MajorIndicesEODQueryParams):
"""FMP Major Indices end of day Query.
Parameter
---------
symbol : str
The symbol of the company.
start_date : date
The start date of the stock data from which to retrieve the data.
end_date : date
The end date of the stock data up to which to retrieve the data.
timeseries : Optional[int]
The number of days to look back.
serietype : Optional[Literal["line"]]
The type of the series. Only "line" is supported.
Source: https://site.financialmodelingprep.com/developer/docs/historical-index-price-api/
"""
symbol: str = Field(min_length=1)
serietype: Optional[Literal["line"]]
start_date: date
end_date: date
timeseries: Optional[NonNegativeInt] # Number of days to looks back
timeseries: Optional[NonNegativeInt] = Field(
default=None, description="Number of days to look back."
)
class FMPMajorIndicesEODData(Data):
date: datetime
open: float
high: float
low: float
close: float
adjClose: float = Field(alias="adj_close")
volume: float
unadjustedVolume: float
change: float
changePercent: float
vwap: float
label: str
changeOverTime: float
class FMPMajorIndicesEODData(MajorIndicesEODData):
"""FMP Major Indices end of day Data."""
date: datetime = Field(description=DATA_DESCRIPTIONS.get("date", ""))
adjClose: float = Field(
description="Adjusted Close Price of the symbol.", alias="adj_close"
)
unadjustedVolume: float = Field(
description="Unadjusted volume of the symbol.", alias="unadjusted_volume"
)
change: float = Field(
description="Change in the price of the symbol from the previous day.",
alias="change",
)
changePercent: float = Field(
description=r"Change \% in the price of the symbol.", alias="change_percent"
)
vwap: float = Field(description="Volume Weighted Average Price of the symbol.")
label: str = Field(description="Human readable format of the date.")
changeOverTime: float = Field(
description=r"Change \% in the price of the symbol over a period of time.",
alias="change_over_time",
)
@validator("date", pre=True)
def time_validate(cls, v): # pylint: disable=E0213
def date_validate(cls, v): # pylint: disable=E0213
return datetime.strptime(v, "%Y-%m-%d")
@@ -74,12 +68,11 @@ class FMPMajorIndicesEODFetcher(
def transform_query(
query: MajorIndicesEODQueryParams, extra_params: Optional[Dict] = None
) -> FMPMajorIndicesEODQueryParams:
raw_end = query.end_date if query.end_date else datetime.now()
return FMPMajorIndicesEODQueryParams(
symbol=query.symbol,
start_date=query.start_date,
end_date=raw_end,
**extra_params if extra_params else {},
end_date=query.end_date,
**extra_params or {},
)
@staticmethod
@@ -89,10 +82,11 @@ class FMPMajorIndicesEODFetcher(
if credentials:
api_key = credentials.get("fmp_api_key")
base_url = "https://financialmodelingprep.com/api/v3/"
base_url = "https://financialmodelingprep.com/api/v3"
query_str = get_querystring(query.dict(), ["symbol"])
query_str = query_str.replace("start_date", "from").replace("end_date", "to")
url = f"{base_url}historical-price-full/{query.symbol}?{query_str}&apikey={api_key}"
url = f"{base_url}/historical-price-full/index/%5E{query.symbol}?{query_str}&apikey={api_key}"
return get_data_many(url, FMPMajorIndicesEODData, "historical")
@staticmethod

View File

@@ -1,48 +1,65 @@
"""Polygon major indices end of day fetcher."""
from datetime import datetime
from typing import Dict, List, Optional
from datetime import datetime, timedelta
from typing import Dict, List, Literal, Optional
from openbb_provider.abstract.fetcher import Fetcher
from openbb_provider.helpers import data_transformer
from openbb_provider.metadata import QUERY_DESCRIPTIONS
from openbb_provider.models.major_indices_eod import (
MajorIndicesEODData,
MajorIndicesEODQueryParams,
)
from openbb_polygon.utils.helpers import get_data
from openbb_polygon.utils.types import BaseStockData, BaseStockQueryParams
from pydantic import Field, PositiveFloat, PositiveInt, validator
class PolygonMajorIndicesEODQueryParams(BaseStockQueryParams):
"""Polygon major indices end of day query.
class PolygonMajorIndicesEODQueryParams(MajorIndicesEODQueryParams):
"""Polygon major indices end of day Query.
Source: https://polygon.io/docs/indices/getting-started
Parameters
----------
symbol : str
The symbol of the stocks to fetch.
start_date : Union[date, datetime]
The start date of the query.
end_date : Union[date, datetime]
The end date of the query.
timespan : Timespan, optional
The timespan of the query, by default Timespan.day
sort : Literal["asc", "desc"], optional
The sort order of the query, by default "desc"
limit : PositiveInt, optional
The limit of the query, by default 49999
adjusted : bool, optional
Whether the query is adjusted, by default True
multiplier : PositiveInt, optional
The multiplier of the query, by default 1
"""
timespan: Literal[
"minute", "hour", "day", "week", "month", "quarter", "year"
] = Field(default="day", description="The timespan of the data.")
sort: Literal["asc", "desc"] = Field(
default="desc", description="Sort order of the data."
)
limit: PositiveInt = Field(
default=49999, description=QUERY_DESCRIPTIONS.get("limit", "")
)
adjusted: bool = Field(default=True, description="Whether the data is adjusted.")
multiplier: PositiveInt = Field(
default=1, description="The multiplier of the timespan."
)
class PolygonMajorIndicesEODData(BaseStockData):
"""Polygon major indices end of day data."""
class PolygonMajorIndicesEODData(MajorIndicesEODData):
"""Polygon major indices end of day Data."""
class Config:
fields = {
"open": "o",
"high": "h",
"low": "l",
"close": "c",
"volume": "v",
}
t: datetime = Field(description="The timestamp of the data.")
n: PositiveInt = Field(
description="The number of transactions for the symbol in the time period."
)
vw: PositiveFloat = Field(
description="The volume weighted average price of the symbol."
)
@validator("t", pre=True, check_fields=False)
def time_validate(cls, v): # pylint: disable=E0213
return datetime.fromtimestamp(v / 1000)
class PolygonMajorIndicesEODFetcher(
@@ -57,11 +74,14 @@ class PolygonMajorIndicesEODFetcher(
def transform_query(
query: MajorIndicesEODQueryParams, extra_params: Optional[Dict] = None
) -> PolygonMajorIndicesEODQueryParams:
now = datetime.now().date()
start_date = query.start_date or (now - timedelta(days=7))
end_date = query.end_date or now
return PolygonMajorIndicesEODQueryParams(
symbol=query.symbol,
start_date=query.start_date,
end_date=query.end_date if query.end_date else datetime.now(),
**extra_params if extra_params else {},
start_date=start_date,
end_date=end_date,
**extra_params or {},
)
@staticmethod
@@ -73,7 +93,7 @@ class PolygonMajorIndicesEODFetcher(
request_url = (
f"https://api.polygon.io/v2/aggs/ticker/"
f"I:{query.stocksTicker.upper()}/range/1/{query.timespan}/"
f"I:{query.symbol}/range/1/{query.timespan}/"
f"{query.start_date}/{query.end_date}?adjusted={query.adjusted}"
f"&sort={query.sort}&limit={query.limit}&multiplier={query.multiplier}"
f"&apiKey={api_key}"

View File

@@ -1,60 +1,32 @@
"""Major indices aggregate end of day price data model."""
from datetime import date, datetime
from datetime import date
from typing import Optional
from pydantic import Field, PositiveFloat
from openbb_provider.abstract.data import Data, QueryParams
from openbb_provider.metadata import QUERY_DESCRIPTIONS
from openbb_provider.metadata import DATA_DESCRIPTIONS, QUERY_DESCRIPTIONS
from openbb_provider.models.base import BaseSymbol
class MajorIndicesEODQueryParams(QueryParams, BaseSymbol):
"""Major Indices end of day query.
"""Major Indices end of day Query."""
Parameter
---------
symbol : str
The symbol of the company.
start_date : date
The start date of the stock data from which to retrieve the data.
end_date : Optional[date]
The end date of the stock data up to which to retrieve the data.
"""
start_date: date = Field(description=QUERY_DESCRIPTIONS.get("start_date", ""))
start_date: Optional[date] = Field(
description=QUERY_DESCRIPTIONS.get("start_date", ""),
)
end_date: Optional[date] = Field(
description=QUERY_DESCRIPTIONS.get("end_date", ""), default=date.today()
description=QUERY_DESCRIPTIONS.get("end_date", ""),
)
class MajorIndicesEODData(Data):
"""Major Indices end of day price data.
"""Major Indices end of day price data."""
Returns
-------
date : datetime
The date of the stock.
open : PositiveFloat
The open price of the stock.
high : PositiveFloat
The high price of the stock.
low : PositiveFloat
The low price of the stock.
close : PositiveFloat
The close price of the stock.
adj_close : Optional[PositiveFloat]
The adjusted close price of the stock.
volume : Optional[PositiveFloat]
The volume of the stock.
"""
date: datetime
open: PositiveFloat
high: PositiveFloat
low: PositiveFloat
close: PositiveFloat
adj_close: Optional[PositiveFloat]
volume: Optional[PositiveFloat]
open: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("open", ""))
high: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("high", ""))
low: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("low", ""))
close: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("close", ""))
volume: PositiveFloat = Field(description=DATA_DESCRIPTIONS.get("volume", ""))