如何接入外汇数据接口 | python 技术论坛-大发黄金版app下载
外汇行情api是一种用于获取外汇市场实时行情数据的工具。随着外汇交易市场的不断发展,越来越多的交易者开始依赖外汇api来获取市场信息,并进行高效、准确的交易。下面将对排名前5的外汇行情api进行评比,并分析它们的优点.在当今的外汇交易市场中,外汇api扮演着至关重要的角色。外汇api为外汇交易者和金融机构提供了关键的数据和功能,帮助他们进行有效的外汇交易。本文将深入介绍几款备受推崇的外汇api,这些api提供了丰富的外汇数据和功能,满足用户对全面外汇交易信息的需求。通过探索这些外汇api,外汇交易者将更好地了解市场动态,从而制定更为精准的交易策略,实现交易目标。同时,我们也将着重介绍5款专注于外汇数据api,为读者提供更全面的外汇数据api选择。
一、接入方法
1.1、步骤一:注册账号和获取api密钥
token申请步骤:
大发黄金版app下载官网:
1.2、步骤二:选择产品,外汇,a股,美股,港股,贵金属,黄金,加密货币都支持。
https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api/blob/main/product_code_list_a_stock_cn.md
https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api/blob/main/product_code_list_hk_stock_cn.md
https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api/blob/main/product_code_list_us_stock_cn.md
https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api/blob/main/product_code_list_commodities_gold_cn.md
https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api/blob/main/product_code_list_cryptocurrency_cn.md
1.3、步骤三:订阅实时报价和行情数据
websocket接口:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api/tree/main/websocket_interface
http接口:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api/tree/main/http_interface
二、逐笔报价查询
逐笔报价功能允许您获取最新的汇率数据,包括买入价、卖出价、最高价、最低价等详细信息。通过订阅相关频道,您可以实时获取汇率变动的信息。请看下面实例代码:
import time
import requests
import json
# extra headers
test_headers = {
'content-type': 'application/json'
}
'''
# special note:
# github: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
# token application: https://alltick.co
# replace "testtoken" in the url below with your own token
# api addresses for forex, cryptocurrencies, and precious metals:
# https://quote.tradeswitcher.com/quote-b-ws-api
# stock api address:
# https://quote.tradeswitcher.com/quote-stock-b-ws-api
encode the following json and copy it to the "query" field of the http query string
{"trace": "python_http_test1", "data": {"code": "700.hk", "kline_type": 1, "kline_timestamp_end": 0, "query_kline_num": 2, "adjust_type": 0}}
{"trace": "python_http_test2", "data": {"symbol_list": [{"code": "700.hk"}, {"code": "unh.us"}]}}
{"trace": "python_http_test3", "data": {"symbol_list": [{"code": "700.hk"}, {"code": "unh.us"}]}}
'''
test_url1 = 'https://quote.tradeswitcher.com/quote-stock-b-api/kline?token=testtoken&query={"trace" : "python_http_test1","data" : {"code" : "700.hk","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}'
test_url2 = 'https://quote.tradeswitcher.com/quote-stock-b-api/depth-tick?token=testtoken&query={"trace" : "python_http_test2","data" : {"symbol_list": [{"code": "700.hk"},{"code": "unh.us"}]}}'
test_url3 = 'https://quote.tradeswitcher.com/quote-stock-b-api/trade-tick?token=testtoken&query={"trace" : "python_http_test3","data" : {"symbol_list": [{"code": "700.hk"},{"code": "unh.us"}]}}'
resp1 = requests.get(url=test_url1, headers=test_headers)
time.sleep(1)
resp2 = requests.get(url=test_url2, headers=test_headers)
time.sleep(1)
resp3 = requests.get(url=test_url3, headers=test_headers)
# decoded text returned by the request
text1 = resp1.text
print(text1)
text2 = resp2.text
print(text2)
text3 = resp3.text
print(text3)
三、批量订阅
批量订阅功能允许您同时查询多个货币对的汇率数据,节省了频繁发送请求的时间和资源消耗。您可以根据需求订阅感兴趣的货币对,获取实时的批量数据。请看下面实例代码:
import json
import websocket # pip install websocket-client
'''
# special note:
# github: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
# token application: https://alltick.co
# replace "testtoken" in the url below with your own token
# api addresses for forex, cryptocurrencies, and precious metals:
# wss://quote.tradeswitcher.com/quote-b-ws-api
# stock api address:
# wss://quote.tradeswitcher.com/quote-stock-b-ws-api
'''
class feed(object):
def __init__(self):
self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=testtoken' # enter your websocket url here
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!')
# start subscribing (an example)
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,
}
]
}
}
# if you want to run for a long time, you need to modify the code to send heartbeats periodically to avoid disconnection, please refer to the api documentation for details
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 arguments.
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
"""
# parse the received message
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()
以上实时外汇汇率api提供了丰富的功能和灵活的接口,使开发者能够轻松获取汇率和行情数据。通过逐笔报价、批量订阅和历史k线等特性,您可以在自己的应用中实时展示最新的汇率信息,并进行相应的数据分析和决策。
目前国内可用的金融行情api并不多,实时的高频数据接口就更少了,这些都以国外的产品为主,详细的选购教程可以看。
本作品采用《cc 协议》,转载必须注明作者和本文链接