Python システムトレード ビットコイン

coincheckのAPIを使ってビットコインのPython自動売買プログラムを作ってみたよ

スポンサードリンク

Contents

coincheckのAPIを使ってビットコインの自動売買を行うPythonプログラム

日本で一番簡単にビットコインが買える取引所 coincheck bitcoin

今回はビットコイン取引所coincheckの
APIを使って板情報と口座資産残高の取得、
ビットコインの注文処理を実装してみました。

API処理クラスファイル

bitFlyerのプログラムと同様に
APIを呼び出すクラスファイルを作成しました。

まずはAPI接続を行うクラスファイルからです。

# -*- coding: utf-8 -*-
"""
Created on Thu Mar  9 12:00:27 2017

@author: ichizo
"""

import json
import requests
import time
import hmac
import hashlib

class ApiCall:
    def __init__(self,api_key,api_secret,api_endpoint):
        self.api_key = api_key
        self.api_secret = api_secret
        self.api_endpoint = api_endpoint
        
    def get_api_call(self,path):
        timestamp = str(int(time.time()))
        text = timestamp + self.api_endpoint + path
        sign = hmac.new(bytes(self.api_secret.encode('ascii')), bytes(text.encode('ascii')), hashlib.sha256).hexdigest()
        request_data=requests.get(
            self.api_endpoint+path
            ,headers = {
                'ACCESS-KEY': self.api_key,
                'ACCESS-NONCE': timestamp,
                'ACCESS-SIGNATURE': sign,
                'Content-Type': 'application/json'
            })
        return request_data
    
    
    def post_api_call(self,path,body):
        body = json.dumps(body)
        timestamp = str(int(time.time()))
        text = timestamp + self.api_endpoint + path + body
        sign = hmac.new(bytes(self.api_secret.encode('ascii')), bytes(text.encode('ascii')), hashlib.sha256).hexdigest()
        request_data=requests.post(
            self.api_endpoint+path
            ,data= body
            ,headers = {
                'ACCESS-KEY': self.api_key,
                'ACCESS-NONCE': timestamp,
                'ACCESS-SIGNATURE': sign,
                'Content-Type': 'application/json'
            })
        return request_data

次に、このクラスを呼び出すプログラムです。

# -*- coding: utf-8 -*-
"""
Created on Thu Mar  9 12:14:31 2017

@author: ichizo
"""

import coincheckApi

api_key = 'APIキー'
api_secret = 'APIシークレット'
api_endpoint = 'https://coincheck.com'

path = '/api/order_books'
balance_path = '/api/accounts/balance'
order_path = '/api/exchange/orders'
body = {
  "pair": "btc_jpy",
  "order_type": "buy",
  "rate": 30000,
  "amount": 0.001,
}


if __name__ == '__main__':
    api = coincheckApi.ApiCall(api_key,api_secret,api_endpoint)
    result = api.get_api_call(path).json()
    print(result)
    
    result = api.get_api_call(balance_path).json()
    print(result)

    result = api.post_api_call(order_path,body).json()
    print(result)

ロジックはbitFlyerとほぼ同じです。

httpヘッダで送信するパラメーターが
少し違うので、そこを書き換えた程度です。

coincheckに口座を持っていない方は以下から登録できます。
日本で一番簡単にビットコインが買える取引所 coincheck bitcoin

-Python, システムトレード, ビットコイン
-, , ,

© 2024 FX・ビットコイン・オプションのシステムトレード開発と取引録 Powered by AFFINGER5