見出し画像

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


こんにちわ、BTCの下げと焼酎水割りが心にしみるブラックかふぇです。

今回は、オーダーのエントリーとクローズについて説明したいと思います。

オーダーエントリー、
売りもしくは買いのロジックになります。
単純には以下のようになります。

指値 10000USD 売り(Short), 
bitmex.create_order('BTC/USD', type='limit', side='sell', amount='0.1', price='10000';)

指値 10000USD 買い(Short),
bitmex.create_order('BTC/USD', type='limit', side='buy', amount='1000', price= '10000')

関数にして、売りでも買いでも、エントリーしたいタイミングで呼び出せる様にします。
以下の様に関数化して、
関数名 entry
パラメータとして、
side(sellもしくはbuy),
order_size(1000USD = 0.1 XBT レートによりますが。。)
entryPrice(BTCレート USD/XBT)

def entry(side,order_size,entryprice):
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))
except Exception as e:
print(e)

使い方、
entry('buy','1000','10000')
entry('sell','1000','10000')
の様にします。

次に、ポジションクローズ、ポジションがあれば、
クローズする単純なロジック。
BitMexは、戻り値が豊富でもっと綺麗なやり方はあるとは思うのですが、
私のやり方でとりあえず、続けます。

まず、bitmex.private_get_position()[0]['currentQty']
で、現在のポジションの総額を出します。
買いであれば、0以上の数字
売りであれば、0以下の数字
ポジションがない場合 0
が返ってきます。
それを利用して、買いor 売りかを判定し、ポジションクローズ(反対決済)します。
価格は、便宜上、最新価格で行います。この辺りをどうするかで、BOTの性能が変わりますよね。皆さんの腕の見せ所でしょうか。。

try:
    order_size = bitmex.private_get_position()[0]['currentQty']
except Exception as e:
    print(e)
if order_size > 0:
    side = "buy"
    oposit_side = 'sell'
elif order_size < 0:
    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!")
except Exception as e:
    print(e)

今日はこのあたりで。。
次回は、Mainループあたり書いてみます



BTCアドレス 3BMEXWoSg5i9zegtEZ3QTEAUDeAV7rKXde