-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrewai-stocks.py
128 lines (105 loc) · 4.86 KB
/
crewai-stocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import os
from datetime import datetime
import yfinance as yf
from crewai import Agent, Task, Crew, Process
from langchain.tools import Tool
from langchain_openai import ChatOpenAI
from langchain_community.tools import DuckDuckGoSearchResults
def fetch_stock_price(ticket):
stock = yf.download(ticket, start="2023-08-08", end="2024-08-08")
return stock
yahoo_finance_tool = Tool(
name = "Yahoo Finance Tool",
description = "Fetches stocks prices for {ticket} from the last year about a specific company from Yahoo Finance API",
func= lambda ticket: fetch_stock_price(ticket)
)
os.environ['OPENAI_API_KEY'] = "YOUR_OPENAI_KEY"
llm = ChatOpenAI(model="gpt-3.5-turbo")
stockPriceAnalyst = Agent(
role="Senior stock price Analyst",
goal="Find the {ticket} stock price and analyses trends",
backstory="""You're a highly experienced in analyzing the price of an specific stock and make predictions about its future price.""",
verbose=True,
llm=llm,
max_iter=5,
memory=True,
tools=[yahoo_finance_tool],
allow_delegation=False
)
getStockPrice = Task(
description="Analyze the stock {ticket} price history and create a trend analyses of up, down or sideways",
expected_output="""" Specify the current trend stock price - up, down or sideways.
eg. stock='APPL, price UP'
""",
agent= stockPriceAnalyst
)
search_tool = DuckDuckGoSearchResults(backend='news', num_results=10)
newsAnalyst = Agent(
role="Stock News Analyst",
goal="""Create a short summary of the market news to the stock {ticket} company. Specify the current trend - up, down or sideways with the news context. For each request stock asset, specify a number between 0 and 100, where 0 is extreme fear and 100 is extreme greed.""",
backstory="""You're a highly experienced in analyzing the market trends and news and have tracked assts for more than 10 years.
You-re also master level analysts in the tradicional markets and have deep understading of human psychology.
You understand news, theirs titles and information, but you look at those with a health dose of skepticism.
You consider also the source of the news articles.
""",
verbose=True,
llm=llm,
max_iter=10,
memory=True,
tools=[yahoo_finance_tool],
allow_delegation=False
)
get_news = Task(
description="""Take the stock and always include BTC to it (if not request).
Use the search tool to search each one individually.
The current date is {datetime.now()}.
Compose the results into a helpfull report""",
expected_output=""""A summary of the overall market and one sentence summary for each request asset.
Include a fear/greed score for each asset based on the news. Use format:
<STOCK ASSET>
<SUMMARY BASED ON NEWS>
<TREND PREDICTION>
<FEAR/GREED SCORE>
""",
agent= newsAnalyst
)
stockAnalystWrite = Agent(
role="Senior Stock Analyst Writer",
goal=""""Analyze the trends price and news and write an insighfully compelling and informative 3 paragraph long newsletter based on the stock report and price trend.
""",
backstory="""You're widely accepted as the best stock analyst in the market. You understand complex concepts and create compelling stories and narratives that resonate with wider audiences.
You understand macro factors and combine multiple theories - eg. cycle theory and fundamental analyses. You're able to hold multiple opinions when analyzing anything.
""",
verbose=True,
llm=llm,
max_iter=5,
memory=True,
allow_delegation=True
)
writeAnalyses = Task(
description="""Use the stock price trend and the stock news report to create an analyses and write the newsletter about the {ticket} company that is brief and highlights the most important points.
Focus on the stock price trend, news and fear/greed score. What are the near future considerations?
Include the previous analyses of stock trend and news summary.
""",
expected_output=""""An eloquent 3 paragraphs newsletter formated as markdown in an easy readable manner. It should contain:
- 3 bullets executive summary
- Introduction - set the overall picture and spike up the interest
- main part provides the meat of the analysis including the news summary and fear/greed scores
- summary - key facts and concrete future trend prediction - up, down or sideways.
""",
agent=stockAnalystWrite,
context=[getStockPrice, get_news]
)
crew = Crew(
agents= [stockPriceAnalyst, newsAnalyst, stockAnalystWrite],
tasks= [getStockPrice, get_news, writeAnalyses],
verbose=2,
process= Process.hierarchical,
full_output=True,
share_crew=False,
manager_llm=llm,
max_iter=15
)
results = crew.kickoff(inputs={'ticket':'AAPL'})
list(results.keys())