Giter Club home page Giter Club logo

ctpbee's Introduction

ctpbee

avatar PyPI Latest Release Build Status PyPI Downloads License - MIT

bee bee .... for developer's trading ~

tiny but strong

ctpbee provide a micro core of trading, you can make trade and backtest in it.

环境设置

#  linux用户快速生成中文支持/ windows用户无须设置 
## for root 
bee init-locale 
## for user, xxx为你的用户密码, 注意你当前用户需要拥有sudo权限 
bee init-locale --password xxxxx 

灵感起源

使用来自于vnpy的交易接口, 重新提供上层封装API, 简化安装流程, 提供快速实现交易功能.

快速安装

mac用户注意, ctpbee_api目前仅仅提供源码安装方式, 需要你预先安装ctpbee_api, 安装参见

# python version: 3.6+


# 源码安装 
git clone https://github.com/ctpbee/ctpbee && cd ctpbee && python3 setup.py install  

# pip源安装
pip3 install ctpbee

支持系统

  • Linux
  • Windows
  • MacOS

文档与交流

文档地址

论坛地址

快速开始

from ctpbee import CtpBee
from ctpbee import CtpbeeApi
from ctpbee.constant import *


class CTA(CtpbeeApi):
    def __init__(self, name):
        super().__init__(name)

    def on_init(self, init: bool) -> None:  # 初始化完成回调 
        self.info("init successful")

    def on_tick(self, tick: TickData) -> None:
        print(tick.datetime, tick.last_price)  # 打印tick时间戳以及最新价格 

        # 买开
        self.action.buy_open(tick.last_price, 1, tick)
        # 买平
        self.action.buy_close(tick.last_price, 1, tick)
        # 卖开
        self.action.sell_open(tick.last_price, 1, tick)
        # 卖平 
        self.action.sell_close(tick.last_price, 1, tick)

        # 获取合约的仓位
        position = self.center.get_position(tick.local_symbol)
        print(position)

    def on_contract(self, contract: ContractData) -> None:
        if contract.local_symbol == "rb2205.SHFE":
            self.action.subscribe(contract.local_symbol)  # 订阅行情 
            print("合约乘数: ", contract.size)


if __name__ == '__main__':
    app = CtpBee('ctp', __name__)
    info = {
        "CONNECT_INFO": {
            "userid": "",
            "password": "",
            "brokerid": "",
            "md_address": "",
            "td_address": "",
            "appid": "",
            "auth_code": "",
            "product_info": ""
        },
        "INTERFACE": "ctp",
        "TD_FUNC": True,  # Open trading feature
    }
    app.config.from_mapping(info)  # loading config from dict object
    cta = CTA("cta")
    app.add_extension(cta)
    app.start() 

功能支持

  • 简单易用的下单功能
  • 仓位盈亏计算
  • 多周期多合约回测
  • 实时行情
  • k线生成
  • 回测报告生成
  • 自动运维
  • 插件系统的支持
  • 多交易接口支持
    • ctp
    • ctp_mini
    • rohon
    • open_ctp

更多相关信息, 请参阅文档

命令行运行效果

avatar

回测截图

支持多周期多合约回测, 回测参考example/backtest示例 avatar

模拟交易测试

本项目推荐使用openctp 或者simnow做模拟交易测试

关于如何对接openctp,请参阅此教程click here

DEMO: 推荐参阅openctp分发实现

遇到问题?

请提交issue或者于issue搜索关键字, 或者查阅此处

历史数据支持

对于本地数据自动运维方案, 请👉 Hive 本项目不提供直接的历史数据访问服务.

插件支持

ctpbee提供了一个ToolRegister机制以支持访问数据触发机制, 可以实现交易各类插件.

欢迎各位大佬参与开发进来. 实现相关生态功能. 如果有相关疑惑, 可以发送邮件到[email protected]寻求技术支持. 下面是提供的插件列表

  • ctpbee_kline k线支持插件
  • ctpbee_webline 使用flask编写后端, 通过with_tools接口接入策略, vue3+tauri 编写桌面应用, 敬请期待

免责声明

本项目长期维护, 开源仅作爱好, 本人不对代码产生的任何使用后果负责. 功能尽可能会保持稳定.

License

  • MIT

ctpbee's People

Contributors

