Skip to main content

Python CTP futures api

Project description


上期技术期货交易api之python封装,实现接口调用。支持windows(x86/x64) linux(x64).



更新:不再支持32位; 解决lnx下so路径问题;解决合约过多导致的bug;


与官方版本保持一致:6.3.15 6.3.16


pip install py-ctp==2.3.15.nnnn pip install py-ctp==2.3.16.nnnn pip install py-ctp==6.5.1.nnnn


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__title__ = 'test py ctp of se'
__author__ = 'HaiFeng'
__mtime__ = '20190506'

from import CtpTrade
from py_ctp.quote import CtpQuote
from py_ctp.enums import *
import time

class TestTrade(object):
    def __init__(self, addr: str, broker: str, investor: str, pwd: str, appid: str, auth_code: str, proc: str):
        self.front = addr = broker
        self.investor = investor
        self.pwd = pwd
        self.appid = appid
        self.authcode = auth_code
        self.proc = proc

        self.t = CtpTrade()
        self.t.OnConnected = self.on_connect
        self.t.OnUserLogin = lambda o, x: print('Trade logon:', x)
        self.t.OnDisConnected = lambda o, x: print(x)
        self.t.OnRtnNotice = lambda obj, time, msg: print(f'OnNotice: {time}:{msg}')
        self.t.OnErrRtnQuote = lambda obj, quote, info: None
        self.t.OnErrRtnQuoteInsert = lambda obj, o: None
        self.t.OnOrder = lambda obj, o: None
        self.t.OnErrOrder = lambda obj, f, info: None
        self.t.OnTrade = lambda obj, o: None
        self.t.OnInstrumentStatus = lambda obj, inst, stat: None

    def on_connect(self, obj):
        self.t.ReqUserLogin(self.investor, self.pwd,, self.proc, self.appid, self.authcode)

    def run(self):
        # self.t.ReqConnect('tcp://')

    def release(self):

class TestQuote(object):

    def __init__(self, addr: str, broker: str, investor: str, pwd: str):
        self.front = addr = broker
        self.investor = investor
        self.pwd = pwd

        self.q = CtpQuote()
        self.q.OnConnected = lambda x: self.q.ReqUserLogin(self.investor, self.pwd,
        self.q.OnUserLogin = lambda o, i: self.q.ReqSubscribeMarketData('rb1910')

    def run(self):

    def release(self):

if __name__ == "__main__":
    front_trade = 'tcp://'
    front_quote = 'tcp://'
    broker = '9999'
    investor = ''
    pwd = ''
    appid = ''
    auth_code = ''
    proc = ''
    if investor == '':
        investor = input('invesotr:')
        pwd = input('password:')
        appid = input('appid:')
        auth_code = input('auth code:')
        proc = input('product info:')
    tt = TestTrade(front_trade, broker, investor, pwd, appid, auth_code, proc)
    # tt.t.ReqOrderInsert('j1905', DirectType.Buy, OffsetType.Open, 2060, 3)

    qq = TestQuote(front_quote, broker, investor, pwd)

    # time.sleep(6)
    # for inst in tt.t.instruments.values():
    #     print(inst)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for py-ctp, version
Filename, size File type Python version Upload date Hashes
Filename, size py_ctp- (11.5 MB) File type Wheel Python version py3 Upload date Hashes View
Filename, size py_ctp- (6.6 MB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page