|
| 1 | +import pandas as pd |
| 2 | +import pendulum |
| 3 | +from fmp_py.fmp_base import FmpBase |
| 4 | +import os |
| 5 | +from dotenv import load_dotenv |
| 6 | + |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | +""" |
| 10 | +Retrieves Stock Spilts Data from Financial Modeling Prep API |
| 11 | +References: |
| 12 | + - https://site.financialmodelingprep.com/developer/docs#splits |
| 13 | +
|
| 14 | +def stock_splits_calendar(self, from_date: str, to_date: str) -> pd.DataFrame: |
| 15 | + Reference: https://site.financialmodelingprep.com/developer/docs#splits-calendar-splits |
| 16 | + |
| 17 | +def stock_splits_historical(self, symbol: str) -> pd.DataFrame: |
| 18 | + Reference: https://site.financialmodelingprep.com/developer/docs#splits-historical-splits |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +class FmpSplits(FmpBase): |
| 23 | + def __init__(self, api_key: str = os.getenv("FMP_API_KEY")) -> None: |
| 24 | + super().__init__(api_key) |
| 25 | + |
| 26 | + ########################################## |
| 27 | + # Stock Splits Histrorical |
| 28 | + ########################################## |
| 29 | + def stock_splits_historical(self, symbol: str) -> pd.DataFrame: |
| 30 | + """ |
| 31 | + Retrieves historical stock splits data for a given symbol. |
| 32 | + Args: |
| 33 | + symbol (str): The stock symbol. |
| 34 | + Returns: |
| 35 | + pd.DataFrame: A DataFrame containing the historical stock splits data. |
| 36 | + """ |
| 37 | + |
| 38 | + url = f"v3/historical-price-full/stock_split/{symbol}" |
| 39 | + response = self.get_request(url)["historical"] |
| 40 | + |
| 41 | + if not response: |
| 42 | + raise ValueError( |
| 43 | + f"Error fetching stock splits historical data for {symbol}" |
| 44 | + ) |
| 45 | + |
| 46 | + data_df = ( |
| 47 | + pd.DataFrame(response) |
| 48 | + .fillna(0) |
| 49 | + .rename( |
| 50 | + columns={ |
| 51 | + "date": "date", |
| 52 | + "label": "label", |
| 53 | + "numerator": "numerator", |
| 54 | + "denominator": "denominator", |
| 55 | + } |
| 56 | + ) |
| 57 | + .astype( |
| 58 | + { |
| 59 | + "date": "datetime64[ns]", |
| 60 | + "label": "str", |
| 61 | + "numerator": "int", |
| 62 | + "denominator": "int", |
| 63 | + } |
| 64 | + ) |
| 65 | + .sort_values(by="date", ascending=True) |
| 66 | + .reset_index(drop=True) |
| 67 | + ) |
| 68 | + data_df["symbol"] = symbol |
| 69 | + |
| 70 | + return data_df |
| 71 | + |
| 72 | + ######################################## |
| 73 | + # Stock Splits Calendar |
| 74 | + ######################################## |
| 75 | + def stock_splits_calendar(self, from_date: str, to_date: str) -> pd.DataFrame: |
| 76 | + """ |
| 77 | + Retrieves the stock splits calendar for a given date range. |
| 78 | + Args: |
| 79 | + from_date (str): The start date of the date range in "YYYY-MM-DD" format. |
| 80 | + to_date (str): The end date of the date range in "YYYY-MM-DD" format. |
| 81 | + Returns: |
| 82 | + pd.DataFrame: A DataFrame containing the stock splits calendar data with the following columns: |
| 83 | + - date: The date of the stock split. |
| 84 | + - label: The label of the stock split. |
| 85 | + - symbol: The symbol of the stock. |
| 86 | + - numerator: The numerator of the stock split ratio. |
| 87 | + - denominator: The denominator of the stock split ratio. |
| 88 | + Raises: |
| 89 | + ValueError: If from_date is greater than to_date or if no data is found for the given date range. |
| 90 | + """ |
| 91 | + |
| 92 | + from_date = pendulum.parse(from_date).format("YYYY-MM-DD") |
| 93 | + to_date = pendulum.parse(to_date).format("YYYY-MM-DD") |
| 94 | + if from_date > to_date: |
| 95 | + raise ValueError("from_date must be less than or equal to to_date") |
| 96 | + |
| 97 | + url = "v3/stock_split_calendar" |
| 98 | + params = {"from": from_date, "to": to_date} |
| 99 | + response = self.get_request(url=url, params=params) |
| 100 | + |
| 101 | + if not response: |
| 102 | + raise ValueError("No data found for the given date range") |
| 103 | + |
| 104 | + data_df = ( |
| 105 | + pd.DataFrame(response) |
| 106 | + .fillna(0) |
| 107 | + .rename( |
| 108 | + columns={ |
| 109 | + "date": "date", |
| 110 | + "label": "label", |
| 111 | + "symbol": "symbol", |
| 112 | + "numerator": "numerator", |
| 113 | + "denominator": "denominator", |
| 114 | + } |
| 115 | + ) |
| 116 | + .astype( |
| 117 | + { |
| 118 | + "date": "datetime64[ns]", |
| 119 | + "label": "str", |
| 120 | + "symbol": "str", |
| 121 | + "numerator": "int", |
| 122 | + "denominator": "int", |
| 123 | + } |
| 124 | + ) |
| 125 | + .sort_values(by="date", ascending=True) |
| 126 | + .reset_index(drop=True) |
| 127 | + ) |
| 128 | + |
| 129 | + return data_df |
0 commit comments