X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=ccxt_wrapper.py;h=d37c306882aaae72fa7c56470eada65f77ef5fa1;hb=123411ccb5e004d38a9c75cb621a732a29edb09a;hp=903bfc489958a4539f69ae2deb5183bda6c2a29d;hpb=aca4d4372553110ab5d76740ff536de83d5617b2;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index 903bfc4..d37c306 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -11,29 +11,6 @@ class poloniexE(poloniex): def nanoseconds(): return int(time.time() * 1000000000) - def nonce(self): - return self.nanoseconds() - - def fetch_balance(self, params={}): - self.load_markets() - balances = self.privatePostReturnCompleteBalances(self.extend({ - 'account': 'all', - }, params)) - result = {'info': balances} - currencies = list(balances.keys()) - for c in range(0, len(currencies)): - id = currencies[c] - balance = balances[id] - currency = self.common_currency_code(id) - account = { - 'free': decimal.Decimal(balance['available']), - 'used': decimal.Decimal(balance['onOrders']), - 'total': decimal.Decimal(0.0), - } - account['total'] = self.sum(account['free'], account['used']) - result[currency] = account - return self.parse_balance(result) - def fetch_margin_balance(self): """ portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) @@ -200,31 +177,8 @@ class poloniexE(poloniex): return all_balances - def parse_ticker(self, ticker, market=None): - timestamp = self.milliseconds() - symbol = None - if market: - symbol = market['symbol'] - return { - 'symbol': symbol, - 'timestamp': timestamp, - 'datetime': self.iso8601(timestamp), - 'high': decimal.Decimal(ticker['high24hr']), - 'low': decimal.Decimal(ticker['low24hr']), - 'bid': decimal.Decimal(ticker['highestBid']), - 'ask': decimal.Decimal(ticker['lowestAsk']), - 'vwap': None, - 'open': None, - 'close': None, - 'first': None, - 'last': decimal.Decimal(ticker['last']), - 'change': decimal.Decimal(ticker['percentChange']), - 'percentage': None, - 'average': None, - 'baseVolume': decimal.Decimal(ticker['quoteVolume']), - 'quoteVolume': decimal.Decimal(ticker['baseVolume']), - 'info': ticker, - } + def create_exchange_order(self, symbol, type, side, amount, price=None, params={}): + return super(poloniexE, self).create_order(symbol, type, side, amount, price=price, params=params) def create_margin_order(self, symbol, type, side, amount, price=None, lending_rate=None, params={}): if type == 'market': @@ -254,17 +208,6 @@ class poloniexE(poloniex): self.orders[id] = order return self.extend({'info': response}, order) - def create_exchange_order(self, symbol, type, side, amount, price=None, params={}): - return super(poloniexE, self).create_order(symbol, type, side, amount, price=price, params=params) - - def create_order(self, symbol, type, side, amount, price=None, account="exchange", lending_rate=None, params={}): - if account == "exchange": - return self.create_exchange_order(symbol, type, side, amount, price=price, params=params) - elif account == "margin": - return self.create_margin_order(symbol, type, side, amount, price=price, lending_rate=lending_rate, params=params) - else: - raise NotImplementedError - def order_precision(self, symbol): return 8 @@ -324,3 +267,73 @@ class poloniexE(poloniex): "total": decimal.Decimal(summary["totalValue"]), } + def nonce(self): + """ + Wrapped to allow nonce with other libraries + """ + return self.nanoseconds() + + def fetch_balance(self, params={}): + """ + Wrapped to get decimals + """ + self.load_markets() + balances = self.privatePostReturnCompleteBalances(self.extend({ + 'account': 'all', + }, params)) + result = {'info': balances} + currencies = list(balances.keys()) + for c in range(0, len(currencies)): + id = currencies[c] + balance = balances[id] + currency = self.common_currency_code(id) + account = { + 'free': decimal.Decimal(balance['available']), + 'used': decimal.Decimal(balance['onOrders']), + 'total': decimal.Decimal(0.0), + } + account['total'] = self.sum(account['free'], account['used']) + result[currency] = account + return self.parse_balance(result) + + def parse_ticker(self, ticker, market=None): + """ + Wrapped to get decimals + """ + timestamp = self.milliseconds() + symbol = None + if market: + symbol = market['symbol'] + return { + 'symbol': symbol, + 'timestamp': timestamp, + 'datetime': self.iso8601(timestamp), + 'high': decimal.Decimal(ticker['high24hr']), + 'low': decimal.Decimal(ticker['low24hr']), + 'bid': decimal.Decimal(ticker['highestBid']), + 'ask': decimal.Decimal(ticker['lowestAsk']), + 'vwap': None, + 'open': None, + 'close': None, + 'first': None, + 'last': decimal.Decimal(ticker['last']), + 'change': decimal.Decimal(ticker['percentChange']), + 'percentage': None, + 'average': None, + 'baseVolume': decimal.Decimal(ticker['quoteVolume']), + 'quoteVolume': decimal.Decimal(ticker['baseVolume']), + 'info': ticker, + } + + def create_order(self, symbol, type, side, amount, price=None, account="exchange", lending_rate=None, params={}): + """ + Wrapped to handle margin and exchange accounts + """ + if account == "exchange": + return self.create_exchange_order(symbol, type, side, amount, price=price, params=params) + elif account == "margin": + return self.create_margin_order(symbol, type, side, amount, price=price, lending_rate=lending_rate, params=params) + else: + raise NotImplementedError + +