DeepSpaceMuse commited on
Commit
63cd85f
·
verified ·
1 Parent(s): ae7a494

adds get_stock_price tool

Browse files
Files changed (1) hide show
  1. app.py +75 -8
app.py CHANGED
@@ -3,20 +3,87 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
 
 
 
6
  from tools.final_answer import FinalAnswerTool
7
 
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
 
 
 
 
 
 
 
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +122,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ import yfinance as yf
7
+ from ta.momentum import RSIIndicator, StochasticOscillator
8
+ from ta.trend import MACD
9
+ from ta.volume import volume_weighted_average_price
10
  from tools.final_answer import FinalAnswerTool
11
 
12
+
13
  from Gradio_UI import GradioUI
14
 
15
+ # # Below is an example of a tool that does nothing. Amaze us with your creativity !
16
+ # @tool
17
+ # def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
18
+ # #Keep this format for the description / args / args description but feel free to modify the tool
19
+ # """A tool that does nothing yet
20
+ # Args:
21
+ # arg1: the first argument
22
+ # arg2: the second argument
23
+ # """
24
+ # return "What magic will you build ?"
25
+
26
  @tool
27
+ def get_stock_price(ticker: str) -> Union[Dict, str]:
28
+ """
29
+ A tool that fetches the historical stock price data and technical indicators for a given ticker.
30
  Args:
31
+ ticker: A string representing a stocke ticker name (e.g AAPL)
 
32
  """
33
+
34
+ try:
35
+ data = yf.download(
36
+ ticker,
37
+ start=dt.datetime.now() - dt.timedelta(weeks=24 * 3),
38
+ end=dt.datetime.now(),
39
+ interval="1wk",
40
+ )
41
+ df = data.copy()
42
+ data.reset_index(inplace=True)
43
+ data.Date = data.Date.astype(str)
44
+
45
+ indicators = {}
46
+
47
+ rsi_series = RSIIndicator(df["Close"], window=14).rsi().iloc[-12:]
48
+ indicators["RSI"] = {
49
+ date.strftime("%Y-%m-%d"): int(value)
50
+ for date, value in rsi_series.dropna().to_dict().items()
51
+ }
52
+
53
+ stochastic_series = (
54
+ StochasticOscillator(df["High"], df["Low"], df["Close"], window=14)
55
+ .stoch()
56
+ .iloc[-12:]
57
+ )
58
+ indicators["Stochastic Oscillator"] = {
59
+ date.strftime("%Y-%m-%d"): int(value)
60
+ for date, value in stochastic_series.dropna().to_dict().items()
61
+ }
62
+
63
+ macd = MACD(df["Close"])
64
+ macd_series = macd.macd().iloc[-12:]
65
+ indicators["MACD"] = {
66
+ date.strftime("%Y-%m-%d"): int(value)
67
+ for date, value in macd_series.to_dict().items()
68
+ }
69
+
70
+ macd_signal_series = macd.macd_signal().iloc[-12:]
71
+ indicators["MACD Signal"] = {
72
+ date.strftime("%Y-%m-%d"): int(value)
73
+ for date, value in macd_signal_series.to_dict().items()
74
+ }
75
+
76
+ vwap_series = volume_weighted_average_price(
77
+ df["High"], df["Low"], df["Close"], df["Volume"]
78
+ ).iloc[-12:]
79
+ indicators["vwap"] = {
80
+ date.strftime("%Y-%m-%d"): int(value)
81
+ for date, value in vwap_series.to_dict().items()
82
+ }
83
+
84
+ return {"stock_price": data.to_dict(orient="records"), "indicators": indicators}
85
+ except Exception as e:
86
+ return f"Error fetching price data: {str(e)}"
87
 
88
  @tool
89
  def get_current_time_in_timezone(timezone: str) -> str:
 
122
 
123
  agent = CodeAgent(
124
  model=model,
125
+ tools=[final_answer, get_stock_price], ## add your tools here (don't remove final answer)
126
  max_steps=6,
127
  verbosity_level=1,
128
  grammar=None,