|
| 1 | +"""AKShare Price Performance Model.""" |
| 2 | + |
| 3 | +# pylint: disable=unused-argument |
| 4 | +from typing import Any, Dict, List, Optional |
| 5 | +from warnings import warn |
| 6 | + |
| 7 | +from openbb_core.provider.abstract.fetcher import Fetcher |
| 8 | +from openbb_core.provider.standard_models.recent_performance import ( |
| 9 | + RecentPerformanceData, |
| 10 | + RecentPerformanceQueryParams, |
| 11 | +) |
| 12 | +from openbb_fmp.utils.helpers import create_url, get_data_urls |
| 13 | +from pydantic import Field, model_validator |
| 14 | + |
| 15 | + |
| 16 | +class AKSharePricePerformanceQueryParams(RecentPerformanceQueryParams): |
| 17 | + """AKShare Price Performance Query. |
| 18 | +
|
| 19 | + Source: https://akshare.akfamily.xyz/data/index/index.html#id3 |
| 20 | + """ |
| 21 | + |
| 22 | + __json_schema_extra__ = {"symbol": {"multiple_items_allowed": True}} |
| 23 | + |
| 24 | + |
| 25 | +class AKSharePricePerformanceData(RecentPerformanceData): |
| 26 | + """AKShare Price Performance Data.""" |
| 27 | + |
| 28 | + #symbol: str = Field(description="The ticker symbol.") |
| 29 | + |
| 30 | + __alias_dict__ = { |
| 31 | + "one_day": "1D", |
| 32 | + "one_week": "5D", |
| 33 | + "one_month": "1M", |
| 34 | + "three_month": "3M", |
| 35 | + "six_month": "6M", |
| 36 | + "one_year": "1Y", |
| 37 | + "three_year": "3Y", |
| 38 | + "five_year": "5Y", |
| 39 | + "ten_year": "10Y", |
| 40 | + } |
| 41 | + |
| 42 | + @model_validator(mode="before") |
| 43 | + @classmethod |
| 44 | + def replace_zero(cls, values): # pylint: disable=no-self-argument |
| 45 | + """Replace zero with None and convert percents to normalized values.""" |
| 46 | + if isinstance(values, dict): |
| 47 | + for k, v in values.items(): |
| 48 | + if k != "symbol": |
| 49 | + values[k] = None if v == 0 else float(v) / 100 |
| 50 | + return values |
| 51 | + |
| 52 | + |
| 53 | +class AKSharePricePerformanceFetcher( |
| 54 | + Fetcher[ |
| 55 | + AKSharePricePerformanceQueryParams, |
| 56 | + List[AKSharePricePerformanceData], |
| 57 | + ] |
| 58 | +): |
| 59 | + """Transform the query, extract and transform the data from the AKShare endpoints.""" |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def transform_query(params: Dict[str, Any]) -> AKSharePricePerformanceQueryParams: |
| 63 | + """Transform the query params.""" |
| 64 | + return AKSharePricePerformanceQueryParams(**params) |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + async def extract_data( |
| 68 | + query: AKSharePricePerformanceQueryParams, |
| 69 | + credentials: Optional[Dict[str, str]], |
| 70 | + **kwargs: Any, |
| 71 | + ) -> List[Dict]: |
| 72 | + """Return the raw data from the AKShare endpoint.""" |
| 73 | + import pandas as pd |
| 74 | + symbols = query.symbol.split(",") |
| 75 | + |
| 76 | + def get_data(code: str) -> dict: |
| 77 | + from mysharelib.tools import calculate_price_performance, last_closing_day |
| 78 | + from openbb_akshare.utils.helpers import get_list_date, ak_download |
| 79 | + |
| 80 | + df = ak_download(symbol=code, start_date=get_list_date(code), end_date=last_closing_day()) |
| 81 | + df['date'] = pd.to_datetime(df['date']) |
| 82 | + df.set_index('date', inplace=True) |
| 83 | + return calculate_price_performance(code, df) |
| 84 | + |
| 85 | + return [get_data(symbol) for symbol in symbols] |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def transform_data( |
| 89 | + query: AKSharePricePerformanceQueryParams, |
| 90 | + data: List[Dict], |
| 91 | + **kwargs: Any, |
| 92 | + ) -> List[AKSharePricePerformanceData]: |
| 93 | + """Return the transformed data.""" |
| 94 | + |
| 95 | + return [AKSharePricePerformanceData.model_validate(i) for i in data] |
0 commit comments