-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_client.py
46 lines (37 loc) · 1.62 KB
/
api_client.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
import ccxt
import pyotp
from config import config
from custom_logging import get_logger
logger = get_logger(__name__)
class APIError(Exception):
def __init__(self, message):
self.message = message
class APIClient:
def __init__(self):
self.api = ccxt.mandala({
'apiKey': config.get('Exchange', 'APIKey'),
'secret': config.get('Exchange', 'APISecret'),
'login': config.get('Exchange', 'Login'),
'password': config.get('Exchange', 'Password')
})
self.otp = pyotp.TOTP(config.get('Exchange', 'TwoFASecret'))
self.api.sign_in({'password': self.otp.now()})
self.api.load_markets()
def ticker(self, currency_pair):
return self.api.fetch_ticker(currency_pair)
def depth(self, currency_pair, limit=100):
limits_allowed = [5, 10, 20, 50, 100, 500, 1000]
if limit not in limits_allowed:
# finding nearest allowed value
limit = min(limits_allowed, key=lambda x: abs(x - limit))
return self.api.fetch_order_book(currency_pair, limit=limit)
def order_create(self, currency_pair, order_type, side, amount, price=None, params=None):
try:
return self.api.create_order(currency_pair, order_type, side, amount, price, params)
except ccxt.errors.ExchangeError as e:
logger.error('Failed to create order: {}', e)
return None
def order_remove(self, currency_pair, order_id, side):
return self.api.cancel_order(order_id, currency_pair, params={'side': side})
def my_open_orders(self):
return self.api.fetch_open_orders()