grr

CCXTの基本

CCXT の基本

CryptoCurrency eXchange Trading Library の基本
https://github.com/ccxt/ccxt
https://github.com/ccxt/ccxt/blob/master/README.md

  > pip search ccxt
  ccxt (1.12.43)
  > pip install ccxt

各取引所オブジェクトに対して共通のメゾット操作が可能

  from pprint import pprint
  import ccxt
  print(ccxt.exchanges)

['bitflyer']

Public API

from pprint import pprint
import ccxt
import json

#API KEY(Public API利用時は必要ない)
bf = ccxt.bitflyer({
    'apiKey': 'xxxxxxxxxxxxx',
    'secret': 'xxxxxxxxxxxxx'
})
pprint(bf)

#通貨の情報
fetch_markets_result = bf.fetch_markets()
pprint(fetch_markets_result[0])
print(json.dumps(fetch_markets_result, indent=True))

#通貨の価格
fetch_markets_ticker = bf.fetch_ticker(symbol='BTC/JPY')
print(json.dumps(fetch_markets_ticker, indent=True))
#fetch_ticker(symbol='BTC/JPY', params={'パラメータ': '値'})  

#板情報
fetch_order_book = bf.fetch_order_book(symbol='BTC/JPY')
print(fetch_order_book)
#print(json.dumps(fetch_order_book, indent=True))

#取引履歴
fetch_order_result = bf.fetch_trades(symbol='BTC/JPY', limit=1)
print(json.dumps(fetch_order_result, indent=True))

結果

[
 {
  "id": "BTC_JPY",
  "symbol": "BTC/JPY",
  "base": "BTC",
  "quote": "JPY",
  "info": {
   "product_code": "BTC_JPY"
  }
 },
#他通貨省略
]
{
 "symbol": "BTC/JPY",
 "timestamp": 1522388592008,
 "datetime": "2018-03-30T05:43:12.800Z",
 "high": null,
 "low": null,
 "bid": 745428.0,
 "bidVolume": null,
 "ask": 745500.0,
 "askVolume": null,
 "vwap": null,
 "open": null,
 "close": 745328.0,
 "last": 745328.0,
 "previousClose": null,
 "change": null,
 "percentage": null,
 "average": null,
 "baseVolume": 31560.82277259,
 "quoteVolume": null,
 "info": {
  "product_code": "BTC_JPY",
  "timestamp": "2018-03-30T05:43:12.08",
  "tick_id": 2107224,
  "best_bid": 745428.0,
  "best_ask": 745500.0,
  "best_bid_size": 2.498,
  "best_ask_size": 3.39208716,
  "total_bid_depth": 2672.21621002,
  "total_ask_depth": 3692.39069499,
  "ltp": 745328.0,
  "volume": 367134.94338736,
  "volume_by_product": 31560.82277259
 }
}
[
#履歴省略
]
#板情報省略

Private API

#Private API
#残高
fetch_balance_result = bf.fetch_balance()
#現物用日本円資産額
print(json.dumps(fetch_balance_result["JPY"]["total"], indent=True))

#指値注文
create_order_result = bf.create_order(symbol='BTC/JPY', type='limit', side='BUY', amount=0.002, price=747950)
print(json.dumps(create_order_result, indent=True))
#返しのidはbitflyer order id

#成行注文
order_result = bf.create_order(symbol='BTC/JPY', type='market', side='BUY', amount=0.002) #null
print(json.dumps(order_result, indent=True))
order_result = bf.create_order(symbol='BTC/JPY', type='market', side='SELL', amount=0.002) #null
print(json.dumps(order_result, indent=True))
#返しのidはbitflyer order id

#注文キャンセル
cancel_order_result = bf.cancel_order(id='xxx-xxxxxxxx-xxxxx', symbol='BTC/JPY')
print(json.dumps(cancel_order_result, indent=True))
#null

#注文履歴
orders_result = bf.fetch_orders(symbol='BTC/JPY')
print(json.dumps(orders_result[-1], indent=True))