VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 8
声望: 1

在github上提了issue,现把内容复制过来

环境

  • 操作系统: Ubuntu 22.04
  • Python版本: Python 3.10.8
  • VeighNa版本: 3.5.0

Issue类型

三选一:Bug

预期程序行为

正常连接或报相关业务错误

实际程序行为

segmentation fault (core dumped)

重现步骤

以下是run.py脚本内容

from vnpy.event import EventEngine

from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp

from vnpy_tts import TtsGateway

from vnpy_ctastrategy import CtaStrategyApp
from vnpy_ctabacktester import CtaBacktesterApp
from vnpy_scripttrader import ScriptTraderApp
from vnpy_chartwizard import ChartWizardApp

def main():
    """"""

    qapp = create_qapp()

    event_engine = EventEngine()

    main_engine = MainEngine(event_engine)

    # main_engine.add_gateway(CtpGateway)
    # NOTE: tts gateway和ctp gateway不能同时使用,否则会报错4097, tts使用了魔改dll, ctp使用的是官方dll,两个同时使用会优先使用官方dll连接tts
    # NOTE: ref https://zhuanlan.zhihu.com/p/453332727
    main_engine.add_gateway(TtsGateway)

    main_engine.add_app(CtaStrategyApp)
    main_engine.add_app(CtaBacktesterApp)
    main_engine.add_app(ScriptTraderApp)
    main_engine.add_app(ChartWizardApp)

    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()

    qapp.exec()


if __name__ == "__main__":
    main()

填写连接信息点击连接以后直接报segmentation fault.

description

description

我写了个脚本测试了一下tts和ctp,只有tts报段错误,不清楚是魔改的动态链接库报错还是vnpy_tts报错。
附上脚本

import time
from vnpy.event import EventEngine, Event
# from vnpy_tts import TtsGateway
from vnpy_ctp import CtpGateway

ee = EventEngine()
# gw = TtsGateway(ee, 'tts_gateway')
gw = CtpGateway(ee, 'ctp_gateway')

conf = {
    "用户名": "{usercode}",
    "密码": "{userpasswd}",
    "经纪商代码": "1234",
    "交易服务器": "tcp://42.192.226.242:20002",
    "行情服务器": "tcp://180.169.112.54.42213",
    "产品名称": "1234",
    "授权编码": "1234"
}

gw.connect(conf)

for i in range(10):
    print(f"sleep {i}")
    time.sleep(3)
Member
avatar
加入于:
帖子: 1613
声望: 115

这块可能是DLL的问题了,TTS最近升级了DLL版本

Member
avatar
加入于:
帖子: 8
声望: 1

更新一下,vnpy-tts的版本是6.6.7.0。

Member
avatar
加入于:
帖子: 8
声望: 1

MTF wrote:

这块可能是DLL的问题了,TTS最近升级了DLL版本

我也是这么想的,但是目前还在想怎么实锤这个问题,不然没法给TTS那边报issue。

Member
avatar
加入于:
帖子: 8
声望: 1

更新一下,写C++直接调用tts提供的libthostmduserapi_se.so和libthosttraderapi_se.so是没有问题的。
附上c++代码

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <thread>
#include "ThostFtdcMdApi.h"
#include "ThostFtdcTraderApi.h"

auto reqtime = std::chrono::steady_clock::now();

void fillLoginField(CThostFtdcReqUserLoginField* req) {
    const char *user = "xxx";
    const char *passwd = "xxx";
    const char *brokerId = "1234";
    const char *appId = "1234";
    const char *authCode = "1234";
    strcpy(req->BrokerID, brokerId);
    strcpy(req->UserID, user);
    strcpy(req->Password, passwd);
}


class CTraderSpi :public CThostFtdcTraderSpi {
public:
    CThostFtdcTraderApi *m_pTraderApi;

public:
    CTraderSpi(CThostFtdcTraderApi* pApi):m_pTraderApi(pApi) {
        pApi->RegisterSpi(this);
    }

