missbaj commited on
Commit
c4253c7
·
verified ·
1 Parent(s): 9c78303
Files changed (1) hide show
  1. app.py +45 -35
app.py CHANGED
@@ -5,51 +5,63 @@ import cryptocompare
5
  from tradingview_ta import TA_Handler, Interval
6
  import gradio as gr
7
 
8
- # Fetch BTC data from multiple sources
9
  def get_coin_gecko_price():
10
- response = requests.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd")
11
- data = response.json()
12
- return data['bitcoin']['usd']
 
 
 
13
 
14
  def get_yfinance_price():
15
- btc_data = yf.Ticker("BTC-USD")
16
- current_price = btc_data.history(period="1d")['Close'][0]
17
- return current_price
 
 
 
18
 
19
  def get_cryptocompare_price():
20
- cryptocompare_price = cryptocompare.get_price('BTC', currency='USD')
21
- return cryptocompare_price['BTC']['USD']
 
 
 
22
 
23
  def get_tradingview_analysis():
24
- handler = TA_Handler(
25
- symbol="BTCUSD",
26
- screener="crypto",
27
- exchange="BINANCE",
28
- interval=Interval.INTERVAL_1_HOUR
29
- )
30
- analysis = handler.get_analysis()
31
- return analysis.summary["RECOMMENDATION"]
 
 
 
32
 
33
- # Calculate technical indicators using pandas-ta
34
  def calculate_technical_indicators():
35
- btc_data = yf.download("BTC-USD", period="30d", interval="1h")
36
- close_prices = btc_data['Close']
37
- ma_50 = ta.sma(close_prices, length=50)
38
- ma_200 = ta.sma(close_prices, length=200)
39
- rsi = ta.rsi(close_prices, length=14)
40
- macd = ta.macd(close_prices)
41
- return {
42
- "MA 50": ma_50.iloc[-1],
43
- "MA 200": ma_200.iloc[-1],
44
- "RSI": rsi.iloc[-1],
45
- "MACD": macd['MACD_12_26_9'].iloc[-1],
46
- "Signal Line": macd['MACDs_12_26_9'].iloc[-1]
47
- }
 
 
 
48
 
49
- # Gradio interface for custom prompts
50
  def analyze_btc(prompt):
51
  prompt_lower = prompt.lower()
52
-
53
  if "price" in prompt_lower:
54
  price_yf = get_yfinance_price()
55
  price_cg = get_coin_gecko_price()
@@ -71,7 +83,6 @@ def analyze_btc(prompt):
71
  else:
72
  return "I'm not sure what you're asking. Try asking about price, RSI, MACD, or trend."
73
 
74
- # Gradio interface setup
75
  interface = gr.Interface(
76
  fn=analyze_btc,
77
  inputs="text",
@@ -80,5 +91,4 @@ interface = gr.Interface(
80
  description="Ask questions about BTC's real-time data (from CoinGecko, Yahoo Finance, CryptoCompare, TradingView)"
81
  )
82
 
83
- # Launch the Gradio app
84
  interface.launch()
 
5
  from tradingview_ta import TA_Handler, Interval
6
  import gradio as gr
7
 
 
8
  def get_coin_gecko_price():
9
+ try:
10
+ response = requests.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd")
11
+ data = response.json()
12
+ return data['bitcoin']['usd']
13
+ except Exception as e:
14
+ return f"Error fetching CoinGecko data: {str(e)}"
15
 
16
  def get_yfinance_price():
17
+ try:
18
+ btc_data = yf.Ticker("BTC-USD")
19
+ current_price = btc_data.history(period="1d")['Close'][0]
20
+ return current_price
21
+ except Exception as e:
22
+ return f"Error fetching Yahoo Finance data: {str(e)}"
23
 
24
  def get_cryptocompare_price():
25
+ try:
26
+ cryptocompare_price = cryptocompare.get_price('BTC', currency='USD')
27
+ return cryptocompare_price['BTC']['USD']
28
+ except Exception as e:
29
+ return f"Error fetching CryptoCompare data: {str(e)}"
30
 
31
  def get_tradingview_analysis():
32
+ try:
33
+ handler = TA_Handler(
34
+ symbol="BTCUSD",
35
+ screener="crypto",
36
+ exchange="BINANCE",
37
+ interval=Interval.INTERVAL_1_HOUR
38
+ )
39
+ analysis = handler.get_analysis()
40
+ return analysis.summary["RECOMMENDATION"]
41
+ except Exception as e:
42
+ return f"Error fetching TradingView data: {str(e)}"
43
 
 
44
  def calculate_technical_indicators():
45
+ try:
46
+ btc_data = yf.download("BTC-USD", period="30d", interval="1h")
47
+ close_prices = btc_data['Close']
48
+ ma_50 = ta.sma(close_prices, length=50)
49
+ ma_200 = ta.sma(close_prices, length=200)
50
+ rsi = ta.rsi(close_prices, length=14)
51
+ macd = ta.macd(close_prices)
52
+ return {
53
+ "MA 50": ma_50.iloc[-1],
54
+ "MA 200": ma_200.iloc[-1],
55
+ "RSI": rsi.iloc[-1],
56
+ "MACD": macd['MACD_12_26_9'].iloc[-1],
57
+ "Signal Line": macd['MACDs_12_26_9'].iloc[-1]
58
+ }
59
+ except Exception as e:
60
+ return f"Error calculating technical indicators: {str(e)}"
61
 
 
62
  def analyze_btc(prompt):
63
  prompt_lower = prompt.lower()
64
+
65
  if "price" in prompt_lower:
66
  price_yf = get_yfinance_price()
67
  price_cg = get_coin_gecko_price()
 
83
  else:
84
  return "I'm not sure what you're asking. Try asking about price, RSI, MACD, or trend."
85
 
 
86
  interface = gr.Interface(
87
  fn=analyze_btc,
88
  inputs="text",
 
91
  description="Ask questions about BTC's real-time data (from CoinGecko, Yahoo Finance, CryptoCompare, TradingView)"
92
  )
93
 
 
94
  interface.launch()