getposition 建玉の状態を表示

こちらの方のコードを参考に(特別なものはないので普遍的なコード)
Python初心者がbitFlyerのAPIを利用して自動取引するPythonプログラムを書いてみた

 

まずは情報を取得してみる

import json
import datetime
import hmac
import hashlib
import requests
import pprint

def check_position():
    api_key = '自分のAPIキー'
    api_secret = '自分のAPIシークレット'

    url = "https://api.bitflyer.jp"
    path_url = "/v1/me/getpositions?product_code=FX_BTC_JPY"
    method = "GET"

    timestamp = str(datetime.datetime.today())

    message = timestamp + method + path_url
    signature = hmac.new(bytearray(api_secret.encode('utf-8')), message.encode('utf-8') , digestmod = hashlib.sha256 ).hexdigest()

    headers = {
        'ACCESS-KEY' : api_key,
        'ACCESS-TIMESTAMP' : timestamp,
        'ACCESS-SIGN' : signature,
        'Content-Type' : 'application/json'
    }

    response = requests.get( url + path_url , headers = headers)
    print( response.status_code )#->200 =「成功」
    pprint.pprint( response.json() )

#main
check_position()

 

返ってきたデータ

200 (「成功」のコード)
[{'commission': 0.0,
  'leverage': 4.0,
  'open_date': '2019-11-28T01:24:07.823',
  'pnl': 74.88,
  'price': 837592.0,
  'product_code': 'FX_BTC_JPY',
  'require_collateral': 2093.98,
  'sfd': 0.0,
  'side': 'SELL',
  'size': 0.01,
  'swap_point_accumulate': 0.0}]

 

<改良版>実際のポジションと損益を、連続的に表示し続ける

'''
 実際に自分のポジションの
 BUY or SELL
 ボジションサイズ(分割の場合合計されないぞなもし?)
 今の値段
 損益
を連続表示(endless)
'''
import json
import datetime
import hmac
import hashlib
import requests
import pprint
import time

def check_position():
    api_key = ''
    api_secret = ''

    url = "https://api.bitflyer.jp"
    path_url = "/v1/me/getpositions?product_code=FX_BTC_JPY"
    method = "GET"

    timestamp = str(datetime.datetime.today())

    message = timestamp + method + path_url
    signature = hmac.new(bytearray(api_secret.encode('utf-8')), message.encode('utf-8') , digestmod = hashlib.sha256 ).hexdigest()

    headers = {
        'ACCESS-KEY' : api_key,
        'ACCESS-TIMESTAMP' : timestamp,
        'ACCESS-SIGN' : signature,
        'Content-Type' : 'application/json'
    }

    while True:
        response = requests.get( url + path_url , headers = headers)
        result=response.json()
        print( result[0]['side'],result[0]['size'],result[0]['price'],result[0]['pnl'])
        time.sleep(2)#情報が多すぎるので2秒に1回で十分
        #print( dir(response))#デバッグ用

#main
check_position()

  side  size  price   pnl

SELL 0.015 815346.0 56.1
SELL 0.015 815346.0 55.92
SELL 0.015 815346.0 55.92
SELL 0.015 815346.0 55.23
SELL 0.015 815346.0 56.58
SELL 0.015 815346.0 56.61
SELL 0.015 815346.0 57.09
SELL 0.015 815346.0 59.145
SELL 0.015 815346.0 58.41
SELL 0.015 815346.0 60.345
SELL 0.015 815346.0 61.035
SELL 0.015 815346.0 59.07
SELL 0.015 815346.0 56.265
SELL 0.015 815346.0 57.87
SELL 0.015 815346.0 60.195
SELL 0.015 815346.0 58.35

pnlとは何だ?:plofit and loss(ピーエル P/L 損益)のことらしい。

詳しい解説

url = “https://api.bitflyer.jp”
path_url = “/v1/me/getpositions?product_code=FX_BTC_JPY”
response = requests.get( url + path , headers = headers)
result=response.json()
因数分解すると
result=
requests.get( https://api.bitflyer.jp/v1/me/getpositions?
product_code=FX_BTC_JPY ,
{ ‘ACCESS-KEY’ : api_key,
‘ACCESS-TIMESTAMP’ : timestamp,
‘ACCESS-SIGN’ : signature,
‘Content-Type’ : ‘application/json’ })

