-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Add Headless Oracle market state provider #7463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HOYALIM
wants to merge
3
commits into
OpenBB-finance:develop
Choose a base branch
from
HOYALIM:feature/headless-oracle-market-state
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+401
−32
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
openbb_platform/core/openbb_core/provider/standard_models/market_state.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from datetime import datetime | ||
|
|
||
| from openbb_core.provider.abstract.data import Data | ||
| from openbb_core.provider.abstract.query_params import QueryParams | ||
| from pydantic import Field | ||
|
|
||
|
|
||
| class MarketStateQueryParams(QueryParams): | ||
| exchange: str = Field(description="Exchange MIC, acronym, or name to check market state for.") | ||
|
|
||
|
|
||
| class MarketStateData(Data): | ||
| exchange: str = Field(description="Normalized exchange acronym.") | ||
| mic: str = Field(description="ISO 10383 MIC code for the exchange.") | ||
| status: str = Field(description="Raw market-state status returned by the provider.") | ||
| is_open: bool = Field(description="Fail-closed market state. Only `OPEN` evaluates to `True`.") | ||
| issued_at: datetime | None = Field(default=None, description="Receipt issue timestamp.") | ||
| expires_at: datetime | None = Field(default=None, description="Receipt expiry timestamp.") | ||
| ttl_seconds: int | None = Field( | ||
| default=None, | ||
| description="Receipt time-to-live in seconds when both timestamps are available.", | ||
| ) | ||
| issuer: str | None = Field(default=None, description="Receipt issuer.") | ||
| source: str | None = Field(default=None, description="Market-state source.") | ||
| halt_detection: str | None = Field(default=None, description="Halt detection mode reported by the provider.") | ||
| receipt_mode: str | None = Field(default=None, description="Receipt mode reported by the provider.") | ||
| schema_version: str | None = Field(default=None, description="Provider receipt schema version.") | ||
| public_key_id: str | None = Field(default=None, description="Identifier for the signing public key.") | ||
| signature: str | None = Field(default=None, description="Signature attached to the receipt payload.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Headless Oracle Provider | ||
|
|
||
| This provider exposes signed market-state receipts from Headless Oracle for OpenBB. | ||
|
|
||
| ## Supported models | ||
|
|
||
| - `MarketState` | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| from openbb import obb | ||
|
|
||
| obb.equity.market_state(exchange="XNYS", provider="headless_oracle") | ||
| ``` |
Empty file.
13 changes: 13 additions & 0 deletions
13
openbb_platform/providers/headless_oracle/openbb_headless_oracle/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from openbb_core.provider.abstract.provider import Provider | ||
| from openbb_headless_oracle.models.market_state import HeadlessOracleMarketStateFetcher | ||
|
|
||
| headless_oracle_provider = Provider( | ||
| name="headless_oracle", | ||
| website="https://headlessoracle.com", | ||
| description="Signed market-state receipts for exchange-open verification.", | ||
| credentials=[], | ||
HOYALIM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fetcher_dict={ | ||
| "MarketState": HeadlessOracleMarketStateFetcher, | ||
| }, | ||
| repr_name="Headless Oracle", | ||
| ) | ||
100 changes: 100 additions & 0 deletions
100
openbb_platform/providers/headless_oracle/openbb_headless_oracle/models/market_state.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime | ||
| from typing import Any | ||
|
|
||
| from openbb_core.app.model.abstract.error import OpenBBError | ||
| from openbb_core.provider.abstract.fetcher import Fetcher | ||
| from openbb_core.provider.standard_models.market_state import ( | ||
| MarketStateData, | ||
| MarketStateQueryParams, | ||
| ) | ||
| from openbb_core.provider.utils.exchange_utils import Exchange | ||
| from pydantic import Field | ||
|
|
||
|
|
||
| class HeadlessOracleMarketStateQueryParams(MarketStateQueryParams): | ||
| exchange: Exchange = Field(description="Exchange MIC, acronym, or name to check market state for.") | ||
|
|
||
|
|
||
| class HeadlessOracleMarketStateFetcher( | ||
| Fetcher[ | ||
| HeadlessOracleMarketStateQueryParams, | ||
| MarketStateData, | ||
| ] | ||
| ): | ||
| require_credentials = False | ||
|
|
||
| @staticmethod | ||
| def transform_query(params: dict[str, Any]) -> HeadlessOracleMarketStateQueryParams: | ||
| return HeadlessOracleMarketStateQueryParams(**params) | ||
HOYALIM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @staticmethod | ||
| def extract_data( | ||
| query: HeadlessOracleMarketStateQueryParams, | ||
| credentials: dict[str, str] | None = None, | ||
| **kwargs: Any, | ||
| ) -> dict[str, Any]: | ||
| # pylint: disable=import-outside-toplevel | ||
| from openbb_core.provider.utils.helpers import make_request | ||
|
|
||
| url = f"https://headlessoracle.com/v5/demo?mic={query.exchange.mic}" | ||
|
|
||
| try: | ||
| response = make_request(url, timeout=30) | ||
| if response.status_code != 200: | ||
| raise OpenBBError(f"Headless Oracle request failed with status {response.status_code}: {response.text}") | ||
|
|
||
| return response.json() | ||
| except OpenBBError: | ||
| raise | ||
| except Exception as error: | ||
| raise OpenBBError(error) from error | ||
|
|
||
| @staticmethod | ||
| def transform_data( | ||
| query: HeadlessOracleMarketStateQueryParams, | ||
| data: dict[str, Any], | ||
| **kwargs: Any, | ||
| ) -> MarketStateData: | ||
| payload = data.get("receipt") | ||
| if not isinstance(payload, dict): | ||
| raise OpenBBError("Headless Oracle response did not include a valid receipt payload.") | ||
|
|
||
| missing_fields = [field for field in ("mic", "status", "issued_at", "expires_at") if not payload.get(field)] | ||
| if missing_fields: | ||
| raise OpenBBError( | ||
| f"Headless Oracle receipt payload is missing required field(s): {', '.join(missing_fields)}" | ||
| ) | ||
|
|
||
| status = str(payload.get("status") or "UNKNOWN").upper() | ||
| issued_at = HeadlessOracleMarketStateFetcher._parse_datetime(payload.get("issued_at")) | ||
| expires_at = HeadlessOracleMarketStateFetcher._parse_datetime(payload.get("expires_at")) | ||
| ttl_seconds = ( | ||
| int((expires_at - issued_at).total_seconds()) if issued_at is not None and expires_at is not None else None | ||
| ) | ||
|
|
||
| return MarketStateData( | ||
| exchange=query.exchange.acronym, | ||
| mic=str(payload.get("mic") or query.exchange.mic), | ||
| status=status, | ||
| is_open=status == "OPEN", | ||
| issued_at=issued_at, | ||
| expires_at=expires_at, | ||
| ttl_seconds=ttl_seconds, | ||
| issuer=payload.get("issuer"), | ||
| source=payload.get("source"), | ||
| halt_detection=payload.get("halt_detection"), | ||
| receipt_mode=payload.get("receipt_mode"), | ||
| schema_version=payload.get("schema_version"), | ||
| public_key_id=payload.get("public_key_id") or payload.get("key_id"), | ||
| signature=payload.get("signature"), | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _parse_datetime(value: Any) -> datetime | None: | ||
| if value in (None, ""): | ||
| return None | ||
| if isinstance(value, datetime): | ||
| return value | ||
| return datetime.fromisoformat(str(value).replace("Z", "+00:00")) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [tool.poetry] | ||
| name = "openbb-headless-oracle" | ||
| version = "1.0.0" | ||
| description = "Headless Oracle provider for OpenBB" | ||
| authors = ["OpenBB Team <hello@openbb.co>"] | ||
| license = "AGPL-3.0-only" | ||
| readme = "README.md" | ||
| packages = [{ include = "openbb_headless_oracle" }] | ||
|
|
||
| [tool.poetry.dependencies] | ||
| python = ">=3.10,<4" | ||
| openbb-core = "^1.6.7" | ||
|
|
||
| [build-system] | ||
| requires = ["poetry-core"] | ||
| build-backend = "poetry.core.masonry.api" | ||
|
|
||
| [tool.poetry.plugins."openbb_provider_extension"] | ||
| headless_oracle = "openbb_headless_oracle:headless_oracle_provider" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.