From 6f9be2f812423015c61a9df4c93a7c991fed9f9e Mon Sep 17 00:00:00 2001 From: Yush Kapoor <55430541+yushdotkapoor@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:44:20 -0800 Subject: [PATCH] Added support for historical data --- src/fear_and_greed/cnn.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/fear_and_greed/cnn.py b/src/fear_and_greed/cnn.py index 049b1ea..4c00a3d 100644 --- a/src/fear_and_greed/cnn.py +++ b/src/fear_and_greed/cnn.py @@ -62,3 +62,24 @@ def get(fetcher: Fetcher = None) -> FearGreedIndex: description=response["rating"], last_update=datetime.datetime.fromisoformat(response["timestamp"]), ) + +def historical(fetcher: Fetcher = None) -> dict: + """Returns CNN's Fear & Greed Index historical data.""" + + if fetcher is None: + fetcher = Fetcher() + + response = fetcher()["fear_and_greed_historical"] + fear_greed_historical = [] + + if "data" in response: + historical_data = response["data"] + for data in historical_data: + fear_greed_historical.append(FearGreedIndex( + value=data["y"], + description=data["rating"], + last_update=datetime.datetime.fromtimestamp(data["x"] / 1000), + )) + + return fear_greed_historical +