Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import pickle
|
| 2 |
-
import gradio as gr
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import ccxt
|
| 5 |
import pandas as pd
|
| 6 |
import numpy as np
|
| 7 |
from sklearn.preprocessing import MinMaxScaler
|
|
@@ -9,35 +7,13 @@ from sklearn.impute import SimpleImputer
|
|
| 9 |
from scipy import stats
|
| 10 |
import yfinance as yf
|
| 11 |
|
| 12 |
-
# Fetch data
|
| 13 |
-
def fetch_binance_data(symbol, timeframe, limit=2000):
|
| 14 |
-
binance = ccxt.binance()
|
| 15 |
-
ohlcv = binance.fetch_ohlcv(symbol, timeframe, limit=limit)
|
| 16 |
-
df = pd.DataFrame(ohlcv, columns=['timestamp','open','high','low','close','volume'])
|
| 17 |
-
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
|
| 18 |
-
return df
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# # Yahoo uses e.g. "BCH-USD" for BCH/USDT
|
| 27 |
-
# ticker = pair.replace("/USDT", "-USD")
|
| 28 |
-
# df = yf.download(ticker, period=period, interval=interval)
|
| 29 |
-
# df = df.reset_index().rename(columns={
|
| 30 |
-
# 'Datetime': 'timestamp',
|
| 31 |
-
# 'Date': 'timestamp',
|
| 32 |
-
# 'Open': 'open',
|
| 33 |
-
# 'High': 'high',
|
| 34 |
-
# 'Low': 'low',
|
| 35 |
-
# 'Close': 'close',
|
| 36 |
-
# 'Volume': 'volume'
|
| 37 |
-
# })
|
| 38 |
-
# # ensure we have a timestamp column in datetime
|
| 39 |
-
# df['timestamp'] = pd.to_datetime(df['timestamp'])
|
| 40 |
-
# return df
|
| 41 |
|
| 42 |
def fetch_yfinance_data(pair: str, period: str, interval: str) -> pd.DataFrame:
|
| 43 |
"""
|
|
@@ -47,7 +23,7 @@ def fetch_yfinance_data(pair: str, period: str, interval: str) -> pd.DataFrame:
|
|
| 47 |
"""
|
| 48 |
ticker = pair.replace("/USDT", "-USD")
|
| 49 |
df = yf.download(ticker, period=period, interval=interval)
|
| 50 |
-
|
| 51 |
# bring the DateTimeIndex into a column, whatever its name was
|
| 52 |
df = df.reset_index()
|
| 53 |
df.rename(columns={df.columns[0]: 'timestamp'}, inplace=True)
|
|
@@ -132,8 +108,8 @@ def generate_advanced_features(d, other_data=None):
|
|
| 132 |
|
| 133 |
if other_data is not None:
|
| 134 |
other_data = other_data.loc[:, ~other_data.columns.duplicated()]
|
| 135 |
-
d['relative_strength'] =
|
| 136 |
-
d['relative_strength_1'] =
|
| 137 |
return d.iloc[:,1:].values
|
| 138 |
|
| 139 |
|
|
@@ -146,8 +122,9 @@ def create_features_and_labels_with_advanced_features(btc, eth):
|
|
| 146 |
# df['future'] = df['close'].rolling(window=5).mean().shift(-1)
|
| 147 |
# df['trend'] = (df['future'] > df['close']).astype(int)
|
| 148 |
# labels = df['trend'].dropna().values
|
|
|
|
| 149 |
features = np.vstack((btc_features, eth_features))
|
| 150 |
-
return features,
|
| 151 |
|
| 152 |
def get_data_predict(
|
| 153 |
btc_ori: pd.DataFrame,
|
|
@@ -159,13 +136,12 @@ def get_data_predict(
|
|
| 159 |
limit: int = 50
|
| 160 |
):
|
| 161 |
period = f'{limit}d' # last N days
|
| 162 |
-
# fetch entirely from yfinance
|
| 163 |
btc_data_ = fetch_yfinance_data('BTC/USDT', period, timeframe)
|
| 164 |
bch_data_ = fetch_yfinance_data(symbol, period, timeframe)
|
| 165 |
|
| 166 |
btc_data_ = remove_outliers(btc_data_, epsilon)
|
| 167 |
bch_data_ = remove_outliers(bch_data_, epsilon)
|
| 168 |
-
|
| 169 |
|
| 170 |
if normalized:
|
| 171 |
# merge with ori if you still want to include historical yf data
|
|
@@ -179,13 +155,12 @@ def get_data_predict(
|
|
| 179 |
return btc_data_, bch_data_, None
|
| 180 |
|
| 181 |
|
| 182 |
-
|
| 183 |
def predictions(model, X1, X2, name, n_steps):
|
| 184 |
features_, labels_ = create_features_and_labels_with_advanced_features(X1, X2)
|
| 185 |
imputer = SimpleImputer(strategy='mean')
|
| 186 |
features_imputed = imputer.fit_transform(features_)
|
| 187 |
y = model.predict_proba(features_imputed)[:,1]
|
| 188 |
-
return y
|
| 189 |
|
| 190 |
def plot(y, label, timeframe='1h', ma=5, n_steps=None):
|
| 191 |
if n_steps is None:
|
|
@@ -206,11 +181,6 @@ def plot(y, label, timeframe='1h', ma=5, n_steps=None):
|
|
| 206 |
plt.legend()
|
| 207 |
return plt.gcf()
|
| 208 |
|
| 209 |
-
# Load pre-trained models
|
| 210 |
-
with open('model_n1d_cat.pkl','rb') as f:
|
| 211 |
-
model_n1d_cat = pickle.load(f)
|
| 212 |
-
with open('model_n4h_cat.pkl','rb') as f:
|
| 213 |
-
model_n4h_cat = pickle.load(f)
|
| 214 |
|
| 215 |
def predict_and_plot(timeframe, limit, epsilon, n_steps, ma):
|
| 216 |
period = f'{limit}d'
|
|
@@ -219,17 +189,10 @@ def predict_and_plot(timeframe, limit, epsilon, n_steps, ma):
|
|
| 219 |
bch_data = fetch_yfinance_data('BCH/USDT', period, timeframe)
|
| 220 |
btc_data, _ = normalize(btc_data)
|
| 221 |
bch_data, _ = normalize(bch_data)
|
| 222 |
-
|
| 223 |
-
# btc_ori, bch_ori,
|
| 224 |
-
# symbol='BCH/USDT',
|
| 225 |
-
# timeframe=timeframe,
|
| 226 |
-
# epsilon=epsilon,
|
| 227 |
-
# normalized=True,
|
| 228 |
-
# limit=limit
|
| 229 |
-
# )
|
| 230 |
model = model_n1d_cat if timeframe=='1d' else model_n4h_cat
|
| 231 |
-
preds = predictions(model, btc_data, bch_data, name=timeframe, n_steps=n_steps)
|
| 232 |
-
fig = plot(preds, label=label, timeframe=timeframe, ma=ma, n_steps=n_steps)
|
| 233 |
return fig
|
| 234 |
|
| 235 |
interface = gr.Interface(
|
|
|
|
| 1 |
import pickle
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
| 7 |
from scipy import stats
|
| 8 |
import yfinance as yf
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Load pre-trained models
|
| 12 |
+
with open('model_n1d_cat.pkl','rb') as f:
|
| 13 |
+
model_n1d_cat = pickle.load(f)
|
| 14 |
+
with open('model_n4h_cat.pkl','rb') as f:
|
| 15 |
+
model_n4h_cat = pickle.load(f)
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def fetch_yfinance_data(pair: str, period: str, interval: str) -> pd.DataFrame:
|
| 19 |
"""
|
|
|
|
| 23 |
"""
|
| 24 |
ticker = pair.replace("/USDT", "-USD")
|
| 25 |
df = yf.download(ticker, period=period, interval=interval)
|
| 26 |
+
df.columns = df.columns.get_level_values(0)
|
| 27 |
# bring the DateTimeIndex into a column, whatever its name was
|
| 28 |
df = df.reset_index()
|
| 29 |
df.rename(columns={df.columns[0]: 'timestamp'}, inplace=True)
|
|
|
|
| 108 |
|
| 109 |
if other_data is not None:
|
| 110 |
other_data = other_data.loc[:, ~other_data.columns.duplicated()]
|
| 111 |
+
d['relative_strength'] = d['close'] / other_data['close']
|
| 112 |
+
d['relative_strength_1'] = d['close'].shift(2) / other_data['close'].shift(2)
|
| 113 |
return d.iloc[:,1:].values
|
| 114 |
|
| 115 |
|
|
|
|
| 122 |
# df['future'] = df['close'].rolling(window=5).mean().shift(-1)
|
| 123 |
# df['trend'] = (df['future'] > df['close']).astype(int)
|
| 124 |
# labels = df['trend'].dropna().values
|
| 125 |
+
label = btc_copy[['timestamp','close']].shift(-1)
|
| 126 |
features = np.vstack((btc_features, eth_features))
|
| 127 |
+
return features, label
|
| 128 |
|
| 129 |
def get_data_predict(
|
| 130 |
btc_ori: pd.DataFrame,
|
|
|
|
| 136 |
limit: int = 50
|
| 137 |
):
|
| 138 |
period = f'{limit}d' # last N days
|
|
|
|
| 139 |
btc_data_ = fetch_yfinance_data('BTC/USDT', period, timeframe)
|
| 140 |
bch_data_ = fetch_yfinance_data(symbol, period, timeframe)
|
| 141 |
|
| 142 |
btc_data_ = remove_outliers(btc_data_, epsilon)
|
| 143 |
bch_data_ = remove_outliers(bch_data_, epsilon)
|
| 144 |
+
|
| 145 |
|
| 146 |
if normalized:
|
| 147 |
# merge with ori if you still want to include historical yf data
|
|
|
|
| 155 |
return btc_data_, bch_data_, None
|
| 156 |
|
| 157 |
|
|
|
|
| 158 |
def predictions(model, X1, X2, name, n_steps):
|
| 159 |
features_, labels_ = create_features_and_labels_with_advanced_features(X1, X2)
|
| 160 |
imputer = SimpleImputer(strategy='mean')
|
| 161 |
features_imputed = imputer.fit_transform(features_)
|
| 162 |
y = model.predict_proba(features_imputed)[:,1]
|
| 163 |
+
return y, labels_
|
| 164 |
|
| 165 |
def plot(y, label, timeframe='1h', ma=5, n_steps=None):
|
| 166 |
if n_steps is None:
|
|
|
|
| 181 |
plt.legend()
|
| 182 |
return plt.gcf()
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
def predict_and_plot(timeframe, limit, epsilon, n_steps, ma):
|
| 186 |
period = f'{limit}d'
|
|
|
|
| 189 |
bch_data = fetch_yfinance_data('BCH/USDT', period, timeframe)
|
| 190 |
btc_data, _ = normalize(btc_data)
|
| 191 |
bch_data, _ = normalize(bch_data)
|
| 192 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
model = model_n1d_cat if timeframe=='1d' else model_n4h_cat
|
| 194 |
+
preds, label = predictions(model, btc_data, bch_data, name=timeframe, n_steps=n_steps)
|
| 195 |
+
fig = plot(preds, label = label, timeframe=timeframe, ma=ma, n_steps=n_steps)
|
| 196 |
return fig
|
| 197 |
|
| 198 |
interface = gr.Interface(
|