-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefi_analysis.py
141 lines (116 loc) · 4.34 KB
/
defi_analysis.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import yfinance as yf
import streamlit as st
# Data Collection and Preprocessing
def fetch_crypto_data(crypto, start_date, end_date):
try:
ticker = yf.Ticker(crypto)
data = ticker.history(start=start_date, end=end_date)
return data['Close']
except Exception as e:
st.error(f"Error fetching data for {crypto}: {str(e)}")
return pd.Series()
def preprocess_data(eth_data, sol_data, matic_data):
df = pd.DataFrame({
'ETH': eth_data,
'SOL': sol_data,
'MATIC': matic_data
})
df['Date'] = df.index
df = df.reset_index(drop=True)
return df
# Data Analysis
def calculate_returns(df):
for coin in ['ETH', 'SOL', 'MATIC']:
df[f'{coin}_Returns'] = df[coin].pct_change()
return df
def calculate_volatility(df):
for coin in ['ETH', 'SOL', 'MATIC']:
df[f'{coin}_Volatility'] = df[f'{coin}_Returns'].rolling(window=30).std() * np.sqrt(365)
return df
def calculate_correlation(df):
return df[['ETH_Returns', 'SOL_Returns', 'MATIC_Returns']].corr()
# Predictive Modeling
def create_features(df):
for coin in ['ETH', 'SOL', 'MATIC']:
df[f'{coin}_MA7'] = df[coin].rolling(window=7).mean()
df[f'{coin}_MA30'] = df[coin].rolling(window=30).mean()
return df.dropna()
def train_model(df, coin):
features = [f'{coin}_MA7', f'{coin}_MA30', f'{coin}_Volatility']
X = df[features]
y = df[coin]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
return model, X_test, y_test
# Visualization
def plot_price_trends(df):
plt.figure(figsize=(12, 6))
for coin in ['ETH', 'SOL', 'MATIC']:
plt.plot(df['Date'], df[coin], label=coin)
plt.title('Price Trends')
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.legend()
return plt
def plot_correlation_heatmap(corr):
plt.figure(figsize=(10, 8))
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
return plt
# Main Analysis Function
def run_analysis():
# Fetch data
start_date = '2020-01-01'
end_date = '2023-12-31'
eth_data = fetch_crypto_data('ETH-USD', start_date, end_date)
sol_data = fetch_crypto_data('SOL-USD', start_date, end_date)
matic_data = fetch_crypto_data('MATIC-USD', start_date, end_date)
# Check if data is empty
if eth_data.empty or sol_data.empty or matic_data.empty:
st.error("Failed to fetch data for one or more cryptocurrencies.")
return None, None, None, None, None
# Preprocess data
df = preprocess_data(eth_data, sol_data, matic_data)
df = calculate_returns(df)
df = calculate_volatility(df)
df = create_features(df)
# Analysis
correlation = calculate_correlation(df)
# Modeling
eth_model, X_test, y_test = train_model(df, 'ETH')
sol_model, _, _ = train_model(df, 'SOL')
matic_model, _, _ = train_model(df, 'MATIC')
return df, correlation, eth_model, sol_model, matic_model
# Streamlit Dashboard
def run_dashboard():
st.title('DeFi Staking Analysis Dashboard')
result = run_analysis()
if result[0] is None:
st.error("Analysis failed due to data fetching issues.")
return
df, correlation, eth_model, sol_model, matic_model = result
st.header('Price Trends')
st.pyplot(plot_price_trends(df))
st.header('Correlation Analysis')
st.pyplot(plot_correlation_heatmap(correlation))
st.header('Volatility Comparison')
st.line_chart(df[['ETH_Volatility', 'SOL_Volatility', 'MATIC_Volatility']])
st.header('Predictive Modeling')
coin = st.selectbox('Select Cryptocurrency', ['ETH', 'SOL', 'MATIC'])
model = {'ETH': eth_model, 'SOL': sol_model, 'MATIC': matic_model}[coin]
features = [f'{coin}_MA7', f'{coin}_MA30', f'{coin}_Volatility']
user_input = {}
for feature in features:
user_input[feature] = st.number_input(f'Enter {feature}')
if st.button('Predict Price'):
prediction = model.predict(pd.DataFrame(user_input, index=[0]))
st.write(f'Predicted {coin} Price: ${prediction[0]:.2f}')
if __name__ == "__main__":
run_dashboard()