Pezh commited on
Commit
7a7a869
1 Parent(s): 194926a

Create Pach.py

Browse files
Files changed (1) hide show
  1. Pach.py +128 -0
Pach.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ model = xgb.train(params, dtrain)
42
+ return model
43
+
44
+ def predict_next_hour_price(df, model, lags):
45
+ X_test = pd.DataFrame()
46
+
47
+ for lag in range(1, lags + 1):
48
+ shifted_close = df['close'].shift(lag)
49
+ X_test[f'close_lag_{lag}'] = shifted_close
50
+
51
+ X_test = X_test.tail(1)
52
+ dtest = xgb.DMatrix(X_test)
53
+ next_hour_price = model.predict(dtest)
54
+ return next_hour_price
55
+
56
+ def evaluate_prediction_accuracy(y_test, y_pred):
57
+ accuracy = r2_score(y_test, y_pred)
58
+ return accuracy
59
+
60
+ def execute_trade(exchange, symbol, side, amount, leverage):
61
+ order = exchange.create_market_order(symbol, side, amount)
62
+
63
+ # Set leverage for the order
64
+ if leverage is not None and leverage != 1:
65
+ position = exchange.fetch_position(symbol)
66
+ if position is not None:
67
+ current_leverage = position['info']['leverage']
68
+ if current_leverage != leverage:
69
+ # Update leverage for the position
70
+ exchange.set_leverage(leverage, symbol)
71
+
72
+ return order
73
+
74
+ symbol = 'BTC/USDT'
75
+ timeframe = '1h'
76
+ limit = 100
77
+ lags = 12
78
+ test_size = 0.3
79
+ leverage = 5
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
+
88
+ X, y = prepare_dataset(df, lags)
89
+
90
+ # Replace missing values with the mean of the column
91
+ X = X.fillna(X.mean())
92
+ y = y.fillna(y.mean())
93
+
94
+ #X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
95
+
96
+ #model, scaler = train_xgboost_model(X_train, y_train)
97
+
98
+ #y_pred = model.predict(X_test)
99
+
100
+ # Inverse transform the predicted values to get the original scale
101
+ #y_pred = scaler.inverse_transform(y_pred.reshape(-1, 1))
102
+
103
+ #accuracy = evaluate_prediction_accuracy(y_test, y_pred)
104
+
105
+ #X, y = prepare_dataset(df, lags)
106
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
107
+
108
+ model = train_xgboost_model(X_train, y_train)
109
+ y_pred = model.predict(X_test)
110
+ accuracy = evaluate_prediction_accuracy(y_test, y_pred)
111
+
112
+ if accuracy > 0.9:
113
+ latest_data = df.tail(lags)
114
+ predicted_price = predict_next_hour_price(latest_data, model, lags)
115
+ current_price = latest_data['close'].iloc[-1]
116
+
117
+ if predicted_price > current_price:
118
+ # Buy BTC
119
+ amount = 0.001 # Set the amount you want to trade
120
+ order = execute_trade(exchange, symbol, 'buy', amount, leverage)
121
+ print(f"Bought {amount} BTC at {current_price} with {leverage}x leverage")
122
+ elif predicted_price < current_price:
123
+ # Sell BTC
124
+ amount = 0.001 # Set the amount you want to trade
125
+ order = execute_trade(exchange, symbol, 'sell', amount, leverage)
126
+ print(f"Sold {amount} BTC at {current_price} with {leverage}x leverage")
127
+ else:
128
+ print("Accuracy is low, not placing order.")