Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Python CTP futures api

Project description


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




#!/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 2.3.2
Filename, size File type Python version Upload date Hashes
Filename, size py_ctp-2.3.2-py3-none-any.whl (11.4 MB) File type Wheel Python version py3 Upload date Hashes View
Filename, size py_ctp-2.3.2.tar.gz (11.3 MB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page