見出し画像

BitMex 版 自動売買(Pivot逆張り) 第五回

こんにちわ。中洲で飲み。シメはやっぱり海鳴ラーメン(ジェノバ)で決まりのブラックかふぇです。まじでおすすめです。

というわけで、今回はコードを全体を紹介いたします。

全体像はこんな感じです。

今までの回でそれぞれの箇所はまとめましたが、改めて紹介します。

def OrderCancel 

def OrderCancel(arg):
   try:
       orders = bitmex.fetch_open_orders()
       for order in orders:
               cancel = bitmex.cancel_order(order['id'])
   except Exception as e:
       print(e)

def entry

def entry(side, order_size,entryPrice):
   print('[' + side + ' Entry]')
   callback = 0
   try:
       callback = bitmex.create_order('BTC/USD', type='limit', side=side, amount=order_size, price=entryPrice)
       if (callback.get('id')):
           print('Order Complete! Entry:' + str(entryPrice))
           return side
       else:
           return 'NONE'
   except Exception as e:
       print(e)

def closePosition

def closePosition(side, order_size):
   oposit_side = 'NONE'
   side = "NONE"
   try:
       order_size = bitmex.private_get_position()[0]['currentQty']
       OrderCancel(1)
   except Exception as e:
       print(e)
   order_size = bitmex.private_get_position()[0]['currentQty']
   if order_size > 0:
       side = "buy"
   elif order_size < 0:
       side = "sell"
   if side == 'buy':
       oposit_side = 'sell'
       closePrice = bitmex.fetch_ticker('BTC/USD')['last']
   elif side == 'sell':
       oposit_side = 'buy'
       closePrice = bitmex.fetch_ticker('BTC/USD')['last']
   try:
       if order_size == 0:
           1+1
       else:
           callback = bitmex.create_order('BTC/USD', type='limit',
           side=oposit_side, amount=order_size, price=closePrice)
       if not(callback.get('id')):
           print("Close Order Complete!Profit",pos["pnl"])
   except Exception as e:
       print(e)

def Pivot

def Pivot(arg):
   periods = [arg]
   R3 = 0
   R2 = 0
   R1 = 0
   PIVOT = 0
   S1 = 0
   S2 = 0
   S3 = 0
   query = {"periods":','.join(periods)}
   try:
       res = json.loads(
       requests.get("https://api.cryptowat.ch/markets/bitmex/btcusd-perpetual-futures/ohlc",params=query).text)["result"]
   except Exception as e:
       print(e)
   period = arg
   row = res[period]
   length = len(row)
   Last1D= row[length-1]
   HIGH = Last1D[2]
   LOW = Last1D[3]
   CLOSE = Last1D[4]
   PIVOT = round((HIGH+LOW+CLOSE)/3,1)
   R3 = round(HIGH + 2 * (PIVOT - LOW) )
   R2 = round(PIVOT + (HIGH - LOW))
   R1 = round((2 * PIVOT) - LOW)
   S1 = round((2 * PIVOT) - HIGH)
   S2 = round(PIVOT - (HIGH - LOW))
   S3 = round(LOW - 2 * (HIGH - PIVOT))
   return R3,R2,R1,PIVOT,S1,S2,S3

これをうまく、組み合わせれば自動売買で小銭は稼げます!
仮想通貨先生が、近い考え方(多少ロジックは違いますが)で運用されております。バックテストを公開されていますのでご参考までに。
最近変動幅がすごいので、オススメは日足です。
完成したコードに需要あればそのまま公開して見ます。ほぼ公開していますけど(笑)
もともとこの資料は、勉強会用にまとめて見たので、説明かなり省いています。。。。ここまで読んで頂いてありがとうございます!





BTCアドレス 3BMEXWoSg5i9zegtEZ3QTEAUDeAV7rKXde