• 大小: 1.85MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-26
  • 语言: Python
  • 标签: ctp  python  

资源简介

原生python ctp 亲测可用,提供与ctp完全一致的接口。

资源截图

代码片段和文件信息

# -*- coding: UTF-8 -*-

import threading signal sys os
from ctypes import *;

from ctp.CtpApiStruct import *;
from ctp.CThostFtdcMdSpi import *;
from ctp.CThostFtdcMdApi import *;

class MyMdSpi(CThostFtdcMdSpi):
    def OnFrontConnected(self):
        print(‘connect successfully.‘)
        field = CThostFtdcReqUserLoginField()
        field.BrokerID = b‘broker888‘
        field.UserID = b‘user‘
        field.Password = b‘passwd‘
        api.ReqUserLogin(byref(field) 1000)

    def OnFrontDisconnected(self nReason):
        print(‘disconnect: ‘ + str(nReason))

    def OnRspUserLogin(self pRspUserLogin pRspInfo nRequestID bIsLast):
        print(‘login successfully.‘)
        insts = (c_char_p * 3)()
        insts[0] = b‘ZC801‘
        insts[1] = b‘rb1801‘
        api.SubscribeMarketData(byref(insts) 2)

    def OnRspSubMarketData(self pSpecificInstrument pRspInfo nRequestID bIsLast):
        print(‘subscribe: ‘ + pSpecificInstrument.contents.InstrumentID.decode())

    def OnRtnDepthMarketData(self pDepthMarketData):
        print (“%s.%s %s %.2f %.2f %d %.2f %d %d %.2f“ %
                    (pDepthMarketData.contents.UpdateTime
                     pDepthMarketData.contents.UpdateMillisec
                     pDepthMarketData.contents.InstrumentID
                     pDepthMarketData.contents.LastPrice
                     pDepthMarketData.contents.BidPrice1
                     pDepthMarketData.contents.BidVolume1
                     pDepthMarketData.contents.AskPrice1
                     pDepthMarketData.contents.AskVolume1
                     pDepthMarketData.contents.Volume
                     pDepthMarketData.contents.Turnover))


CDLL(“./v6.3.6/libthostmduserapi.so“ mode=RTLD_GLOBAL)
CDLL(“./v6.3.6/libthosttraderapi.so“ mode=RTLD_GLOBAL)

CThostFtdcMdSpi._dll = cdll.LoadLibrary(‘./libpyctpmdapi.so‘)
CThostFtdcMdApi._dll = cdll.LoadLibrary(‘./libpyctpmdapi.so‘)

md_addr = b‘tcp://180.168.102.194:41213‘


tmp_path = ‘./tmp‘
if not os.path.exists(tmp_path):
    os.makedirs(tmp_path)

spi = MyMdSpi()
api = CThostFtdcMdApi(tmp_path + ‘/‘)

def run():
    global api
    global md_addr
    print(‘connect to market server: ‘ + md_addr.decode())
    api.RegisterSpi(spi.GetSpi())
    api.RegisterFront(md_addr)
    api.Init()
    print(“market run...“)

def quit(signo frame):
    global api
    global spi
    print(“exit(%d)!“ % signo)
    api.RegisterSpi(None)
    api.Release()
    del spi
    sys.exit()

signal.signal(signal.SIGINT quit)
signal.signal(signal.SIGTERM quit)
ser = threading.Thread(target = run)
ser.setDaemon(True)
ser.start()
while True:
    pass



 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-11-21 14:55  pyctp-master\
     目录           0  2017-11-21 14:55  pyctp-master\.gitignore\
     文件         270  2017-11-21 14:55  pyctp-master\.gitignore\.gitignore
     文件          65  2017-11-21 14:55  pyctp-master\README.md
     目录           0  2017-11-21 14:55  pyctp-master\source\
     目录           0  2017-11-21 14:55  pyctp-master\source\cpp\
     文件        8637  2017-11-21 14:55  pyctp-master\source\cpp\pyctpmdapi.cpp
     文件        1242  2017-11-21 14:55  pyctp-master\source\cpp\pyctpmdapi.h
     文件       85891  2017-11-21 14:55  pyctp-master\source\cpp\pyctptraderapi.cpp
     文件        8845  2017-11-21 14:55  pyctp-master\source\cpp\pyctptraderapi.h
     目录           0  2017-11-21 14:55  pyctp-master\source\ctp\
     文件        1961  2017-11-21 14:55  pyctp-master\source\ctp\CThostFtdcMdApi.py
     文件        5444  2017-11-21 14:55  pyctp-master\source\ctp\CThostFtdcMdSpi.py
     文件       13587  2017-11-21 14:55  pyctp-master\source\ctp\CThostFtdcTraderApi.py
     文件       54617  2017-11-21 14:55  pyctp-master\source\ctp\CThostFtdcTraderSpi.py
     文件      183301  2017-11-21 14:55  pyctp-master\source\ctp\CtpApiStruct.py
     文件          93  2017-11-21 14:55  pyctp-master\source\ctp\PyCtpobject.py
     文件          23  2017-11-21 14:55  pyctp-master\source\ctp\__init__.py
     文件       21961  2017-11-21 14:55  pyctp-master\source\libpyctpmdapi.so
     文件      122417  2017-11-21 14:55  pyctp-master\source\libpyctptraderapi.so
     文件          91  2017-11-21 14:55  pyctp-master\source\md.sh
     文件        2644  2017-11-21 14:55  pyctp-master\source\test_md.py
     文件        7005  2017-11-21 14:55  pyctp-master\source\test_trader.py
     文件          99  2017-11-21 14:55  pyctp-master\source\trader.sh
     目录           0  2017-11-21 14:55  pyctp-master\source\v6.3.6\
     文件        6032  2017-11-21 14:55  pyctp-master\source\v6.3.6\ThostFtdcMdApi.h
     文件       33526  2017-11-21 14:55  pyctp-master\source\v6.3.6\ThostFtdcTraderApi.h
     文件      252114  2017-11-21 14:55  pyctp-master\source\v6.3.6\ThostFtdcUserApiDataType.h
     文件      221502  2017-11-21 14:55  pyctp-master\source\v6.3.6\ThostFtdcUserApiStruct.h
     文件         184  2017-11-21 14:55  pyctp-master\source\v6.3.6\error.dtd
     文件       15366  2017-11-21 14:55  pyctp-master\source\v6.3.6\error.xml
............此处省略2个文件信息

评论

共有 条评论