Create Posis.py
Browse files
Posis.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ccxt
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
import xgboost as xgb
|
5 |
+
from sklearn.metrics import r2_score
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
|
8 |
+
exchange = ccxt.mexc({
|
9 |
+
'apiKey': 'mx0vglbkoCOwmqV5tn',
|
10 |
+
'secret': 'c6d8cc8953fd405787ed54e3488ae0db',
|
11 |
+
'enableRateLimit': True,
|
12 |
+
})
|
13 |
+
|
14 |
+
def fetch_ohlcv_data(symbol, timeframe, limit):
|
15 |
+
return exchange.fetch_ohlcv(symbol, timeframe, since=None, limit=limit)
|
16 |
+
|
17 |
+
def data_to_dataframe(data):
|
18 |
+
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
|
19 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
|
20 |
+
df.set_index('timestamp', inplace=True)
|
21 |
+
return df
|
22 |
+
|
23 |
+
def prepare_dataset(df, lags):
|
24 |
+
X = pd.DataFrame()
|
25 |
+
y = pd.DataFrame()
|
26 |
+
|
27 |
+
for lag in range(1, lags + 1):
|
28 |
+
shifted_close = df['close'].shift(lag)
|
29 |
+
X[f'close_lag_{lag}'] = shifted_close
|
30 |
+
|
31 |
+
y = df['close'].shift(-1)
|
32 |
+
return X, y
|
33 |
+
|
34 |
+
def train_xgboost_model(X_train, y_train):
|
35 |
+
dtrain = xgb.DMatrix(X_train, label=y_train)
|
36 |
+
params = {
|
37 |
+
'objective': 'reg:squarederror',
|
38 |
+
'eval_metric': 'rmse',
|
39 |
+
}
|
40 |
+
|
41 |
+
try:
|
42 |
+
model = xgb.train(params, dtrain)
|
43 |
+
except xgb.core.XGBoostError as e:
|
44 |
+
print(f"Error training XGBoost model: {e}")
|
45 |
+
return None
|
46 |
+
|
47 |
+
return model
|
48 |
+
|
49 |
+
def predict_next_hour_price(df, model, lags):
|
50 |
+
X_test = pd.DataFrame()
|
51 |
+
|
52 |
+
for lag in range(1, lags + 1):
|
53 |
+
shifted_close = df['close'].shift(lag)
|
54 |
+
X_test[f'close_lag_{lag}'] = shifted_close
|
55 |
+
|
56 |
+
X_test = X_test.tail(1)
|
57 |
+
dtest = xgb.DMatrix(X_test)
|
58 |
+
|
59 |
+
try:
|
60 |
+
next_hour_price = model.predict(dtest)
|
61 |
+
except xgb.core.XGBoostError as e:
|
62 |
+
print(f"Error making prediction with XGBoost model: {e}")
|
63 |
+
return None
|
64 |
+
|
65 |
+
return next_hour_price
|
66 |
+
|
67 |
+
def evaluate_prediction_accuracy(y_test, y_pred):
|
68 |
+
accuracy = r2_score(y_test, y_pred)
|
69 |
+
return accuracy
|
70 |
+
|
71 |
+
def execute_trade(exchange, symbol, side, amount):
|
72 |
+
order = exchange.create_market_order(symbol, side, amount)
|
73 |
+
return order
|
74 |
+
|
75 |
+
symbol = 'BTC/USDT'
|
76 |
+
timeframe = '1h'
|
77 |
+
limit = 100
|
78 |
+
lags = 12
|
79 |
+
test_size = 0.3
|
80 |
+
|
81 |
+
ohlcv_data = fetch_ohlcv_data(symbol, timeframe, limit)
|
82 |
+
df = data_to_dataframe(ohlcv_data)
|
83 |
+
|
84 |
+
if st.button("hoo"):
|
85 |
+
st.write(df)
|
86 |
+
|
87 |
+
X, y = prepare_dataset(df, lags)
|
88 |
+
|
89 |
+
if X.isnull().values.any() or y.isnull().values.any():
|
90 |
+
print("Error: Null values found in X or y")
|
91 |
+
else:
|
92 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
|
93 |
+
|
94 |
+
model = train_xgboost_model(X_train, y_train)
|
95 |
+
|
96 |
+
if model is None:
|
97 |
+
print("Error: XGBoost model could not be trained")
|
98 |
+
else:
|
99 |
+
y_pred = model.predict(X_test)
|
100 |
+
accuracy = evaluate_prediction_accuracy(y_test, y_pred)
|
101 |
+
|
102 |
+
if accuracy > 0.9:
|
103 |
+
latest_data = df.tail(lags)
|
104 |
+
predicted_price = predict_next_hour_price(latest_data, model, lags)
|
105 |
+
|
106 |
+
if predicted_price is None:
|
107 |
+
print("Error: XGBoost model could not make prediction")
|
108 |
+
else:
|
109 |
+
current_price = latest_data['close'].iloc[-1]
|
110 |
+
|
111 |
+
if predicted_price > current_price:
|
112 |
+
# Buy BTC
|
113 |
+
amount = 0.001 # Set the amount you want to trade
|
114 |
+
order = execute_trade(exchange, symbol, 'buy', amount)
|
115 |
+
print(f"Bought {amount} BTC at {current_price}")
|
116 |
+
elif predicted_price < current_price:
|
117 |
+
# Sell BTC
|
118 |
+
amount = 0.001 # Set the amount you want to trade
|
119 |
+
order = execute_trade(exchange, symbol, 'sell', amount)
|
120 |
+
print(f"Sold {amount} BTC at {current_price}")
|
121 |
+
else:
|
122 |
+
print("Accuracy is low, not placing order.")
|