Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Poloniex API using requests
- 'BTS: litepresence1'
- import requests
- import hmac
- import hashlib
- import time
- class private: # keyed api access defitions; LIVE buy/sell etc.
- print('API private')
- def __init__(self, APIKey, Secret):
- self.APIKey = APIKey
- self.Secret = Secret
- def api_query(self, payload={}):
- uri = "https://poloniex.com/tradingApi"
- payload['nonce'] = int(time.time() * 1000)
- u = {"'": "", " ": "", "{": "", "}": "", ",": "&", ":": "="}
- post_data = ''.join(u[i] if i in u else i for i in str(payload))
- sign = hmac.new(self.Secret, post_data, hashlib.sha512).hexdigest()
- headers = {'Sign': sign, 'Key': self.APIKey}
- ret = requests.post(uri, data=payload, headers=headers).json()
- return addstamp(ret)
- def balances(self):
- payload = {'command': 'returnBalances'}
- return self.api_query(payload=payload)
- def complete_balances(self):
- payload = {'command': 'returnCompleteBalances'}
- return self.api_query(payload=payload)
- def orders(self, pair):
- payload = {'command': 'returnOpenOrders', 'currencyPair': pair}
- return self.api_query(payload=payload)
- def buy(self, pair, rate, amount):
- payload = {'command': 'buy', 'currencyPair': pair,
- 'rate': ('%.8f' % rate), 'amount': ('%.8f' % amount)}
- return self.api_query(payload=payload)
- def sell(self, pair, rate, amount):
- payload = {'command': 'sell', 'currencyPair': pair,
- 'rate': ('%.8f' % rate), 'amount': ('%.8f' % amount)}
- return self.api_query(payload=payload)
- def cancel(self, pair, orderNumber):
- payload = {'command': 'cancelOrder', 'currencyPair': pair,
- 'orderNumber': orderNumber}
- return self.api_query(payload=payload)
- def orderbook(pair, depth): # public api poloniex
- print('API orderbook')
- uri = 'https://poloniex.com/public'
- params = {'command': 'returnOrderBook',
- 'currencyPair': pair, 'depth': depth}
- return requests.get(uri, params=params).json()
- def mhistory(pair): # public api poloniex
- print('API mhistory')
- uri = 'https://poloniex.com/public'
- params = {'command': 'returnTradeHistory', 'currencyPair': pair}
- return addstamp(requests.get(uri, params=params).json())
- def addstamp(b): # add unix timestamp if only 'date' or 'datetime' in dict
- a = b
- if ('return' in a) and (isinstance(a['return'], list)):
- for x in range(0, len(a['return'])):
- if (isinstance(a['return'][x], dict) and
- ('datetime' in a['return'][x]) and
- ('timestamp' not in a['return'][x])):
- a['return'][x]['timestamp'] = float(
- time.mktime(time.strptime(
- a['return'][x]['datetime'],
- "%Y-%m-%d %H:%M:%S")))
- elif (isinstance(a, list)):
- for x in range(0, len(a)):
- if (isinstance(a[x], dict) and
- ('date' in a[x]) and ('timestamp' not in a[x])):
- a[x]['timestamp'] = float(time.mktime(time.strptime(
- a[x]['date'], "%Y-%m-%d %H:%M:%S")))
- return a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement