Spaces:
Running
Running
fic the changes
Browse files- .DS_Store +0 -0
- .gitignore +6 -0
- README.md +17 -1
- StockSavvy +1 -0
- app_test3_memory.py +303 -0
- df_history.csv +0 -63
- good_day.jpg +0 -0
- historicalprices.py +36 -0
- homepage.png +0 -0
- requirements.txt +2 -1
- search_news.txt +1 -0
- sentiment_analysis/__init__.py +0 -0
- sentiment_analysis/client.py +63 -0
- sentiment_analysis/requirements.txt +13 -0
- sentiment_analysis/sentiment_analysis_pipeline.py +151 -0
- tools/data_analyst.py +4 -0
- tools/investment_advisor.py +10 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
.gitignore
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
__pycache__/
|
3 |
+
.chainlit
|
4 |
+
*.faiss
|
5 |
+
*.pkl
|
6 |
+
.files
|
README.md
CHANGED
@@ -1 +1,17 @@
|
|
1 |
-
# StockSavvy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# StockSavvy
|
2 |
+
|
3 |
+
```bash
|
4 |
+
git clone https://github.com/amalaj7/Chatbot-using-Langchain-Chainlit.git
|
5 |
+
```
|
6 |
+
Install dependencies
|
7 |
+
```bash
|
8 |
+
pip install -r requirements.txt
|
9 |
+
|
10 |
+
```
|
11 |
+
Run Chainlit app
|
12 |
+
```python
|
13 |
+
chainlit run app.py
|
14 |
+
```
|
15 |
+
|
16 |
+
UI:
|
17 |
+
![Homepage_UI](homepage.png)
|
StockSavvy
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Subproject commit 96ebbbcb8d85092d08cbf7726124f44af073d060
|
app_test3_memory.py
ADDED
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_experimental.agents import create_pandas_dataframe_agent
|
2 |
+
from langchain.llms import OpenAI
|
3 |
+
import chainlit as cl
|
4 |
+
from plotly.subplots import make_subplots
|
5 |
+
import utils as u
|
6 |
+
from langchain.agents import AgentExecutor, create_openai_tools_agent
|
7 |
+
from langchain_core.messages import BaseMessage, HumanMessage
|
8 |
+
from langchain_openai import ChatOpenAI
|
9 |
+
from langchain_core.output_parsers.openai_functions import JsonOutputFunctionsParser
|
10 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
11 |
+
from tools import data_analyst
|
12 |
+
from tools import stock_sentiment_evalutor
|
13 |
+
import functools
|
14 |
+
from typing import Annotated
|
15 |
+
import operator
|
16 |
+
from typing import Sequence, TypedDict
|
17 |
+
from langchain.agents import initialize_agent, Tool
|
18 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
19 |
+
from langgraph.graph import END, StateGraph
|
20 |
+
import numpy as np
|
21 |
+
import pandas as pd
|
22 |
+
from dotenv import load_dotenv
|
23 |
+
import os
|
24 |
+
import yfinance as yf
|
25 |
+
import functools
|
26 |
+
from typing import Annotated
|
27 |
+
import operator
|
28 |
+
from typing import Sequence, TypedDict
|
29 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
30 |
+
from langgraph.graph import END, StateGraph
|
31 |
+
from tools import data_analyst, forecasting_expert_arima, forecasting_expert_rf, evaluator, stock_sentiment_evalutor, investment_advisor
|
32 |
+
from chainlit.input_widget import Select
|
33 |
+
import matplotlib.pyplot as plt
|
34 |
+
from langgraph.checkpoint.memory import MemorySaver
|
35 |
+
|
36 |
+
load_dotenv()
|
37 |
+
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
38 |
+
|
39 |
+
from GoogleNews import GoogleNews
|
40 |
+
|
41 |
+
def search_news(stockticker):
|
42 |
+
"""Useful to search the internet for news about a given topic and return relevant results."""
|
43 |
+
# Set the number of top news results to return
|
44 |
+
googlenews = GoogleNews()
|
45 |
+
googlenews.set_period('7d')
|
46 |
+
googlenews.get_news(stockticker)
|
47 |
+
result_string=googlenews.get_texts()
|
48 |
+
|
49 |
+
return result_string
|
50 |
+
|
51 |
+
|
52 |
+
def create_agent(llm: ChatOpenAI, tools: list, system_prompt: str):
|
53 |
+
# Each worker node will be given a name and some tools.
|
54 |
+
prompt = ChatPromptTemplate.from_messages(
|
55 |
+
[
|
56 |
+
(
|
57 |
+
"system",
|
58 |
+
system_prompt,
|
59 |
+
),
|
60 |
+
MessagesPlaceholder(variable_name="messages"),
|
61 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
62 |
+
]
|
63 |
+
)
|
64 |
+
agent = create_openai_tools_agent(llm, tools, prompt)
|
65 |
+
executor = AgentExecutor(agent=agent, tools=tools)
|
66 |
+
return executor
|
67 |
+
|
68 |
+
|
69 |
+
def agent_node(state, agent, name):
|
70 |
+
result = agent.invoke(state)
|
71 |
+
return {"messages": [HumanMessage(content=result["output"], name=name)]}
|
72 |
+
|
73 |
+
llm = ChatOpenAI(model="gpt-3.5-turbo")
|
74 |
+
|
75 |
+
#======================== AGENTS ==================================
|
76 |
+
# The agent state is the input to each node in the graph
|
77 |
+
class AgentState(TypedDict):
|
78 |
+
# The annotation tells the graph that new messages will always
|
79 |
+
# be added to the current states
|
80 |
+
messages: Annotated[Sequence[BaseMessage], operator.add]
|
81 |
+
# The 'next' field indicates where to route to next
|
82 |
+
next: str
|
83 |
+
|
84 |
+
# DATA ANALYST
|
85 |
+
prompt_data_analyst="You are a stock data analyst.\
|
86 |
+
Provide correct stock ticker from Yahoo Finance.\
|
87 |
+
Expected output: stocticker.\
|
88 |
+
Provide it in the following format: >>stockticker>> \
|
89 |
+
for example: >>AAPL>>"
|
90 |
+
|
91 |
+
tools_data_analyst=data_analyst.data_analyst_tools()
|
92 |
+
data_agent = create_agent(
|
93 |
+
llm,
|
94 |
+
tools_data_analyst,
|
95 |
+
prompt_data_analyst)
|
96 |
+
get_historical_prices = functools.partial(agent_node, agent=data_agent, name="Data_analyst")
|
97 |
+
|
98 |
+
#ARIMA Forecasting expert
|
99 |
+
prompt_forecasting_expert_arima="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
100 |
+
You are stock prediction expert, \
|
101 |
+
take historical stock data from message and train the ARIMA model from statsmodels Python library on the last week,then provide prediction for the 'Close' price for the next day.\
|
102 |
+
Give the value for mae_arima to Evaluator.\
|
103 |
+
Expected output:list of predicted prices with predicted dates for a selected stock ticker and mae_arima value.\n
|
104 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
105 |
+
|
106 |
+
tools_forecasting_expert_arima=forecasting_expert_arima.forecasting_expert_arima_tools()
|
107 |
+
code_forecasting_arima = create_agent(
|
108 |
+
llm,
|
109 |
+
tools_forecasting_expert_arima,
|
110 |
+
prompt_forecasting_expert_arima,
|
111 |
+
)
|
112 |
+
predict_future_prices_arima = functools.partial(agent_node, agent=code_forecasting_arima, name="Forecasting_expert_ARIMA")
|
113 |
+
|
114 |
+
# RF Forecasting expert
|
115 |
+
prompt_forecasting_expert_random_forest="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
116 |
+
You are stock prediction expert, \
|
117 |
+
take historical stock data from message and train the Random forest model from statsmodels Python library on the last week,then provide prediction for the 'Close' price for the next day.\
|
118 |
+
Give the value for mae_rf to Evaluator.\
|
119 |
+
Expected output:list of predicted prices with predicted dates for a selected stock ticker and mae_rf value.\n
|
120 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
121 |
+
|
122 |
+
tools_forecasting_expert_random_forest=forecasting_expert_rf.forecasting_expert_rf_tools()
|
123 |
+
code_forecasting_random_forest = create_agent(
|
124 |
+
llm,
|
125 |
+
tools_forecasting_expert_random_forest,
|
126 |
+
prompt_forecasting_expert_random_forest,
|
127 |
+
)
|
128 |
+
predict_future_prices_random_forest = functools.partial(agent_node, agent=code_forecasting_random_forest, name="Forecasting_expert_random_forest")
|
129 |
+
|
130 |
+
# EVALUATOR
|
131 |
+
prompt_evaluator="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
132 |
+
You are an evaluator retrieve arima_prediction and arima mean average error from forecasting expert arima and rf_prediction and mean average error for random forest from forecasting expert random forest\
|
133 |
+
print final prediction number.
|
134 |
+
Next, compare prediction price and current price to provide reccommendation if he should buy/sell/hold the stock. \
|
135 |
+
Expected output: one value for the prediction, explain why you have selected this value, reccommendation buy or sell stock and why.\
|
136 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
137 |
+
|
138 |
+
tools_evaluate=evaluator.evaluator_tools()
|
139 |
+
code_evaluate = create_agent(
|
140 |
+
llm,
|
141 |
+
tools_evaluate,
|
142 |
+
prompt_evaluator,
|
143 |
+
)
|
144 |
+
evaluate = functools.partial(agent_node, agent=code_evaluate, name="Evaluator")
|
145 |
+
|
146 |
+
#Stock Sentiment Evaluator
|
147 |
+
prompt_sentiment_evaluator="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
148 |
+
You are a stock sentiment evaluator, that takes in a stock ticker and
|
149 |
+
then using your StockSentimentAnalysis tool retrieve news for the stock based on the configured data range starting today and their corresponding sentiment,
|
150 |
+
alongwith the most dominant sentiment for the stock\
|
151 |
+
Expected output: List ALL stock news and their sentiment from the StockSentimentAnalysis tool response, and the dominant sentiment for the stock also in StockSentimentAnalysis tool response as is without change\
|
152 |
+
Also ensure you use the tool only once and do not make changes to messages
|
153 |
+
Also you are not to change the response from the tool\
|
154 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
155 |
+
|
156 |
+
tools_sentiment_evaluator=stock_sentiment_evalutor.sentimental_analysis_tools()
|
157 |
+
sentiment_evaluator = create_agent(
|
158 |
+
llm,
|
159 |
+
tools_sentiment_evaluator,
|
160 |
+
prompt_sentiment_evaluator,
|
161 |
+
)
|
162 |
+
evaluate_sentiment = functools.partial(agent_node, agent=sentiment_evaluator, name="Sentiment_Evaluator")
|
163 |
+
|
164 |
+
# Investment advisor
|
165 |
+
prompt_inv_advisor="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
166 |
+
Provide personalized investment advice and recommendations.\
|
167 |
+
Consider user input message for the latest news on the stock.\
|
168 |
+
Provide overall sentiment of the news Positive/Negative/Neutral, and recommend if the user should invest in such stock.\
|
169 |
+
MUST finish the analysis with a summary on the latest news from the user input on the stock!\
|
170 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
171 |
+
|
172 |
+
tools_reccommend=investment_advisor.investment_advisor_tools()
|
173 |
+
|
174 |
+
code_inv_advisor = create_agent(
|
175 |
+
llm,
|
176 |
+
tools_reccommend,
|
177 |
+
prompt_inv_advisor,
|
178 |
+
)
|
179 |
+
|
180 |
+
reccommend = functools.partial(agent_node, agent=code_inv_advisor, name="Investment_advisor")
|
181 |
+
|
182 |
+
workflow_data = StateGraph(AgentState)
|
183 |
+
workflow_data.add_node("Data_analyst", get_historical_prices)
|
184 |
+
workflow_data.set_entry_point("Data_analyst")
|
185 |
+
graph_data=workflow_data.compile()
|
186 |
+
|
187 |
+
workflow = StateGraph(AgentState)
|
188 |
+
#workflow.add_node("Data_analyst", get_historical_prices)
|
189 |
+
workflow.add_node("Forecasting_expert_random_forest", predict_future_prices_random_forest)
|
190 |
+
workflow.add_node("Forecasting_expert_ARIMA", predict_future_prices_arima)
|
191 |
+
workflow.add_node("Evaluator", evaluate)
|
192 |
+
|
193 |
+
|
194 |
+
# Finally, add entrypoint
|
195 |
+
workflow.set_entry_point("Forecasting_expert_random_forest")
|
196 |
+
workflow.add_edge("Forecasting_expert_random_forest","Forecasting_expert_ARIMA")
|
197 |
+
workflow.add_edge("Forecasting_expert_ARIMA","Evaluator")
|
198 |
+
workflow.add_edge("Evaluator",END)
|
199 |
+
graph = workflow.compile()
|
200 |
+
|
201 |
+
#Print graph
|
202 |
+
#graph.get_graph().print_ascii()
|
203 |
+
|
204 |
+
memory = MemorySaver()
|
205 |
+
workflow_news = StateGraph(AgentState)
|
206 |
+
workflow_news.add_node("Investment_advisor", reccommend)
|
207 |
+
workflow_news.set_entry_point("Investment_advisor")
|
208 |
+
workflow_news.add_edge("Investment_advisor",END)
|
209 |
+
graph_news = workflow_news.compile(checkpointer=memory)
|
210 |
+
|
211 |
+
from langchain_core.runnables import RunnableConfig
|
212 |
+
@cl.on_chat_start
|
213 |
+
async def on_chat_start():
|
214 |
+
cl.user_session.set("counter", 0)
|
215 |
+
# Sending an image with the local file path
|
216 |
+
elements = [
|
217 |
+
cl.Image(name="image1", display="inline", path="./stock_image1.png",size="large")
|
218 |
+
]
|
219 |
+
await cl.Message(content="Hello there, Welcome to ##StockSavyy!", elements=elements).send()
|
220 |
+
await cl.Message(content="Tell me the stockticker you want me to analyze.").send()
|
221 |
+
|
222 |
+
@cl.on_message
|
223 |
+
async def main(message: cl.Message):
|
224 |
+
#"what is the weather in sf"
|
225 |
+
counter = cl.user_session.get("counter")
|
226 |
+
counter += 1
|
227 |
+
cl.user_session.set("counter", counter)
|
228 |
+
await cl.Message(content=f"You sent {counter} message(s)!").send()
|
229 |
+
if counter==1:
|
230 |
+
inputs = {"messages": [HumanMessage(content=message.content)]}
|
231 |
+
|
232 |
+
res_data = graph_data.invoke(inputs, config=RunnableConfig(callbacks=[
|
233 |
+
cl.LangchainCallbackHandler(
|
234 |
+
to_ignore=["ChannelRead", "RunnableLambda", "ChannelWrite", "__start__", "_execute"]
|
235 |
+
# can add more into the to_ignore: "agent:edges", "call_model"
|
236 |
+
# to_keep=
|
237 |
+
|
238 |
+
)]))
|
239 |
+
#print(res_data)
|
240 |
+
await cl.Message(content=res_data["messages"][-1].content).send()
|
241 |
+
#print('ticker',str(res_data).split(">>"))
|
242 |
+
if len(str(res_data).split(">>")[1])<10:
|
243 |
+
stockticker=(str(res_data).split(">>")[1])
|
244 |
+
else:
|
245 |
+
stockticker=(str(res_data).split(">>")[0])
|
246 |
+
#print('ticker1',stockticker)
|
247 |
+
print('here')
|
248 |
+
df=u.get_stock_price(stockticker)
|
249 |
+
df_history=u.historical_stock_prices(stockticker,90)
|
250 |
+
df_history_to_msg1=eval(str(list((pd.DataFrame(df_history['Close'].values.reshape(1, -1)[0]).T).iloc[0,:])))
|
251 |
+
inputs_all = {"messages": [HumanMessage(content=(f"Predict {stockticker}, historical prices are: {df_history_to_msg1}."))]}
|
252 |
+
#print(inputs_all)
|
253 |
+
df_history=pd.DataFrame(df_history)
|
254 |
+
df_history['stockticker']=np.repeat(stockticker,len(df_history))
|
255 |
+
df_history.to_csv('df_history.csv')
|
256 |
+
|
257 |
+
res = graph.invoke(inputs_all, config=RunnableConfig(callbacks=[
|
258 |
+
cl.LangchainCallbackHandler(
|
259 |
+
to_ignore=["ChannelRead", "RunnableLambda", "ChannelWrite", "__start__", "_execute"]
|
260 |
+
# can add more into the to_ignore: "agent:edges", "call_model"
|
261 |
+
# to_keep=
|
262 |
+
|
263 |
+
)]))
|
264 |
+
await cl.Message(content= res["messages"][-2].content + '\n\n' + res["messages"][-1].content).send()
|
265 |
+
|
266 |
+
df_history=pd.read_csv('df_history.csv')
|
267 |
+
stockticker=str(df_history['stockticker'][0])
|
268 |
+
df_search=search_news(stockticker)
|
269 |
+
with open('search_news.txt', 'w') as a:
|
270 |
+
a.write(str(df_search[0:10]))
|
271 |
+
file = open("search_news.txt", "r")
|
272 |
+
df_search = file.read()
|
273 |
+
print(stockticker)
|
274 |
+
|
275 |
+
config = {"configurable": {"thread_id": "1"}}
|
276 |
+
inputs_news = {"messages": [HumanMessage(content=(f"Summarize articles for {stockticker} to write 2 sentences about following articles: {df_search}."))]}
|
277 |
+
k=0
|
278 |
+
for event in graph_news.stream(inputs_news, config, stream_mode="values"):
|
279 |
+
k+=1
|
280 |
+
if k>1:
|
281 |
+
await cl.Message(content=event["messages"][-1].content).send()
|
282 |
+
|
283 |
+
|
284 |
+
if counter==1:
|
285 |
+
df=u.historical_stock_prices(stockticker,90)
|
286 |
+
df=u.calculate_MACD(df, fast_period=12, slow_period=26, signal_period=9)
|
287 |
+
fig = u.plot_macd2(df)
|
288 |
+
|
289 |
+
if fig:
|
290 |
+
elements = [cl.Pyplot(name="plot", figure=fig, display="inline",size="large"),
|
291 |
+
]
|
292 |
+
await cl.Message(
|
293 |
+
content="Here is the MACD plot",
|
294 |
+
elements=elements,
|
295 |
+
).send()
|
296 |
+
else:
|
297 |
+
await cl.Message(
|
298 |
+
content="Failed to generate the MACD plot."
|
299 |
+
).send()
|
300 |
+
|
301 |
+
|
302 |
+
|
303 |
+
|
df_history.csv
DELETED
@@ -1,63 +0,0 @@
|
|
1 |
-
Date,Open,High,Low,Close,Volume,Dividends,Stock Splits,stockticker
|
2 |
-
2024-04-22 00:00:00-04:00,399.3596471827562,402.1246793258712,395.03745670124425,400.2380676269531,20286900,0.0,0.0,MSFT
|
3 |
-
2024-04-23 00:00:00-04:00,403.51216021293357,407.4650522060062,402.33429210205253,406.836181640625,15734500,0.0,0.0,MSFT
|
4 |
-
2024-04-24 00:00:00-04:00,408.82258607970806,411.72735028943725,406.0475926794115,408.323486328125,15065300,0.0,0.0,MSFT
|
5 |
-
2024-04-25 00:00:00-04:00,393.32054400787314,399.1700088625431,387.3313470651067,398.321533203125,40586500,0.0,0.0,MSFT
|
6 |
-
2024-04-26 00:00:00-04:00,411.4279132786848,412.25640548421114,405.02945064216703,405.58843994140625,29694700,0.0,0.0,MSFT
|
7 |
-
2024-04-29 00:00:00-04:00,404.52035539531056,405.5884361925186,398.4712687423875,401.5257568359375,19582100,0.0,0.0,MSFT
|
8 |
-
2024-04-30 00:00:00-04:00,400.76710737423014,401.4359144425664,388.4693126899027,388.6289978027344,28781400,0.0,0.0,MSFT
|
9 |
-
2024-05-01 00:00:00-04:00,391.9030904630616,400.9967037344784,389.6072438016868,394.2289123535156,23562500,0.0,0.0,MSFT
|
10 |
-
2024-05-02 00:00:00-04:00,396.94401914412265,399.20992105581087,393.9394288835304,397.1236877441406,17709400,0.0,0.0,MSFT
|
11 |
-
2024-05-03 00:00:00-04:00,401.55570709720826,406.4169339510819,401.13644988960164,405.9278259277344,17446700,0.0,0.0,MSFT
|
12 |
-
2024-05-06 00:00:00-04:00,408.024048156178,413.1847226485525,405.63833666603693,412.7954406738281,16996600,0.0,0.0,MSFT
|
13 |
-
2024-05-07 00:00:00-04:00,413.91342570011614,413.92341744357753,408.35344694069664,408.6029968261719,20018200,0.0,0.0,MSFT
|
14 |
-
2024-05-08 00:00:00-04:00,407.4351142805277,411.48780192255407,405.97772103822234,409.80084228515625,11792300,0.0,0.0,MSFT
|
15 |
-
2024-05-09 00:00:00-04:00,409.8307875446534,411.97691043744567,408.363433019907,411.57763671875,14689700,0.0,0.0,MSFT
|
16 |
-
2024-05-10 00:00:00-04:00,412.1965086797442,414.6321179246016,411.05854661467066,413.9932556152344,13402300,0.0,0.0,MSFT
|
17 |
-
2024-05-13 00:00:00-04:00,417.2573820335048,417.5967662074119,410.08032520369875,412.97509765625,15440200,0.0,0.0,MSFT
|
18 |
-
2024-05-14 00:00:00-04:00,411.2781631216723,416.73831581889846,410.8090081198034,415.80999755859375,15109300,0.0,0.0,MSFT
|
19 |
-
2024-05-15 00:00:00-04:00,417.8999938964844,423.80999755859375,417.2699890136719,423.0799865722656,22239500,0.75,0.0,MSFT
|
20 |
-
2024-05-16 00:00:00-04:00,421.79998779296875,425.4200134277344,420.3500061035156,420.989990234375,17530100,0.0,0.0,MSFT
|
21 |
-
2024-05-17 00:00:00-04:00,422.5400085449219,422.9200134277344,418.0299987792969,420.2099914550781,15352200,0.0,0.0,MSFT
|
22 |
-
2024-05-20 00:00:00-04:00,420.2099914550781,426.7699890136719,419.989990234375,425.3399963378906,16272100,0.0,0.0,MSFT
|
23 |
-
2024-05-21 00:00:00-04:00,426.8299865722656,432.9700012207031,424.8500061035156,429.0400085449219,21453300,0.0,0.0,MSFT
|
24 |
-
2024-05-22 00:00:00-04:00,430.0899963378906,432.4100036621094,427.1300048828125,430.5199890136719,18073700,0.0,0.0,MSFT
|
25 |
-
2024-05-23 00:00:00-04:00,432.9700012207031,433.6000061035156,425.4200134277344,427.0,17211700,0.0,0.0,MSFT
|
26 |
-
2024-05-24 00:00:00-04:00,427.19000244140625,431.05999755859375,424.4100036621094,430.1600036621094,11845800,0.0,0.0,MSFT
|
27 |
-
2024-05-28 00:00:00-04:00,429.6300048828125,430.82000732421875,426.6000061035156,430.32000732421875,15718000,0.0,0.0,MSFT
|
28 |
-
2024-05-29 00:00:00-04:00,425.69000244140625,430.94000244140625,425.69000244140625,429.1700134277344,15517100,0.0,0.0,MSFT
|
29 |
-
2024-05-30 00:00:00-04:00,424.29998779296875,424.29998779296875,414.239990234375,414.6700134277344,28424800,0.0,0.0,MSFT
|
30 |
-
2024-05-31 00:00:00-04:00,416.75,416.75,404.510009765625,415.1300048828125,47995300,0.0,0.0,MSFT
|
31 |
-
2024-06-03 00:00:00-04:00,415.5299987792969,416.42999267578125,408.9200134277344,413.5199890136719,17484700,0.0,0.0,MSFT
|
32 |
-
2024-06-04 00:00:00-04:00,412.42999267578125,416.44000244140625,409.67999267578125,416.07000732421875,14348900,0.0,0.0,MSFT
|
33 |
-
2024-06-05 00:00:00-04:00,417.80999755859375,424.0799865722656,416.29998779296875,424.010009765625,16988000,0.0,0.0,MSFT
|
34 |
-
2024-06-06 00:00:00-04:00,424.010009765625,425.30999755859375,420.5799865722656,424.5199890136719,14861300,0.0,0.0,MSFT
|
35 |
-
2024-06-07 00:00:00-04:00,426.20001220703125,426.2799987792969,423.0,423.8500061035156,13621700,0.0,0.0,MSFT
|
36 |
-
2024-06-10 00:00:00-04:00,424.70001220703125,428.0799865722656,423.8900146484375,427.8699951171875,14003000,0.0,0.0,MSFT
|
37 |
-
2024-06-11 00:00:00-04:00,425.4800109863281,432.82000732421875,425.25,432.67999267578125,14551100,0.0,0.0,MSFT
|
38 |
-
2024-06-12 00:00:00-04:00,435.32000732421875,443.3999938964844,433.25,441.05999755859375,22366200,0.0,0.0,MSFT
|
39 |
-
2024-06-13 00:00:00-04:00,440.8500061035156,443.3900146484375,439.3699951171875,441.5799865722656,15960600,0.0,0.0,MSFT
|
40 |
-
2024-06-14 00:00:00-04:00,438.2799987792969,443.1400146484375,436.7200012207031,442.57000732421875,13582000,0.0,0.0,MSFT
|
41 |
-
2024-06-17 00:00:00-04:00,442.5899963378906,450.94000244140625,440.7200012207031,448.3699951171875,20790000,0.0,0.0,MSFT
|
42 |
-
2024-06-18 00:00:00-04:00,449.7099914550781,450.1400146484375,444.8900146484375,446.3399963378906,17112500,0.0,0.0,MSFT
|
43 |
-
2024-06-20 00:00:00-04:00,446.29998779296875,446.5299987792969,441.2699890136719,445.70001220703125,19877400,0.0,0.0,MSFT
|
44 |
-
2024-06-21 00:00:00-04:00,447.3800048828125,450.5799865722656,446.510009765625,449.7799987792969,34486200,0.0,0.0,MSFT
|
45 |
-
2024-06-24 00:00:00-04:00,449.79998779296875,452.75,446.4100036621094,447.6700134277344,15913700,0.0,0.0,MSFT
|
46 |
-
2024-06-25 00:00:00-04:00,448.25,451.4200134277344,446.75,450.95001220703125,16747500,0.0,0.0,MSFT
|
47 |
-
2024-06-26 00:00:00-04:00,449.0,453.6000061035156,448.19000244140625,452.1600036621094,16507000,0.0,0.0,MSFT
|
48 |
-
2024-06-27 00:00:00-04:00,452.17999267578125,456.1700134277344,451.7699890136719,452.8500061035156,14806300,0.0,0.0,MSFT
|
49 |
-
2024-06-28 00:00:00-04:00,453.07000732421875,455.3800048828125,446.4100036621094,446.95001220703125,28362300,0.0,0.0,MSFT
|
50 |
-
2024-07-01 00:00:00-04:00,448.6600036621094,457.3699951171875,445.6600036621094,456.7300109863281,17662800,0.0,0.0,MSFT
|
51 |
-
2024-07-02 00:00:00-04:00,453.20001220703125,459.5899963378906,453.1099853515625,459.2799987792969,13979800,0.0,0.0,MSFT
|
52 |
-
2024-07-03 00:00:00-04:00,458.19000244140625,461.0199890136719,457.8800048828125,460.7699890136719,9932800,0.0,0.0,MSFT
|
53 |
-
2024-07-05 00:00:00-04:00,459.6099853515625,468.3500061035156,458.9700012207031,467.55999755859375,16000300,0.0,0.0,MSFT
|
54 |
-
2024-07-08 00:00:00-04:00,466.54998779296875,467.70001220703125,464.4599914550781,466.239990234375,12962300,0.0,0.0,MSFT
|
55 |
-
2024-07-09 00:00:00-04:00,467.0,467.3299865722656,458.0,459.5400085449219,17207200,0.0,0.0,MSFT
|
56 |
-
2024-07-10 00:00:00-04:00,461.2200012207031,466.4599914550781,458.8599853515625,466.25,18196100,0.0,0.0,MSFT
|
57 |
-
2024-07-11 00:00:00-04:00,462.9800109863281,464.7799987792969,451.54998779296875,454.70001220703125,23111200,0.0,0.0,MSFT
|
58 |
-
2024-07-12 00:00:00-04:00,454.3299865722656,456.3599853515625,450.6499938964844,453.54998779296875,16324300,0.0,0.0,MSFT
|
59 |
-
2024-07-15 00:00:00-04:00,453.29998779296875,457.260009765625,451.42999267578125,453.9599914550781,14429400,0.0,0.0,MSFT
|
60 |
-
2024-07-16 00:00:00-04:00,454.2200012207031,454.29998779296875,446.6600036621094,449.5199890136719,17175700,0.0,0.0,MSFT
|
61 |
-
2024-07-17 00:00:00-04:00,442.5899963378906,444.8500061035156,439.17999267578125,443.5199890136719,21778000,0.0,0.0,MSFT
|
62 |
-
2024-07-18 00:00:00-04:00,444.3399963378906,444.6499938964844,434.3999938964844,440.3699951171875,20794800,0.0,0.0,MSFT
|
63 |
-
2024-07-19 00:00:00-04:00,433.1000061035156,441.1400146484375,432.0,437.1099853515625,20862400,0.0,0.0,MSFT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
good_day.jpg
ADDED
historicalprices.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import re
|
4 |
+
|
5 |
+
def get_historical_prices(product_url):
|
6 |
+
headers = {
|
7 |
+
'User-Agent': 'Your User Agent'
|
8 |
+
}
|
9 |
+
response = requests.get(product_url, headers=headers)
|
10 |
+
|
11 |
+
if response.status_code == 200:
|
12 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
13 |
+
price_data = {}
|
14 |
+
|
15 |
+
# Extract historical price data
|
16 |
+
price_blocks = soup.find_all('div', class_='price-history__row')
|
17 |
+
|
18 |
+
for block in price_blocks:
|
19 |
+
date = block.find('span', class_='price-history__date').text.strip()
|
20 |
+
price = block.find('span', class_='price-history__price').text.strip()
|
21 |
+
price_data[date] = price
|
22 |
+
|
23 |
+
return price_data
|
24 |
+
else:
|
25 |
+
print(f"Failed to retrieve data. Status code: {response.status_code}")
|
26 |
+
return None
|
27 |
+
|
28 |
+
# Example usage
|
29 |
+
if __name__ == '__main__':
|
30 |
+
product_url = 'https://camelcamelcamel.com/product/ASIN'
|
31 |
+
historical_prices = get_historical_prices(product_url)
|
32 |
+
|
33 |
+
if historical_prices:
|
34 |
+
print("Historical Prices:")
|
35 |
+
for date, price in historical_prices.items():
|
36 |
+
print(f"{date}: {price}")
|
homepage.png
ADDED
requirements.txt
CHANGED
@@ -21,4 +21,5 @@ alpaca_trade_api
|
|
21 |
transformers
|
22 |
pandas
|
23 |
GoogleNews
|
24 |
-
streamlit
|
|
|
|
21 |
transformers
|
22 |
pandas
|
23 |
GoogleNews
|
24 |
+
streamlit
|
25 |
+
googlenews
|
search_news.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
['Investing in Amazon Stock (AMZN)', 'Amazon.com, Inc. (NASDAQ:AMZN) Shares Could Be 40% Below Their Intrinsic Value Estimate', 'Why Amazon Stock Is the Biggest Bargain After Amazon Prime Day', 'Is Amazon Stock A Buy As Analysts Project Strong Prime Day Sales?', "Amazon cracks down on 'coffee badging' employees by tracking individual hours spent in the office", 'Amazon Stock (AMZN) Price Prediction and Forecast 2025-2030', 'Amazon CEO Andy Jassy says being ‘ravenous’ about one thing will determine if your career is a success', 'Amazon says this year’s Prime Day was its biggest ever', 'Amazon Prime Day 2024 returns this July', '4 stocks to watch on Thursday: NFLX, AMZN and more']
|
sentiment_analysis/__init__.py
ADDED
File without changes
|
sentiment_analysis/client.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from alpaca_trade_api import REST
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
|
6 |
+
class AlpacaNewsFetcher:
|
7 |
+
"""
|
8 |
+
A class for fetching news articles related to a specific stock from Alpaca API.
|
9 |
+
|
10 |
+
Attributes:
|
11 |
+
- api_key (str): Alpaca API key for authentication.
|
12 |
+
- api_secret (str): Alpaca API secret for authentication.
|
13 |
+
- rest_client (alpaca_trade_api.REST): Alpaca REST API client.
|
14 |
+
"""
|
15 |
+
|
16 |
+
def __init__(self, api_key, api_secret):
|
17 |
+
"""
|
18 |
+
Initializes the AlpacaNewsFetcher object.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
- api_key (str): Alpaca API key for authentication.
|
22 |
+
- api_secret (str): Alpaca API secret for authentication.
|
23 |
+
"""
|
24 |
+
self.api_key = api_key
|
25 |
+
self.api_secret = api_secret
|
26 |
+
self.rest_client = REST(api_key, api_secret)
|
27 |
+
|
28 |
+
load_dotenv()
|
29 |
+
self.no_of_newsarticles_to_fetch = os.environ["NO_OF_NEWSARTICLES_TO_FETCH"]
|
30 |
+
|
31 |
+
def fetch_news(self, symbol, start_date, end_date):
|
32 |
+
"""
|
33 |
+
Fetches news articles for a given stock symbol within a specified date range.
|
34 |
+
|
35 |
+
Args:
|
36 |
+
- symbol (str): Stock symbol for which news articles are to be fetched (e.g., "AAPL").
|
37 |
+
- start_date (str): Start date of the range in the format "YYYY-MM-DD".
|
38 |
+
- end_date (str): End date of the range in the format "YYYY-MM-DD".
|
39 |
+
|
40 |
+
Returns:
|
41 |
+
- list: A list of dictionaries containing relevant information for each news article.
|
42 |
+
"""
|
43 |
+
news_articles = self.rest_client.get_news(symbol, start_date, end_date, limit=self.no_of_newsarticles_to_fetch )
|
44 |
+
formatted_news = []
|
45 |
+
print("-----------------------------------------------------")
|
46 |
+
print(len(news_articles))
|
47 |
+
print("-----------------------------------------------------")
|
48 |
+
|
49 |
+
for article in news_articles:
|
50 |
+
summary = article.summary
|
51 |
+
title = article.headline
|
52 |
+
timestamp = article.created_at
|
53 |
+
|
54 |
+
relevant_info = {
|
55 |
+
'timestamp': timestamp,
|
56 |
+
'title': title,
|
57 |
+
'summary': summary
|
58 |
+
}
|
59 |
+
|
60 |
+
formatted_news.append(relevant_info)
|
61 |
+
|
62 |
+
return formatted_news
|
63 |
+
|
sentiment_analysis/requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
alpaca_trade_api
|
2 |
+
transformers
|
3 |
+
einops
|
4 |
+
accelerate
|
5 |
+
langchain
|
6 |
+
bitsandbytes
|
7 |
+
#sentencepeice
|
8 |
+
openai
|
9 |
+
backtrader
|
10 |
+
yfinance
|
11 |
+
pandas
|
12 |
+
pyfolio
|
13 |
+
python-dotenv
|
sentiment_analysis/sentiment_analysis_pipeline.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# !pip install transformers
|
2 |
+
from transformers import pipeline
|
3 |
+
from client import AlpacaNewsFetcher
|
4 |
+
from alpaca_trade_api import REST
|
5 |
+
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import pandas as pd
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
from collections import defaultdict
|
10 |
+
from datetime import date
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
class NewsSentimentAnalysis:
|
15 |
+
"""
|
16 |
+
A class for sentiment analysis of news articles using the Transformers library.
|
17 |
+
|
18 |
+
Attributes:
|
19 |
+
- classifier (pipeline): Sentiment analysis pipeline from Transformers.
|
20 |
+
"""
|
21 |
+
|
22 |
+
def __init__(self):
|
23 |
+
"""
|
24 |
+
Initializes the NewsSentimentAnalysis object.
|
25 |
+
"""
|
26 |
+
self.classifier = pipeline('sentiment-analysis')
|
27 |
+
|
28 |
+
def analyze_sentiment(self, news_article):
|
29 |
+
"""
|
30 |
+
Analyzes the sentiment of a given news article.
|
31 |
+
|
32 |
+
Args:
|
33 |
+
- news_article (dict): Dictionary containing 'summary', 'headline', and 'created_at' keys.
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
- dict: A dictionary containing sentiment analysis results.
|
37 |
+
"""
|
38 |
+
summary = news_article['summary']
|
39 |
+
title = news_article['title']
|
40 |
+
timestamp = news_article['timestamp']
|
41 |
+
|
42 |
+
relevant_text = summary + title
|
43 |
+
sentiment_result = self.classifier(relevant_text)
|
44 |
+
|
45 |
+
analysis_result = {
|
46 |
+
'timestamp': timestamp,
|
47 |
+
'title': title,
|
48 |
+
'summary': summary,
|
49 |
+
'sentiment': sentiment_result
|
50 |
+
}
|
51 |
+
|
52 |
+
return analysis_result
|
53 |
+
|
54 |
+
def plot_sentiment_graph(self, sentiment_analysis_result):
|
55 |
+
"""
|
56 |
+
Plots a sentiment analysis graph
|
57 |
+
|
58 |
+
Args:
|
59 |
+
- sentiment_analysis_result): (dict): Dictionary containing 'summary', 'headline', and 'created_at' keys.
|
60 |
+
|
61 |
+
Returns:
|
62 |
+
- dict: A dictionary containing sentiment analysis results.
|
63 |
+
"""
|
64 |
+
df = pd.DataFrame(sentiment_analysis_result)
|
65 |
+
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
|
66 |
+
df['Date'] = df['Timestamp'].dt.date
|
67 |
+
|
68 |
+
#Group by Date, sentiment value count
|
69 |
+
grouped = df.groupby(by='Date')['Sentiment'].value_counts()
|
70 |
+
grouped.plot.pie()
|
71 |
+
|
72 |
+
|
73 |
+
def get_dominant_sentiment (self, sentiment_analysis_result):
|
74 |
+
"""
|
75 |
+
Returns overall sentiment, negative or positive or neutral depending on the count of negative sentiment vs positive sentiment
|
76 |
+
|
77 |
+
Args:
|
78 |
+
- sentiment_analysis_result): (dict): Dictionary containing 'summary', 'headline', and 'created_at' keys.
|
79 |
+
|
80 |
+
Returns:
|
81 |
+
- dict: A dictionary containing sentiment analysis results.
|
82 |
+
"""
|
83 |
+
df = pd.DataFrame(sentiment_analysis_result)
|
84 |
+
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
|
85 |
+
df['Date'] = df['Timestamp'].dt.date
|
86 |
+
|
87 |
+
#Group by Date, sentiment value count
|
88 |
+
grouped = df.groupby(by='Date')['Sentiment'].value_counts()
|
89 |
+
df = pd.DataFrame(list(grouped.items()), columns=['Sentiment', 'count'])
|
90 |
+
df['date'] = df['Sentiment'].apply(lambda x: x[0])
|
91 |
+
df['sentiment'] = df['Sentiment'].apply(lambda x: x[1])
|
92 |
+
df.drop('Sentiment', axis=1, inplace=True)
|
93 |
+
result = df.groupby('sentiment')['count'].sum().reset_index()
|
94 |
+
|
95 |
+
# Determine the sentiment with the most count
|
96 |
+
dominant_sentiment = result.loc[result['count'].idxmax()]
|
97 |
+
|
98 |
+
return dominant_sentiment
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
#starting point of the program
|
103 |
+
if __name__ == '__main__':
|
104 |
+
# Example Usage:
|
105 |
+
# Initialize the AlpacaNewsFetcher object
|
106 |
+
|
107 |
+
#Load Alpaca Key and Secret from environment.
|
108 |
+
load_dotenv()
|
109 |
+
api_key = os.environ["ALPACA_API_KEY"]
|
110 |
+
api_secret = os.environ["ALPACA_SECRET"]
|
111 |
+
|
112 |
+
#Initialize AlpacaNewsFetcher, a class for fetching news articles related to a specific stock from Alpaca API.
|
113 |
+
news_fetcher = AlpacaNewsFetcher(api_key, api_secret)
|
114 |
+
|
115 |
+
# Fetch news (contains - title of the news, timestamp and summary) for AAPL from 2021-01-01 to 2021-12-31
|
116 |
+
news_data = news_fetcher.fetch_news("AAPL", "2021-01-01", "2021-12-31")
|
117 |
+
|
118 |
+
# Initialize the NewsSentimentAnalysis object
|
119 |
+
news_sentiment_analyzer = NewsSentimentAnalysis()
|
120 |
+
analysis_result = []
|
121 |
+
# Assume 'news_data' is a list of news articles (each as a dictionary)
|
122 |
+
for article in news_data:
|
123 |
+
sentiment_analysis_result = news_sentiment_analyzer.analyze_sentiment(article)
|
124 |
+
|
125 |
+
# Display sentiment analysis results
|
126 |
+
""" print(f'Timestamp: {sentiment_analysis_result["timestamp"]}, '
|
127 |
+
f'Title: {sentiment_analysis_result["title"]}, '
|
128 |
+
f'Summary: {sentiment_analysis_result["summary"]}')
|
129 |
+
|
130 |
+
print(f'Sentiment: {sentiment_analysis_result["sentiment"]}', '\n') """
|
131 |
+
|
132 |
+
#Extracting timestamp of article and sentiment of article for graphing
|
133 |
+
result = {
|
134 |
+
'Timestamp': sentiment_analysis_result["timestamp"],
|
135 |
+
'News- Title:Summary': sentiment_analysis_result["title"] + sentiment_analysis_result["summary"],
|
136 |
+
'Sentiment': sentiment_analysis_result["sentiment"][0]['label']
|
137 |
+
}
|
138 |
+
|
139 |
+
analysis_result.append(result)
|
140 |
+
|
141 |
+
#Graph dominant sentiment based on sentiment analysis data of news articles
|
142 |
+
dominant_sentiment = news_sentiment_analyzer.get_dominant_sentiment(analysis_result)
|
143 |
+
|
144 |
+
final_result = {
|
145 |
+
'Sentiment-analysis-result' : analysis_result,
|
146 |
+
'Dominant-sentiment' : dominant_sentiment['sentiment']
|
147 |
+
}
|
148 |
+
|
149 |
+
print(final_result)
|
150 |
+
|
151 |
+
|
tools/data_analyst.py
CHANGED
@@ -64,7 +64,11 @@ def data_analyst_tools():
|
|
64 |
func=StockPriceTool,
|
65 |
args_schema=StockPriceCheckInput,
|
66 |
description="Function to get current stock prices.",
|
|
|
67 |
),
|
|
|
|
|
|
|
68 |
# StructuredTool.from_function(
|
69 |
# func=HistoricalStockPricesTool,
|
70 |
# args_schema=HistoricalStockPricesInput,
|
|
|
64 |
func=StockPriceTool,
|
65 |
args_schema=StockPriceCheckInput,
|
66 |
description="Function to get current stock prices.",
|
67 |
+
<<<<<<< HEAD
|
68 |
),
|
69 |
+
=======
|
70 |
+
)
|
71 |
+
>>>>>>> 594c18622dd698f1854229dda280817492475d75
|
72 |
# StructuredTool.from_function(
|
73 |
# func=HistoricalStockPricesTool,
|
74 |
# args_schema=HistoricalStockPricesInput,
|
tools/investment_advisor.py
CHANGED
@@ -5,7 +5,10 @@ from langchain.tools import StructuredTool
|
|
5 |
import yfinance as yf
|
6 |
from typing import List
|
7 |
from datetime import datetime,timedelta
|
|
|
8 |
import pandas as pd
|
|
|
|
|
9 |
|
10 |
def investment_advisor_tools():
|
11 |
|
@@ -31,6 +34,7 @@ def investment_advisor_tools():
|
|
31 |
|
32 |
args_schema: Optional[Type[BaseModel]] = newsSummaryInput
|
33 |
|
|
|
34 |
def analyze_prices():
|
35 |
"""Take historical prices, analyze them and answer user's questions."""
|
36 |
df_prices=pd.read_csv('../df_history.csv')
|
@@ -52,17 +56,23 @@ def investment_advisor_tools():
|
|
52 |
raise NotImplementedError("This tool does not support async")
|
53 |
|
54 |
args_schema: Optional[Type[BaseModel]] = pricesInput
|
|
|
|
|
|
|
55 |
|
56 |
tools_reccommend = [
|
57 |
StructuredTool.from_function(
|
58 |
func=newsSummaryTool,
|
59 |
args_schema=newsSummaryInput,
|
60 |
description="Summarize articles.",
|
|
|
61 |
),
|
62 |
StructuredTool.from_function(
|
63 |
func=pricesTool,
|
64 |
args_schema=pricesInput,
|
65 |
description="Analyze stock prices.",
|
|
|
|
|
66 |
)
|
67 |
]
|
68 |
return tools_reccommend
|
|
|
5 |
import yfinance as yf
|
6 |
from typing import List
|
7 |
from datetime import datetime,timedelta
|
8 |
+
<<<<<<< HEAD
|
9 |
import pandas as pd
|
10 |
+
=======
|
11 |
+
>>>>>>> 594c18622dd698f1854229dda280817492475d75
|
12 |
|
13 |
def investment_advisor_tools():
|
14 |
|
|
|
34 |
|
35 |
args_schema: Optional[Type[BaseModel]] = newsSummaryInput
|
36 |
|
37 |
+
<<<<<<< HEAD
|
38 |
def analyze_prices():
|
39 |
"""Take historical prices, analyze them and answer user's questions."""
|
40 |
df_prices=pd.read_csv('../df_history.csv')
|
|
|
56 |
raise NotImplementedError("This tool does not support async")
|
57 |
|
58 |
args_schema: Optional[Type[BaseModel]] = pricesInput
|
59 |
+
=======
|
60 |
+
|
61 |
+
>>>>>>> 594c18622dd698f1854229dda280817492475d75
|
62 |
|
63 |
tools_reccommend = [
|
64 |
StructuredTool.from_function(
|
65 |
func=newsSummaryTool,
|
66 |
args_schema=newsSummaryInput,
|
67 |
description="Summarize articles.",
|
68 |
+
<<<<<<< HEAD
|
69 |
),
|
70 |
StructuredTool.from_function(
|
71 |
func=pricesTool,
|
72 |
args_schema=pricesInput,
|
73 |
description="Analyze stock prices.",
|
74 |
+
=======
|
75 |
+
>>>>>>> 594c18622dd698f1854229dda280817492475d75
|
76 |
)
|
77 |
]
|
78 |
return tools_reccommend
|