Skip to content

Commit

Permalink
Merge pull request #51 from shivam096/shivam-new-excpetions
Browse files Browse the repository at this point in the history
Shivam new excpetions
  • Loading branch information
shivam096 authored Mar 13, 2024
2 parents 002c8cc + 296b65e commit 0f145af
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 221 deletions.
74 changes: 37 additions & 37 deletions dinero/backend/main.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
from pprint import pprint
from backend.sentiment_analysis import get_sentiment_value
from backend.req import get_news_articles
from backend.transform import get_filter_dates
from backend.kpi_manager import get_technical_indicator
import pandas as pd
# from pprint import pprint
# from backend.sentiment_analysis import get_sentiment_value
# from backend.req import get_news_articles
# from backend.transform import get_filter_dates
# from backend.kpi_manager import get_technical_indicator
# import pandas as pd

from backend.processing import get_sentiments
# from backend.processing import get_sentiments

sentiment_date = {}
percent_change = 5
stock_symbol = "AAPL"
# sentiment_date = {}
# percent_change = 5
# stock_symbol = "AAPL"


print(get_sentiments(stock_symbol,percent_change))
# print(get_sentiments(stock_symbol,percent_change))


# data = {
# 'Date': [],
# 'Title': [],
# 'Link': [],
# 'Compound Sentiment Score': [],
# 'Positive Sentiment Score': [],
# 'Negative Sentiment Score': [],
# 'Neutral Sentiment Score': []
# }
# # data = {
# # 'Date': [],
# # 'Title': [],
# # 'Link': [],
# # 'Compound Sentiment Score': [],
# # 'Positive Sentiment Score': [],
# # 'Negative Sentiment Score': [],
# # 'Neutral Sentiment Score': []
# # }

# for date, articles in sentiment_date.items():
# for title, details in articles.items():
# data['Date'].append(date)
# data['Title'].append(title)
# data['Link'].append(details['link'])
# data['Compound Sentiment Score'].append(details['sentiment_score']['compound'])
# data['Positive Sentiment Score'].append(details['sentiment_score']['pos'])
# data['Negative Sentiment Score'].append(details['sentiment_score']['neg'])
# data['Neutral Sentiment Score'].append(details['sentiment_score']['neu'])
# # for date, articles in sentiment_date.items():
# # for title, details in articles.items():
# # data['Date'].append(date)
# # data['Title'].append(title)
# # data['Link'].append(details['link'])
# # data['Compound Sentiment Score'].append(details['sentiment_score']['compound'])
# # data['Positive Sentiment Score'].append(details['sentiment_score']['pos'])
# # data['Negative Sentiment Score'].append(details['sentiment_score']['neg'])
# # data['Neutral Sentiment Score'].append(details['sentiment_score']['neu'])

# df = pd.DataFrame(data)
# print(df)
# # df = pd.DataFrame(data)
# # print(df)




# # usage of KPI
# ticker_symbol = 'AAPL'
# length = 50
# indicator = 'MA'
# # # usage of KPI
# # ticker_symbol = 'AAPL'
# # length = 50
# # indicator = 'MA'

# indicator_data = get_technical_indicator(ticker_symbol, length, indicator)
# print(indicator_data)
# # indicator_data = get_technical_indicator(ticker_symbol, length, indicator)
# # print(indicator_data)
132 changes: 80 additions & 52 deletions dinero/backend/processing.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,80 @@
"""
Module: dinero_analysis
This module provides functions for analyzing financial data related to stocks.
Functions:
- process_dict_to_df
- get_sentiments
"""

import os
import pandas as pd
from pprint import pprint
from backend.transform import get_filter_dates
from backend.sentiment_analysis import get_sentiment_value


def process_dict_to_df(sentiment_data):
data = {
'Date': [],
'Title': [],
'Link': [],
'Compound Sentiment Score': [],
'Positive Sentiment Score': [],
'Negative Sentiment Score': [],
'Neutral Sentiment Score': []
}

for date, articles in sentiment_data.items():
for title, details in articles.items():
data['Date'].append(date)
data['Title'].append(title)
data['Link'].append(details['link'])
data['Compound Sentiment Score'].append(details['sentiment_score']['compound'])
data['Positive Sentiment Score'].append(details['sentiment_score']['pos'])
data['Negative Sentiment Score'].append(details['sentiment_score']['neg'])
data['Neutral Sentiment Score'].append(details['sentiment_score']['neu'])

formatted_df = pd.DataFrame(data)
return formatted_df


def get_sentiments(stock_symbol,percent_change):
sentiment_data ={}
file_path = os.path.join("data",f"{stock_symbol}.csv")
dates_dictionary = get_filter_dates(file_path, percent_change, stock_symbol)

for key,value in dates_dictionary.items():
sentiment_data[key] = get_sentiment_value(value)

data_frame = process_dict_to_df(sentiment_data=sentiment_data)
return data_frame
"""
Module: dinero_analysis
This module provides functions for analyzing financial data related to stocks.
Functions:
- process_dict_to_df: Processes sentiment data from a dictionary to a pandas DataFrame.
- get_sentiments: Retrieves sentiment analysis for a given stock symbol and percent change.
"""
import os
import pandas as pd
from backend.transform import get_filter_dates
from backend.sentiment_analysis import get_sentiment_value


