├── CHANGES.txt ├── LICENSE ├── README.md ├── cexapi ├── __init__.py ├── cexapi.py └── test.py ├── setup.cfg └── setup.py /CHANGES.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matveyco/cex.io-api-python/db37d223ac23f96425d106f1940fc882826cfbd8/CHANGES.txt -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 matveyco 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cex.io-api-python 2 | ================= 3 | 4 | CEX.IO API integration. Python sources. 5 | 6 | ##Intro 7 | 8 | 1. Download lib 9 | 2. Get API key and API secret on https://cex.io/trade/profile 10 | 11 | ##How to use? 12 | 13 | ###1. Create your python project 14 | 15 | ###2. Add "import cexapi" 16 | 17 | ###3. Create class 18 | ```python 19 | api = cexapi.api(username,api_key,api_secret) 20 | ``` 21 | username - your username on cex.io 22 | api_key - your API key 23 | api_secret - your API secret code 24 | 25 | ###4. Methods and parameters: 26 | 27 | ####a) API method parametrs 28 | ``` 29 | 1. couple = ("GHS\BTC" | "BF1\BTC") currency pair 30 | 2. since = integer return trades with tid >= since 31 | 3. order_id = integer 32 | 4. ptype = ("sell" | "buy") type of order 33 | 5. amount = float 34 | 6. price = float 35 | ``` 36 | 37 | ####b) API methods 38 | ``` 39 | 1. ticker(couple = 'GHS/BTC') - get ticker 40 | 2. order_book(couple = 'GHS/BTC') - get order 41 | 3. trade_history(since = 1, couple = 'GHS/BTC') - get all order 42 | 4. balance() - get your balance 43 | 5. current_orders(couple = 'GHS/BTC') - get open order 44 | 6. cancel_order(order_id) - cancel order №order_id 45 | 7. place_order(ptype = 'buy', amount = 1, price = 1, couple = 'GHS/BTC') - create order 46 | ``` 47 | 48 | ####c) Full API documentation: https://cex.io/api 49 | 50 | ###5. Examples 51 | 52 | ####Connect and get balance: 53 | ```python 54 | import cexapi 55 | api = cexapi.api(username, api_key, api_secret) 56 | print api.balance() 57 | ``` 58 | ```json 59 | {'timestamp': '1383378763', 'BTC': {'available': '0.04722110', 'orders': '0.00170000'}, 'GHS': {'available': '0.01000000'} } 60 | ``` 61 | 62 | ####Get balance: 63 | ```python 64 | print api.balance() 65 | ``` 66 | ```json 67 | {'timestamp': '1383379054', 'BTC': {'available': '0.04614310', 'orders': '0.00170000'}, 'GHS': {'available': '0.02000000'}} 68 | ``` 69 | 70 | ####Get API ticker: 71 | ```python 72 | print api.ticker('GHS/BTC') 73 | ``` 74 | ```json 75 | {'volume': '7154.78339022', 'last': '0.1078', 'timestamp': '1383379041', 'bid': '0.10778', 'high': '0.10799999', 'low': '0.10670076', 'ask': '0.10780000000000001'} 76 | ``` 77 | 78 | ####Get order book: 79 | ```python 80 | print api.order_book('BF1/BTC') 81 | ``` 82 | ```json 83 | {'timestamp': '1383378967', 'bids': [['1.7', '0.30100000'], ['1.67', '0.00011000'], ['0.8', '0.02070000'], ['0.1002', '0.27748002'], ['0.1', '0.10000000'], ['0.011', '0.30500000'], ['0.009', '1.00000000'], ['0.00171', '0.00100000'], ['0.0012', '1.00000000'], ['0.00116819', '0.50000000'], ['0.001002', '33.00000000'], ['0.001001', '53.00000000'], ['0.001', '3.00000000'], ['0.00097626', '36.00000000'], ['0.0006', '85.00000000'], ['0.00058409', '0.50000000'], ['0.0004889', '0.06823960'], ['0.0003', '1.00000000'], ['0.00029204', '0.90000000'], ['0.0001', '101.00000000']], 'asks': []} 84 | ``` 85 | 86 | ####Get your current active orders: 87 | ```python 88 | print api.current_orders('BF1/BTC') 89 | ``` 90 | ```json 91 | [{'price': '1.7', 'amount': '0.00100000', 'time': '1383378514737', 'type': 'buy', 'id': '6219104', 'pending': '0.00100000'}] 92 | ``` 93 | 94 | ####Place new order: 95 | ```python 96 | print api.place_order('buy', 0.001, 1.7, 'BF1/BTC') 97 | ``` 98 | ```json 99 | {'price': '1.7', 'amount': '0.00100000', 'time': 1383378987622, 'type': 'buy', 'id': '6219145', 'pending': '0.00100000'} 100 | ``` 101 | 102 | ####Place another order (GHS/BTC): 103 | ```python 104 | print api.place_order('buy', 0.01, 0.10789, 'GHS/BTC') 105 | ``` 106 | ```json 107 | {'price': '0.10789', 'amount': '0.01000000', 'time': 1383379024072, 'type': 'buy', 'id': '6219150', 'pending': '0.00000000'} 108 | ``` 109 | 110 | ####Cancel order: 111 | ```python 112 | print api.cancel_order(6219145) 113 | ``` 114 | ```json 115 | True 116 | ``` 117 | 118 | -------------------------------------------------------------------------------- /cexapi/__init__.py: -------------------------------------------------------------------------------- 1 | from cexapi import * 2 | -------------------------------------------------------------------------------- /cexapi/cexapi.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Author: t0pep0 3 | # e-mail: t0pep0.gentoo@gmail.com 4 | # Jabber: t0pep0@jabber.ru 5 | # BTC : 1ipEA2fcVyjiUnBqUx7PVy5efktz2hucb 6 | # donate free =) 7 | import hmac 8 | import hashlib 9 | import time 10 | import urllib 11 | import urllib2 12 | import json 13 | 14 | 15 | class API(object): 16 | __username = '' 17 | __api_key = '' 18 | __api_secret = '' 19 | __nonce_v = '' 20 | 21 | # Init class 22 | def __init__(self, username, api_key, api_secret): 23 | self.__username = username 24 | self.__api_key = api_key 25 | self.__api_secret = api_secret 26 | 27 | # get timestamp as nonce 28 | def __nonce(self): 29 | self.__nonce_v = '{:.10f}'.format(time.time() * 1000).split('.')[0] 30 | 31 | # generate segnature 32 | def __signature(self): 33 | string = self.__nonce_v + self.__username + self.__api_key # create string 34 | signature = hmac.new(self.__api_secret, string, digestmod=hashlib.sha256).hexdigest().upper() # create signature 35 | return signature 36 | 37 | def __post(self, url, param): # Post Request (Low Level API call) 38 | params = urllib.urlencode(param) 39 | req = urllib2.Request(url, params, {'User-agent': 'bot-cex.io-' + self.__username}) 40 | page = urllib2.urlopen(req).read() 41 | return page 42 | 43 | def api_call(self, method, param={}, private=0, couple=''): # api call (Middle level) 44 | url = 'https://cex.io/api/' + method + '/' # generate url 45 | if couple != '': 46 | url = url + couple + '/' # set couple if needed 47 | if private == 1: # add auth-data if needed 48 | self.__nonce() 49 | param.update({ 50 | 'key': self.__api_key, 51 | 'signature': self.__signature(), 52 | 'nonce': self.__nonce_v}) 53 | answer = self.__post(url, param) # Post Request 54 | return json.loads(answer) # generate dict and return 55 | 56 | def ticker(self, couple='GHS/BTC'): 57 | return self.api_call('ticker', {}, 0, couple) 58 | 59 | def order_book(self, couple='GHS/BTC'): 60 | return self.api_call('order_book', {}, 0, couple) 61 | 62 | def trade_history(self, since=1, couple='GHS/BTC'): 63 | return self.api_call('trade_history', {"since": str(since)}, 0, couple) 64 | 65 | def balance(self): 66 | return self.api_call('balance', {}, 1) 67 | 68 | def current_orders(self, couple='GHS/BTC'): 69 | return self.api_call('open_orders', {}, 1, couple) 70 | 71 | def cancel_order(self, order_id): 72 | return self.api_call('cancel_order', {"id": order_id}, 1) 73 | 74 | def place_order(self, ptype='buy', amount=1, price=1, couple='GHS/BTC'): 75 | return self.api_call('place_order', {"type": ptype, "amount": str(amount), "price": str(price)}, 1, couple) 76 | 77 | def price_stats(self, last_hours, max_resp_arr_size, couple='GHS/BTC'): 78 | return self.api_call( 79 | 'price_stats', 80 | {"lastHours": last_hours, "maxRespArrSize": max_resp_arr_size}, 81 | 0, couple) 82 | -------------------------------------------------------------------------------- /cexapi/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cexapi 3 | 4 | demo = cexapi.api(username, api_key, api_secret) 5 | print "Ticker (GHS/BTC)" 6 | print demo.ticker() ## or demo.ticker('GHS/BTC') 7 | print "Ticker (BF1/BTC)" 8 | print demo.ticker('BF1/BTC') 9 | print "Order book (GHS/BTC)" 10 | print demo.order_book() ## or demo.order_book('GHS/BTC') 11 | print "Order book (BF1/BTC)" 12 | print demo.order_book('BF1/BTC') 13 | print "Trade history since=100 (GHS/BTC)" 14 | print demo.trade_history(100) ## or (100,'GHS/BTC') 15 | print "Trade history since=100 (BF1/BTC)" 16 | print demo.trade_history(100,'BF1/BTC') 17 | print "Balance" 18 | print demo.balance() 19 | print "Open orders (GHS/BTC)" 20 | print demo.current_orders() ## or ('GHS/BTC') 21 | print "Open orders (BF1/BTC)" 22 | print demo.current_orders('BF1/BTC') 23 | print "Cancel order (order_id=100)" 24 | print demo.cancel_order(100) 25 | print "Plaсe order buy 4GHS/0.1BTC)" 26 | print demo.place_order('buy',1,0.1) ## or ('buy',1,0.1,'GHS/BTC') 27 | print "Open orders sell 1BF1/1.5BTC" 28 | print demo.place_order('sell',1,1.5,'BF1/BTC') 29 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [nosetests] 2 | match = ^test 3 | nocapture = 1 4 | cover-package = v12 5 | with-coverage = 1 6 | cover-erase = 1 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from setuptools import setup, find_packages 4 | 5 | here = os.path.abspath(os.path.dirname(__file__)) 6 | README = open(os.path.join(here, 'README.md')).read() 7 | CHANGES = open(os.path.join(here, 'CHANGES.txt')).read() 8 | 9 | requires = [ 10 | ] 11 | 12 | setup( 13 | name='cexapi', 14 | version='0.1', 15 | description='CEX.IO API integration.', 16 | long_description=README + '\n\n' + CHANGES, 17 | classifiers=[ 18 | "Programming Language :: Python", 19 | "Topic :: Office/Business :: Financial" 20 | ], 21 | author='t0pep0', 22 | author_email='t0pep0.gentoo@gmail.com', 23 | url='', 24 | keywords='cex api bitcoin', 25 | packages=find_packages(), 26 | include_package_data=True, 27 | zip_safe=False, 28 | install_requires=requires, 29 | tests_require=requires, 30 | test_suite="cexapi", 31 | ) 32 | --------------------------------------------------------------------------------