Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines45678910111213141516171819201112212223242526272829171819202122233031323325263435363738394030313241424344454647484950515253545556575859603761626364656667686970717273743 unmodified linesfrom datetime import datetime, timedeltaimport gradio as gr
import yfinance as yfimport pandas as pdimport matplotlib.pyplot as pltfrom datetime import datetime, timedelta
# ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐdef fetch_data(): end_date = datetime.now() start_date = end_date - timedelta(days=5 * 365) # ๋ฏธ๊ตญ์ 10๋
๋ฌผ ๊ตญ์ฑ ์์ต๋ฅ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ us_interest_rate_data = yf.download('^TNX', start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d')) us_interest_rate_daily = us_interest_rate_data['Close']
# ๋นํธ์ฝ์ธ ๊ฐ๊ฒฉ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ bnb_data = yf.download('BNB-USD', start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d')) return us_interest_rate_data['Close'], bnb_data['Close'] btc_data = yf.download('BTC-USD', start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d')) btc_daily = btc_data['Close']
return us_interest_rate_daily, btc_daily
# ๋ฐ์ดํฐ ์ ๊ทํdef normalize_data(data): return (data - data.min()) / (data.max() - data.min())
def plot_data(interest_rate_data, bnb_prices): # ๋ฐ์ดํฐ ๊ฒ์ฆ if interest_rate_data.empty or bnb_prices.empty: print("Error: Input data is empty.") return None
# ๋ฐ์ดํฐ ์ ๊ทํ ๋ฐ ์ฐจ์ด ๊ณ์ฐ# ๋ฐ์ดํฐ ๋ถ์ ๋ฐ ๊ทธ๋ํ ์์ฑdef plot_data(interest_rate_data, btc_prices): # ๋ฐ์ดํฐ ์ ๊ทํ interest_rate_normalized = normalize_data(interest_rate_data) bnb_normalized = normalize_data(bnb_prices) diff_normalized = normalize_data(bnb_normalized - interest_rate_normalized) btc_normalized = normalize_data(btc_prices)
# ๋ ๋ฐ์ดํฐ์ ์ฐจ์ด ๊ณ์ฐ ๋ฐ ์ ๊ทํ diff_normalized = normalize_data(btc_normalized - interest_rate_normalized)
# ๊ทธ๋ํ ์์ฑ plt.figure(figsize=(14, 7)) plt.plot(diff_normalized, color='green', label='Normalized Difference (BNB Price - US 10-Year Treasury Yield)') plt.axhline(y=0.5, color='black', linestyle='--') plt.title('Difference between Binance Coin Price and US 10-Year Treasury Yield') plt.plot(diff_normalized, color='green', label='Normalized Difference (Bitcoin Price - US 10-Year Treasury Yield)') plt.axhline(y=0.5, color='black', linestyle='--', label='Normalized Value: 0.5')
# ๋ฐฐ๊ฒฝ์ ์ค์ plt.axhspan(0.0, 0.2, color='blue', alpha=0.3) plt.axhspan(0.2, 0.4, color='lightblue', alpha=0.3) plt.axhspan(0.4, 0.5, color='skyblue', alpha=0.3) plt.axhspan(0.5, 0.6, color='yellow', alpha=0.3) plt.axhspan(0.6, 0.8, color='orange', alpha=0.3) plt.axhspan(0.8, 1.0, color='red', alpha=0.3)
# ํ์ฌ ์ ๊ทํ๋ ๊ฐ ํ์ current_value = diff_normalized.iloc[-1] plt.text(len(diff_normalized) - 1, current_value, f'Normalized Value: {current_value:.3f}', verticalalignment='bottom', horizontalalignment='right')
plt.title('Difference between Bitcoin Price and US 10-Year Treasury Yield') plt.xlabel('Date') plt.ylabel('Normalized Value') plt.legend() plt.grid(True) return plt.gcf() # ํ์ฌ Figure ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ ํ์ํ์ง ์์ fig = plt.gcf() # Get the current figure before plt.show() plt.close() # Close the plot to free up memory return fig
if __name__ == "__main__": interest_rate_data, btc_prices = fetch_data() fig = plot_data(interest_rate_data, btc_prices) # ์ฌ๊ธฐ์ fig ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ๋ฌํ๊ฑฐ๋, ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ํ์ฉํ ์ ์์ต๋๋ค.
def run_app():app.py+49-15