summaryrefslogtreecommitdiffstats
path: root/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_eps_estimates.py
blob: fb0c4458eca96b5faf8eb89b4c664b6439d5290a (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
"""Seeking Alpha Forward EPS Estimates Model."""

# pylint: disable=unused-argument

from typing import Any, Dict, List, Literal, Optional
from warnings import warn

from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.forward_eps_estimates import (
    ForwardEpsEstimatesData,
    ForwardEpsEstimatesQueryParams,
)
from openbb_core.provider.utils.helpers import amake_request
from openbb_seeking_alpha.utils.helpers import HEADERS, get_seekingalpha_id
from pydantic import Field, field_validator


class SAForwardEpsEstimatesQueryParams(ForwardEpsEstimatesQueryParams):
    """Seeking Alpha Forward EPS Estimates Query.

    Source: https://seekingalpha.com/earnings/earnings-calendar
    """

    __json_schema_extra__ = {"symbol": {"multiple_items_allowed": True}}

    period: Literal["annual", "quarter"] = Field(
        default="quarter",
        description="The reporting period.",
        json_schema_extra={"choices": ["annual", "quarter"]},
    )

    @field_validator("symbol", mode="before", check_fields=False)
    @classmethod
    def check_symbol(cls, value):
        """Check the symbol."""
        if not value:
            raise RuntimeError("Error: Symbol is a required field for Seeking Alpha.")
        return value


class SAForwardEpsEstimatesData(ForwardEpsEstimatesData):
    """Seeking Alpha Forward EPS Estimates Data."""

    normalized_actual: Optional[float] = Field(
        default=None,
        description="Actual normalized EPS.",
    )
    period_growth: Optional[float] = Field(
        default=None,
        description="Estimated (or actual if reported) EPS growth for the period.",
        json_schema_extra={"x-unit_measurement": "percent", "x-frontend_multiply": 100},
    )
    low_estimate_gaap: Optional[float] = Field(
        default=None,
        description="Estimated GAAP EPS low for the period.",
    )
    high_estimate_gaap: Optional[float] = Field(
        default=None,
        description="Estimated GAAP EPS high for the period.",
    )
    mean_gaap: Optional[float] = Field(
        default=None,
        description="Estimated GAAP EPS mean for the period.",
    )
    gaap_actual: Optional[float] = Field(
        default=None,
        description="Actual GAAP EPS.",
    )


class SAForwardEpsEstimatesFetcher(
    Fetcher[
        SAForwardEpsEstimatesQueryParams,
        List[SAForwardEpsEstimatesData],
    ]
):
    """Seeking Alpha Forward EPS Estimates Fetcher."""

    @staticmethod
    def transform_query(params: Dict[str, Any]) -> SAForwardEpsEstimatesQueryParams:
        """Transform the query."""
        return SAForwardEpsEstimatesQueryParams(**params)

    @staticmethod
    async def aextract_data(
        query: SAForwardEpsEstimatesQueryParams,
        credentials: Optional[Dict[str, str]],
        **kwargs: Any,
    ) -> Dict:
        """Return the raw data from the Seeking Alpha endpoint."""
        tickers = query.symbol.split(",")
        fp = query.period if query.period == "annual" else "quarterly"
        url = "https://seekingalpha.com/api/v3/symbol_data/estimates"
        querystring: Dict = {
            "estimates_data_items": "eps_normalized_actual,eps_normalized_consensus_low,eps_normalized_consensus_mean,"
            "eps_normalized_consensus_high,eps_normalized_num_of_estimates,"
            "eps_gaap_actual,eps_gaap_consensus_low,eps_gaap_consensus_mean,eps_gaap_consensus_high,",
            "period_type": fp,
            "relative_periods": "-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12",
        }
        ids: Dict = {ticker: await get_seekingalpha_id(ticker) for ticker in tickers}
        querystring["ticker_ids"] = (",").join(list(ids.values()))
        payload: str = ""
        response = await amake_request(
            url, data=payload, headers=HEADERS, params=querystring
        )
        estimates: Dict = response.get("estimates", {})  # type: ignore
        if not estimates:
            raise RuntimeError(f"No estimates data was returned for: {query.symbol}")

        output: Dict = {"ids": ids, "estimates": estimates}

        return output

    @staticmethod
    def transform_data(
        query: SAForwardEpsEstimatesQueryParams,
        data: Dict,
        **kwargs: Any,
    ) -> List[SAForwardEpsEstimatesData]:
        """Transform the data to the standard format."""
        tickers = query.symbol.split(",")
        ids = data.get("ids")
        estimates = data.get("estimates")
        results: List[SAForwardEpsEstimatesData] = []
        for ticker in tickers:
            sa_id = str(ids.get(ticker, ""))
            if sa_id == "" or sa_id not in estimates:
                warn(f"Symbol Error: No data found for, {ticker}")
            seek_object = estimates.get(sa_id)
            items = len(seek_object["eps_normalized_num_of_estimates"])
            for i in range(0, items - 4):
                eps_estimates: Dict = {}
                eps_estimates["symbol"] = ticker
                num_estimates = seek_object["eps_normalized_num_of_estimates"].get(str(i))
                if not num_estimates:
                    continue
                period = num_estimates[0].get("period", {})
                if period:
                    period_type = period.get("periodtypeid")
                    eps_estimates["calendar_year"] = period.get("calendaryear")
                    eps_estimates["calendar_period"] = (
                        "Q" + str(period.get("calendarquarter", ""))
                        if period_type == "quarterly"
                        else "FY"
                    )
                    eps_estimates["date"] = period.get("periodenddate").split("T")[0]
                    eps_estimates["fiscal_year"] = period.get("fiscalyear")
                    eps_estimates["fiscal_period"] = (
                        "Q" + str(period.get("fiscalquarter", ""))
                        if period_type == "quarterly"
                        else "FY"
                    )
                eps_estimates["number_of_analysts"] = num_estimates[0].get(
                    "dataitemvalue"
                )
                actual = seek_object["eps_normalized_actual"].get(str(i))
                if actual:
                    eps_estimates["normalized_actual"] = actual[0].get("dataitemvalue")
                gaap_actual = seek_object["eps_gaap_actual"].get(str(i))
                if gaap_actual:
                    eps_estimates["gaap_actual"] = gaap_actual[0].get("dataitemvalue")
                low = seek_object["eps_normalized_consensus_low"].get(str(i))
                if low:
                    eps_estimates["low_estimate"] = low[0].get("dataitemvalue")
                gaap_low = seek_object["eps_gaap_consensus_low"].get(str(i))
                if gaap_low:
                    eps_estimates["low_estimate_gaap"] = gaap_low[0].get(
                        "dataitemvalue"
                    )
                high = seek_object["eps_normalized_consensus_high"].get(str(i))
                if high:
                    eps_estimates["high_estimate"] = high[0].get("dataitemvalue")
                gaap_high = seek_object["eps_gaap_consensus_high"].get(str(i))
                if gaap_high:
                    eps_estimates["high_estimate_gaap"] = gaap_high[0].get(
                        "dataitemvalue"
                    )
                mean = seek_object["eps_normalized_consensus_mean"].get(str(i))
                if mean:
                    mean = mean[0].get("dataitemvalue")
                    eps_estimates["mean"] = mean
                gaap_mean = seek_object["eps_gaap_consensus_mean"].get(str(i))
                if gaap_mean:
                    eps_estimates["mean_gaap"] = gaap_mean[0].get("dataitemvalue")
                # Calculate the estimated growth percent.
                this = float(mean) if mean else None
                prev = None
                percent = None
                try:
                    prev = float(
                        seek_object["eps_normalized_actual"][str(i - 1)][0].get(
                            "dataitemvalue"
                        )
                    )
                except KeyError:
                    prev = float(
                        seek_object["eps_normalized_consensus_mean"][str(i - 1)][0].get(
                            "dataitemvalue"
                        )
                    )
                if this and prev:
                    percent = (this - prev) / prev
                eps_estimates["period_growth"] = percent
                results.append(SAForwardEpsEstimatesData.model_validate(eps_estimates))

        return results