-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
156 lines (140 loc) · 4.82 KB
/
client.py
File metadata and controls
156 lines (140 loc) · 4.82 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import hashlib
import json
import logging
import time
from datetime import datetime
from json import JSONDecodeError
from typing import Optional
import requests
import backoff
from logger import log
from settings import (
ACCESS_ID, SECRET_KEY, API_WAIT_TIME, URL_API, LOGGING,
TIMEOUT, HIDDEN
)
log = log.getChild('client')
class Api:
@property
def _headers(self):
headers = {
"User-Agent": "Python/CoinEx API client",
"Content-Type": "application/json",
"Authorization": ""
}
return headers
@property
def ts(self):
now = int(datetime.now().timestamp() * 1000)
return now + self.ts_shift
def __init__(self, access_id=ACCESS_ID, secret_key=SECRET_KEY,
api_url=URL_API):
self.access_id = access_id
self.secret_key = secret_key
self.api_url = api_url
self.ts_shift = 0
def _sign(self, data):
string_to_sign = ''
for k, v in sorted(data.items()):
string_to_sign += f'{k}={v}&'
string_to_sign += f'secret_key={self.secret_key}'
sign = hashlib.md5(string_to_sign.encode('utf-8')).hexdigest()
return sign.upper()
def _check_results(self, res):
if res.status_code != 200:
self.error = True
if LOGGING:
log.error(f'Ошибка: {res.status_code}')
return
try:
result = json.loads(res.content)
self.error = False
return result
except JSONDecodeError as e:
self.error = True
if LOGGING:
log.error(f'JSON decoding error: {e}')
@backoff.on_exception(backoff.fibo, Exception, logger=log, max_value=10, backoff_log_level=logging.WARNING)
def show_pair(self, ticker: str):
time.sleep(API_WAIT_TIME)
res = requests.get(
url=f'{URL_API}market/ticker',
params={'market': ticker},
timeout=TIMEOUT,
)
results = self._check_results(res)
if results.get('data').get('date'):
self.ts_shift = results.get('data').get('date') - self.ts
return results
@backoff.on_exception(backoff.fibo, Exception, logger=log, max_value=10, backoff_log_level=logging.WARNING)
def place_limit_order(self, ticker: str, price: float,
amount: Optional[float], side: str
):
time.sleep(API_WAIT_TIME)
headers = self._headers
payload = {
"access_id": self.access_id,
"amount": amount, # order count
"price": price, # order price
"type": side, # order type
"market": ticker, # market type
"tonce": self.ts,
"hide": HIDDEN
}
headers['Authorization'] = self._sign(payload)
res = requests.post(
url=f'{URL_API}order/limit',
headers=headers,
json=payload
)
return self._check_results(res)
@backoff.on_exception(backoff.fibo, Exception, logger=log, max_value=10, backoff_log_level=logging.WARNING)
def check_order_status(self, order_id: int, ticker: str):
time.sleep(API_WAIT_TIME)
headers = self._headers
payload = {
"access_id": self.access_id,
"id": order_id,
"market": ticker,
"tonce": self.ts,
}
headers['Authorization'] = self._sign(payload)
res = requests.get(
url=f'{URL_API}order/status',
headers=headers,
params=payload,
timeout=TIMEOUT,
)
return self._check_results(res)
@backoff.on_exception(backoff.fibo, Exception, logger=log, max_value=10, backoff_log_level=logging.WARNING)
def balance_info(self):
time.sleep(API_WAIT_TIME)
headers = self._headers
payload = {
"access_id": self.access_id,
"tonce": self.ts,
}
headers['Authorization'] = self._sign(payload)
res = requests.get(
url=f'{URL_API}balance/info',
headers=headers,
params=payload,
timeout=TIMEOUT,
)
return self._check_results(res)
@backoff.on_exception(backoff.fibo, Exception, logger=log, max_value=10, backoff_log_level=logging.WARNING)
def cancel_all_orders(self, ticker, account_id: int = 0):
time.sleep(API_WAIT_TIME)
headers = self._headers
payload = {
"access_id": self.access_id,
"account_id": account_id,
"market": ticker,
"tonce": self.ts,
}
headers['Authorization'] = self._sign(payload)
res = requests.delete(
url=f'{URL_API}order/pending',
headers=headers,
params=payload
)
return self._check_results(res)