
APIインターフェースを介して株式ティックデータを照会する方法
AllTickリアルタイム市場データAPI-無料株・為替・暗号ティック
1. 株式K線データの取得
例:Apple社の1分足K線を照会する。
import time
import requests
import json
# 追加ヘッダー
test_headers = {
'Content-Type':'application/json'
}
'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
無料トークンの申請: https://alltick.co/register
公式サイト: https://alltick.co
以下のJSONをURLエンコードし、HTTPリクエスト文字列の`query`フィールドにコピーしてください:
{"trace":"python_http_test1","data":{"code":"AAPL.US","kline_type":1,"kline_timestamp_end":0,"query_kline_num":2,"adjust_type":0}}
'''
test_url1 = 'https://quote.alltick.io/quote-stock-b-api/kline?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&query=%7B%22trace%22%3A%22python_http_test1%22%2C%22data%22%3A%7B%22code%22%3A%22AAPL.US%22%2C%22kline_type%22%3A1%2C%22kline_timestamp_end%22%3A0%2C%22query_kline_num%22%3A2%2C%22adjust_type%22%3A0%7D%7D'
resp1 = requests.get(url=test_url1, headers=test_headers)
# リクエストから返されたデコード済みテキスト
text1 = resp1.text
print(text1)
注意: 他の種類のK線データを照会するには、kline_typeに以下の値を設定します:
1 – 分足、2 – 5分足、3 – 15分足、4 – 30分足、5 – 時間足、6 – 2時間足、7 – 4時間足、8 – 日足、9 – 週足、10 – 月足。
2. 最新取引(ティック)データの取得
import time
import requests
import json
# 追加ヘッダー
test_headers = {
'Content-Type':'application/json'
}
'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
無料トークンの申請: https://alltick.co/register
公式サイト: https://alltick.co
以下のJSONをURLエンコードし、`query`フィールドにコピーしてください:
{"trace":"python_http_test2","data":{"symbol_list":[{"code": "700.HK"},{"code": "UNH.US"},{"code": "600416.SH"}]}}
'''
test_url1 = 'https://quote.alltick.io/quote-stock-b-api/trade-tick?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&query=%7B%22trace%22%3A%22python_http_test2%22%2C%22data%22%3A%7B%22symbol_list%22%3A%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%22%7D%2C%7B%22code%22%3A%20%22600416.SH%22%7D%5D%7D%7D'
resp1 = requests.get(url=test_url1, headers=test_headers)
# リクエストから返されたデコード済みテキスト
text1 = resp1.text
print(text1)
3. WebSocketによるリアルタイム株式データの購読
import json
import websocket # pip install websocket-client
'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
無料トークンの申請: https://alltick.co/register
公式サイト: https://alltick.co
'''
class Feed(object):
def __init__(self):
self.url = 'wss://quote.alltick.io/quote-stock-b-ws-api?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806' # WebSocketのURL
self.ws = None
def on_open(self, ws):
"""
WebSocket接続が開かれたときに呼び出されるコールバック。
引数:
@ ws: WebSocketAppオブジェクト
"""
print('新しいWebSocketAppが開かれました!')
# 購読を開始(例)
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,
}
]
}
}
# 長時間実行する場合は、購読の送信に加えて、切断を防ぐために定期的にハートビートを送信する必要があります。詳細はAPIドキュメントを参照してください。
sub_str = json.dumps(sub_param)
ws.send(sub_str)
print("板情報の購読が完了しました!")
def on_data(self, ws, string, type, continue_flag):
"""
4つの引数。
1番目:このクラスオブジェクト。
2番目:サーバーから受信したUTF-8文字列。
3番目:データタイプ(ABNF.OPCODE_TEXT または ABNF.OPCODE_BINARY)。
4番目:継続フラグ(0の場合はデータが続くことを示す)。
"""
def on_message(self, ws, message):
"""
メッセージを受信したときに呼び出されるコールバック。
2つの引数:
@ ws: WebSocketAppオブジェクト
@ message: サーバーから受信したUTF-8データ
"""
# 受信メッセージを解析
result = eval(message)
print(result)
def on_error(self, ws, error):
"""
エラーが発生したときに呼び出されるコールバック。
2つの引数:
@ ws: WebSocketAppオブジェクト
@ error: 例外オブジェクト
"""
print(error)
def on_close(self, ws, close_status_code, close_msg):
"""
接続が閉じられたときに呼び出されるコールバック。
3つの引数:
@ ws: WebSocketAppオブジェクト
@ close_status_code
@ close_msg
"""
print('接続が閉じられました!')
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()
4. 最新板情報(オーダーブック)データの取得
import time
import requests
import json
# 追加ヘッダー
test_headers = {
'Content-Type':'application/json'
}
'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
無料トークンの申請: https://alltick.co/register
公式サイト: https://alltick.co
以下のJSONをURLエンコードし、`query`フィールドにコピーしてください:
{"trace":"python_http_test2","data":{"symbol_list":[{"code": "700.HK"},{"code": "UNH.US"},{"code": "600416.SH"}]}}
'''
test_url1 = 'https://quote.alltick.io/quote-stock-b-api/depth-tick?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&query=%7B%22trace%22%3A%22python_http_test2%22%2C%22data%22%3A%7B%22symbol_list%22%3A%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%22%7D%2C%7B%22code%22%3A%20%22600416.SH%22%7D%5D%7D%7D'
resp1 = requests.get(url=test_url1, headers=test_headers)
# リクエストから返されたデコード済みテキスト
text1 = resp1.text
print(text1)
AllTick金融市場行情API無料トライアル
AllTickは安定した信頼性の高い金融市場データを提供しており、香港株CFD、米国株CFD、外国為替、商品、仮想通貨など幅広い商品をカバーしています。現在、無料トライアルを実施中です。アカウントを登録し、上記のサンプルコードを使用してテストできます。ご質問があれば、カスタマーサービスまでお問い合わせください。