File size: 2,415 Bytes
b6a287b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import ccxt
import pandas as pd
import xgboost as xgb
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
import time

exchange = ccxt.mexc({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET_KEY',
    'enableRateLimit': True,
})

def fetch_ohlcv_data(symbol, timeframe, limit):
    return exchange.fetch_ohlcv(symbol, timeframe, since=None, limit=limit)

def data_to_dataframe(data):
    df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
    df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
    df.set_index('timestamp', inplace=True)
    return df

def prepare_dataset(df, lags):
    X = pd.DataFrame()
    y = pd.DataFrame()

    for lag in range(1, lags + 1):
        shifted_close = df['close'].shift(lag)
        X[f'close_lag_{lag}'] = shifted_close

    y = df['close'].shift(-1)
    return X, y

def train_xgboost_model(X_train, y_train):
    dtrain = xgb.DMatrix(X_train, label=y_train)
    params = {
        'objective': 'reg:squarederror',
        'eval_metric': 'rmse',
    }

    model = xgb.train(params, dtrain)
    return model

def predict_next_hour_price(df, model, lags):
    X_test = pd.DataFrame()

    for lag in range(1, lags + 1):
        shifted_close = df['close'].shift(lag)
        X_test[f'close_lag_{lag}'] = shifted_close

    X_test = X_test.tail(1)
    dtest = xgb.DMatrix(X_test)
    next_hour_price = model.predict(dtest)
    return next_hour_price

def evaluate_prediction_accuracy(y_test, y_pred):
    accuracy = r2_score(y_test, y_pred)
    return accuracy

symbol = 'BTC/USDT'
timeframe = '1h'
limit = 100000
lags = 12
test_size = 0.3

# Fetch initial data
ohlcv_data = fetch_ohlcv_data(symbol, timeframe, limit)
df = data_to_dataframe(ohlcv_data)

# Prepare dataset and train the model
X, y = prepare_dataset(df, lags)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
model = train_xgboost_model(X_train, y_train)

while True:
    # Fetch new data every hour
    ohlcv_data = fetch_ohlcv_data(symbol, timeframe, limit)
    df = data_to_dataframe(ohlcv_data)

    # Make prediction
    latest_data = df.tail(lags)
    predicted_price = predict_next_hour_price(latest_data, model, lags)

    # Print predicted next hour price
    print(f"Predicted next hour price: ${predicted_price[0]}")

    # Sleep for 1 hour (3600 seconds)
    time.sleep(3600)