    void OnFrontConnected() {
        std::cout << "connected." << std::endl;
        reqtime = std::chrono::steady_clock::now();
        CThostFtdcReqUserLoginField Req;
        fillLoginField(&Req);
        m_pTraderApi->ReqUserLogin(&Req, 0);
    }

    void OnFrontDisconnected(int nReason) {
        std::cout << "disconnected." << std::endl;
        exit(0);
    }

    void OnRspUserLogin(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast)
    {
        auto rsptime = std::chrono::steady_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(rsptime - reqtime);
        std::cout << "resp code: " << pRspInfo->ErrorID << "resp messgage " << std::string(pRspInfo->ErrorMsg) << std::endl;
        std::cout << "response time: " << duration.count() << " milliseconds" << std::endl;

        CThostFtdcQryInstrumentField req;
        strcpy(req.ExchangeID, "SHFE");
        strcpy(req.InstrumentID, "ag2306");
        m_pTraderApi->ReqQryInstrument(&req, 1);
    }

    void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {
        if (pInstrument) {
            std::cout << std::string(pInstrument->InstrumentID) << std::endl;
        }
        if (pRspInfo) {
            std::cout << "instrument resp code: " << pRspInfo->ErrorID << " resp messgage " << std::string(pRspInfo->ErrorMsg) << std::endl;
        }
    }
};

class CMarketSpi :public CThostFtdcMdSpi
{
public:
    CMarketSpi(CThostFtdcMdApi* pApi):m_pMarketApi(pApi)
    {
        pApi->RegisterSpi(this);
    }

    void OnFrontConnected()
    {
        std::cout << "connected." << std::endl;

        reqtime = std::chrono::steady_clock::now();
        CThostFtdcReqUserLoginField Req;
        fillLoginField(&Req);
        m_pMarketApi->ReqUserLogin(&Req, 0);
    }

    void OnFrontDisconnected(int nReason)
    {
        std::cout << "disconnected." << std::endl;
        exit(0);
    }

    void OnRspUserLogin(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast)
    {
        auto rsptime = std::chrono::steady_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(rsptime - reqtime);

        std::cout << "response time: " << duration.count() << " milliseconds" << std::endl;

        char instrumentId[10];
        strcpy(instrumentId, "ag2306");
        char* instrumentIds[1] = {instrumentId};
        m_pMarketApi->SubscribeMarketData(instrumentIds, 1);
    }

    void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) {
        std::cout << std::string(pDepthMarketData->InstrumentID) << " " << pDepthMarketData->LastPrice << std::endl;
    }

    CThostFtdcMdApi* m_pMarketApi;
};

int main(int argc,char *argv[])
{
    if (argc != 2) {
        std::cout << "usage:ctpping address" << std::endl;
        std::cout << "example:ctpping tcp://180.168.146.187:10100" << std::endl;
        return 0;
    }
    std::cout << "version:" << CThostFtdcMdApi::GetApiVersion() << std::endl;

    CThostFtdcMdApi* pApi = CThostFtdcMdApi::CreateFtdcMdApi();
    CMarketSpi Spi(pApi);
    pApi->RegisterFront(argv[1]);
    pApi->Init();

    CThostFtdcTraderApi* pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi();
    CTraderSpi TraderSpi(pTraderApi);
    // pTraderApi->RegisterFront(argv[1]);
    // pTraderApi->Init();

    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "time out." << std::endl;

    return 0;
}
Member
avatar
加入于:
帖子: 8
声望: 1

segmentation fault的问题查出来了,把vnpy_tts用6.5.1.2就可以正常工作了。
这时候去连tts的话可能会报4079的错误,这是因为vnpy_tts里的libthost***.so用的是ctp的so文件。只要去openctp下载一下,然后替换需要的so就可以解决了。
希望能帮到有需要的人。

description

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】