Skip to content

Commit

Permalink
Include data for one year ago in crypo sentiment
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchiang committed Sep 23, 2023
1 parent 5c8e452 commit 5844ac9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/service/crypto/crypto_sentiment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from statistics import mean
from typing import List

Expand All @@ -8,6 +9,7 @@
from src.util.date_util import parse
from src.util.list_util import is_list_out_of_range

logger = logging.getLogger('Crypto sentiment service')
class CryptoSentimentService:
def __init__(self):
self.alternativeme_service = AlternativeMeAPI().alternativeme_service
Expand All @@ -32,6 +34,7 @@ def transform_data(self, data: AlternativeMeFearGreedIndex) -> List[FearGreedDat
{'text': 'Last week', 'list_index': -8},
{'text': 'Last month', 'list_index': -31},
{'text': 'Last 3 months', 'list_index': -91},
{'text': 'Last year', 'list_index': self._get_last_year_list_index(data=data)}
]

res: List[FearGreedData] = []
Expand Down Expand Up @@ -59,7 +62,8 @@ def transform_average(self, data: AlternativeMeFearGreedIndex) -> List[FearGreed
average_params = [
{'timeframe': '7d', 'list_end_index': -7},
{'timeframe': '30d', 'list_end_index': -30},
{'timeframe': '90d', 'list_end_index': -90}
{'timeframe': '90d', 'list_end_index': -90},
{'timeframe': '1 year', 'list_end_index': self._get_last_year_list_index(data=data)}
]

res: List[FearGreedAverage] = []
Expand All @@ -83,6 +87,14 @@ def transform_average(self, data: AlternativeMeFearGreedIndex) -> List[FearGreed

return res

def _get_last_year_list_index(self, data: AlternativeMeFearGreedIndex):
last_year_list_index = -365
if len(data.data.datasets) > 200 and len(data.fear_and_greed_historical.data) < abs(last_year_list_index):
logger.info(
f'Last year list index is {last_year_list_index} while length of fear greed index is {len(data.fear_and_greed_historical.data)}. Setting last year list index to 0')
last_year_list_index = 0
return last_year_list_index


# returns data in chronological order
async def get_crypto_fear_greed_index_from_source(self, days=365) -> AlternativeMeFearGreedIndex:
Expand Down
3 changes: 2 additions & 1 deletion src/service/stocks_sentiment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from statistics import mean
from typing import List

Expand All @@ -11,7 +12,7 @@
from src.util.list_util import is_list_out_of_range
from src.util.logger import logger


logger = logging.getLogger('Stocks sentiment service')
class StocksSentimentService:
def __init__(self):
env = config.get_env()
Expand Down

0 comments on commit 5844ac9

Please sign in to comment.