openctp / openctp-ctp-python

CTPAPI的Python接口,使用Swig技术制作,支持pip install。
http://www.openctp.cn
BSD 3-Clause "New" or "Revised" License
137 stars 26 forks source link

Linux下无法读取腾讯行情 #7

Closed congcong009 closed 1 year ago

congcong009 commented 1 year ago

使用6.67的文件,同样代码,在Win下环境中,可以正常实现订阅腾讯行情,Linux中无法订阅,很奇怪。

尝试使用库模式,直接运行demo目录中的mdapi,结果如下

root@fcd736623733:~/root/demo# python mdapi.py ApiVersion: v6.6.7_20220613 14:52:04.1708 press Enter key to exit ... OnFrontConnected OnRspUserLogin: ErrorID=0, ErrorMsg=CTP:No Error OnRspSubMarketData:ErrorID= 0 ErrorMsg= CTP:No Error OnRspSubMarketData:ErrorID= 0 ErrorMsg= CTP:No Error OnRspSubMarketData:ErrorID= 0 ErrorMsg= CTP:No Error OnRspSubMarketData:ErrorID= 0 ErrorMsg= CTP:No Error OnRspSubMarketData:ErrorID= 0 ErrorMsg= CTP:No Error

没有行情输出,该 demo 在 Win 下运行一切正常

Jedore commented 1 year ago

从行情源头到你的程序,有许多阶段,你这样问,我也不好回答。
是否你的测试时间点就没有行情呢? 多查证一下,多分析,可能答案就出来了 😢 或者你提供更多细节

congcong009 commented 1 year ago

明白,现在是开盘时间段,我写了一个最简单的行情demo,代码如下:

# coding:utf-8
"""
行情API demo
"""
import random

# import openctp_ctp.thostmduserapi as mdapi
import path.dll.linux.thostmduserapi as mdapi

# 因为是腾讯行情,这里先忽略
md_front = random.choice(('tcp://180.168.146.187:10131',
                              'tcp://180.168.146.187:10211',
                              'tcp://180.168.146.187:10212',
                              'tcp://180.168.146.187:10213'))

instruments = ('au2305', 'rb2305', 'TA305', 'i2305', 'IF2302', "600000", "000001", "00700", "AAPL")

class CMdSpiImpl(mdapi.CThostFtdcMdSpi):
    def __init__(self, md_api):
        mdapi.CThostFtdcMdSpi.__init__(self)
        # super().__init__()
        self.md_api = md_api
        self.tick = None

    def OnRtnDepthMarketData(self, pDepthMarketData: mdapi.CThostFtdcDepthMarketDataField):
        print(
            pDepthMarketData.ActionDay,
            pDepthMarketData.ExchangeID,
            pDepthMarketData.InstrumentID,
            pDepthMarketData.TradingDay,
            pDepthMarketData.reserve1,
            pDepthMarketData.reserve2,
            pDepthMarketData.LastPrice,
            pDepthMarketData.UpdateTime,
        )

def main():
    mduserapi = mdapi.CThostFtdcMdApi.CreateFtdcMdApi()
    mduserspi = CMdSpiImpl(mduserapi)
    mduserapi.RegisterSpi(mduserspi)
    mduserapi.Init()

    mduserapi.SubscribeMarketData([i.encode("utf8") for i in instruments], len(instruments))
    input()

if __name__ == '__main__':
    main()

配置如下,运行环境 python环境3.8

win环境取py38里文件和6.6.7根目录文件,包括

linux取linux64下全部文件,包括

现在我在Win环境运行,并将这里路径改为

import path.win.thostmduserapi as mdapi

运行结果:

2023-09-12 10:30:17 20230912 SZSE 000001 20239110   11.27 10:30:15
2023-09-12 10:30:17 20230912 HKEX 00700 20230912   322.8 10:15:15
2023-09-12 10:30:17 20230912 SSE 600000 20239110   7.04 10:30:15
2023-09-12 10:30:17 20230911 NASD AAPL 20230911   179.36 16:00:00
2023-09-12 10:30:19 20230912 SZSE 000001 20239110   11.28 10:30:18
2023-09-12 10:30:19 20230912 HKEX 00700 20230912   322.8 10:15:15
2023-09-12 10:30:19 20230912 SSE 600000 20239110   7.04 10:30:18
2023-09-12 10:30:19 20230911 NASD AAPL 20230911   179.36 16:00:00

接着在linux中运行,将路径修改为

import path.linux.thostmduserapi as mdapi

运行结果:

root@fcd736623733:~/openctp/ctp_base/demo# python ctp_python_demo.py 

press Enter key to exit ...
root@fcd736623733:~/openctp/ctp_base/demo# 

并没有任何输出,也没有报错,我也不是很确定接下来应该怎么做了,你看这样描述的信息足够么?

Jedore commented 1 year ago

详细的,我下午看看

Jedore commented 1 year ago

@congcong009 可能你的文件使用的不对,参考这个 https://zhuanlan.zhihu.com/p/613699044 的linux说明

congcong009 commented 1 year ago
# 来自腾讯 https://github.com/openctp/openctp/tree/master/ctp2QQ%E8%A1%8C%E6%83%85/6.6.1_P1_20210406/lin64/libthostmduserapi_se.so
libthostmduserapi_se.so
# https://github.com/openctp/openctp-ctp-python/blob/main/6.6.1_P1_20210406/linux64/thostmduserapi.py
thostmduserapi.py
# https://github.com/openctp/openctp-ctp-python/blob/main/6.6.1_P1_20210406/linux64/_thostmduserapi.so
_thostmduserapi.so

按照他的说明,第一个文件就不存在呢

libthostmduserapi_se.so

返回:

404 - page not found
The master branch of openctp does not contain the path 
ctp2QQ行情/6.6.1_P1_20210406/lin64/libthostmduserapi_se.so.

能麻烦看看是不是库里被归档了?

Jedore commented 1 year ago

@congcong009 哦, 写错了,应该是 https://github.com/openctp/openctp/tree/master/ctp2QQ%E8%A1%8C%E6%83%85/6.6.1_P1_20210406/lin64/thostmduserapi_se.so 🏃‍♂️

congcong009 commented 1 year ago

那么目前目录中存在以下几个文件

我直接把文章中的测试代码复制过来,动态库路径正常,运行后报错如下:

root@fcd736623733:~/openctp/dll/linux# python demo.py
Traceback (most recent call last):
  File "demo.py", line 5, in <module>
    import thostmduserapi as mdapi
  File "/root/openctp/dll/linux/thostmduserapi.py", line 15, in <module>
    import _thostmduserapi
ImportError: libthostmduserapi_se.so: cannot open shared object file: No such file or directory

然后我做了一件事情,我把 thostmduserapi_se.so 重命名为 libthostmduserapi_se.so,你猜怎么着····

root@fcd736623733:~/openctp/dll/linux# python demo.py
20239116  SZSE  11.28 000001 0.0
20230912  HKEX  322.0 00700 0.0
20239115  SSE  7.06 600000 0.0
20230912  NASD  177.87 AAPL 0.0
20239116  SZSE  11.28 000001 0.0
20230912  HKEX  322.0 00700 0.0
20239115  SSE  7.06 600000 0.0
20230912  NASD  177.87 AAPL 0.0
20239116  SZSE  11.28 000001 0.0
20230912  HKEX  322.0 00700 0.0
20239115  SSE  7.06 600000 0.0
20230912  NASD  177.87 AAPL 0.0

居然,就这么解决了··· >.<