-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorder_executor.py
138 lines (112 loc) · 4.26 KB
/
order_executor.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import json
import time
import base64
import hmac
import hashlib
import requests
import pymysql
class Order_Executor:
def __init__(self, is_sandbox=True):
config_file_path = "./config.json"
with open(config_file_path, "r") as handler:
self.config = json.load(handler)
if is_sandbox:
self.api_key = self.config["sandbox_api"]["key"]
self.api_secret = self.config["sandbox_api"]["secret"]
self.api_passphrase = self.config["sandbox_api"]["passphrase"]
else:
self.api_key = self.config["api"]["key"]
self.api_secret = self.config["api"]["secret"]
self.api_passphrase = self.config["api"]["passphrase"]
self.base_url = (
"https://openapi-sandbox.kucoin.com"
if is_sandbox
else "https://api.kucoin.com"
)
def place_market_order(self, side, funds):
api_endpoint = "/api/v1/orders"
url = f"{self.base_url}{api_endpoint}"
now = int(time.time() * 1000)
data = {
"clientOid": str(now),
"side": side,
"symbol": "ETH-USDT",
"type": "market",
"funds": funds,
}
data_json = json.dumps(data)
str_to_sign = str(now) + "POST" + api_endpoint + data_json
signature = self._sign(str_to_sign)
headers = {
"Content-Type": "application/json",
"KC-API-KEY": self.api_key,
"KC-API-SIGN": signature,
"KC-API-TIMESTAMP": str(now),
"KC-API-PASSPHRASE": self.api_passphrase,
}
response = requests.request("POST", url, headers=headers, data=data_json)
print(response.status_code)
print(response.json())
self._store_order_id(
now, response.json()["data"]["orderId"], "sandbox" in self.base_url
)
return response.json
def list_orders(self):
api_endpoint = "/api/v1/hist-orders"
url = f"{self.base_url}{api_endpoint}"
now = int(time.time() * 1000)
str_to_sign = str(now) + "GET" + api_endpoint
signature = self._sign(str_to_sign)
headers = {
"Content-Type": "application/json",
"KC-API-KEY": self.api_key,
"KC-API-SIGN": signature,
"KC-API-TIMESTAMP": str(now),
"KC-API-PASSPHRASE": self.api_passphrase,
}
response = requests.request("GET", url, headers=headers)
print(response.status_code)
print(response.json())
return response.json
def get_order(self, orderId):
api_endpoint = f"/api/v1/orders/{orderId}"
url = f"{self.base_url}{api_endpoint}"
now = int(time.time() * 1000)
str_to_sign = str(now) + "GET" + api_endpoint
signature = self._sign(str_to_sign)
headers = {
"Content-Type": "application/json",
"KC-API-KEY": self.api_key,
"KC-API-SIGN": signature,
"KC-API-TIMESTAMP": str(now),
"KC-API-PASSPHRASE": self.api_passphrase,
}
response = requests.request("GET", url, headers=headers)
print(response.status_code)
print(response.json())
return response.json
def _sign(self, str_to_sign):
return base64.b64encode(
hmac.new(
self.api_secret.encode("utf-8"),
str_to_sign.encode("utf-8"),
hashlib.sha256,
).digest()
)
def _store_order_id(self, timestamp, order_id, is_sandbox):
host = self.config["database"]["host"]
port = int(self.config["database"]["port"])
user = self.config["database"]["user"]
password = self.config["database"]["password"]
database = self.config["database"]["database"]
connection = pymysql.connect(
host, user=user, port=port, passwd=password, database=database
)
with connection.cursor() as cursor:
arr = [str(timestamp), order_id, is_sandbox]
sql = "INSERT INTO `orders` VALUES (%s, %s, %s)"
cursor.execute(sql, tuple(arr))
connection.commit()
test_order_placer = Order_Executor()
# test_order_placer.place_market_order(5)
# test_order_placer.get_order("5edb03b01794c300063a1541")