Spaces:
Runtime error
Runtime error
File size: 2,891 Bytes
c18a3e3 4594ff4 f2f89a2 c9ee9c6 9b2256d c9ee9c6 c2bc125 c18a3e3 c2bc125 c18a3e3 c2bc125 c18a3e3 c2bc125 c18a3e3 c2bc125 c18a3e3 c2bc125 ba68605 c18a3e3 c2bc125 c18a3e3 3241355 c2bc125 9fa02b2 f2f89a2 9fa02b2 c58b9ff 9fa02b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import gradio as gr
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
app = Flask(__name__)
CORS(app, supports_credentials=True, resources={r"/*": {"origins": "*"}})
# ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
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']
# ๋นํธ์ฝ์ธ ๊ฐ๊ฒฉ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
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, btc_prices):
# ๋ฐ์ดํฐ ์ ๊ทํ
interest_rate_normalized = normalize_data(interest_rate_data)
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 (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)
# ํ์ฌ Figure ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ ํ์ํ์ง ์์
fig = plt.gcf() # Get the current figure before plt.show()
plt.close() # Close the plot to free up memory
return fig
def run_app():
interest_rate_data, btc_prices = fetch_data()
return plot_data(interest_rate_data, btc_prices)
iface = gr.Interface(fn=run_app, inputs=[], outputs="plot", title="Financial Data Comparison", live=True)
if __name__ == "__main__":
iface.launch(share=True)
|