der-rote-elefant avatar hukybo avatar junfalin avatar somewheve avatar tonyzaizai avatar yutiansut avatar zhengquantao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ctpbee's Issues

策略构造函数中无法使用logger

在策略的构造函数中使用self.info方法会报错AttributeError: 'NoneType' object has no attribute 'logger'

在on_tick里可正常使用self.info方法。

docker相关问题

  1. docker 启动后不能自己后台运行,必须自己添加&命令才行
  2. 镜像文件占了1.22G......
  3. 启动后访问报了404
    虚拟机,centos,
    centos 版本:CentOS Linux release 8.0.1905 (Core)
    docker版本:Docker version 19.03.3, build a872fc2f86

image

image

image

image

防火墙是关的,selinux是关的,本机也是404

CEP.[future]

需求/request

想了一下量化的最终形态,先想一下作为一名普通用户我们需要啥东西,仔细一思考,其实问题很简单,我们需要的是服务。并不是编程,并不是接口,而是集成的服务,我想要一个服务

  • 可以让我无缝监管我策略发生的情况
  • 可以让我让我简单的轻松添加策略或者原子任务
  • 可以让我使用系统的最大性能
  • 可以让我在App端简单轻松查看
  • 可以让我简单轻松获取到消息
  • 可以让我拥有强大的数据支持
  • 可以让我手动下单
  • 可以让我一键无缝安装,直接获取服务

当前的ctpbee可能更多来说是一个开发工具而不是一个服务,我很遗憾,所以我感觉走偏了。

最终表现效果 / result
参上

请问目前支持策略级别的持仓隔离吗

请问目前支持策略级别的持仓隔离吗?

比如目前有2个策略同时在进行实盘, A策略和B策略都会交易jm这个品种, 此时A策略多开一手jm, B策略也多开一手, 期待的结果是在A策略中查询持仓, 显示jm持仓一手, B策略也是

关于TradingDay

你好,我看到helper.py里面的TradingDay部分被注释掉了,请问下交易时间和节假日这一块 框架这里是怎么处理的呢

示例文件无法运行

您好!感谢您的代码。
我pip install ctpbee后,运行示例代码easy_use.py,出现如下错误,请怎样才能运行起来?
Traceback (most recent call last):
File "e:/myquant/ctpbee-master/examples/easy_use.py", line 30, in
market.start(log_output=True)
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\app.py", line 300, in start
self.running(logout=log_output)
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\app.py", line 244, in running
self.init_interface()
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\app.py", line 263, in init_interface
MdApi, TdApi = Interface.get_interface(self)
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\interface_init
.py", line 46, in get_interface
return cls.load_interface(app.config.get("INTERFACE"))
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\interface_init
.py", line 39, in load_interface
from ctpbee.interface.ctp_mini import MMdApi, MTdApi
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\interface\ctp_mini_init_.py", line 1, in
from .md_api import MMdApi
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\interface\ctp_mini\md_api.py", line 4, in
from .lib import *
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee\interface\ctp_mini\lib.py", line 2, in
from ctpbee_api.ctp_mini import *
File "e:\myquant\venv_quant389\lib\site-packages\ctpbee_api\ctp_mini_init_.py", line 1, in
from .CMdApi import MdApi as MiniMdApi
ModuleNotFoundError: No module named 'ctpbee_api.ctp_mini.CMdApi'

self.app.center.get_tick()报错

Describe the bug / 问题描述

self.app.center.get_tick()报错
TypeError: 'TickData' object is not subscriptable

查看源代码后发现调用下面这个代码
self.app.recorder.get_tick()[:-1]

但是self.app.recorder.get_tick()貌似只返回单个TickData

API支持.rst中描述了engine_method,可否支持robinhood/faust,实现分布式+异步

robinhood/faust
https://github.com/robinhood/faust

   Faust is a **stream processing library**, porting the ideas from Kafka Streams to Python.

It is used at Robinhood to 
	**build high performance distributed systems and 
	real-time data pipelines that process billions of events every day.**

Faust provides both **stream processing and event processing**, 
	sharing similarity with tools such as Kafka Streams, Apache Spark/Storm/Samza/Flink.

It does not use a DSL, it's just Python! 
	**This means you can use all your favorite Python libraries when stream processing: NumPy, PyTorch, Pandas, NLTK, Django, Flask, SQLAlchemy,...**

	Faust requires Python 3.6 or later for the new async/await syntax, and variable type annotations.

