├── .gitignore ├── LICENSE ├── README.md ├── demo.py └── lib ├── __init__.py ├── auth.py ├── client.py └── conf.py.sample /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | bin/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg 24 | 25 | # Installer logs 26 | pip-log.txt 27 | pip-delete-this-directory.txt 28 | 29 | # Unit test / coverage reports 30 | htmlcov/ 31 | .tox/ 32 | .coverage 33 | .cache 34 | nosetests.xml 35 | coverage.xml 36 | 37 | # Translations 38 | *.mo 39 | 40 | # Mr Developer 41 | .mr.developer.cfg 42 | .project 43 | .pydevproject 44 | 45 | # Rope 46 | .ropeproject 47 | 48 | # Django stuff: 49 | *.log 50 | *.pot 51 | 52 | # Sphinx documentation 53 | docs/_build/ 54 | 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator and 7 | subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for the 11 | purpose of contributing to a commons of creative, cultural and scientific 12 | works ("Commons") that the public can reliably and without fear of later 13 | claims of infringement build upon, modify, incorporate in other works, reuse 14 | and redistribute as freely as possible in any form whatsoever and for any 15 | purposes, including without limitation commercial purposes. These owners may 16 | contribute to the Commons to promote the ideal of a free culture and the 17 | further production of creative, cultural and scientific works, or to gain 18 | reputation or greater distribution for their Work in part through the use and 19 | efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any expectation 22 | of additional consideration or compensation, the person associating CC0 with a 23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 25 | and publicly distribute the Work under its terms, with knowledge of his or her 26 | Copyright and Related Rights in the Work and the meaning and intended legal 27 | effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not limited 32 | to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 35 | and translate a Work; 36 | 37 | ii. moral rights retained by the original author(s) and/or performer(s); 38 | 39 | iii. publicity and privacy rights pertaining to a person's image or likeness 40 | depicted in a Work; 41 | 42 | iv. rights protecting against unfair competition in regards to a Work, 43 | subject to the limitations in paragraph 4(a), below; 44 | 45 | v. rights protecting the extraction, dissemination, use and reuse of data in 46 | a Work; 47 | 48 | vi. database rights (such as those arising under Directive 96/9/EC of the 49 | European Parliament and of the Council of 11 March 1996 on the legal 50 | protection of databases, and under any national implementation thereof, 51 | including any amended or successor version of such directive); and 52 | 53 | vii. other similar, equivalent or corresponding rights throughout the world 54 | based on applicable law or treaty, and any national implementations thereof. 55 | 56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 59 | and Related Rights and associated claims and causes of action, whether now 60 | known or unknown (including existing as well as future claims and causes of 61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 62 | duration provided by applicable law or treaty (including future time 63 | extensions), (iii) in any current or future medium and for any number of 64 | copies, and (iv) for any purpose whatsoever, including without limitation 65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 66 | the Waiver for the benefit of each member of the public at large and to the 67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 68 | shall not be subject to revocation, rescission, cancellation, termination, or 69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 70 | by the public as contemplated by Affirmer's express Statement of Purpose. 71 | 72 | 3. Public License Fallback. Should any part of the Waiver for any reason be 73 | judged legally invalid or ineffective under applicable law, then the Waiver 74 | shall be preserved to the maximum extent permitted taking into account 75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 76 | is so judged Affirmer hereby grants to each affected person a royalty-free, 77 | non transferable, non sublicensable, non exclusive, irrevocable and 78 | unconditional license to exercise Affirmer's Copyright and Related Rights in 79 | the Work (i) in all territories worldwide, (ii) for the maximum duration 80 | provided by applicable law or treaty (including future time extensions), (iii) 81 | in any current or future medium and for any number of copies, and (iv) for any 82 | purpose whatsoever, including without limitation commercial, advertising or 83 | promotional purposes (the "License"). The License shall be deemed effective as 84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 85 | License for any reason be judged legally invalid or ineffective under 86 | applicable law, such partial invalidity or ineffectiveness shall not 87 | invalidate the remainder of the License, and in such case Affirmer hereby 88 | affirms that he or she will not (i) exercise any of his or her remaining 89 | Copyright and Related Rights in the Work or (ii) assert any associated claims 90 | and causes of action with respect to the Work, in either case contrary to 91 | Affirmer's express Statement of Purpose. 92 | 93 | 4. Limitations and Disclaimers. 94 | 95 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 96 | surrendered, licensed or otherwise affected by this document. 97 | 98 | b. Affirmer offers the Work as-is and makes no representations or warranties 99 | of any kind concerning the Work, express, implied, statutory or otherwise, 100 | including without limitation warranties of title, merchantability, fitness 101 | for a particular purpose, non infringement, or the absence of latent or 102 | other defects, accuracy, or the present or absence of errors, whether or not 103 | discoverable, all to the greatest extent permissible under applicable law. 104 | 105 | c. Affirmer disclaims responsibility for clearing rights of other persons 106 | that may apply to the Work or any use thereof, including without limitation 107 | any person's Copyright and Related Rights in the Work. Further, Affirmer 108 | disclaims responsibility for obtaining any necessary consents, permissions 109 | or other rights required for any use of the Work. 110 | 111 | d. Affirmer understands and acknowledges that Creative Commons is not a 112 | party to this document and has no duty or obligation with respect to this 113 | CC0 or use of the Work. 114 | 115 | For more information, please see 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | peatio-client-python 2 | ==================== 3 | 4 | A simple python client for Crypto Trade Site [Peatio.com](http://peatio.com) 5 | 6 | ## Usage ## 7 | 8 | ### Membership API ### 9 | 10 | ```python 11 | from lib.client import Client, get_api_path 12 | 13 | client = Client(access_key='your access key', secret_key='your secret key') 14 | 15 | #get members API url using get_api_path by key 'members' 16 | #API mapping of get_api_path 17 | { 18 | 'my_trades' : '/api/v2/trades/my.json' 19 | 'members' : '/api/v2/members/me.json' 20 | 'orders' : '/api/v2/orders.json' 21 | 'delete_order': '/api/v2/order/delete.json' 22 | 'order_book' : '/api/v2/order_book.json' 23 | 'clear' : '/api/v2/orders/clear.json' 24 | 'trades' : '/api/v2/trades.json' 25 | 'order' : '/api/v2/order.json' 26 | 'multi_orders': '/api/v2/orders/multi.json' 27 | 'tickers' : '/api/v2/tickers/{market}.json' 28 | 'markets' : '/api/v2/markets.json' 29 | 'k' : '/api/v2/k.json' 30 | } 31 | 32 | url = get_api_path('members') 33 | 34 | 35 | #or you can give url a plain text, just like this: 36 | url = "/api/v2/members/me.json" 37 | 38 | #send request 39 | res = client.get(url) 40 | 41 | #if your access key and secret are correct, res should be a dictionary like this: 42 | { 43 | u'email': u'zhaoyu.johnny@@gmail.com', 44 | u'activated': True, 45 | u'sn': u'PEAA*******', 46 | u'name': u'JohnnyZhao', 47 | u'accounts': 48 | [ 49 | {u'currency': u'cny', 50 | u'balance': u'1000', 51 | u'locked': u'0.0'}, 52 | {u'currency': u'btc', 53 | u'balance': u'31', 54 | u'locked': u'0.0'}, 55 | {u'currency': u'pts', 56 | u'balance': u'60', 57 | u'locked': u'0.0'}, 58 | {u'currency': u'dog', 59 | u'balance': u'23952', 60 | u'locked': u'11.0'} 61 | ], 62 | } 63 | 64 | ``` 65 | 66 | ### Markets API ### 67 | 68 | ```python 69 | 70 | #get markets 71 | markets = client.get(get_api_path('markets')) 72 | print markets 73 | 74 | #markets should be a list like this 75 | [ 76 | {u'id': u'btccny', u'name': u'BTC/CNY'}, 77 | {u'id': u'ptscny', u'name': u'PTS/CNY'}, 78 | {u'id': u'dogcny', u'name': u'DOG/CNY'}, 79 | {u'id': u'dogbtc', u'name': u'DOG/BTC'} 80 | ] 81 | 82 | ``` 83 | 84 | ### Orders API ### 85 | 86 | ```python 87 | 88 | #get your orders in a specific market 89 | orders = client.get(get_api_path('orders'), {'market': 'btccny'}) 90 | print orders 91 | 92 | #orders will be an empty list or list like this 93 | [ 94 | {u'created_at': u'2014-07-05T14:56:25+08:00', 95 | u'remaining_volume': u'11.0', 96 | u'price': u'0.01', 97 | u'side': u'sell', 98 | u'volume': u'11.0', 99 | u'state': u'wait', 100 | u'avg_price': u'0.0', 101 | u'executed_volume': u'0.0', 102 | u'id': 299751, 103 | u'market': u'dogcny' 104 | } 105 | ] 106 | 107 | #create new order 108 | #sell 10 dogecoins at price 0.01 109 | params = {'market': 'dogcny', 'side': 'sell', 'volume': 10, 'price': 0.01} 110 | res = client.post(get_api_path('orders'), params) 111 | print res 112 | 113 | #buy 10 dogecoins at price 0.001 114 | params = {'market': 'dogcny', 'side': 'buy', 'volume': 10, 'price': 0.001} 115 | res = client.post(get_api_path('orders'), params) 116 | print res 117 | 118 | #clear orders 119 | #clear all orders in all markets 120 | res = client.post(get_api_path('clear')) 121 | print res 122 | 123 | #delete a specific order by order_id 124 | #first, let's create an sell order 125 | #sell 10 dogecoins at price 0.01 126 | params = {'market': 'dogcny', 'side': 'sell', 'volume': 12, 'price': 0.01} 127 | res = client.post(get_api_path('orders'), params) 128 | print res 129 | order_id = res['id'] 130 | 131 | #delete this order 132 | params = {"id": order_id} 133 | res = client.post(get_api_path('delete_order'), params) 134 | 135 | #create multi orders 136 | params = {'market': 'dogcny', 'orders': [{'side': 'buy', 'volume': 12, 'price': 0.0002}, {'side': 'sell', 'volume': 11, 'price': 0.01}]} 137 | res = client.post(get_api_path('multi_orders'), params) 138 | 139 | ``` 140 | 141 | ## More demos ## 142 | 143 | ### for more usage demos, checkout [demo.py](https://github.com/JohnnyZhao/peatio-client-python/blob/master/demo.py) file ### 144 | 145 | ## Contribute ## 146 | 147 | Pull requests are welcomed 148 | -------------------------------------------------------------------------------- /demo.py: -------------------------------------------------------------------------------- 1 | import time 2 | import urllib2 3 | from lib.client import Client, get_api_path 4 | 5 | client = Client(access_key='your access key', secret_key='your secret key') 6 | 7 | #demo of GET APIs 8 | 9 | #get member info 10 | print client.get(get_api_path('members')) 11 | 12 | #get markets 13 | markets = client.get(get_api_path('markets')) 14 | print "markets:", markets 15 | 16 | #get tickers of each market 17 | #market should be specified in url 18 | print 19 | print "tickers in markets" 20 | for market in markets: 21 | print client.get(get_api_path('tickers') % market['id']) 22 | 23 | #get orders of each market 24 | #market should be specified in params 25 | print 26 | print "orders in markets" 27 | for market in markets: 28 | print client.get(get_api_path('orders'), {'market': market['id']}) 29 | 30 | #get order book 31 | print client.get(get_api_path('order_book'), params={'market': 'btccny'}) 32 | 33 | #get tardes 34 | print client.get(get_api_path('trades'), params={'market': 'btccny'}) 35 | 36 | #get my trades 37 | print client.get(get_api_path('my_trades'), params={'market': 'btccny'}) 38 | 39 | #get k line 40 | print client.get(get_api_path('k'), params={'market': 'btccny'}) 41 | 42 | 43 | #demo of POST APIs 44 | #DANGROUS, you better use test account to debug POST APIs 45 | 46 | """ 47 | markets = client.get(get_api_path('markets')) 48 | print markets 49 | 50 | #sell 10 dogecoins at price 0.01 51 | params = {'market': 'dogcny', 'side': 'sell', 'volume': 10, 'price': 0.01} 52 | res = client.post(get_api_path('orders'), params) 53 | print res 54 | 55 | #buy 10 dogecoins at price 0.001 56 | params = {'market': 'dogcny', 'side': 'buy', 'volume': 10, 'price': 0.001} 57 | res = client.post(get_api_path('orders'), params) 58 | print res 59 | 60 | #clear all orders in all markets 61 | res = client.post(get_api_path('clear')) 62 | print res 63 | #delete a specific order by order_id 64 | 65 | #first, let's create an sell order 66 | #sell 10 dogecoins at price 0.01 67 | params = {'market': 'dogcny', 'side': 'sell', 'volume': 12, 'price': 0.01} 68 | res = client.post(get_api_path('orders'), params) 69 | print res 70 | order_id = res['id'] 71 | 72 | #delete this order 73 | params = {"id": order_id} 74 | res = client.post(get_api_path('delete_order'), params) 75 | print res 76 | 77 | #create multi orders 78 | params = {'market': 'dogcny', 'orders': [{'side': 'buy', 'volume': 12, 'price': 0.0002}, {'side': 'sell', 'volume': 11, 'price': 0.01}]} 79 | res = client.post(get_api_path('multi_orders'), params) 80 | print res 81 | """ 82 | -------------------------------------------------------------------------------- /lib/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohnnyZhao/peatio-client-python/94a51010a63ff730a10ec58c6b32c647a8ba24a6/lib/__init__.py -------------------------------------------------------------------------------- /lib/auth.py: -------------------------------------------------------------------------------- 1 | import hmac 2 | import hashlib 3 | import time 4 | import urllib 5 | 6 | class Auth(): 7 | def __init__(self, access_key, secret_key): 8 | self.access_key = access_key 9 | self.secret_key = secret_key 10 | 11 | def urlencode(self, params): 12 | keys = params.keys() 13 | keys.sort() 14 | query = '' 15 | for key in keys: 16 | value = params[key] 17 | if key != "orders": 18 | query = "%s&%s=%s" % (query, key, value) if len(query) else "%s=%s" % (key, value) 19 | else: 20 | #this ugly code is for multi orders API, there should be an elegant way to do this 21 | d = {key: params[key]} 22 | for v in value: 23 | ks = v.keys() 24 | ks.sort() 25 | for k in ks: 26 | item = "orders[][%s]=%s" % (k, v[k]) 27 | query = "%s&%s" % (query, item) if len(query) else "%s" % item 28 | return query 29 | 30 | def sign(self, verb, path, params=None): 31 | query = self.urlencode(params) 32 | msg = "|".join([verb, path, query]) 33 | signature = hmac.new(self.secret_key, msg=msg, digestmod=hashlib.sha256).hexdigest() 34 | return signature 35 | 36 | def sign_params(self, verb, path, params=None): 37 | if not params: 38 | params = {} 39 | params.update({'tonce': int(1000*time.time()), 'access_key': self.access_key}) 40 | query = self.urlencode(params) 41 | signature = self.sign(verb, path, params) 42 | return signature, query 43 | -------------------------------------------------------------------------------- /lib/client.py: -------------------------------------------------------------------------------- 1 | import urllib2 2 | import json 3 | 4 | from lib.auth import Auth 5 | 6 | BASE_URL = 'https://peatio.com' 7 | 8 | API_BASE_PATH = '/api/v2' 9 | API_PATH_DICT = { 10 | # GET 11 | 'members': '%s/members/me.json', 12 | 'markets': '%s/markets.json', 13 | 14 | #market code required in url as {market}.json 15 | 'tickers' : '%s/tickers/%%s.json', 16 | #market required in url query string as '?market={market}' 17 | 'orders': '%s/orders.json', 18 | 19 | #order id required in url query string as '?id={id}' 20 | 'order': '%s/order.json', 21 | 22 | #market required in url query string as '?market={market}' 23 | 'order_book': '%s/order_book.json', 24 | 25 | #market required in url query string as '?market={market}' 26 | 'trades': '%s/trades.json', 27 | 28 | #market required in url query string as '?market={market}' 29 | 'my_trades': '%s/trades/my.json', 30 | 31 | 'k': '%s/k.json', 32 | #clear orders in all markets 33 | 'clear': '%s/orders/clear.json', 34 | 35 | #delete a specific order 36 | 'delete_order': '%s/order/delete.json', 37 | 38 | #TODO multi orders API 39 | 'multi_orders': '%s/orders/multi.json', 40 | } 41 | 42 | def get_api_path(name): 43 | path_pattern = API_PATH_DICT[name] 44 | return path_pattern % API_BASE_PATH 45 | 46 | class Client(): 47 | 48 | def __init__(self, access_key=None, secret_key=None): 49 | if access_key and secret_key: 50 | self.auth = Auth(access_key, secret_key) 51 | else: 52 | from conf import ACCESS_KEY, SECRET_KEY 53 | self.auth = Auth(ACCESS_KEY, SECRET_KEY) 54 | 55 | def get(self, path, params=None): 56 | verb = "GET" 57 | signature, query = self.auth.sign_params(verb, path, params) 58 | url = "%s%s?%s&signature=%s" % (BASE_URL, path, query, signature) 59 | resp = urllib2.urlopen(url) 60 | data = resp.readlines() 61 | if len(data): 62 | return json.loads(data[0]) 63 | 64 | def post(self, path, params=None): 65 | verb = "POST" 66 | print params 67 | signature, query = self.auth.sign_params(verb, path, params) 68 | url = "%s%s" % (BASE_URL, path) 69 | data = "%s&signature=%s" % (query, signature) 70 | print data 71 | print url 72 | resp = urllib2.urlopen(url, data) 73 | data = resp.readlines() 74 | if len(data): 75 | return json.loads(data[0]) 76 | -------------------------------------------------------------------------------- /lib/conf.py.sample: -------------------------------------------------------------------------------- 1 | ACCESS_KEY = "YOUR ACCESS KEY" 2 | SECRET_KEY = "YOUR SECRET KEY" 3 | 4 | --------------------------------------------------------------------------------