Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- crypto_tools.py +57 -0
- news_tools.py +14 -0
- sentiment_tools.py +15 -0
crypto_tools.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.tools import tool
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
class CryptoTools:
|
7 |
+
@tool("Get Cryptocurrency Price")
|
8 |
+
def get_crypto_price(crypto):
|
9 |
+
"""Fetches the current price of a given cryptocurrency."""
|
10 |
+
url = f"https://api.coingecko.com/api/v3/simple/price?ids={crypto}&vs_currencies=usd"
|
11 |
+
response = requests.get(url)
|
12 |
+
if response.status_code == 200:
|
13 |
+
data = response.json()
|
14 |
+
return data[crypto]['usd']
|
15 |
+
else:
|
16 |
+
return "Error fetching price data"
|
17 |
+
|
18 |
+
@tool("Get Market Cap")
|
19 |
+
def get_market_cap(crypto):
|
20 |
+
"""Fetches the current market cap of a given cryptocurrency."""
|
21 |
+
url = f"https://api.coingecko.com/api/v3/coins/{crypto}"
|
22 |
+
response = requests.get(url)
|
23 |
+
if response.status_code == 200:
|
24 |
+
data = response.json()
|
25 |
+
return data['market_data']['market_cap']['usd']
|
26 |
+
else:
|
27 |
+
return "Error fetching market cap data"
|
28 |
+
|
29 |
+
@tool("Calculate RSI")
|
30 |
+
def calculate_rsi(crypto, period=14):
|
31 |
+
"""Calculates the Relative Strength Index (RSI) for a given cryptocurrency."""
|
32 |
+
url = f"https://api.coingecko.com/api/v3/coins/{crypto}/market_chart?vs_currency=usd&days={period}"
|
33 |
+
response = requests.get(url)
|
34 |
+
if response.status_code == 200:
|
35 |
+
data = response.json()
|
36 |
+
prices = [price[1] for price in data['prices']]
|
37 |
+
df = pd.DataFrame(prices, columns=['price'])
|
38 |
+
delta = df['price'].diff()
|
39 |
+
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
|
40 |
+
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
|
41 |
+
rs = gain / loss
|
42 |
+
rsi = 100 - (100 / (1 + rs))
|
43 |
+
return rsi.iloc[-1]
|
44 |
+
else:
|
45 |
+
return "Error calculating RSI"
|
46 |
+
|
47 |
+
@tool("Calculate Moving Average")
|
48 |
+
def calculate_moving_average(crypto, days):
|
49 |
+
"""Calculates the moving average for a given cryptocurrency over a specified number of days."""
|
50 |
+
url = f"https://api.coingecko.com/api/v3/coins/{crypto}/market_chart?vs_currency=usd&days={days}"
|
51 |
+
response = requests.get(url)
|
52 |
+
if response.status_code == 200:
|
53 |
+
data = response.json()
|
54 |
+
prices = [price[1] for price in data['prices']]
|
55 |
+
return np.mean(prices)
|
56 |
+
else:
|
57 |
+
return "Error calculating moving average"
|
news_tools.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.tools import tool
|
2 |
+
import requests
|
3 |
+
|
4 |
+
class NewsTools:
|
5 |
+
@tool("Search Crypto News")
|
6 |
+
def search_crypto_news(query):
|
7 |
+
"""Searches for cryptocurrency news articles based on a given query."""
|
8 |
+
url = f"https://cryptonews-api.com/api/v1/category?section=general&items=10&token=YOUR_API_KEY&q={query}"
|
9 |
+
response = requests.get(url)
|
10 |
+
if response.status_code == 200:
|
11 |
+
articles = response.json()['data']
|
12 |
+
return [{'title': article['title'], 'source': article['source_name'], 'url': article['news_url']} for article in articles]
|
13 |
+
else:
|
14 |
+
return "Error fetching news data"
|
sentiment_tools.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.tools import tool
|
2 |
+
from textblob import TextBlob
|
3 |
+
|
4 |
+
class SentimentTools:
|
5 |
+
@tool("Analyze Sentiment")
|
6 |
+
def analyze_sentiment(text):
|
7 |
+
"""Analyzes the sentiment of a given text."""
|
8 |
+
blob = TextBlob(text)
|
9 |
+
sentiment = blob.sentiment.polarity
|
10 |
+
if sentiment > 0:
|
11 |
+
return "Positive"
|
12 |
+
elif sentiment < 0:
|
13 |
+
return "Negative"
|
14 |
+
else:
|
15 |
+
return "Neutral"
|