接收不到行情

今天运行例子文件double_ma.py,可以链接上,但收不到行情。以前是可以的,不知是simnow出问题还是ctpbee出问题了?

DCE 1分钟bar合成问题

DCE在合成1分钟bar时存在问题:合成bar不及时,比如11:20的tick都收到了,才合成11:18的
下面是部分日志[T]是tick, [B]是1分钟bar
看日志DCE的tick无法保证1秒两次,不知道与这个有没有关系

[T] 11:11:16 2022-03-03 11:11:16.500000 a2205.DCE 6304.0
[T] 11:11:20 2022-03-03 11:11:20 a2205.DCE 6303.0
[T] 11:11:20 2022-03-03 11:11:20.500000 a2205.DCE 6303.0
[T] 11:11:25 2022-03-03 11:11:25.500000 a2205.DCE 6303.0
[T] 11:11:26 2022-03-03 11:11:26.500000 a2205.DCE 6303.0
[T] 11:11:27 2022-03-03 11:11:27 a2205.DCE 6303.0
[T] 11:11:29 2022-03-03 11:11:29 a2205.DCE 6304.0
[T] 11:11:37 2022-03-03 11:11:37 a2205.DCE 6304.0
[T] 11:11:40 2022-03-03 11:11:40.500000 a2205.DCE 6304.0
[T] 11:11:42 2022-03-03 11:11:42 a2205.DCE 6303.0
[T] 11:11:42 2022-03-03 11:11:42.500000 a2205.DCE 6303.0
[T] 11:11:43 2022-03-03 11:11:43 a2205.DCE 6303.0
[T] 11:11:43 2022-03-03 11:11:43.500000 a2205.DCE 6303.0
[T] 11:11:47 2022-03-03 11:11:47 a2205.DCE 6303.0
[T] 11:11:50 2022-03-03 11:11:50.500000 a2205.DCE 6305.0
[T] 11:11:52 2022-03-03 11:11:52 a2205.DCE 6305.0
[T] 11:12:10 2022-03-03 11:12:10.500000 a2205.DCE 6305.0
[T] 11:12:12 2022-03-03 11:12:12 a2205.DCE 6303.0
[T] 11:12:14 2022-03-03 11:12:14 a2205.DCE 6305.0
[T] 11:12:17 2022-03-03 11:12:17 a2205.DCE 6305.0
[T] 11:12:17 2022-03-03 11:12:17.500000 a2205.DCE 6305.0
[T] 11:12:18 2022-03-03 11:12:18 a2205.DCE 6305.0
[T] 11:12:19 2022-03-03 11:12:19 a2205.DCE 6305.0
[T] 11:12:21 2022-03-03 11:12:21 a2205.DCE 6305.0
[T] 11:12:21 2022-03-03 11:12:21.500000 a2205.DCE 6305.0
[T] 11:12:22 2022-03-03 11:12:22 a2205.DCE 6305.0
[T] 11:12:22 2022-03-03 11:12:22.500000 a2205.DCE 6305.0
[T] 11:12:23 2022-03-03 11:12:23 a2205.DCE 6305.0
[T] 11:12:23 2022-03-03 11:12:23.500000 a2205.DCE 6305.0
[T] 11:12:32 2022-03-03 11:12:32 a2205.DCE 6305.0
[T] 11:12:33 2022-03-03 11:12:33.500000 a2205.DCE 6305.0
[T] 11:12:34 2022-03-03 11:12:34 a2205.DCE 6305.0
[T] 11:12:38 2022-03-03 11:12:38 a2205.DCE 6305.0
[T] 11:12:38 2022-03-03 11:12:38.500000 a2205.DCE 6306.0
[T] 11:12:39 2022-03-03 11:12:39 a2205.DCE 6306.0
[T] 11:12:40 2022-03-03 11:12:40.500000 a2205.DCE 6306.0
[T] 11:12:42 2022-03-03 11:12:42 a2205.DCE 6306.0
[T] 11:12:44 2022-03-03 11:12:44 a2205.DCE 6307.0
[T] 11:12:45 2022-03-03 11:12:45.500000 a2205.DCE 6307.0
[T] 11:12:46 2022-03-03 11:12:46 a2205.DCE 6307.0
[T] 11:12:46 2022-03-03 11:12:46.500000 a2205.DCE 6307.0
[T] 11:12:47 2022-03-03 11:12:47 a2205.DCE 6306.0
[T] 11:12:47 2022-03-03 11:12:47.500000 a2205.DCE 6306.0
[T] 11:12:50 2022-03-03 11:12:50.500000 a2205.DCE 6306.0
[T] 11:12:55 2022-03-03 11:12:55.500000 a2205.DCE 6306.0
[T] 11:12:58 2022-03-03 11:12:58 a2205.DCE 6306.0
[T] 11:12:58 2022-03-03 11:12:58.500000 a2205.DCE 6307.0
[T] 11:12:59 2022-03-03 11:12:59 a2205.DCE 6308.0
[B] 11:13:00 2022-03-03 11:10:48 a2205.DCE 6308.0
[T] 11:13:00 2022-03-03 11:13:00 a2205.DCE 6308.0

