Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added place_market_order #16

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>cex.io-api-python</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions .pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
</pydev_project>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding//cexapi/cexapi.py=utf-8
Empty file modified CHANGES.txt
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Binary file added cexapi/.cexapi.py.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion cexapi/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from cexapi import *
from .cexapi import *
168 changes: 87 additions & 81 deletions cexapi/cexapi.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,81 +1,87 @@
# -*- coding: utf-8 -*-
# Author: t0pep0
# e-mail: t0pep0.gentoo@gmail.com
# Jabber: t0pep0@jabber.ru
# BTC : 1ipEA2fcVyjiUnBqUx7PVy5efktz2hucb
# donate free =)
import hmac
import hashlib
import time
import urllib
import urllib2
import json


class API(object):
__username = ''
__api_key = ''
__api_secret = ''
__nonce_v = ''

# Init class
def __init__(self, username, api_key, api_secret):
self.__username = username
self.__api_key = api_key
self.__api_secret = api_secret

# get timestamp as nonce
def __nonce(self):
self.__nonce_v = '{:.10f}'.format(time.time() * 1000).split('.')[0]

# generate segnature
def __signature(self):
string = self.__nonce_v + self.__username + self.__api_key # create string
signature = hmac.new(self.__api_secret, string, digestmod=hashlib.sha256).hexdigest().upper() # create signature
return signature

def __post(self, url, param): # Post Request (Low Level API call)
params = urllib.urlencode(param)
req = urllib2.Request(url, params, {'User-agent': 'bot-cex.io-' + self.__username})
page = urllib2.urlopen(req).read()
return page

def api_call(self, method, param={}, private=0, couple=''): # api call (Middle level)
url = 'https://cex.io/api/' + method + '/' # generate url
if couple != '':
url = url + couple + '/' # set couple if needed
if private == 1: # add auth-data if needed
self.__nonce()
param.update({
'key': self.__api_key,
'signature': self.__signature(),
'nonce': self.__nonce_v})
answer = self.__post(url, param) # Post Request
return json.loads(answer) # generate dict and return

def ticker(self, couple='GHS/BTC'):
return self.api_call('ticker', {}, 0, couple)

def order_book(self, couple='GHS/BTC'):
return self.api_call('order_book', {}, 0, couple)

def trade_history(self, since=1, couple='GHS/BTC'):
return self.api_call('trade_history', {"since": str(since)}, 0, couple)

def balance(self):
return self.api_call('balance', {}, 1)

def current_orders(self, couple='GHS/BTC'):
return self.api_call('open_orders', {}, 1, couple)

def cancel_order(self, order_id):
return self.api_call('cancel_order', {"id": order_id}, 1)

def place_order(self, ptype='buy', amount=1, price=1, couple='GHS/BTC'):
return self.api_call('place_order', {"type": ptype, "amount": str(amount), "price": str(price)}, 1, couple)

def price_stats(self, last_hours, max_resp_arr_size, couple='GHS/BTC'):
return self.api_call(
'price_stats',
{"lastHours": last_hours, "maxRespArrSize": max_resp_arr_size},
0, couple)
# -*- coding: utf-8 -*-
# Author: t0pep0
# e-mail: t0pep0.gentoo@gmail.com
# Jabber: t0pep0@jabber.ru
# BTC : 1ipEA2fcVyjiUnBqUx7PVy5efktz2hucb
# donate free =)
import hashlib
import hmac
import time
import requests


class API(object):
__username = ''
__api_key = ''
__api_secret = ''
__nonce_v = ''

# Init class
def __init__(self, username="", api_key="", api_secret=""):
self.__username = username
self.__api_key = api_key
self.__api_secret = api_secret

# get timestamp as nonce
def __nonce(self):
self.__nonce_v = '{:.10f}'.format(time.time() * 1000).split('.')[0]

# generate segnature
def __signature(self):
string = self.__nonce_v + self.__username + self.__api_key # create string
signature = hmac.new(self.__api_secret, string, digestmod=hashlib.sha256).hexdigest(
).upper() # create signature
return signature

def __post(self, url, params): # Post Request (Low Level API call)
req = requests.post(
url, data=params, headers={'User-agent': 'bot-cex.io-' + self.__username})
page = req.json()
return page

def api_call(self, method, param={}, private=0, couple=''): # api call (Middle level)
url = 'https://cex.io/api/' + method + '/' # generate url
if couple != '':
url = url + couple + '/' # set couple if needed
if private == 1: # add auth-data if needed
self.__nonce()
param.update({
'key': self.__api_key,
'signature': self.__signature(),
'nonce': self.__nonce_v})
answer = self.__post(url, param) # Post Request
return answer # generate dict and return

