summaryrefslogtreecommitdiffstats
path: root/openbb_platform/providers/intrinio/openbb_intrinio/models/options_snapshots.py
blob: 3bef6a7261190b9142de8f2cae6ed2eaa63160f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""Intrinio Options Snapshots Model."""

# pylint: disable=unused-argument

import gzip
from datetime import (
    date as dateType,
    datetime,
)
from io import BytesIO
from typing import Any, Dict, List, Optional, Union

import numpy as np
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.options_snapshots import (
    OptionsSnapshotsData,
    OptionsSnapshotsQueryParams,
)
from openbb_core.provider.utils.helpers import amake_request
from pandas import DataFrame, NaT, Series, read_csv, to_datetime
from pydantic import Field
from pytz import timezone


class IntrinioOptionsSnapshotsQueryParams(OptionsSnapshotsQueryParams):
    """Intrinio Options Snapshots Query.

    Source: https://docs.intrinio.com/documentation/web_api/get_options_snapshots_v2
    """

    date: Optional[Union[dateType, datetime, str]] = Field(
        default=None,
        description="The date of the data. Can be a datetime or an ISO datetime string."
        + " Data appears to go back to around 2022-06-01"
        + " Example: '2024-03-08T12:15:00+0400'",
    )
    only_traded: bool = Field(
        default=True,
        description="Only include options that have been traded during the session, default is True."
        + " Setting to false will dramatically increase the size of the response - use with caution.",
    )


class IntrinioOptionsSnapshotsData(OptionsSnapshotsData):
    """Intrinio Options Snapshots Data. Warning: This is a large file."""

    bid: List[Union[float, None]] = Field(
        default_factory=list,
        description="The last bid price at the time.",
        json_schema_extra={"x-unit_measurement": "currency"},
    )
    bid_size: List[Union[int, None]] = Field(
        default_factory=list,
        description="The size of the last bid price.",
    )
    bid_timestamp: List[Union[datetime, None]] = Field(
        default_factory=list,
        description="The timestamp of the last bid price.",
    )
    ask: List[Union[float, None]] = Field(
        default_factory=list,
        description="The last ask price at the time.",
        json_schema_extra={"x-unit_measurement": "currency"},
    )
    ask_size: List[Union[int, None]] = Field(
        default_factory=list,
        description="The size of the last ask price.",
    )
    ask_timestamp: List[Union[datetime, None]] = Field(
        default_factory=list,
        description="The timestamp of the last ask price.",
    )
    total_bid_volume: List[Union[int, None]] = Field(
        default_factory=list,
        description="Total volume of bids.",
    )
    bid_high: List[Union[float, None]] = Field(
        default_factory=list,
        description="The highest bid price.",
        json_schema_extra={"x-unit_measurement": "currency"},
    )
    bid_low: List[Union[float, None]] = Field(
        default_factory=list,
        description="The lowest bid price.",
        json_schema_extra={"x-unit_measurement": "currency"},
    )
    total_ask_volume: List[Union[int, None]] = Field(
        default_factory=list,
        description="Total volume of asks.",
    )
    ask_high: List[Union[float, None]] = Field(
        default_factory=list,
        description="The highest ask price.",
        json_schema_extra={"x-unit_measurement": "currency"},
    )
    ask_low: List[Union[float, None]] = Field(
        default_factory=list,
        description="The lowest ask price.",
        json_schema_extra={"x-unit_measurement": "currency"},
    )


class IntrinioOptionsSnapshotsFetcher(
    Fetcher[
        IntrinioOptionsSnapshotsQueryParams,
        List[IntrinioOptionsSnapshotsData],
    ]
):
    """Intrinio Options Snapshots Fetcher."""

    @staticmethod
    def transform_query(params: Dict[str, Any]) -> IntrinioOptionsSnapshotsQueryParams:
        """Transform the query params."""
        transformed_params = params.copy()
        if "date" in transformed_params:
            if isinstance(transformed_params["date"], datetime):
                dt = transformed_params["date"]
                dt = dt.astimezone(tz=timezone("America/New_York"))
            if isinstance(transformed_params["date"], dateType):
                dt = transformed_params["date"]  # type: ignore
                if isinstance(dt, dateType):
                    dt = datetime(
                        dt.year,
                        dt.month,
                        dt.day,
                        16,
                        15,
                        0,
                        0,
                        tzinfo=timezone("America/New_York"),
                    )
            if isinstance(transformed_params["date"], str):
                dt = datetime.fromisoformat(transformed_params["date"])
            else:
                try:
                    dt = datetime.fromisoformat(str(transformed_params["date"]))  # type: ignore
                except ValueError as exc:
                    raise OpenBBError(
                        "Invalid date format. Please use '2024-03-08T12:15-0400'."
                    ) from exc

            transformed_params["date"] = (
                dt.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
                .replace("+", "-")
                .replace("T00:", "T20:")
                if isinstance(dt, datetime)
                else dt
            )
        return IntrinioOptionsSnapshotsQueryParams(**transformed_params)

    @staticmethod
    async def aextract_data(
        query: IntrinioOptionsSnapshotsQueryParams,
        credentials: Optional[Dict[str, str]],
        **kwargs: Any,
    ) -> DataFrame:
        """Return the raw data from the Intrinio endpoint."""
        api_key = credentials.get("intrinio_api_key") if credentials else ""

        # This gets the URL to the actual file.
        url = f"https://api-v2.intrinio.com/options/snapshots?api_key={api_key}"
        if query.date:
            url += f"&at_datetime={query.date}"

        try:
            response = await amake_request(url, **kwargs)
        except Exception as exc:
            raise OpenBBError("Could not fetch data from Intrinio.") from exc

        if isinstance(response, dict) and "error" in response:
            raise OpenBBError(
                f"{response.get('error')}. Message: {response.get('message')}"
            )
        urls = []
        # Get the URL to the CSV file.
        if response.get("snapshots"):  # type: ignore
            for d in response["snapshots"]:  # type: ignore
                if d.get("files"):
                    for f in d["files"]:
                        if f.get("url"):
                            urls.append(f.get("url"))
        if not urls:
            raise OpenBBError("No snapshots found.")

        async def response_callback(response, _):
            """Response Callback."""
            return await response.read()

        async def get_csv(url) -> DataFrame:
            """Return the CSV data."""
            try:
                response = await amake_request(
                    url, response_callback=response_callback, **kwargs
                )
                df = DataFrame()
                if isinstance(response, bytes):
                    file = gzip.decompress(response)
                    df = read_csv(BytesIO(file))

                return df

            except Exception as exc:
                try:
                    df = read_csv(response)
                    return df
                except Exception:
                    raise OpenBBError("Could not read file from URL.") from exc

        # There should only be one URL with this bulk data.
        return await get_csv(urls[0])

    @staticmethod
    def transform_data(
        query: IntrinioOptionsSnapshotsQueryParams,
        data: DataFrame,
        **kwargs: Any,
    ) -> List[IntrinioOptionsSnapshotsData]:
        """Return the transformed data."""
        df = data
        if df.empty:
            raise OpenBBError("Empty CSV file")
        COL_MAP = {
            "CONTRACT ID": "contract_symbol",
            "OPEN INTEREST": "open_interest",
            "TRADE PRICE": "last_price",
            "TRADE SIZE": "last_size",
            "TOTAL TRADE VOLUME": "volume",
            "LAST TRADE TIMESTAMP": "last_timestamp",
            "TRADE HIGH PRICE": "high",
            "TRADE LOW PRICE": "low",
            "ASK PRICE": "ask",
            "ASK SIZE&qu