python 通达信pytdx标准行情扩展行情连接方法 可获取美股港股行情

说明:在以下给出的代码连接失败的情况下,请更换ip和port。ip和port请从下面链接中找一个合适的,或者ping通里面每一个,直到返回True。学会看源码才是关键。

https://github.com/rainx/pytdx/blob/master/pytdx/util/best_ip.py

标准行情

可以设置全局超时

import socket
import time

socket.setdefaulttimeout(20)

导入库

from pytdx.hq import TdxHq_API

连接标准行情接口

standard_api = TdxHq_API(auto_retry=True, raise_exception=False)
while True:
    try:
        is_tdx_standard_connect = standard_api.connect('119.147.212.81', 7709, time_out=30)
    except Exception as e:
        print('time out to connect to pytdx')
        time.sleep(3)
        continue
    if is_tdx_standard_connect is not False:# 失败了返回False,成功了返回地址
        print('connect to pytdx standard api successful')
        break
    else:
        time.sleep(3)
        continue
    pass

扩展行情

导入库

from pytdx.exhq import TdxExHq_API, TDXParams

连接扩展行情接口

ex_api = TdxExHq_API(auto_retry=True, raise_exception=False)
while True:
    try:
        is_tdx_ex_connect = ex_api.connect('106.14.95.149', 7727, time_out=30)
    except Exception as e:
        print('time out to connect to pytdx')
        print(e)
        time.sleep(3)
        continue
    if is_tdx_ex_connect is not False:# 失败了返回False,成功了返回地址
        print('connect to pytdx extend api successful')
        break
    else:
        time.sleep(3)
        continue
    pass

你可能感兴趣的:(python,pytdx,通达信)