from pycoingecko import CoinGeckoAPI

from datetime import datetime, timedelta

import pandas as pd

import numpy as np

# Initialize Coingecko API

cg = CoinGeckoAPI()

# Get current price and market data for BNB

bnb_data = cg.get_price(ids='binancecoin', vs_currencies='usd')

bnb_market = cg.get_coin_by_id(id='binancecoin', localization=False, tickers=False, market_data=True, community_data=False, developer_data=False, sparkline=False)

current_price = bnb_data['binancecoin']['usd']

market_cap = bnb_market['market_data']['market_cap']['usd']

volume_24h = bnb_market['market_data']['total_volume']['usd']

price_change_24h = bnb_market['market_data']['price_change_percentage_24h']

price_change_7d = bnb_market['market_data']['price_change_percentage_7d_in_currency']['usd']

price_change_30d = bnb_market['market_data']['price_change_percentage_30d_in_currency']['usd']

# Get historical data for last 30 days

end_date = datetime.now()

start_date = end_date - timedelta(days=30)

historical = cg.get_coin_market_chart_range_by_id(id='binancecoin', vs_currency='usd', from_timestamp=int(start_date.timestamp()), to_timestamp=int(end_date.timestamp()))

df = pd.DataFrame(historical['prices'], columns=['timestamp', 'price'])

df['date'] = pd.to_datetime(df['timestamp'], unit='ms')

df = df.set_index('date')

# Simple moving averages

df['SMA_7'] = df['price'].rolling(window=7).mean()

df['SMA_30'] = df['price'].rolling(window=30).mean()

# RSI calculation

def calculate_rsi(prices, window=14):

delta = prices.diff()

gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()

loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()

rs = gain / loss

rsi = 100 - (100 / (1 + rs))

return rsi

df['RSI'] = calculate_rsi(df['price'])

latest = df.iloc[-1]

current_sma7 = latest['SMA_7']

current_sma30 = latest['SMA_30']

current_rsi = latest['RSI']

print(f"Current Price: ${current_price}")

print(f"Market Cap: ${market_cap:,.0f}")

print(f"24h Volume: ${volume_24h:,.0f}")

print(f"24h Change: {price_change_24h:.2f}%")

print(f"7d Change: {price_change_7d:.2f}%")

print(f"30d Change: {price_change_30d:.2f}%")

print(f"SMA 7: ${current_sma7:.2f}")

print(f"SMA 30: ${current_sma30:.2f}")

print(f"RSI: {current_rsi:.2f}")

print("\nRecent Prices (last 5 days):")

print(df.tail(5)[['price']])