无法启动

Describe the bug / 问题描述
最新版本的ctpbee会出现无法启动, 需要开启一个while True , 同时需要线程开启出现问题 ? 计时器是否可以更加优雅点,

**To Reproduce / 复现步骤 **

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior / 期望结果
A clear and concise description of what you expected to happen.

Screenshots / 截图

If applicable, add screenshots to help explain your problem./如果可能请将截图放于此处

Environment/ 你的运行环境

  • OS: [e.g. Linux]
  • Python Version: [e.g.Python36]
  • ctpbee Version: [e.g. 0.50]

关于回测使用教程

需求/request
教程

请在此处详细描述你的诉求/这样做的理由 / The reason why should I add the function to this framework
回测目前就我们公司用 ,而我又一直在开发策略 , 然后一直没时间进行写<太懒了 不想写 哈哈哈哈哈哈>

目前期货回测应该是完善的

如果有兴趣的朋友想使用ctpbee的回测并且愿意写教程的 可以联系我 [email protected]

然后就是希望大家早点策略上线咯

关于6.6.1_CP

请问下目前ctpbee_api使用的是6.6.1,如何使用6.6.1_cp 版本呢

安装qactpbeebroker时报错

Describe the bug / 问题描述

在我安装QACTPBeeBroker,

  • 弹出了ERROR: Package 'dataclasses' requires a different Python: 3.7.6 not in '>=3.6, <3.7'报错,

  • 我看了下ericvsmith/dataclasses的兼容性说明,dataclasses因为在3.7里是内置模块,所以这个项目只支持3.6的版本

  • 我在3.7里的环境里,可以正常执行from dataclasses import dataclass, asdict语句,但不清楚是否影响ctpbee正常运作.

  • 233, 改了好多次,其实是qactpbeebroker没做更新了,其中ctpbee限制在了0.31.2的版本,但貌似0.31.2的版本没有对python的环境做判断,所以要求安装dataclasses,

  • 最后吐槽一下自己,貌似反馈错地方了,应该去qactpbeebroker反馈

Environment/ 你的运行环境

  • OS: win 10
  • Python Version: Python 3.7
  • ctpbee Version: None

todo

  • 2022 - 升级教程
  • xtp接口
  • 升级ctp期货接口
  • 持仓盈亏与定时盈亏计算

交易所套利指令的平仓支持

开仓是能支持的,但是开仓后,交易所是分拆成一对持仓,平仓时CTPBEE是按套利代码查持仓的,所以会发现仓位不足,平仓失败。所以对IPS / SP这类代码的平仓,可以不检测。

股票功能需求

需求/request

需要实现了股票的回测功能, 日线级别的回测以及股票T0级别的回测,注意需要和现有接口进行兼容

**最终表现效果 / result
多品种级别的股票能够进行回测, 同时收益与沪深300收益进行对比

文档征集帖

由于本人实在太笨(太懒),本项目缺乏大量策略实现例子

有无朋友有兴趣基于ctpbee实现各种经典策略, 我可以提供技术支持, 包括相关问题

回测打点显示

需求/request

针对多个品种实现回测打点图的显示 可以更加直观的 显示 b/ s点

k线图和tick都需要支持生成

策略的断线重连

如何在策略的时候断线的时候恢复同步 ?
如果大佬有啥意见 ,可以提 ,我可以根据你的意见做出修改

