Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,37 +4,71 @@ import matplotlib.pyplot as plt
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def fetch_data():
|
| 8 |
end_date = datetime.now()
|
| 9 |
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'))
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
def normalize_data(data):
|
| 15 |
return (data - data.min()) / (data.max() - data.min())
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
print("Error: Input data is empty.")
|
| 21 |
-
return None
|
| 22 |
-
|
| 23 |
-
# 데이터 정규화 및 차이 계산
|
| 24 |
interest_rate_normalized = normalize_data(interest_rate_data)
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# 그래프 생성
|
| 29 |
plt.figure(figsize=(14, 7))
|
| 30 |
-
plt.plot(diff_normalized, color='green', label='Normalized Difference (
|
| 31 |
-
plt.axhline(y=0.5, color='black', linestyle='--')
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
plt.xlabel('Date')
|
| 34 |
plt.ylabel('Normalized Value')
|
| 35 |
plt.legend()
|
| 36 |
plt.grid(True)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def run_app():
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
import yfinance as yf
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from datetime import datetime, timedelta
|
| 11 |
+
|
| 12 |
+
# 데이터 가져오기
|
| 13 |
def fetch_data():
|
| 14 |
end_date = datetime.now()
|
| 15 |
start_date = end_date - timedelta(days=5 * 365)
|
| 16 |
+
# 미국의 10년물 국채 수익률 데이터 가져오기
|
| 17 |
us_interest_rate_data = yf.download('^TNX', start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d'))
|
| 18 |
+
us_interest_rate_daily = us_interest_rate_data['Close']
|
| 19 |
+
|
| 20 |
+
# 비트코인 가격 데이터 가져오기
|
| 21 |
+
btc_data = yf.download('BTC-USD', start=start_date.strftime('%Y-%m-%d'), end=end_date.strftime('%Y-%m-%d'))
|
| 22 |
+
btc_daily = btc_data['Close']
|
| 23 |
+
|
| 24 |
+
return us_interest_rate_daily, btc_daily
|
| 25 |
|
| 26 |
+
# 데이터 정규화
|
| 27 |
def normalize_data(data):
|
| 28 |
return (data - data.min()) / (data.max() - data.min())
|
| 29 |
|
| 30 |
+
# 데이터 분석 및 그래프 생성
|
| 31 |
+
def plot_data(interest_rate_data, btc_prices):
|
| 32 |
+
# 데이터 정규화
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
interest_rate_normalized = normalize_data(interest_rate_data)
|
| 34 |
+
btc_normalized = normalize_data(btc_prices)
|
| 35 |
+
|
| 36 |
+
# 두 데이터의 차이 계산 및 정규화
|
| 37 |
+
diff_normalized = normalize_data(btc_normalized - interest_rate_normalized)
|
| 38 |
|
| 39 |
# 그래프 생성
|
| 40 |
plt.figure(figsize=(14, 7))
|
| 41 |
+
plt.plot(diff_normalized, color='green', label='Normalized Difference (Bitcoin Price - US 10-Year Treasury Yield)')
|
| 42 |
+
plt.axhline(y=0.5, color='black', linestyle='--', label='Normalized Value: 0.5')
|
| 43 |
+
|
| 44 |
+
# 배경색 설정
|
| 45 |
+
plt.axhspan(0.0, 0.2, color='blue', alpha=0.3)
|
| 46 |
+
plt.axhspan(0.2, 0.4, color='lightblue', alpha=0.3)
|
| 47 |
+
plt.axhspan(0.4, 0.5, color='skyblue', alpha=0.3)
|
| 48 |
+
plt.axhspan(0.5, 0.6, color='yellow', alpha=0.3)
|
| 49 |
+
plt.axhspan(0.6, 0.8, color='orange', alpha=0.3)
|
| 50 |
+
plt.axhspan(0.8, 1.0, color='red', alpha=0.3)
|
| 51 |
+
|
| 52 |
+
# 현재 정규화된 값 표시
|
| 53 |
+
current_value = diff_normalized.iloc[-1]
|
| 54 |
+
plt.text(len(diff_normalized) - 1, current_value, f'Normalized Value: {current_value:.3f}', verticalalignment='bottom', horizontalalignment='right')
|
| 55 |
+
|
| 56 |
+
plt.title('Difference between Bitcoin Price and US 10-Year Treasury Yield')
|
| 57 |
plt.xlabel('Date')
|
| 58 |
plt.ylabel('Normalized Value')
|
| 59 |
plt.legend()
|
| 60 |
plt.grid(True)
|
| 61 |
+
|
| 62 |
+
# 현재 Figure 객체를 반환하고 표시하지 않음
|
| 63 |
+
fig = plt.gcf() # Get the current figure before plt.show()
|
| 64 |
+
plt.close() # Close the plot to free up memory
|
| 65 |
+
return fig
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
interest_rate_data, btc_prices = fetch_data()
|
| 69 |
+
fig = plot_data(interest_rate_data, btc_prices)
|
| 70 |
+
# 여기서 fig 객체를 다른 라이브러리에 전달하거나, 다른 방식으로 활용할 수 있습니다.
|
| 71 |
+
|
| 72 |
|
| 73 |
|
| 74 |
def run_app():
|