Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. # Poloniex API using requests
  2.  
  3. 'BTS: litepresence1'
  4.  
  5. import requests
  6. import hmac
  7. import hashlib
  8. import time
  9.  
  10. class private:  # keyed api access defitions; LIVE buy/sell etc.
  11.  
  12.     print('API private')
  13.  
  14.     def __init__(self, APIKey, Secret):
  15.         self.APIKey = APIKey
  16.         self.Secret = Secret
  17.  
  18.     def api_query(self, payload={}):
  19.  
  20.         uri = "https://poloniex.com/tradingApi"
  21.         payload['nonce'] = int(time.time() * 1000)
  22.         u = {"'": "", " ": "", "{": "", "}": "", ",": "&", ":": "="}
  23.         post_data = ''.join(u[i] if i in u else i for i in str(payload))
  24.         sign = hmac.new(self.Secret, post_data, hashlib.sha512).hexdigest()
  25.         headers = {'Sign': sign, 'Key': self.APIKey}
  26.         ret = requests.post(uri, data=payload, headers=headers).json()
  27.         return addstamp(ret)
  28.  
  29.     def balances(self):
  30.         payload = {'command': 'returnBalances'}
  31.         return self.api_query(payload=payload)
  32.  
  33.     def complete_balances(self):
  34.         payload = {'command': 'returnCompleteBalances'}
  35.         return self.api_query(payload=payload)
  36.  
  37.     def orders(self, pair):
  38.         payload = {'command': 'returnOpenOrders', 'currencyPair': pair}
  39.         return self.api_query(payload=payload)
  40.  
  41.     def buy(self, pair, rate, amount):
  42.         payload = {'command': 'buy', 'currencyPair': pair,
  43.                    'rate': ('%.8f' % rate), 'amount': ('%.8f' % amount)}
  44.         return self.api_query(payload=payload)
  45.  
  46.     def sell(self, pair, rate, amount):
  47.         payload = {'command': 'sell', 'currencyPair': pair,
  48.                    'rate': ('%.8f' % rate), 'amount': ('%.8f' % amount)}
  49.         return self.api_query(payload=payload)
  50.  
  51.     def cancel(self, pair, orderNumber):
  52.         payload = {'command': 'cancelOrder', 'currencyPair': pair,
  53.                    'orderNumber': orderNumber}
  54.         return self.api_query(payload=payload)
  55.  
  56.  
  57. def orderbook(pair, depth):  # public api poloniex
  58.  
  59.     print('API orderbook')
  60.     uri = 'https://poloniex.com/public'
  61.     params = {'command': 'returnOrderBook',
  62.               'currencyPair': pair, 'depth': depth}
  63.     return requests.get(uri, params=params).json()
  64.  
  65.  
  66. def mhistory(pair):  # public api poloniex
  67.  
  68.     print('API mhistory')
  69.     uri = 'https://poloniex.com/public'
  70.     params = {'command': 'returnTradeHistory', 'currencyPair': pair}
  71.     return addstamp(requests.get(uri, params=params).json())
  72.  
  73.  
  74. def addstamp(b):  # add unix timestamp if only 'date' or 'datetime' in dict
  75.  
  76.     a = b
  77.     if ('return' in a) and (isinstance(a['return'], list)):
  78.         for x in range(0, len(a['return'])):
  79.             if (isinstance(a['return'][x], dict) and
  80.                 ('datetime' in a['return'][x]) and
  81.                     ('timestamp' not in a['return'][x])):
  82.                 a['return'][x]['timestamp'] = float(
  83.                     time.mktime(time.strptime(
  84.                                 a['return'][x]['datetime'],
  85.                                 "%Y-%m-%d %H:%M:%S")))
  86.     elif (isinstance(a, list)):
  87.         for x in range(0, len(a)):
  88.             if (isinstance(a[x], dict) and
  89.                     ('date' in a[x]) and ('timestamp' not in a[x])):
  90.                 a[x]['timestamp'] = float(time.mktime(time.strptime(
  91.                     a[x]['date'], "%Y-%m-%d %H:%M:%S")))
  92.     return a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement