Skip to content

Commit

Permalink
add coincheck
Browse files Browse the repository at this point in the history
  • Loading branch information
kmn committed Apr 6, 2015
1 parent d389586 commit 944596a
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
*.pyc
*.swp
*.swo
settings.py
samples/
__pycache__/*
*.python-version
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,6 @@
## 0.0.1 (2015-04-06)

- Birth!



2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 S.Kamon
Copyright (c) 2015 Shohei Kamon <kamonshohei@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
105 changes: 105 additions & 0 deletions README.md
@@ -0,0 +1,105 @@
# Coincheck: Coincheck Api Library

coincheck は [coincheck](https://coincheck.jp) の python 製 api ライブラリです.

```
> from coincheck import market
> market.ticker()
'{"last":30930,"bid":30810,"ask":30930,"high":31500,"low":30220,"volume":"560.51814602","timestamp":1428338044}'
```


## Documentation

Documentation for Coincheck API is available at https://coincheck.jp/documents/exchange/api

### Order

注文が決済されたら、キャンセルできないので注意してください.

```
# 20000 jpy で 0.1 の bitcoin を購入する注文をする.
> coincheck.order.buy_btc_jpy(rate=20000, amount=0.1)
{"success":true,"id":1355414,"amount":"0.1","rate":"20000.0","order_type":"buy","pair":"btc_jpy","created_at":"2015-04-06T16:56:19.821Z"}
# 40000 jpy で 0.1 の bitcoin を売却する注文をする.
> coincheck.order.sell_btc_jpy(rate=40000, amount=0.1)
{"success":true,"id":1355421,"amount":"0.1","rate":"40000.0","order_type":"sell","pair":"btc_jpy","created_at":"2015-04-06T16:57:26.487Z"}
# 自分の注文の一覧を表示する.
> coincheck.order.list()
{"success":true,"orders":[{"id":676197,"order_type":"buy","rate":20000,"pair":"btc_jpy","pending_amount":"0.1","created_at":"2015-04-06T16:56:19.000Z"},{"id":676201,"order_type":"sell","rate":40000,"pair":"btc_jpy","pending_amount":"0.1","created_at":"2015-04-06T16:57:26.000Z"}]}
# 注文番号 676197 の注文をキャンセルする. (注文番号は coincheck.order.list() のid)
> coincheck.order.cancel('676197')
{"success":true,"id":676197}
# 自分の取引履歴を表示する.
> coincheck.order.history()
{"success":true,"transactions":[{"id":21118,"created_at":"2015-04-06T16:39:10.000Z","funds":{"btc":"-0.02","jpy":"624.6"}}]}
```

### Market

coincheck の市場情報を取得します.

```
# coincheck の最新市場情報を取得する.
> coincheck.market.ticker()
{"last":30820,"bid":30810,"ask":30820,"high":31500,"low":30220,"volume":"559.78124602","timestamp":1428340013}
# coincheck の最新取引履歴を取得する.
> coincheck.market.trade()
[{"id":16143,"amount":"0.25","rate":30820,"order_type":"sell","created_at":"2015-04-06T17:02:04.000Z"},{"id":16142,"amount":"0.249","rate":30930,"order_type":"buy","created_
at":"2015-04-06T16:33:12.000Z"},{"id":16141,"amount":"0.4174","rate":30810,"order_type": ...
# coincheck の最新板情報を取得する.
> coincheck.market.orderbooks()
{"asks":[[31740,"2.25731223"],[31750,"0.35"],...],"bids":[[30810,"0.16228497"],[30700,"3.404"],...]}
```

### Account

ユーザのアカウント情報を取得します.

```
# アカウントの情報を表示します.
> coincheck.account.get_info()
{"success":true,"id":0000,"email":"kamonshohei@gmail.com","identity_status":"identity_verified","bitcoin_address":"..."}
# アカウントの残高を確認できます.
> coincheck.account.get_balance()
{"success":true,"jpy":"...","btc":"...","jpy_reserved":"...","btc_reserved":"...","jpy_lend_in_use":"...","btc_lend_in_use":"...","jpy_lent":"...","btc_lent":"...","jpy_debt":"...","btc_debt":"..."}
```


## Environment

- support Python 3.x
- Do NOT support python 2.x

## Installation

```
git clone git@github.com:kmn/coincheck.git
```

## Initialization

1. copy coincheck/settings.py.tmpl to coincheck/settings.py
2. set your api access-key and secret-key to "coincheck/settings.py"
to get your api keys , see [API key](https://coincheck.jp/api_settings).


```
cat coincheck/settings.py
access_key = "your access key"
secret_key = "your secret key"
```


## TODO

- add test case
- add offset to market.trade()
8 changes: 8 additions & 0 deletions coincheck/__init__.py
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

__title__ = 'coincheck'
__version__ = '0.0.1'
__author__ = 'Shohei Kamon'
__license__ = 'MIT license'
__copyright__ = 'Copyright 2015 Shohei Kamon'

32 changes: 32 additions & 0 deletions coincheck/account.py
@@ -0,0 +1,32 @@
import time
import hmac
import hashlib
import requests
import settings
import json
from utils import make_header

"""
document: https://coincheck.jp/documents/exchange/api
"""

def get_info():
''' show user information
'''
url= 'https://coincheck.jp/api/accounts'
headers = make_header(url)
r = requests.get(url,headers=headers)
return r.text

def get_balance():
''' confirm balance
'''
url = 'https://coincheck.jp/api/accounts/balance'
headers = make_header(url)
r = requests.get(url,headers=headers)
return r.text

if __name__ == '__main__':
pass
#print(get_info())
#print(get_balance())
38 changes: 38 additions & 0 deletions coincheck/market.py
@@ -0,0 +1,38 @@
import requests

"""
document: https://coincheck.jp/documents/exchange/api
"""

base_url = "https://coincheck.jp"
api_urls = { 'ticker' : '/api/ticker',
'trades' : '/api/trades',
'order_books': '/api/order_books'
}

def public_api(url):
''' template function of public api'''
try :
url in api_urls
return requests.get(base_url + api_urls.get(url)).text
except Exception as e:
print(e)

def ticker():
'''get latest information of coincheck market'''
return public_api('ticker')

def trades():
'''get latest deal history of coincheck market'''
return public_api('trades')

def orderbooks():
'''get latest asks/bids information of coincheck market'''
return public_api('order_books')


if __name__ == '__main__':
pass
#print('ticker: ' + ticker())
#print('trades: ' + trades())
#print('order_books: ' + orderbooks())
77 changes: 77 additions & 0 deletions coincheck/order.py
@@ -0,0 +1,77 @@
import time
import hmac
import hashlib
import requests
import settings
from utils import make_header

"""
document: https://coincheck.jp/documents/exchange/api
"""


def create(rate, amount, order_type, pair):
''' create new order function
:param rate: float
:param amount: float
:param order_type: str; set 'buy' or 'sell'
:param pair: str; set 'btc_jpy'
'''
nonce = str(time.time()).split('.')[0]
key = settings.access_key
secret = settings.secret_key
payload = { 'rate': rate,
'amount': amount,
'order_type': order_type,
'pair': pair
}
url= 'https://coincheck.jp/api/exchange/orders'
body = 'rate={rate}&amount={amount}&order_type={order_type}&pair={pair}'.format(**payload)
message = nonce + url + body
signature = hmac.new(secret.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()
headers = {
'ACCESS-KEY' : key,
'ACCESS-NONCE' : nonce,
'ACCESS-SIGNATURE': signature
}
r = requests.post(url,headers=headers,data=body)
return r.text

def buy_btc_jpy(**kwargs):
return create(order_type='buy', pair='btc_jpy',**kwargs)

def sell_btc_jpy(**kwargs):
return create(order_type='sell', pair='btc_jpy',**kwargs)

def list():
''' list all open orders func
'''
url= 'https://coincheck.jp/api/exchange/orders/opens'
headers = make_header(url)
r = requests.get(url,headers=headers)
return r.text

def cancel(order_id):
''' cancel the specified order
:param order_id: order_id to be canceled
'''
url= 'https://coincheck.jp/api/exchange/orders/' + order_id
headers = make_header(url)
r = requests.delete(url,headers=headers)
return r.text

def history():
''' show payment history
'''
url= 'https://coincheck.jp/api/exchange/orders/transactions'
headers = make_header(url)
r = requests.get(url,headers=headers)
return r.text

if __name__ == '__main__':
pass
#print(buy_btc_jpy(rate=20000, amount=0.1))
#print(sell_btc_jpy(rate=40000,amount=0.1))
#print(list())
#print(history())
#print(cancel('659546'))
2 changes: 2 additions & 0 deletions coincheck/settings.py.tmpl
@@ -0,0 +1,2 @@
access_key = ""
secret_key = ""
21 changes: 21 additions & 0 deletions coincheck/utils.py
@@ -0,0 +1,21 @@
import time
import hmac
import hashlib
import settings

def make_header(url):
''' create request header function
:param url: URL for the new :class:`Request` object.
'''
nonce = str(time.time()).split('.')[0]
key = settings.access_key
secret = settings.secret_key
url = url
message = nonce + url
signature = hmac.new(secret.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()
headers = {
'ACCESS-KEY' : key,
'ACCESS-NONCE' : nonce,
'ACCESS-SIGNATURE': signature
}
return headers
1 change: 1 addition & 0 deletions requirements.txt
@@ -0,0 +1 @@
requests==2.6.0

0 comments on commit 944596a

Please sign in to comment.