OfirMatzlawi commited on
Commit
a265793
1 Parent(s): 20127ed

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -110
main.py CHANGED
@@ -8,14 +8,7 @@ from bs4 import BeautifulSoup
8
  import pandas as pd
9
  import json # for graph plotting in website
10
 
11
- # NLTK VADER for sentiment analysis
12
- import nltk
13
 
14
- nltk.downloader.download("vader_lexicon")
15
- from nltk.sentiment.vader import SentimentIntensityAnalyzer
16
-
17
- import subprocess
18
- import os
19
 
20
  import datetime
21
 
@@ -31,109 +24,13 @@ app = FastAPI()
31
  @app.get("/")
32
  def read_root():
33
  return {
34
- "message": "Hello, Please type a ticker at the end of the URL to get the stock sentiment.",
35
- "format": "https://yaakovy-fin-proj-docker.hf.space/ticker/[TICKER]",
36
- "example": "https://yaakovy-fin-proj-docker.hf.space/ticker/msft",
37
- }
38
-
39
-
40
- def get_news(ticker):
41
- url = finviz_url + ticker
42
- req = Request(
43
- url=url,
44
- headers={
45
- "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"
46
- },
47
- )
48
- response = urlopen(req)
49
- # Read the contents of the file into 'html'
50
- html = BeautifulSoup(response)
51
- # Find 'news-table' in the Soup and load it into 'news_table'
52
- news_table = html.find(id="news-table")
53
- return news_table
54
-
55
-
56
- # parse news into dataframe
57
- def parse_news(news_table):
58
- parsed_news = []
59
- today_string = datetime.datetime.today().strftime("%Y-%m-%d")
60
-
61
- for x in news_table.findAll("tr"):
62
- try:
63
- # read the text from each tr tag into text
64
- # get text from a only
65
- text = x.a.get_text()
66
- # splite text in the td tag into a list
67
- date_scrape = x.td.text.split()
68
- # if the length of 'date_scrape' is 1, load 'time' as the only element
69
-
70
- if len(date_scrape) == 1:
71
- time = date_scrape[0]
72
-
73
- # else load 'date' as the 1st element and 'time' as the second
74
- else:
75
- date = date_scrape[0]
76
- time = date_scrape[1]
77
-
78
- # Append ticker, date, time and headline as a list to the 'parsed_news' list
79
- parsed_news.append([date, time, text])
80
- except:
81
- pass
82
-
83
- # Set column names
84
- columns = ["date", "time", "headline"]
85
- # Convert the parsed_news list into a DataFrame called 'parsed_and_scored_news'
86
- parsed_news_df = pd.DataFrame(parsed_news, columns=columns)
87
- # Create a pandas datetime object from the strings in 'date' and 'time' column
88
- parsed_news_df["date"] = parsed_news_df["date"].replace("Today", today_string)
89
- # parsed_news_df["datetime"] = pd.to_datetime(
90
- # parsed_news_df["date"] + " " + parsed_news_df["time"],
91
- # format="%Y-%m-%d %H:%M",
92
- # )
93
-
94
- return parsed_news_df
95
-
96
-
97
- def score_news(parsed_news_df):
98
- # Instantiate the sentiment intensity analyzer
99
- vader = SentimentIntensityAnalyzer()
100
-
101
- # Iterate through the headlines and get the polarity scores using vader
102
- scores = parsed_news_df["headline"].apply(vader.polarity_scores).tolist()
103
-
104
- # Convert the 'scores' list of dicts into a DataFrame
105
- scores_df = pd.DataFrame(scores)
106
-
107
- # Join the DataFrames of the news and the list of dicts
108
- parsed_and_scored_news = parsed_news_df.join(scores_df, rsuffix="_right")
109
- # parsed_and_scored_news = parsed_and_scored_news.set_index("datetime")
110
- parsed_and_scored_news = parsed_and_scored_news.drop(["date", "time"], axis=1)
111
- parsed_and_scored_news = parsed_and_scored_news.rename(
112
- columns={"compound": "sentiment_score"}
113
- )
114
- return parsed_and_scored_news
115
 
116
-
117
- # for extracting data from finviz
118
- finviz_url = "https://finviz.com/quote.ashx?t="
119
-
120
-
121
- def get_stock_data(ticker):
122
- news_table = get_news(ticker)
123
- parsed_news_df = parse_news(news_table)
124
- parsed_and_scored_news = score_news(parsed_news_df)
125
- return parsed_and_scored_news
126
-
127
-
128
- @app.get("/ticker/{ticker}")
129
- def read_item(ticker: str):
130
- stock_data = get_stock_data(ticker)
131
- result = stock_data.to_json(orient="records")
132
- return result
133
 
134
  ## TimeGPT
135
 
136
- @app.get("/ticker/{ticker}")
137
  def get_data_from_yahoo(ticker: str):
138
  yf.pdr_override()
139
  data = pdr.get_data_yahoo(ticker, start="2023-01-01", end=None)
@@ -151,12 +48,12 @@ def apikey(api_key):
151
  key_valid = nixtla_client.validate_api_key()
152
  return key_valid
153
 
154
- def forecasting(key_valid,df):
155
- if key_valid == True:
156
  ## forecasting 3 months
157
  timegpt_fcst_df = nixtla_client.forecast(df=df, h=3, freq='MS', time_col='Date', target_col='Adj Close')
158
- else:
159
- timegpt_fcst_df= 'API key is not Valid'
160
 
161
  return timegpt_fcst_df, nixtla_client.plot(df, timegpt_fcst_df, time_col='Date', target_col='Adj Close')
162
 
@@ -170,4 +67,14 @@ def anomaly_detect(df):
170
  timegpt_anomalies_df,
171
  time_col='Date',
172
  target_col='Adj Close')
 
 
 
 
 
 
 
 
 
 
173
 
 
8
  import pandas as pd
9
  import json # for graph plotting in website
10
 
 
 
11
 
 
 
 
 
 
12
 
13
  import datetime
14
 
 
24
  @app.get("/")
25
  def read_root():
26
  return {
27
+ "message": "Hello, Please type a ticker at the end of the URL to get the stock sentiment."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  ## TimeGPT
32
 
33
+
34
  def get_data_from_yahoo(ticker: str):
35
  yf.pdr_override()
36
  data = pdr.get_data_yahoo(ticker, start="2023-01-01", end=None)
 
48
  key_valid = nixtla_client.validate_api_key()
49
  return key_valid
50
 
51
+ def forecasting(df):
52
+ ## if key_valid == True:
53
  ## forecasting 3 months
54
  timegpt_fcst_df = nixtla_client.forecast(df=df, h=3, freq='MS', time_col='Date', target_col='Adj Close')
55
+ # else:
56
+ # timegpt_fcst_df= 'API key is not Valid'
57
 
58
  return timegpt_fcst_df, nixtla_client.plot(df, timegpt_fcst_df, time_col='Date', target_col='Adj Close')
59
 
 
67
  timegpt_anomalies_df,
68
  time_col='Date',
69
  target_col='Adj Close')
70
+
71
+
72
+ @app.get("/ticker/{ticker}")
73
+ def read_item(ticker: str):
74
+ data = get_data_from_yahoo(ticker)
75
+ stock_forecast = forecasting(df)
76
+ result = stock_forecast.to_json()
77
+ return result
78
+
79
+
80