Giter Site home page Giter Site logo

ctpbee_api'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_api's People

Contributors

der-rote-elefant avatar somewheve avatar

Stargazers

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

Watchers

 avatar  avatar

ctpbee_api's Issues

穿透测试出错,急

大佬,我在测试穿透验证。
配置文件里已经设置接口为 ctp_se
然后运行测试程序,报错如下
image
追踪到源码,发现
E:\myquant\venv_quant389\Lib\site-packages\ctpbee\interface_init_.py文件里:

class Interface:
""" 按照接口进行加载而不是一次性进行加载 """

@classmethod
def load_interface(cls, interface):
    if interface == "ctp":
        from ctpbee.interface.ctp import BeeTdApi, BeeMdApi
        return BeeMdApi, BeeTdApi
    if interface == "looper":
        from ctpbee.interface.looper import LooperMe, LooperYou
        return LooperMe, LooperYou
    if interface == "ctp_mini":
        from ctpbee.interface.ctp_mini import MMdApi, MTdApi
        return MMdApi, MTdApi
    else:
        raise ValueError("错误参数")

并没有ctp_se接口。所以报错误参数错误。 怎么解决呢?急啊!

ctpbee_api 0.40 编译失败, 以前的0.33 可以编译成功

ctpbee_api 0.40 编译失败, 以前的0.33 可以编译成功

gcc 版本

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-6' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-6)

Building wheels for collected packages: cologer, ctpbee_api
Building wheel for cologer (setup.py) ... done
Created wheel for cologer: filename=cologer-2.0-py3-none-any.whl size=3947 sha256=f3c4f157f071465d2f1a4b06456518c999564cdf3b641d700ef715b1d5c3ce7e
Stored in directory: /root/.cache/pip/wheels/8d/12/d2/20f1e804758e7271140ee84907669f66006045f65f14f222ca
Building wheel for ctpbee_api (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> [496 lines of output]
.................
^
In file included from /usr/local/include/python3.10/Python.h:130,
from ctpbee_api/ctp_mini/include/pybind11/detail/common.h:112,
from ctpbee_api/ctp_mini/include/pybind11/pytypes.h:12,
from ctpbee_api/ctp_mini/include/pybind11/cast.h:13,
from ctpbee_api/ctp_mini/include/pybind11/attr.h:13,
from ctpbee_api/ctp_mini/include/pybind11/pybind11.h:43,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_mini.h:10,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_minimd/vnctp_minimd.h:6,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_minimd/vnctp_minimd.cpp:3:
/usr/local/include/python3.10/ceval.h:122:37: note: declared here
Py_DEPRECATED(3.9) PyAPI_FUNC(void) PyEval_InitThreads(void);
^~~~~~~~~~~~~~~~~~
In file included from ctpbee_api/ctp_mini/include/pybind11/cast.h:16,
from ctpbee_api/ctp_mini/include/pybind11/attr.h:13,
from ctpbee_api/ctp_mini/include/pybind11/pybind11.h:43,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_mini.h:10,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_minimd/vnctp_minimd.h:6,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_minimd/vnctp_minimd.cpp:3:
ctpbee_api/ctp_mini/include/pybind11/detail/internals.h:194:28: warning: ‘void PyEval_InitThreads()’ is deprecated [-Wdeprecated-declarations]
PyEval_InitThreads();
^
In file included from /usr/local/include/python3.10/Python.h:130,
from ctpbee_api/ctp_mini/include/pybind11/detail/common.h:112,
from ctpbee_api/ctp_mini/include/pybind11/pytypes.h:12,
from ctpbee_api/ctp_mini/include/pybind11/cast.h:13,
from ctpbee_api/ctp_mini/include/pybind11/attr.h:13,
from ctpbee_api/ctp_mini/include/pybind11/pybind11.h:43,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_mini.h:10,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_minimd/vnctp_minimd.h:6,
from ctpbee_api/ctp_mini/vnctp_mini/vnctp_minimd/vnctp_minimd.cpp:3:
/usr/local/include/python3.10/ceval.h:122:37: note: declared here
Py_DEPRECATED(3.9) PyAPI_FUNC(void) PyEval_InitThreads(void);
^~~~~~~~~~~~~~~~~~
gcc -pthread -shared -Wl,--strip-all build/temp.linux-x86_64-3.10/ctpbee_api/ctp_mini/vnctp_mini/vnctp_minimd/vnctp_minimd.o -Lctpbee_api/ctp_mini -L/usr/local/lib -Wl,--enable-new-dtags,-R$ORIGIN -lthostmduserapi -lthosttraderapi -o build/lib.linux-x86_64-3.10/ctpbee_api/ctp_mini/vnctp_minimd.cpython-310-x86_64-linux-gnu.so -lstdc++
/usr/bin/ld: ctpbee_api/ctp_mini/libthostmduserapi.so: file not recognized: file truncated
collect2: error: ld returned 1 exit status
error: command '/usr/bin/gcc' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for ctpbee_api
Running setup.py clean for ctpbee_api
Successfully built cologer
Failed to build ctpbee_api

楼主能让ctpbee的api扩展一下,支持openctp吗?

openctp api https://github.com/krenx1983/openctp
有很多,据说vnpy已支持它。

据openctp说,只要用它的thostmduserapi_se.dll 、thosttraderapi_se.dll替换ctpbee的对应文件,就可以使用它的7*24仿真环境,叫tts。我试验了一下,有时可以,有时不行,出现如下错误。不知这是openctp的问题还是ctpbee的问题。

2022-04-23 13:27:20 app INFO app 行情服务器连接成功
2022-04-23 13:27:20 app INFO app 交易连接成功
2022-04-23 13:27:202022-04-23 13:27:20 app app INFOINFO appapp 交易服务器验证成功行情服务器登录成功

2022-04-23 13:27:20 app INFO app 交易登录成功
2022-04-23 13:27:20 app INFO app 结算信息确认成功
e:\myquant\venv_quant389\lib\site-packages\ctpbee\interface\ctp\td_api.py:308: UserWarning: 未预料到的合约问题 错误信息: 'TTS'
warnings.warn(f"未预料到的合约问题 错误信息: {e}")

ctpbee-api在ubuntu上,python3.6编译时一直过不了

ctpbee-api在ubuntu上,python3.6编译时一直过不了,一直卡在这里,不知道需要怎么操作才能正确安装
Stored in directory: /root/.cache/pip/wheels/cb/e4/30/89948c08ef14ddab0845ba330395decc45e9704af58e877b4c
Running setup.py bdist_wheel for ctpbee-api ... \

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.