Skip to main content

Python CTP futures api

Project description

py_ctp

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

更新

v6.6.8 全函数封装

ctp 接口封装由 ctp_generate 生成

安装

pip install py-ctp==6.7.2.20230913

示例

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

from py_ctp.trade 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
        self.broker = 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.broker, self.proc, self.appid, self.authcode)

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

    def release(self):
        self.t.ReqUserLogout()


class TestQuote(object):
    """TestQuote"""

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

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

    def run(self):
        self.q.ReqConnect(self.front)

    def release(self):
        self.q.ReqUserLogout()


if __name__ == "__main__":
    front_trade = 'tcp://180.168.146.187:10202'
    front_quote = 'tcp://180.168.146.187:10212'
    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.run()
    time.sleep(5)
    # tt.t.ReqOrderInsert('rb2409', DirectType.Buy, OffsetType.Open, 2060, 3)

    time.sleep(3)
    qq = TestQuote(front_quote, broker, investor, pwd)
    qq.run()

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

Project details


Download files

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

Source Distribution

py_ctp-6.7.2.20230913.tar.gz (6.5 MB view details)

Uploaded Source

Built Distribution

py_ctp-6.7.2.20230913-py3-none-any.whl (9.9 MB view details)

Uploaded Python 3

File details

Details for the file py_ctp-6.7.2.20230913.tar.gz.

File metadata

  • Download URL: py_ctp-6.7.2.20230913.tar.gz
  • Upload date:
  • Size: 6.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.9

File hashes

Hashes for py_ctp-6.7.2.20230913.tar.gz
Algorithm Hash digest
SHA256 e5a76407a6424e42cdc398f2a301678057b2b8f37514c1a73005a46173e86440
MD5 67b9f5dc70e1b6223c78e5f0d0304c17
BLAKE2b-256 36e048267c6a90620ec4d29253873700a6fa42bb7212d651a126538c77d01c37

See more details on using hashes here.

File details

Details for the file py_ctp-6.7.2.20230913-py3-none-any.whl.

File metadata

File hashes

Hashes for py_ctp-6.7.2.20230913-py3-none-any.whl
Algorithm Hash digest
SHA256 902a112be851deba1839bf7b001bc56ecf6174720f27201b20bf02cdb11bcf67
MD5 d6b859adac64c081cfe002b59c2806ed
BLAKE2b-256 29f1c3798557ea53c6a03fdcae264ccbc73677768cca191492768d2f21922bf5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page