Last active
November 21, 2017 18:48
-
-
Save veox/fa2a406de0a0dcfda234374fa014e444 to your computer and use it in GitHub Desktop.
ExtendedAPI snippet for https://github.com/veox/python3-krakenex/issues/66
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INFO:extendedapi.TODO:about to query_private() | |
DEBUG:urllib3.connectionpool:https://api.kraken.com:443 "POST /0/private/AddOrder HTTP/1.1" 502 None | |
WARNING:extendedapi.TODO:query_private() failed:502 Server Error: Bad Gateway for url: https://api.kraken.com/0/private/AddOrder | |
DEBUG:extendedapi.TODO:sleeping... | |
INFO:extendedapi.TODO:about to session.send() | |
DEBUG:urllib3.connectionpool:https://api.kraken.com:443 "POST /0/private/AddOrder HTTP/1.1" 200 None | |
DEBUG:extendedapi.TODO:session.send() done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
ExtendedAPI | |
''' | |
from decimal import Decimal as D | |
from pprint import pprint | |
import logging | |
import time | |
import krakenex | |
from requests.exceptions import HTTPError | |
def now(): | |
return D(time.time()) | |
logging.basicConfig(filename='debug.log', level=logging.DEBUG) | |
class ExtendedAPI(krakenex.API): | |
def __init__(self): | |
super().__init__() | |
self.logger = logging.getLogger(self.__module__ + '.TODO') | |
self.logger.setLevel(logging.DEBUG) | |
self.cooldown = 10 | |
return | |
def add_order(self, pair: str, type_: str, price: str, volume: str): | |
response = None | |
before = now() # DEBUG | |
try: | |
self.logger.info('about to query_private()') # DEBUG | |
response = self.query_private('AddOrder', | |
{'pair': pair, 'type': type_, 'ordertype': 'limit', | |
'price': price, 'volume': volume, 'userref': '1'}) | |
self.logger.debug('query_private() done') # DEBUG | |
except HTTPError as e: | |
self.logger.warning('query_private() failed:' + str(e)) # DEBUG | |
while True: | |
self.logger.debug('sleeping...') # DEBUG | |
time.sleep(self.cooldown) | |
try: | |
self.logger.info('about to session.send()') # DEBUG | |
response = self.session.send(self.response.request) | |
# FIXME: same as in krakenex.API._query()! | |
self.response = response # DEBUG | |
if self.response.status_code not in (200, 201, 202): | |
self.response.raise_for_status() | |
self.logger.debug('session.send() done') # DEBUG | |
response = response.json() | |
break | |
except HTTPError as ee: | |
self.logger.warning('session.send() failed:' + str(ee)) | |
continue | |
after = now() #DEBUG | |
# DEBUG | |
print('# response') | |
pprint(response) | |
print('# roundtrip', after - before, 'seconds') | |
return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
response = k.add_order(pair = 'BCHXBT', type_ = 'buy', volume = '0.01', price = '0.01') | |
# response | |
{'error': ['EAPI:Invalid nonce']} | |
# roundtrip 51.5344731807708740234375 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment