Pezh commited on
Commit
73b58b3
1 Parent(s): 25dcd8b

Create Sosis.py

Browse files
Files changed (1) hide show
  1. Sosis.py +83 -0
Sosis.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import hmac
3
+ import hashlib
4
+ import time
5
+ import pandas as pd
6
+ import xgboost as xgb
7
+ from sklearn.model_selection import train_test_split
8
+
9
+ # Replace with your own API key and secret
10
+ api_key = 'your_api_key'
11
+ api_secret = 'your_api_secret'
12
+
13
+ def create_signature(query_string, secret):
14
+ return hmac.new(secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).digest()
15
+
16
+ def place_order(side, amount, price):
17
+ query = f'access_id={api_key}&amount={amount}&price={price}&side={side}&type=limit'
18
+ signature = create_signature(query, api_secret)
19
+
20
+ headers = {
21
+ 'Content-Type': 'application/json',
22
+ 'Authorization': f'{api_key}:{signature.hex()}'
23
+ }
24
+
25
+ response = requests.post('https://api.coinex.com/v1/order/place', data=query, headers=headers)
26
+ return response.json()
27
+
28
+ def get_data():
29
+ response = requests.get('https://api.coinex.com/v1/market/kline/BTCUSDT?interval=1h&limit=60')
30
+ data = response.json()
31
+ df = pd.DataFrame(data['data'])
32
+ df['time'] = pd.to_datetime(df['time'], unit='ms')
33
+ df.set_index('time', inplace=True)
34
+ df.drop(['vol', 'amount'], axis=1, inplace=True)
35
+ return df
36
+
37
+ def train_model(df):
38
+ X = df.drop('close', axis=1)
39
+ y = df['close']
40
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
41
+ dtrain = xgb.DMatrix(X_train, label=y_train)
42
+ dtest = xgb.DMatrix(X_test, label=y_test)
43
+ params = {
44
+ 'objective': 'reg:squarederror',
45
+ 'eval_metric': 'rmse',
46
+ 'max_depth': 3,
47
+ 'eta': 0.1
48
+ }
49
+ model = xgb.train(params, dtrain)
50
+ predictions = model.predict(dtest)
51
+ accuracy = 1 - (sum((predictions - y_test) ** 2) / sum(y_test ** 2))
52
+ return model, accuracy
53
+
54
+ def predict_next_hour(model, df):
55
+ last_row = df.iloc[-1]
56
+ X = pd.DataFrame([[last_row['open'], last_row['high'], last_row['low']]], index=[last_row.name + pd.Timedelta(hours=1)])
57
+ dtest = xgb.DMatrix(X)
58
+ prediction = model.predict(dtest)[0]
59
+ return prediction
60
+
61
+ def main():
62
+ while True:
63
+ df = get_data()
64
+ model, accuracy = train_model(df)
65
+ print(f'Model accuracy: {accuracy}')
66
+
67
+ if accuracy > 0.9:
68
+ prediction = predict_next_hour(model, df)
69
+
70
+ if prediction > df.iloc[-1]['close']:
71
+ # Place buy order
72
+ order = place_order('buy', 0.001, df.iloc[-1]['close'])
73
+ print(f'Placed buy order: {order}')
74
+ else:
75
+ # Place sell order
76
+ order = place_order('sell', 0.001, df.iloc[-1]['close'])
77
+ print(f'Placed sell order: {order}')
78
+
79
+ # Wait for some time before checking the price again
80
+ time.sleep(60)
81
+
82
+ if __name__ == '__main__':
83
+ main()