ctpbee的center脚本里,查询持仓的方法只有self.app.center.get_positon(),只能查询单个合约的持仓,没有能一次性查询账户全部持仓合约的.position(),只能另外用self.app.recorder.position_manager.get_all_positions()查询

Describe the bug / 问题描述

请在此处描述问题

**To Reproduce / 复现步骤 **

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior / 期望结果
A clear and concise description of what you expected to happen.

Screenshots / 截图

If applicable, add screenshots to help explain your problem./如果可能请将截图放于此处

Environment/ 你的运行环境

  • OS: [e.g. Linux]
  • Python Version: [e.g.Python36]
  • ctpbee Version: [e.g. 0.50]

Bugs

请问如何运行回测?
我尝试了looper_example.py,但没有data_api 模块
又尝试了文档里的方式,但没有Vessel模块

如果可以的话,最好给一个样例数据,
直接读取该文件能够跑通。

针对于连续主力合约的回测的一些猜想

好像可以维护一个主力合约和到期时间的链表,等到到期时间 进行切换主力合约 、
尽量做到贴合实际? 是否可以考虑移仓功能 ?
如果你有什么意见 ,请在下方留言

Bugs `Indicator`的`add_bar`在opens为True时报错

Describe the bug / 问题描述
实盘时,在on_bar函数中self.bar_3 = Indicator(),在self.bar_3.add_bar(bar)时,indicatorinterface.py文件中Indicatoradd_bar方法的opens设为True时,报错:

发生异常: AttributeError
'list' object has no attribute 'endswith'

Environment/ 你的运行环境

  • OS: [e.g. Linux]
  • Python Version: [e.g.Python37]
  • ctpbee Version: [e.g. 1.0]

一个征集需求

需求/request

准备重写所有的函数注解 生成和tq一样的文档, 但是我一个人可能精力不足, 需要有人帮我review 代码 。也就是我提交的注解 以防出现错字 ····

最终表现效果 / result
获取完整的ctpbee文档

关于风险模型RiskLevel

大佬,你好,看了您的风控模型文档,我把他们加进了你的例子文件,如下。
可是运行后,总是提示事前检查失败。可是我在before_send_order里return true了。
是不是我用法有误?

class RiskMe(RiskLevel):
    def before_send_order(self, *args, **kwargs):
        print('before_send_order',self)
        return True, args, kwargs

    def after_send_order(self, result):
        print("I got the result: ",result)
  

class ActionMe(Action):
    def __init__(self, app):
        super().__init__(app)
        self.add_risk_check(self.send_order)

class Demo(CtpbeeApi):
    def __init__(self, name):
        super().__init__(name)
        self.instrument_set = ["ag2206.SHFE"]
        self.isok = False

    def on_tick(self, tick: TickData) -> None:
        self.action.buy(5069, 1, tick, price_type=OrderType.MARKET)
   
    def on_init(self, init):
        if init:
            self.isok = True

    def on_trade(self, trade: TradeData) -> None:
        print('on_trade',trade, "\n")


def letsgo():
    app = CtpBee(name="demo", import_name=__name__, refresh=True, risk=RiskLevel, action_class=ActionMe)
    demo = Demo("test")
    app.add_extension(demo)

    app.config.from_json("config.json")
    app.start(log_output=True)



if __name__ == '__main__':
    letsgo()

对大商所的交易日字段取本地日期

大商所tick中的日期是交易日日期,日盘没有问题,夜盘就会有一些影响,vnpy针对DCE做了转换,建议ctpbee也同步下

if not data["ActionDay"] or contract.exchange == Exchange.DCE:
    date_str: str = self.current_date
else:
    date_str: str = data["ActionDay"]

4097错误

请问下,我这边ctpbee_api用的0.31,也就是6.6.1 版本的ctp, 然后申请的穿透式也是6.6.1 ,但是还是报错4097,请问有什么办法可以debug吗

CEP.[100]

需求/request

添加一个g的全局变量用于存储某些信息,但是注意这个全局变量执行的作用于是想较于当前账户作用域? 还是全局作用域
Account global variable VS Global Variable

最终表现效果 / result

from ctpbee import g
初始化的时候设置
g.value = 1

其他全局进行访问
print(g.value)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.