[Python bitFlyer SFD] 証拠金変動履歴から bitFlyer の SFD 徴収/付与 情報を取得する

やりたいこと

SFDでいくら得した(or 損した)を知りたい


実行結果

こんな感じ(裏にTweetDeckが透けてるけど気にしない


コード

安定のクソコード
・証拠金変動履歴(GET /v1/me/getcollateralhistory)の、reason_code=SFDから計算
・pandas🐼使ってます

# -*- Coding: utf-8 -*-
import ccxt
from datetime import datetime, timedelta
import dateutil.parser
import pandas as pd

# Settings
API_KEY = 'YourApiKey'
API_SECRET = 'YourApiSecret'
SYMBOL_BTCFX = 'FX_BTC_JPY'

def get_datetime(d):
  date = d.replace('T', ' ')[:-1]
  return dateutil.parser.parse(date) + timedelta(hours=9)

# Main Logics
if __name__ == "__main__":
  before = 999999999999
  after = 0
  profit, profitPlus, profitMinus, plusCnt, minusCnt = 0, 0, 0, 0, 0
  df = pd.DataFrame(columns=["date", "profit", "profitPlus", "profitMinus", "plusCnt", "minusCnt"])

  bf = ccxt.bitflyer({
    'apiKey': API_KEY,
    'secret': API_SECRET,
  })

  histories = bf.fetch2(path='getcollateralhistory', api='private', method='GET', params={"count": 500, "before": before, "after": after})
  date = get_datetime(histories[0]["date"])
  datePrev = get_datetime(histories[0]["date"])
  while len(histories) > 0:
    for h in histories:
      date = get_datetime(h["date"])
      if date.day != datePrev.day:
        df = df.append(pd.Series([date.strftime("%Y/%m/%d"), profit, profitPlus, profitMinus, plusCnt, minusCnt], index=["date", "profit", "profitPlus", "profitMinus", "plusCnt", "minusCnt"]), ignore_index=True)
        profit, profitPlus, profitMinus, plusCnt, minusCnt = 0, 0, 0, 0, 0

      if h["reason_code"] == "SFD":
        if h["change"] > 0:
          profitPlus += h["change"]
          plusCnt += 1
        else:
          profitMinus += h["change"]
          minusCnt += 1
        profit += h["change"]
      datePrev = date
    before = histories[-1]["id"]
    histories = bf.fetch2(path='getcollateralhistory', api='private', method='GET', params={"count": 500, "before": before, "after": after})
    # sleep(0.13)

  df = df.append(pd.Series([date.strftime("%Y/%m/%d"), profit, profitPlus, profitMinus, plusCnt, minusCnt], index=["date", "profit", "profitPlus", "profitMinus", "plusCnt", "minusCnt"]), ignore_index=True)
  print(df)

  print("=== Total ===")
  print(pd.Series([df["profit"].sum(), df["profitPlus"].sum(), df["profitMinus"].sum(), df["plusCnt"].sum(), df["minusCnt"].sum()], index=["profit", "profitPlus", "profitMinus", "plusCnt", "minusCnt"]))


関連ツイート


取引回数が多いと、APIがエラーになるとか何とか…


おわりに

有料(¥100)にしてるけど、これで内容は全部です。募金してくれる人がいれば、ジュース代としていただけると嬉しい。コードは、インデントくずれが起きたりするようなので、コピペ時には注意してください。


マガジン


コメント用note(未購入者向け)


干し芋


続きをみるには

残り 0字

¥ 100

期間限定 PayPay支払いすると抽選でお得に!

サポート頂けると励みになります BTC,BCH: 39kcicufyycWVf8gcGxgsFn2B8Nd7reNUA LTC: LUFGHgdx1qqashDw4WxDcSYQPzd9w9f3iL MONA: MJXExiB7T7FFXKYf9SLqykrtGYDFn3gnaM