继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

测试一个股票行情API - 接入代码示例

慕运维7132774
关注TA
已关注
手记 14
粉丝 0
获赞 0
import time
 
import requests	# pip3 install requests
import json
 
# Extra headers
test_headers = {
    'Content-Type' : 'application/json'
}
 
'''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
申请免费token:https://alltick.co/register
官网:https://alltick.co
备用地址: https://alltick.io
 
将如下JSON进行url的encode,复制到http的查询字符串的query字段里
{"trace" : "python_http_test1","data" : {"code" : "USDJPY","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}
{"trace" : "python_http_test2","data" : {"symbol_list": [{"code": "GOLD"}]}}
{"trace" : "python_http_test3","data" : {"symbol_list": [{"code": "GOLD"}]}}
'''
test_url1 = 'https://quote.aatest.online/quote-b-api/kline?token=3662a972-1a5d-4bb1-88b4-66ca0c402a03-1688712831841&query=%7B%22trace%22%20%3A%20%22python_http_test1%22%2C%22data%22%20%3A%20%7B%22code%22%20%3A%20%22USDJPY%22%2C%22kline_type%22%20%3A%201%2C%22kline_timestamp_end%22%20%3A%200%2C%22query_kline_num%22%20%3A%202%2C%22adjust_type%22%3A%200%7D%7D'
 
resp1 = requests.get(url=test_url1, headers=test_headers)
 
# Decoded text returned by the request
text1 = resp1.text
print(text1)

下面是测试返回的结果:

{
    "ret":200,
    "msg":"ok",
    "cmd_id":14015,
    "seq_id":123,
    "ext":"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806",
    "data":{
        "data_list":[
            {
		"symbol_id": 1123,
                "trade_type": 1,
                "trade_mode": 0,
                "kline_type": 1,
                "price_digits": 2,
                "kline_list":[
                    {
                        "timestamp": 1605509068,
                        "open_price": "651.12",
                        "close_price": "623.12",
                        "high_price": "674.12",
                        "low_price": "619.12",
                        "last_tick_time": 1605509068000001,
                        "last_tick_seq": 1605509068000001,
                        "transactions_number": "12345.6",
                    },
                ]
            },
        ],
    }    
}

更多接入教程可以看文档

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP