2020年10月14日水曜日

bitFlyer API with Python

Source 

KEY = 'kkkkkkkk' SECRET = 'xxxxxxxxxxxx' ENDPOINT = 'https://api.bitflyer.com' MKT = 'ETH_JPY'
import urllib.request
import requests
import json
import pprint
import datetime
import hmac
import hashlib

def getBoard():
    path = '/v1/executions'

    url = ENDPOINT + path +  '?product_code=' + MKT
    print(url)

    req = urllib.request.Request(urlmethod='GET')
 
    req.add_header('Content-Type''application/json')

    try:
        with urllib.request.urlopen(reqas res:
            return pd.read_json(res.read())
    except urllib.error.HTTPError as e:
        print('exception http')
        print(e)
        content = json.loads(e.read())
        pprint.pprint(content)
    except Exception as e:
        print(e)

def makeCancel():
    timestamp = datetime.datetime.now().strftime('%y%m%d%H%M%S')
    method = 'POST'
    path = '/v1/me/cancelallchildorders'

    body = json.JSONEncoder()
    jsonObj = {'product_code'MKT}
    body = json.dumps(jsonObj)

    text = timestamp + method + path + body

    bSecret = SECRET.encode('utf-8')
    bMsg = text.encode('utf-8')
    sign = hmac.new(bSecret,bMsg,hashlib.sha256).hexdigest()
    bBody = body.encode('utf-8')

    url = ENDPOINT + path
    print(url)

    req = urllib.request.Request(url,data=bBodymethod='POST')

    req.add_header('ACCESS-KEY'KEY)
    req.add_header('ACCESS-TIMESTAMP'timestamp)
    req.add_header('ACCESS-SIGN'sign)
    req.add_header('Content-Type''application/json')

    try:
        with urllib.request.urlopen(reqas res:
            print(res.status, res.reason)
            for header in res.getheaders():
                print(header)
            print()
            content = json.loads(res.read())
            pprint.pprint(content)
    except urllib.error.HTTPError as e:
        print('exception http')
        print(e)
        content = json.loads(e.read())
        pprint.pprint(content)
        return json.loads(e.read())
    except Exception as e:
        print(e)

def getCollateral():
    timestamp = datetime.datetime.now().strftime('%y%m%d%H%M%S')
    method = 'GET'
    path = '/v1/me/getcollateral'
    # path = '/v1/me/getbalance'
    body = json.JSONEncoder()

    text = timestamp + method + path

    bSecret = SECRET.encode('utf-8')
    bMsg = text.encode('utf-8')
    sign = hmac.new(bSecret,bMsg,hashlib.sha256).hexdigest()

    url = ENDPOINT + path
    print(url)

    req = urllib.request.Request(urlmethod='GET')

    req.add_header('ACCESS-KEY'KEY)
    req.add_header('ACCESS-TIMESTAMP'timestamp)
    req.add_header('ACCESS-SIGN'sign)
    req.add_header('Content-Type''application/json')

    try:
        with urllib.request.urlopen(reqas res:
            print(res.status, res.reason)
           return pd.read_json(res.read())
    except urllib.error.HTTPError as e:
        print('exception http')
        print(e)
        content = json.loads(e.read())
        pprint.pprint(content)
    except Exception as e:
        print(e)

#execut
df = getBoard()

0 件のコメント:

コメントを投稿