#!/usr/bin/python3 import requests import ccxt HIGH = 2 LOW = 3 LOT_RATE = 0.7 # ロット(割合) LEVERAGE = 3 #レバレッジ設定 # ポジション情報取得関数 def position(): pos = bitmex.private_get_position()[0] if pos['currentQty'] > 0: side = 'LONG' else: side = 'SHORT' return {'side': side, 'currentQty': round(pos['currentQty']), 'avgEntryPrice': pos['avgEntryPrice']} #BitMEX操作オブジェクト取得 bitmex = ccxt.bitmex({ 'apiKey': 'ご自分の環境のAPI-KEY', 'secret': 'ご自分の環境のSecret-KEY', }) #レバレッジ設定をBitMEXに反映する(設定値を取得するAPIが見つからないため、こちらから設定してしまいます) bitmex.private_post_position_leverage({"symbol":"XBTUSD", "leverage": str(LEVERAGE)}) #最終価格を取得する last = bitmex.fetch_ticker('BTC/USD')['last'] #現在の保有BTC総量を取得 total_btc = bitmex.fetch_balance()['BTC']['total'] #前ポジションの数量(USD)取得 pos = position() current_position_qty = pos['currentQty'] if pos['side'] == 'LONG' else - pos['currentQty'] if pos['side'] == 'SHORT' else 0 #最大ポジション数量(レバレッジ設定×保有BTC総量×Price最終値×ロット割合)!!実際は成行注文をするので、この値より多少前後します!! max_position_qty = int(LEVERAGE * total_btc * last) #新ポジションの数量(USD)の作成(最大ポジション数量×ロット割合) new_position_qty = int(max_position_qty * LOT_RATE) print("現ポジションUSD: " + str(current_position_qty)) print("最大ポジションUSD: " + str(max_position_qty)) print("新ポジションUSD: " + str(new_position_qty))