result=requests.get()外部関数を使って記載のアドレスから情報を取ってきて、変数resultに格納せよ。

返ってきたデータ

[持ち玉は1個しか無いが、全体はリスト形式[0]番(複数[1],[2],[3]・・・)
{辞書形式のデータ
“product_code”: “FX_BTC_JPY”,
“side”: “SELL”, “price”: 36000,
“size”: 10,
“commission”: 0,
“swap_point_accumulate”: -35,
“require_collateral”: 120000,
“open_date”: “2015-11-03T10:04:45.011”,
“leverage”: 3,
“pnl”: 965,
“sfd”: -0.5 } ]

この中から、背景の付いたデータを取り出すには

print( result [0][‘side’],result [0][‘size’],result [0][‘price’],result [0][‘pnl’])

結果

SELL 10.0 36000.0 965.0 が取り出せる

ここまでpython入門中2019/11


ここからpython慣れてきた頃2020/03

ポジションをpybitflyerで取得して、pprintで表示してみた

import pybitflyer
from pprint import pprint
api_key="自分のkey"
api_secret="自分のsecret"
api = pybitflyer.API(api_key, api_secret)
#get
position= api.getpositions(product_code='FX_BTC_JPY')
#有るだけ廻して表示
for i in position:
    pprint(i)

 

ポジションを7個持っている
{'commission': 0.0,
 'leverage': 4.0,
 'open_date': '2020-03-16T14:31:05.003',
 'pnl': -1044.303844,
 'price': 544350.0,
 'product_code': 'FX_BTC_JPY',
 'require_collateral': 1360.875,
 'sfd': 0.0,
 'side': 'SELL',
 'size': 0.01,
 'swap_point_accumulate': 17.583844}
{'commission': 0.0,
 'leverage': 4.0,
 'open_date': '2020-03-17T01:00:39.16',
 'pnl': -1048.675832,
 'price': 543701.0,
 'product_code': 'FX_BTC_JPY',
 'require_collateral': 1359.2525,
 'sfd': 0.0,
 'side': 'SELL',
 'size': 0.01,
 'swap_point_accumulate': 15.465832}
{'commission': 0.0,
 'leverage': 4.0,
 'open_date': '2020-03-17T01:10:28.243',
 'pnl': -988.625832,
 'price': 549706.0,
 'product_code': 'FX_BTC_JPY',
 'require_collateral': 1374.265,
 'sfd': 0.0,
 'side': 'SELL',
 'size': 0.01,
 'swap_point_accumulate': 15.465832}
{'commission': 0.0,
 'leverage': 4.0,
 'open_date': '2020-03-17T01:34:39.13',
 'pnl': -761.725832,
 'price': 572396.0,
 'product_code': 'FX_BTC_JPY',
 'require_collateral': 1430.99,
 'sfd': 0.0,
 'side': 'SELL',
 'size': 0.01,
 'swap_point_accumulate': 15.465832}
{'commission': 0.0,
 'leverage': 4.0,
 'open_date': '2020-03-20T13:18:59.55',
 'pnl': 771.35584,
 'price': 724994.0,
 'product_code': 'FX_BTC_JPY',
 'require_collateral': 1812.485,
 'sfd': 0.0,
 'side': 'SELL',
 'size': 0.01,
 'swap_point_accumulate': 8.36416}
{'commission': 0.0,
 'leverage': 4.0,
 'open_date': '2020-03-21T11:53:37.267',
 'pnl': 267.686092,
 'price': 674333.0,
 'product_code': 'FX_BTC_JPY',
 'require_collateral': 1685.8325,
 'sfd': 0.0,
 'side': 'SELL',
 'size': 0.01,
 'swap_point_accumulate': 5.423908}
{'commission': 0.0,
 'leverage': 4.0,
 'open_date': '2020-03-23T01:06:23.99',
 'pnl': 14.25,
 'price': 648447.0,
 'product_code': 'FX_BTC_JPY',
 'require_collateral': 1621.1175,
 'sfd': 0.0,
 'side': 'SELL',
 'size': 0.01,
 'swap_point_accumulate': 0.0}

コロナショックの時、大底でショートした。下手くそだ。

コメント

タイトルとURLをコピーしました