def ticker(self, couple='GHS/BTC'):
return self.api_call('ticker', {}, 0, couple)

def order_book(self, couple='GHS/BTC'):
return self.api_call('order_book', {}, 0, couple)

def trade_history(self, since=1, couple='GHS/BTC'):
return self.api_call('trade_history', {"since": str(since)}, 0, couple)

def balance(self):
return self.api_call('balance', {}, 1)

def current_orders(self, couple='GHS/BTC'):
return self.api_call('open_orders', {}, 1, couple)

def cancel_order(self, order_id):
return self.api_call('cancel_order', {"id": order_id}, 1)

def place_order(self, ptype='buy', amount=1, price=1, couple='GHS/BTC'):
return self.api_call('place_order', {"type": ptype, "amount": str(amount), "price": str(price)}, 1, couple)

def place_market_order(self, ptype='buy', amount=1, couple='GHS/BTC'):
return self.api_call('place_order', {"type": ptype, "amount": str(amount), "order_type": "market"}, 1, couple)

def markets(self):
response = self.api_call("currency_limits", )
return [pair['symbol1'] + "/" + pair['symbol2'] for pair in response['data']['pairs']]

def price_stats(self, last_hours, max_resp_arr_size, couple='GHS/BTC'):
return self.api_call(
'price_stats',
{"lastHours": last_hours, "maxRespArrSize": max_resp_arr_size},
0, couple)
56 changes: 28 additions & 28 deletions cexapi/test.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# -*- coding: utf-8 -*-
import cexapi

demo = cexapi.api(username, api_key, api_secret)
print "Ticker (GHS/BTC)"
print demo.ticker() ## or demo.ticker('GHS/BTC')
print "Ticker (BF1/BTC)"
print demo.ticker('BF1/BTC')
print "Order book (GHS/BTC)"
print demo.order_book() ## or demo.order_book('GHS/BTC')
print "Order book (BF1/BTC)"
print demo.order_book('BF1/BTC')
print "Trade history since=100 (GHS/BTC)"
print demo.trade_history(100) ## or (100,'GHS/BTC')
print "Trade history since=100 (BF1/BTC)"
print demo.trade_history(100,'BF1/BTC')
print "Balance"
print demo.balance()
print "Open orders (GHS/BTC)"
print demo.current_orders() ## or ('GHS/BTC')
print "Open orders (BF1/BTC)"
print demo.current_orders('BF1/BTC')
print "Cancel order (order_id=100)"
print demo.cancel_order(100)
print "Plaсe order buy 4GHS/0.1BTC)"
print demo.place_order('buy',1,0.1) ## or ('buy',1,0.1,'GHS/BTC')
print "Open orders sell 1BF1/1.5BTC"
print demo.place_order('sell',1,1.5,'BF1/BTC')
# -*- coding: utf-8 -*-
from . import cexapi
demo = cexapi.api(username, api_key, api_secret)
print("Ticker (GHS/BTC)")
print(demo.ticker()) ## or demo.ticker('GHS/BTC')
print("Ticker (BF1/BTC)")
print(demo.ticker('BF1/BTC'))
print("Order book (GHS/BTC)")
print(demo.order_book()) ## or demo.order_book('GHS/BTC')
print("Order book (BF1/BTC)")
print(demo.order_book('BF1/BTC'))
print("Trade history since=100 (GHS/BTC)")
print(demo.trade_history(100)) ## or (100,'GHS/BTC')
print("Trade history since=100 (BF1/BTC)")
print(demo.trade_history(100,'BF1/BTC'))
print("Balance")
print(demo.balance())
print("Open orders (GHS/BTC)")
print(demo.current_orders()) ## or ('GHS/BTC')
print("Open orders (BF1/BTC)")
print(demo.current_orders('BF1/BTC'))
print("Cancel order (order_id=100)")
print(demo.cancel_order(100))
print("Plaсe order buy 4GHS/0.1BTC)")
print(demo.place_order('buy',1,0.1)) ## or ('buy',1,0.1,'GHS/BTC')
print("Open orders sell 1BF1/1.5BTC")
print(demo.place_order('sell',1,1.5,'BF1/BTC'))
Empty file modified setup.cfg
100644 → 100755
Empty file.
Empty file modified setup.py
100644 → 100755
Empty file.