MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / build_tool

Function build_tool

bmtools/tools/stock/api.py:8–105  ·  view source on GitHub ↗
(config)

Source from the content-addressed store, hash-verified

6from ..tool import Tool
7
8def build_tool(config) -> Tool:
9 tool = Tool(
10 "Stock Info",
11 "Look up stock information",
12 name_for_model="Stock",
13 description_for_model="Plugin for look up stock information",
14 logo_url="https://your-app-url.com/.well-known/logo.png",
15 contact_email="hello@contact.com",
16 legal_info_url="hello@legal.com"
17 )
18
19 functions = ['TIME_SERIES_INTRADAY', 'TIME_SERIES_INTRADAY_EXTENDED','TIME_SERIES_DAILY', 'TIME_SERIES_DAILY_ADJUSTED']
20 types = ['open', 'close', 'high', 'low']
21
22 KEY = config["subscription_key"]
23 BASE_URL = 'https://www.alphavantage.co/query?'
24
25 def get_json_data(function, symbol, interval = '5min', adjusted='true', outputsize='compact', datatype='json'):
26 url = BASE_URL + 'function=' + function + '&symbol=' + symbol + '&apikey=' + KEY
27 r = requests.get(url)
28 data = json.loads(r.text)
29 return data
30
31 @tool.get("/get_today_date")
32 def get_today_date():
33 '''Get today's date
34 '''
35 today = date.today()
36 return today.strftime("%Y-%m-%d")
37
38 @tool.get('/add_date')
39 def add_date(date : str, days : int):
40 '''Add days to a date. Date should be pass as 'yyyy-mm-dd'.
41 '''
42 date = datetime.strptime(date, "%Y-%m-%d")
43 new_date = date + timedelta(days=days)
44 return new_date.strftime("%Y-%m-%d")
45
46 @tool.get('/get_daily_prices')
47 def get_daily_prices(symbol : str, date : str = ''):
48 '''Get the stock price of an entity in the stock market. Date should be pass as 'yyyy-mm-dd'.
49 '''
50 if "," in symbol:
51 symbol, date = symbol.split(",")
52 if date.strip() == "":
53 return "Please specify a date and try again. You can you get_today_date to up-to-date time information."
54 data = get_json_data('TIME_SERIES_DAILY_ADJUSTED', symbol)
55 #print(data.keys())
56 time_series = data["Time Series (Daily)"]
57 final_time = ''
58 print(time_series)
59 # 查找最接近查询日期的数据
60 for timestamp, daily_data in time_series.items():
61 print(timestamp)
62 if timestamp == date:
63 open_price = daily_data["1. open"]
64 high_price = daily_data["2. high"]
65 low_price = daily_data["3. low"]

Callers

nothing calls this directly

Calls 1

ToolClass · 0.50

Tested by

no test coverage detected