Pezh commited on
Commit
fb532ef
1 Parent(s): e35fd75

Create Pas.py

Browse files
Files changed (1) hide show
  1. Pas.py +95 -0
Pas.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ccxt
2
+ import pandas as pd
3
+ import xgboost as xgb
4
+ from sklearn.metrics import r2_score
5
+ from sklearn.model_selection import train_test_split
6
+
7
+ exchange = ccxt.mexc({
8
+ 'apiKey': 'YOUR_API_KEY',
9
+ 'secret': 'YOUR_SECRET_KEY',
10
+ 'enableRateLimit': True,
11
+ })
12
+
13
+ def fetch_ohlcv_data(symbol, timeframe, limit):
14
+ return exchange.fetch_ohlcv(symbol, timeframe, since=None, limit=limit)
15
+
16
+ def data_to_dataframe(data):
17
+ df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
18
+ df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
19
+ df.set_index('timestamp', inplace=True)
20
+ return df
21
+
22
+ def prepare_dataset(df, lags):
23
+ X = pd.DataFrame()
24
+ y = pd.DataFrame()
25
+
26
+ for lag in range(1, lags + 1):
27
+ shifted_close = df['close'].shift(lag)
28
+ X[f'close_lag_{lag}'] = shifted_close
29
+
30
+ y = df['close'].shift(-1)
31
+ return X, y
32
+
33
+ def train_xgboost_model(X_train, y_train):
34
+ dtrain = xgb.DMatrix(X_train, label=y_train)
35
+ params = {
36
+ 'objective': 'reg:squarederror',
37
+ 'eval_metric': 'rmse',
38
+ }
39
+
40
+ model = xgb.train(params, dtrain)
41
+ return model
42
+
43
+ def predict_next_hour_price(df, model, lags):
44
+ X_test = pd.DataFrame()
45
+
46
+ for lag in range(1, lags + 1):
47
+ shifted_close = df['close'].shift(lag)
48
+ X_test[f'close_lag_{lag}'] = shifted_close
49
+
50
+ X_test = X_test.tail(1)
51
+ dtest = xgb.DMatrix(X_test)
52
+ next_hour_price = model.predict(dtest)
53
+ return next_hour_price
54
+
55
+ def evaluate_prediction_accuracy(y_test, y_pred):
56
+ accuracy = r2_score(y_test, y_pred)
57
+ return accuracy
58
+
59
+ def execute_trade(exchange, symbol, side, amount):
60
+ order = exchange.create_market_order(symbol, side, amount)
61
+ return order
62
+
63
+ symbol = 'BTC/USDT'
64
+ timeframe = '1h'
65
+ limit = 100000
66
+ lags = 12
67
+ test_size = 0.3
68
+
69
+ ohlcv_data = fetch_ohlcv_data(symbol, timeframe, limit)
70
+ df = data_to_dataframe(ohlcv_data)
71
+
72
+ X, y = prepare_dataset(df, lags)
73
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
74
+
75
+ model = train_xgboost_model(X_train, y_train)
76
+ y_pred = model.predict(X_test)
77
+ accuracy = evaluate_prediction_accuracy(y_test, y_pred)
78
+
79
+ if accuracy > 0.9:
80
+ latest_data = df.tail(lags)
81
+ predicted_price = predict_next_hour_price(latest_data, model, lags)
82
+ current_price = latest_data['close'].iloc[-1]
83
+
84
+ if predicted_price > current_price:
85
+ # Buy BTC
86
+ amount = 0.001 # Set the amount you want to trade
87
+ order = execute_trade(exchange, symbol, 'buy', amount)
88
+ print(f"Bought {amount} BTC at {current_price}")
89
+ elif predicted_price < current_price:
90
+ # Sell BTC
91
+ amount = 0.001 # Set the amount you want to trade
92
+ order = execute_trade(exchange, symbol, 'sell', amount)
93
+ print(f"Sold {amount} BTC at {current_price}")
94
+ else:
95
+ print("Accuracy is low, not placing order.")