def process_dict_to_df(sentiment_data):
"""
Processes sentiment data from a dictionary to a pandas DataFrame.
Args:
sentiment_data (dict): A dictionary containing sentiment data for each date.
Returns:
pd.DataFrame: A DataFrame containing processed sentiment data.
"""
data = {
'Date': [],
'Title': [],
'Link': [],
'Compound Sentiment Score': [],
'Positive Sentiment Score': [],
'Negative Sentiment Score': [],
'Neutral Sentiment Score': []
}

for date, articles in sentiment_data.items():
for title, details in articles.items():
data['Date'].append(date)
data['Title'].append(title)
data['Link'].append(details.get('link', 'N/A'))
sentiment_score = details.get('sentiment_score', {})
data['Compound Sentiment Score'].append(sentiment_score.get('compound', 0))
data['Positive Sentiment Score'].append(sentiment_score.get('pos', 0))
data['Negative Sentiment Score'].append(sentiment_score.get('neg', 0))
data['Neutral Sentiment Score'].append(sentiment_score.get('neu', 0))

formatted_df = pd.DataFrame(data)
return formatted_df


def get_sentiments(stock_symbol, percent_change):
"""
Retrieves sentiment analysis for a given stock symbol and percent change.
Args:
stock_symbol (str): The stock symbol for which sentiment analysis is to be retrieved.
percent_change (float): The percent change threshold for filtering dates.
Returns:
pd.DataFrame or None: A DataFrame containing sentiment analysis data,
or None if an error occurs.
"""
sentiment_data = {}
try:
if percent_change is None:
raise ValueError("percent_change argument is None")
if stock_symbol is None:
raise ValueError("stock_symbol argument is None")
file_path = os.path.join("data", f"{stock_symbol}.csv")
dates_dictionary = get_filter_dates(file_path, percent_change, stock_symbol)

for key, value in dates_dictionary.items():
sentiment_data[key] = get_sentiment_value(value)

data_frame = process_dict_to_df(sentiment_data=sentiment_data)
return data_frame

except ValueError as e:
print(f"Value Error raised: {e}")
return None
56 changes: 31 additions & 25 deletions dinero/backend/req.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
"""
Module for retrieving news articles related to a specific stock ticker.
Function :
- get_news_articles
"""
import requests

def get_news_articles(ticker : str, date: str) :
"""
Retrieve news articles related to a specific stock ticker on a given date.
Args:
ticker (str): The stock ticker symbol.
date (str): The date for which news articles are to be retrieved.
Should be in the format 'YYYY-MM-DD'.
Returns:
dict: A dictionary containing the news articles data.
"""
url = f'https://eodhd.com/api/news?s={ticker}&offset=0&api_token=demo&fmt=json&from={date}&to={date}'
data = requests.get(url, timeout= 100).json()
return data
"""
Module for retrieving news articles related to a specific stock ticker.
Function :
- get_news_articles
"""
import requests

def get_news_articles(ticker: str, date: str):
"""
Retrieve news articles related to a specific stock ticker on a given date.
Args:
ticker (str): The stock ticker symbol.
date (str): The date for which news articles are to be retrieved.
Should be in the format 'YYYY-MM-DD'.
Returns:
dict: A dictionary containing the news articles data.
"""
try:
url = f'https://eodhd.com/api/news?s={ticker}\
&offset=0&api_token=demo&fmt=json&from={date}&to={date}'
response = requests.get(url, timeout=100)
response.raise_for_status() # Raise an exception for HTTP errors (status codes >= 400)
data = response.json()
return data
except requests.RequestException as e:
print(f"Error retrieving news articles: {e}")
return None

70 changes: 37 additions & 33 deletions dinero/backend/sentiment_analysis.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
"""
Module: sentiment_analysis
This module provides functions for sentiment analysis of text data.
Function:
- get_sentiment_value
"""

from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk
nltk.download('vader_lexicon')

def get_sentiment_value(title_list: list) -> dict:
"""
Analyzes the sentiment of each title in the given list using
VADER (Valence Aware Dictionary and sEntiment Reasoner).
Args:
- title_list (list): A list of strings representing titles or sentences to analyze.
Returns:
- dict: A dictionary where keys are the titles/sentences and
values are dictionaries containing sentiment scores.
The sentiment scores include 'neg' (negative),
'neu' (neutral), 'pos' (positive), and 'compound' (overall sentiment).
"""
senti_dict = {}
analyzer = SentimentIntensityAnalyzer()
for sentence in title_list:
vs = analyzer.polarity_scores(sentence.get('content'))
senti_dict[sentence.get('title')] = {'sentiment_score':vs, 'link':sentence.get('link')}

return senti_dict
"""
Module: sentiment_analysis
This module provides functions for sentiment analysis of text data.
Function:
- get_sentiment_value
"""

from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk
nltk.download('vader_lexicon')

def get_sentiment_value(title_list: list) -> dict:
"""
Analyzes the sentiment of each title in the given list using
VADER (Valence Aware Dictionary and sEntiment Reasoner).
Args:
- title_list (list): A list of dictionaries representing titles or sentences to analyze.
Each dictionary should have 'title', 'content', and 'link' keys.
Returns:
- dict: A dictionary where keys are the titles/sentences and
values are dictionaries containing sentiment scores.
The sentiment scores include 'neg' (negative),
'neu' (neutral), 'pos' (positive), and 'compound' (overall sentiment).
"""
senti_dict = {}
analyzer = SentimentIntensityAnalyzer()
for sentence in title_list:
content = sentence['content']
title = sentence['title']
link = sentence['link']
vs = analyzer.polarity_scores(content)
senti_dict[title] = {'sentiment_score': vs, 'link': link}

return senti_dict
Loading

0 comments on commit 0f145af

Please sign in to comment.