import streamlit as st import pandas as pd import ta import requests import json def load_data(file_path): data = pd.read_csv(file_path) return data def calculate_indicators(data): if len(data) > 0: data['RSI'] = ta.momentum.rsi(data['Close'], window=14) data['Stochastic'] = ta.momentum.stoch(data['High'], data['Low'], data['Close'], k=14, d=3)['STOCHk_14_3_3'] macd = ta.trend.macd(data['Close'], window_slow=26, window_fast=12, window_sign=9) data['MACD'] = macd['MACD_12_26_9'] data['SMA'] = ta.trend.sma_indicator(data['Close'], window=50) data['EMA'] = ta.trend.ema_indicator(data['Close'], window=50) else: data['RSI'] = pd.Series([None]*len(data)) data['Stochastic'] = pd.Series([None]*len(data)) data['MACD'] = pd.Series([None]*len(data)) data['SMA'] = pd.Series([None]*len(data)) data['EMA'] = pd.Series([None]*len(data)) return data def generate_signals(data): buy_signal = None sell_signal = None if len(data) < 1: return buy_signal, sell_signal latest_data = data.iloc[-1] if latest_data['RSI'] < 30 and latest_data['Stochastic'] < 20: buy_signal = (latest_data.name, latest_data['Close'], latest_data['Close'] * 0.95, "High Risk", 3) elif latest_data['RSI'] > 70 and latest_data['Stochastic'] > 80: sell_signal = (latest_data.name, latest_data['Close'], latest_data['Close'] * 1.05, "High Risk", 3) return buy_signal, sell_signal def get_fear_and_greed_index(): response = requests.get("https://api.alternative.me/fng/?limit=1") if response.status_code == 200: return response.json()["data"][0]["value"] else: return None def get_crypto_data_from_coinmarketcap(api_key, crypto_symbol): url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest" parameters = {'symbol': crypto_symbol, 'convert': 'USD'} headers = {'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': api_key} response = requests.get(url, headers=headers, params=parameters) data = response.json() return data['data'][crypto_symbol]['quote']['USD'] def get_crypto_news(api_key, crypto_symbol): url = f"https://newsapi.org/v2/everything?q={crypto_symbol}&apiKey={api_key}" response = requests.get(url) return response.json().get('articles', []) def analyze_news_sentiment(news): for article in news: article['sentiment'] = 'Neutral' # Placeholder for sentiment analysis return news def load_api_keys(): with open("api_keys.json", "r") as file: return json.load(file) def main(): st.title("Cryptocurrency Dashboard") menu = ["Home", "News"] choice = st.sidebar.selectbox("Menu", menu) language = st.sidebar.selectbox("Language", ["English", "Farsi"]) if choice == "Home": if language == "English": st.subheader("Cryptocurrency Data") else: st.subheader("داده‌های ارز دیجیتال") data_file = st.file_uploader("Upload CSV", type=["csv"]) if data_file is not None: data = load_data(data_file) data = calculate_indicators(data) st.dataframe(data) buy_signal, sell_signal = generate_signals(data) if buy_signal: st.success(f"Buy Signal: {buy_signal}") if sell_signal: st.error(f"Sell Signal: {sell_signal}") elif choice == "News": if language == "English": st.subheader("Cryptocurrency News") else: st.subheader("اخبار ارز دیجیتال") crypto_symbol = st.selectbox("Cryptocurrency Symbol", ["BTC", "ETH", "LTC", "BCH"]) api_keys = load_api_keys() if 'newsapi_key' in api_keys and api_keys['newsapi_key']: news = get_crypto_news(api_keys['newsapi_key'], crypto_symbol) news = analyze_news_sentiment(news) sort_by = st.radio("Sort News By", ("publishedAt", "relevancy", "popularity"), index=0) news = sorted(news, key=lambda x: x[sort_by]) if language == "English": st.subheader(f"News for {crypto_symbol}") else: st.subheader(f"اخبار برای {crypto_symbol}") page = st.slider("Select page", min_value=1, max_value=(len(news) // 5) + 1) news_to_display = news[(page - 1) * 5: page * 5] for article in news_to_display: st.write(f"Title: {article['title']}") st.write(f"Description: {article['description']}") st.write(f"Sentiment: {article['sentiment']}") st.write(f"Published At: {article['publishedAt']}") st.write(f"Read more: [Link]({article['url']})") else: if language == "English": st.warning("API key for NewsAPI is not set. Please contact the admin.") else: st.warning("کلید API برای NewsAPI تنظیم نشده است. لطفاً با مدیر تماس بگیرید.") if __name__ == '__main__': main()