昔作ったお遊びBOT

こんなうまくはいかないよね。まぁ、何かのひな型にはなるかもって感じ。

import time
import py_bitflyer_jsonrpc
import pandas as pd
import traceback
import datetime
# シンボル設定
symbol = 'FX_BTC_JPY'
# 買い側の最終ID
board_side_buy_tail = 0
# 売り側の最終ID
board_side_sell_tail = 0
# カウント
count = 0
# 買い側の出来高
buy_size_total = 0
# 買い側の合計金額
buy_multi_total = 0
# 売り側の出来高
sell_size_total = 0
# 売り側の合計金額
sell_multi_total = 0
# 総合合計金額
all_multi_total = 0
# 総合合計出来高
all_size_total = 0
# 売り%
sell_per = 0
# 買い%
buy_per = 0
# 売買フラグ
ls_flg = ""
# 使う予定だったフラグ、、、だと思う
ls_change_flg = 0
# 最終取引価格
mid_price = 0
# 買い価格
buy_price = 0
# 売り価格
sell_price = 0
# 合計収益
totalResult = 0
#
snapshot = []
snapboard = []
dfboard = []
dfboard_side_buy = []
dfboard_side_sell = []
try:
   # Realtime API接続用オブジェクトを生成
   rpc = py_bitflyer_jsonrpc.BitflyerJSON_RPC(symbol=symbol, reconnect=True)
   while True:
       # オブジェクトの破棄
       del snapshot, snapboard, dfboard_side_buy, dfboard_side_sell
       # 日付取得
       dt = datetime.datetime.now()
       # 秒が0の場合、決済処理をして各値を初期化
       if str(dt.second) == "0":
           # フラグが設定されていない場合
           if ls_flg == "":
               # 売買価格に最終取引価格を設定
               sell_price = mid_price
               buy_price = mid_price
           # フラグが買いの場合
           elif ls_flg == "BUY":
               # 合計収益に取引利益を加える
               totalResult = totalResult + ((mid_price - buy_price) * 0.1)
               # 売買価格に最終取引価格を設定
               sell_price = mid_price
               buy_price = mid_price
           # フラグが売りの場合
           elif ls_flg == "SELL":
               # 合計収益に取引利益を加える
               totalResult = totalResult + ((sell_price - mid_price) * 0.1)
               # 売買価格に最終取引価格を設定
               sell_price = mid_price
               buy_price = mid_price
           # 買い側の出来高
           buy_size_total = 0
           # 買い側の合計金額
           buy_multi_total = 0
           # 売り側の出来高
           sell_size_total = 0
           # 売り側の合計金額
           sell_multi_total = 0
       # 板情報を取得
       snapshot = rpc.get_board_snapshot()
       # 最終取引価格を設定
       mid_price = snapshot['mid_price']
       # 約定情報を取得
       snapboard = rpc.get_execution()
       # 約定情報を買いと売りに分けてデータフレームに設定
       dfboard_side_buy = pd.DataFrame([i for i in snapboard if i['side'] == 'BUY'])
       dfboard_side_sell = pd.DataFrame([i for i in snapboard if i['side'] == 'SELL'])
       # 価格と出来高から合計金額を算出
       dfboard_side_buy['multi'] = dfboard_side_buy['price'] * dfboard_side_buy['size']
       dfboard_side_sell['multi'] = dfboard_side_sell['price'] * dfboard_side_sell['size']
       # 買い側IDが設定されていない場合
       if board_side_buy_tail == 0:
           # 出来高、合計金額を設定し最終行の買い側IDを設定
           buy_size_total = dfboard_side_buy['size'].sum(axis=0)
           buy_multi_total = dfboard_side_buy['multi'].sum(axis=0)
           board_side_buy_tail = int(dfboard_side_buy.tail(1)['id'])
       # 買い側IDが設定されている場合
       else:
           # 買い側IDから取得した約定情報の重複分以外を設定
           for index, row in dfboard_side_buy.iterrows():
               if row['id'] > board_side_buy_tail:
                   buy_size_total = buy_size_total + row['size']
                   buy_multi_total = buy_multi_total + row['multi']
                   board_side_buy_tail = row['id']
       # 売り側IDが設定されていない場合
       if board_side_sell_tail == 0:
           # 出来高、合計金額を設定し最終行の売り側IDを設定
           sell_size_total = dfboard_side_sell['size'].sum(axis=0)
           sell_multi_total = dfboard_side_sell['multi'].sum(axis=0)
           board_side_sell_tail = int(dfboard_side_sell.tail(1)['id'])
       # 売り側IDが設定されている場合
       else:
           # 売り側IDから取得した約定情報の重複分以外を設定
           for index, row in dfboard_side_sell.iterrows():
               if row['id'] > board_side_sell_tail:
                   sell_size_total = sell_size_total + row['size']
                   sell_multi_total = sell_multi_total + row['multi']
                   board_side_sell_tail = row['id']
       # 総合合計金額を設定
       all_multi_total = sell_multi_total + buy_multi_total
       # 総合合計出来高を設定
       all_size_total = sell_size_total + buy_size_total
       # 売り%を設定
       if (sell_multi_total or all_multi_total) == 0:
           sell_per = 0
       else:
           sell_per = int((sell_multi_total / all_multi_total) * 100)
       # 買い%を設定
       if (buy_multi_total or all_multi_total) == 0:
           buy_per = 0
       else:
           buy_per = int((buy_multi_total / all_multi_total) * 100)
       # 売り%が買い%より大きく、フラグが売りではないの場合
       if (sell_per > buy_per) and ls_flg != "SELL":
           # フラグが設定されていない場合
           if ls_flg == "":
               # 売買価格に最終取引価格を設定
               sell_price = mid_price
               buy_price = mid_price
           # フラグが買いの場合
           elif ls_flg == "BUY":
               # 合計収益に取引利益を加える
               totalResult = totalResult + ((mid_price - buy_price) * 0.1)
               # 売買価格に最終取引価格を設定
               sell_price = mid_price
               buy_price = mid_price
           # フラグを売りに設定
           ls_flg = "SELL"
       # 売り%が買い%より小さく、フラグが買いではないの場合
       elif (sell_per < buy_per) and ls_flg != "BUY":
           # フラグが設定されていない場合
           if ls_flg == "":
               # 売買価格に最終取引価格を設定
               sell_price = mid_price
               buy_price = mid_price
           # フラグが売りの場合
           elif ls_flg == "SELL":
               # 合計収益に取引利益を加える
               totalResult = totalResult + ((sell_price - mid_price) * 0.1)
               # 売買価格に最終取引価格を設定
               sell_price = mid_price
               buy_price = mid_price
           # フラグを買いに設定
           ls_flg = "BUY"
       # 書き出し
       print("##### SELL TOTAL:" + str(int(sell_multi_total)) + " #######", flush=True)
       print("##### SELL SIZE :" + str(int(sell_size_total)) + " #######", flush=True)
       print("##### SELL PER  :" + str(sell_per) + "% ######", flush=True)
       print("##### SELL ID   :" + str(board_side_sell_tail) + " ###### END SELL SIDE ID ######", flush=True)
       print("##### BUY  ID   :" + str(board_side_buy_tail) + " ###### END BUY  SIDE ID ######", flush=True)
       print("##### BUY  PER  :" + str(buy_per) + "% #######", flush=True)
       print("##### BUY  SIZE :" + str(int(buy_size_total)) + " #######", flush=True)
       print("##### BUY  TOTAL:" + str(int(buy_multi_total)) + " #######", flush=True)
       print("*****" + ls_flg + "*************************************************************", flush=True)
       print("##### BUY  PRICE:" + str(int(buy_price)) + " #######", flush=True)
       print("##### MIDS PRICE:" + str(mid_price) + " #######", flush=True)
       print("##### SELL PRICE:" + str(int(sell_price)) + " #######", flush=True)
       print("*****TOTAL RESULT:" + str(totalResult) + "YEN****************************************", flush=True)
       print("******************************************************************", flush=True)
       time.sleep(0.6)
except:
   traceback.print_exc()

この記事が気に入ったらサポートをしてみませんか?