私のauカブコム証券のkabuステーションで自動売買プログラムの説明(6) すでに株価が利食い価格を超えていないかチェック (board2.py)

スポンサーリンク

board2.pyのソースコード

import urllib.request
import json
import pprint
import time
import sendorder_takeprofit
import settle_now
import settings

def board2():
    url = 'http://localhost:' + settings.port + '/kabusapi/board/' + settings.symbol + '@1'
    req = urllib.request.Request(url, method='GET')
    req.add_header('Content-Type', 'application/json')
    req.add_header('X-API-KEY', settings.token)

    try:
        print('###board2')
        with urllib.request.urlopen(req) as res:
            content = json.loads(res.read())
            pprint.pprint(content)
            curPrice = content["CurrentPrice"]

            # 現在価格が目標価格に達していたら即決済
            if(curPrice <= settings.orderPrice - settings.margin):
                settle_now.settle_now()
            else:
                #利食い注文(売り発注)
                sendorder_takeprofit.sendorder_takeprofit()

    except urllib.error.HTTPError as e:
        print(e)
        content = json.loads(e.read())
        pprint.pprint(content)
    except Exception as e:
        print(e)

if __name__ == "__main__":
    import sys
    board2()

現在の株価を取得

これも、株価取得APIの呼びたし自体は、board.pyと同じでkabuステーションのサンプルとほぼ同じです。

https://github.com/kabucom/kabusapi/blob/master/sample/Python/kabusapi_board.py

現在価格がすでに目標価格に達していたら即決済

23行目で現在の株価と利食い価格を比較し、利食い価格に達していた場合は、24行目で数珠繋ぎ方式で成行注文で決済注文を発注します。

利食い価格の計算方法としては、グローバル変数からエントリ価格を取り出し、売りエントリなので利食い幅をそこから引いています。

現在価格がまだ目標価格に達していなければ利食い指値注文発注

現在の株価がまだ利食い価格に達していない場合(大抵そうですが)は、27行目でこれも数珠繋ぎ方式で利食い注文を指値で発注しています。

コメント