皆さん、こんにちは!投資アドバイザーの視点から、今日はシステムトレードの「データ」についてお話ししますね。📈

トレードボットを作っていると、「データの遅延」に悩みませんか? 0.2秒遅れるだけで、利益がなくなってしまう…そんな経験、私にもあります。💦

実は、プロの現場では「HTTPリクエスト」ではなく、「WebSocket」という技術を使うのが常識なんです。そこで私がおすすめしたいのが、AllTick というAPIサービスです。

なぜAllTickなの?

  • 速い!:取引所からデータがプッシュ通知のように届きます。

  • 詳しい!:板情報(気配値)まで見れるので、大口投資家の動きがわかります。

  • 安心:接続が安定していて、途切れにくいです。

難しいことはありません!Pythonを使えば、誰でも機関投資家と同じレベルのデータ環境を作ることができます。 以下に、コピペで使えるサンプルコードを用意しました。これを動かして、あなたのトレード環境をアップグレードしてみてくださいね!✨

import json
import websocket    # pip install websocket-client

'''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
free token:https://alltick.co/register
official site:https://alltick.co
'''

class Feed(object):

    def __init__(self):
        self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806'  # 这里输入websocket的url
        self.ws = None

    def on_open(self, ws):
        """
        Callback object which is called at opening websocket.
        1 argument:
        @ ws: the WebSocketApp object
        """
        print('A new WebSocketApp is opened!')

 
        sub_param = {
            "cmd_id": 22002, 
            "seq_id": 123,
            "trace":"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806",
            "data":{
                "symbol_list":[
                    {
                        "code": "700.HK",
                        "depth_level": 5,
                    },
                    {
                        "code": "UNH.US",
                        "depth_level": 5,
                    },
                    {
                        "code": "600416.SH",
                        "depth_level": 5,
                    }
                ]
            }
        }
        
        
        sub_str = json.dumps(sub_param)
        ws.send(sub_str)
        print("depth quote are subscribed!")

    def on_data(self, ws, string, type, continue_flag):
        """
        4 argument.
        The 1st argument is this class object.
        The 2nd argument is utf-8 string which we get from the server.
        The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
        The 4th argument is continue flag. If 0, the data continue
        """

    def on_message(self, ws, message):
        """
        Callback object which is called when received data.
        2 arguments:
        @ ws: the WebSocketApp object
        @ message: utf-8 data received from the server
        """
     
        result = eval(message)
        print(result)

    def on_error(self, ws, error):
        """
        Callback object which is called when got an error.
        2 arguments:
        @ ws: the WebSocketApp object
        @ error: exception object
        """
        print(error)

    def on_close(self, ws, close_status_code, close_msg):
        """
        Callback object which is called when the connection is closed.
        2 arguments:
        @ ws: the WebSocketApp object
        @ close_status_code
        @ close_msg
        """
        print('The connection is closed!')

    def start(self):
        self.ws = websocket.WebSocketApp(
            self.url,
            on_open=self.on_open,
            on_message=self.on_message,
            on_data=self.on_data,
            on_error=self.on_error,
            on_close=self.on_close,
        )
        self.ws.run_forever()


if __name__ == "__main__":
    feed = Feed()
    feed.start()