SnowHA commited on
Commit
4353dc0
·
verified ·
1 Parent(s): 481515e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py CHANGED
@@ -44,6 +44,67 @@ def get_crypto_price(crypto: str, currency: str) -> str:
44
  except requests.RequestException as e:
45
  return f"Error fetching price: {e}"
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  @tool
48
  def get_current_time_in_timezone(timezone: str) -> str:
49
  """A tool that fetches the current local time in a specified timezone.
 
44
  except requests.RequestException as e:
45
  return f"Error fetching price: {e}"
46
 
47
+ @tool
48
+ def get_crypto_history(crypto: str, currency: str, days: int) -> str:
49
+ """A tool to fetch historical cryptocurrency prices from CoinGecko API.
50
+
51
+ Args:
52
+ crypto: The cryptocurrency symbol (e.g., 'bitcoin', 'ethereum').
53
+ currency: The fiat currency (e.g., 'usd', 'twd').
54
+ days: The number of past days to retrieve (e.g., 30 for the past month).
55
+
56
+ Returns:
57
+ A list of historical prices over the specified time period.
58
+ """
59
+ url = f"https://api.coingecko.com/api/v3/coins/{crypto}/market_chart?vs_currency={currency}&days={days}&interval=daily"
60
+ try:
61
+ response = requests.get(url)
62
+ response.raise_for_status()
63
+ data = response.json()
64
+ prices = data.get("prices", [])
65
+
66
+ if not prices:
67
+ return "No historical data available."
68
+
69
+ prices_str = "\n".join([f"Date: {pd.to_datetime(p[0], unit='ms').date()}, Price: {p[1]:.2f} {currency}" for p in prices])
70
+ return f"Historical prices for {crypto} in {currency} over {days} days:\n{prices_str}"
71
+
72
+ except requests.RequestException as e:
73
+ return f"Error fetching historical data: {e}"
74
+
75
+ @tool
76
+ def get_crypto_moving_average(crypto: str, currency: str, days: int, window: int) -> str:
77
+ """A tool to compute the moving average of cryptocurrency prices.
78
+
79
+ Args:
80
+ crypto: The cryptocurrency symbol (e.g., 'bitcoin', 'ethereum').
81
+ currency: The fiat currency (e.g., 'usd', 'twd').
82
+ days: The number of past days to retrieve.
83
+ window: The moving average window size (e.g., 7 for 7-day SMA).
84
+
85
+ Returns:
86
+ A list of moving average values.
87
+ """
88
+ url = f"https://api.coingecko.com/api/v3/coins/{crypto}/market_chart?vs_currency={currency}&days={days}&interval=daily"
89
+ try:
90
+ response = requests.get(url)
91
+ response.raise_for_status()
92
+ data = response.json()
93
+ prices = data.get("prices", [])
94
+
95
+ if not prices or len(prices) < window:
96
+ return "Not enough data for moving average calculation."
97
+
98
+ df = pd.DataFrame(prices, columns=["timestamp", "price"])
99
+ df["date"] = pd.to_datetime(df["timestamp"], unit="ms").dt.date
100
+ df["sma"] = df["price"].rolling(window=window).mean()
101
+
102
+ moving_avg_str = "\n".join([f"Date: {df['date'][i]}, SMA: {df['sma'][i]:.2f} {currency}" for i in range(len(df)) if not np.isnan(df['sma'][i])])
103
+ return f"Moving Average (window={window}) for {crypto} in {currency} over {days} days:\n{moving_avg_str}"
104
+
105
+ except requests.RequestException as e:
106
+ return f"Error fetching historical data: {e}"
107
+
108
  @tool
109
  def get_current_time_in_timezone(timezone: str) -> str:
110
  """A tool that fetches the current local time in a specified timezone.