Convert Figma logo to code with AI

ricequant logorqalpha

A extendable, replaceable Python algorithmic backtest && trading framework supporting multiple securities

5,323
1,609
5,323
24

Top Related Projects

17,609

Zipline, a Pythonic Algorithmic Trading Library

25,149

基于Python的开源量化交易平台开发框架

15,357

Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.

Python Algorithmic Trading Library

An Algorithmic Trading Library for Crypto-Assets in Python

9,713

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)

Quick Overview

RQAlpha is an open-source quantitative trading and backtesting system developed by Ricequant Inc. It provides a Python-based framework for developing, testing, and deploying trading strategies, with support for various data sources and trading instruments.

Pros

  • Flexible and extensible architecture allowing for custom modules and plugins
  • Supports multiple asset classes including stocks, futures, and options
  • Provides realistic simulation with accurate transaction costs and slippage models
  • Offers both event-driven and vector-based backtesting engines for different performance needs

Cons

  • Learning curve can be steep for beginners in quantitative finance
  • Documentation is primarily in Chinese, which may be challenging for non-Chinese speakers
  • Limited built-in strategies and examples compared to some other backtesting platforms
  • Requires additional setup for real-time trading integration

Code Examples

  1. Creating a simple moving average crossover strategy:
from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"
    context.SHORT_PERIOD = 20
    context.LONG_PERIOD = 120

def handle_bar(context, bar_dict):
    prices = history_bars(context.s1, context.LONG_PERIOD, '1d', 'close')
    short_ma = prices[-context.SHORT_PERIOD:].mean()
    long_ma = prices.mean()

    cur_position = context.portfolio.positions[context.s1].quantity
    shares = context.portfolio.cash / bar_dict[context.s1].close

    if short_ma > long_ma and cur_position == 0:
        order_shares(context.s1, shares)
    elif short_ma < long_ma and cur_position > 0:
        order_shares(context.s1, -cur_position)
  1. Accessing fundamental data:
from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    pe_ratio = fundamental.get_fundamentals(
        query(fundamentals.eod_derivative_indicator.pe_ratio)
        .filter(fundamentals.eod_derivative_indicator.stockcode == context.s1)
    ).pe_ratio[0]

    if pe_ratio < 10:
        order_target_percent(context.s1, 1)
    elif pe_ratio > 20:
        order_target_percent(context.s1, 0)
  1. Using technical indicators:
from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    close_prices = history_bars(context.s1, 14, '1d', 'close')
    rsi = talib.RSI(close_prices, timeperiod=14)[-1]

    if rsi < 30:
        order_target_percent(context.s1, 1)
    elif rsi > 70:
        order_target_percent(context.s1, 0)

Getting Started

  1. Install RQAlpha:
pip install rqalpha
  1. Create a new strategy file (e.g., my_strategy.py) with your trading logic.

  2. Run the backtest:

rqalpha run -f my_strategy.py -s 2015-01-01 -e 2020-01-01 -a stock 100000 --benchmark 000300.XSHG
  1. Analyze the results using the generated report and plots.

Competitor Comparisons

17,609

Zipline, a Pythonic Algorithmic Trading Library

Pros of Zipline

  • More mature and widely adopted in the quantitative finance community
  • Extensive documentation and community support
  • Seamless integration with Quantopian's research platform

Cons of Zipline

  • Less focus on Chinese markets compared to RQAlpha
  • Steeper learning curve for beginners
  • Limited built-in support for multi-asset class strategies

Code Comparison

Zipline example:

from zipline.api import order, record, symbol

def initialize(context):
    context.asset = symbol('AAPL')

def handle_data(context, data):
    order(context.asset, 10)
    record(AAPL=data.current(context.asset, 'price'))

RQAlpha example:

from rqalpha.api import order_shares, current_price

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    order_shares(context.s1, 100)
    logger.info("Price of {} is {}".format(context.s1, current_price(context.s1)))

Both frameworks offer similar functionality for basic trading strategies, but RQAlpha's syntax is more intuitive for beginners and focuses on Chinese market symbols. Zipline provides more advanced features and integrations with other tools in the Quantopian ecosystem.

25,149

基于Python的开源量化交易平台开发框架

Pros of vnpy

  • More comprehensive trading platform with support for multiple asset classes
  • Larger community and more frequent updates
  • Modular architecture allowing for easier customization and extension

Cons of vnpy

  • Steeper learning curve due to its complexity
  • Potentially overwhelming for beginners or those focused solely on algorithmic trading
  • Requires more system resources due to its extensive features

Code Comparison

vnpy:

from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp

def main():
    qapp = create_qapp()
    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)
    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()
    qapp.exec_()

rqalpha:

from rqalpha import run_func

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    order_shares(context.s1, 100)

config = {
    "base": {
        "start_date": "2015-01-01",
        "end_date": "2016-01-01",
        "benchmark": "000001.XSHE",
        "accounts": {
            "stock": 100000
        }
    }
}

run_func(init, handle_bar, config)
15,357

Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.

Pros of qlib

  • More comprehensive and advanced machine learning capabilities for quantitative investing
  • Extensive documentation and examples for various trading strategies
  • Active development and support from Microsoft's research team

Cons of qlib

  • Steeper learning curve due to its more complex architecture
  • Primarily focused on machine learning-based strategies, which may not suit all trading approaches
  • Requires more computational resources for advanced models

Code comparison

rqalpha:

from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    order_shares(context.s1, 100)

qlib:

from qlib.contrib.strategy import TopkDropoutStrategy
from qlib.contrib.evaluate import backtest_daily

strategy = TopkDropoutStrategy(
    model=model, dataset=dataset, topk=50, n_drop=5
)
report, positions = backtest_daily(strategy)

Summary

While rqalpha offers a simpler and more intuitive approach to backtesting and strategy development, qlib provides a more advanced platform for quantitative investing with a focus on machine learning techniques. rqalpha may be more suitable for beginners or those seeking a straightforward backtesting solution, while qlib caters to researchers and professionals looking to leverage cutting-edge AI/ML methods in their trading strategies.

Python Algorithmic Trading Library

Pros of PyAlgoTrade

  • More extensive documentation and tutorials
  • Supports a wider range of data sources, including Yahoo Finance and Google Finance
  • Easier to integrate with external libraries and custom data sources

Cons of PyAlgoTrade

  • Less active development and community support
  • Fewer built-in strategies and indicators compared to RQAlpha
  • Limited support for Chinese markets and A-shares

Code Comparison

PyAlgoTrade:

from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        super(MyStrategy, self).__init__(feed)
        self.__instrument = instrument

RQAlpha:

from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    order_shares(context.s1, 100)

Both frameworks provide a straightforward way to define trading strategies, but RQAlpha's API is more concise and tailored for the Chinese market. PyAlgoTrade offers more flexibility in terms of data sources and strategy implementation, while RQAlpha provides a more streamlined approach for backtesting and live trading in Chinese markets.

An Algorithmic Trading Library for Crypto-Assets in Python

Pros of Catalyst

  • Supports cryptocurrency trading and backtesting
  • Integrates with popular exchanges like Binance and Coinbase
  • Offers real-time market data and live trading capabilities

Cons of Catalyst

  • Less active development and community support
  • Limited documentation and learning resources
  • Narrower focus on cryptocurrency markets

Code Comparison

RQAlpha:

from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    order_shares(context.s1, 100)

Catalyst:

from catalyst import run_algorithm
import catalyst.api as api

def initialize(context):
    context.asset = api.symbol('btc_usdt')

def handle_data(context, data):
    api.order(context.asset, 1)

Both frameworks use a similar structure with initialization and event-handling functions. RQAlpha focuses on traditional securities, while Catalyst is tailored for cryptocurrency trading. Catalyst uses specific cryptocurrency symbols and APIs, whereas RQAlpha uses stock tickers and more general order functions.

9,713

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)

Pros of Lean

  • More extensive documentation and community support
  • Broader asset class coverage, including stocks, forex, and cryptocurrencies
  • Robust backtesting and live trading capabilities

Cons of Lean

  • Steeper learning curve due to its complexity
  • Requires more computational resources for large-scale backtesting

Code Comparison

RQAlpha:

from rqalpha.api import *

def init(context):
    context.s1 = "000001.XSHE"

def handle_bar(context, bar_dict):
    order_shares(context.s1, 100)

Lean:

public class BasicTemplateAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        SetStartDate(2013, 10, 7);
        SetEndDate(2013, 10, 11);
        SetCash(100000);
        AddEquity("SPY", Resolution.Minute);
    }

    public override void OnData(Slice data)
    {
        if (!Portfolio.Invested)
        {
            SetHoldings("SPY", 1);
        }
    }
}

RQAlpha focuses on simplicity and ease of use for the Chinese market, while Lean offers a more comprehensive and flexible platform for global markets. RQAlpha's Python-based API is more accessible for beginners, whereas Lean's C# implementation provides enhanced performance and extensibility for advanced users.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

======= RQAlpha

.. image:: https://raw.githubusercontent.com/ricequant/rq-resource/master/rqalpha/logo.jpg

.. image:: https://github.com/ricequant/rqalpha/workflows/Test/badge.svg :target: https://github.com/ricequant/rqalpha/actions?query=workflow%3ATest :alt: GitHub Actions status for master branch

.. image:: https://coveralls.io/repos/github/ricequant/rqalpha/badge.svg?branch=master :target: https://coveralls.io/github/ricequant/rqalpha?branch=master

.. image:: https://readthedocs.org/projects/rqalpha/badge/?version=latest :target: http://rqalpha.readthedocs.io/zh_CN/latest/?badge=latest :alt: Documentation Status

.. image:: https://img.shields.io/pypi/v/rqalpha.svg :target: https://pypi.python.org/pypi/rqalpha :alt: PyPI Version

.. image:: https://img.shields.io/pypi/pyversions/rqalpha.svg :target: https://pypi.python.org/pypi/rqalpha :alt: Python Version Support

.. image:: https://img.shields.io/pypi/dm/rqalpha?label=pypi%20downloads :target: https://pypi.python.org/pypi/rqalpha :alt: PyPI - Downloads

RQAlpha 从数据获取、算法交易、回测引擎,实盘模拟,实盘交易到数据分析,为程序化交易者提供了全套解决方案。

仅限非商业使用。如需商业使用,请联系我们:public@ricequant.com

RQAlpha 具有灵活的配置方式,强大的扩展性,用户可以非常容易地定制专属于自己的程序化交易系统。

RQAlpha 所有的策略都可以直接在 Ricequant_ 上进行回测和实盘模拟,并且可以通过微信和邮件实时推送您的交易信号。

Ricequant_ 是一个开放的量化算法交易社区,为程序化交易者提供免费的回测和实盘模拟环境,并且会不间断举行实盘资金投入的量化比赛。

特点

====================== ================================================================================= 易于使用 让您集中于策略的开发,一行简单的命令就可以执行您的策略。 完善的文档 您可以直接访问 RQAlpha 文档_ 或者 Ricequant 文档_ 来获取您需要的信息。 活跃的社区 您可以通过访问 Ricequant 社区_ 获取和询问有关 RQAlpha 的一切问题,有很多优秀的童鞋会解答您的问题。 稳定的环境 每天都有会大量的算法交易在 Ricequant 上运行,无论是 RQAlpha,还是数据,我们能会做到问题秒处理,秒解决。 灵活的配置 您可以使用多种方式来配置和运行策略,只需简单的配置就可以构建适合自己的交易系统。 强大的扩展性 开发者可以基于我们提供的 Mod Hook 接口来进行扩展。 ====================== =================================================================================

快速指引

  • RQAlpha 介绍_
  • 安装指南_
  • 10分钟学会 RQAlpha_
  • 策略示例_

API 手册

  • API_ : RQAlpha API 手册

更新记录

  • CHANGELOG_ RQALPHA 更新记录

Mod

RQAlpha 提供了极具拓展性的 Mod Hook 接口,这意味着开发者可以非常容易的对接第三方库。

您可以通过如下方式使用 安装和使用Mod:

.. code-block:: bash

# 查看当前安装的 Mod 列表及状态
$ rqalpha mod list
# 启用 Mod
$ rqalpha mod enable xxx
# 禁用 Mod
$ rqalpha mod disable xxx

以下是目前已经集成的 Mod 列表:

======================= ================================================================================== Mod名 说明 ======================= ================================================================================== sys_accounts_ 提供了股票、期货的下单 API 实现及持仓模型的实现 sys_analyser_ 记录每天的下单、成交、投资组合、持仓等信息,并计算风险度指标,并以csv、plot图标等形式输出分析结果 sys_progress_ 在控制台输出当前策略的回测进度。 sys_risk_ 对订单进行事前风控校验 sys_scheduler_ 提供了定时器,即按照特定周期执行指定逻辑的功能 sys_simulation_ 提供了模拟撮合引擎及回测事件源等模块,为回测和模拟交易提供支持 sys_transaction_cost_ 实现了股票、期货的交易税费计算逻辑 ======================= ==================================================================================

如果您基于 RQAlpha 进行了 Mod 扩展,欢迎告知我们,在审核通过后,会在 Mod 列表中添加您的 Mod 信息和链接。

关于 4.x 版本数据 bundle 变更的说明

RQAlpha 于近期更新了 4.0.0 版本,4.0.0 添加了大量功能改进和体验改善。

其中一点需要您额外注意:我们在 4.0.0 版本中重构了数据 bundle 的格式,原 3.x 版本的 bundle 已停止更新,您需要更新 RQAlpha 至 4.x 以使用优化过的 bundle。 另外,为了平衡您的使用体验与我们的维护成本,4.x 版本提供下载的 bundle 改为月度更新,但您仍可以使用 RQData_ 在本地 随时 使用最新数据更新 bundle, 具体操作可查看 RQAlpha 文档 <https://rqalpha.readthedocs.io/zh_CN/latest/intro/install.html#intro-install-get-data>_ 。

RQData数据本地化服务

为专业投资者提供便利易用的金融数据方案,免除数据整理、清洗及运维的困扰,使投研人员及策略开发者可以更专注于投研及模型开发等关键环节。米筐RQData金融数据API可无缝对接RQAlpha,您只需在策略中import rqdatac,即可通过API本地调用以下数据:

============================= ================================================================================== 合约信息 中国A股、指数、场内场外基金、期货、场内债券的基本合约信息 A股基础信息 交易日、股票拆分和分红、停牌、ST股判断等数据 行情数据 A股2005年至今及实时行情数据(含连续竞价时间段);指数快照行情、历史权重、指数估值指标等 基金数据 基础数据、净值数据、报告披露、持仓数据等 期货、期权和现货数据 全市场期权数据;期货历史及快照行情数据等;期货主力连续合约;期货会员持仓排名及仓单 可转债数据 可转债基础合约;可转债股价、转债导致规模变化、现金等数据 A股上市以来的所有财务数据 基础财务数据、营运、盈利能力、估值等;财务快报及业绩预告、TTM滚动财务数据等;支持财务数据Point in Time API 行业、板块、概念分类 股票资金现金流入流出、换手率 风格因子数据 风格因子暴露度、收益率、协方差和特异风险。(每个交易日8:30开始更新增量数据) 宏观经济数据 存款准备金率、货币供应量、大量宏观因子等数据 电商数据 天猫、淘宝、京东三大平台(日更新)。注:与超对称科技合作提供 舆情数据 雪球与东方财富股吧。注:与数据合作方合作提供 ============================= ==================================================================================

目前RQData已正式上线,支持Python API、Matlab API及Excel插件等多种调取方式,欢迎 免费试用 <https://www.ricequant.com/welcome/rqdata>_ 和 咨询私有化部署 <https://www.ricequant.com/welcome/pricing>_ 。

加入开发

  • 如何贡献代码_
  • 基本概念_
  • RQAlpha 基于 Mod 进行扩展_

获取帮助

关于RQAlpha的任何问题可以通过以下途径来获取帮助

  • 可以通过 索引_ 或者使用搜索功能来查找特定问题
  • 在 Github Issues_ 中提交issue
  • RQAlpha 交流群「487188429」

.. _Github Issues: https://github.com/ricequant/rqalpha/issues .. _Ricequant: https://www.ricequant.com/algorithms .. _RQAlpha 文档: http://rqalpha.readthedocs.io/zh_CN/latest/ .. _Ricequant 文档: https://www.ricequant.com/api/python/chn .. _Ricequant 社区: https://www.ricequant.com/community/category/all/ .. _FAQ: http://rqalpha.readthedocs.io/zh_CN/latest/faq.html .. _索引: http://rqalpha.readthedocs.io/zh_CN/latest/genindex.html .. _RQPro: https://www.ricequant.com/rqpro_propaganda/?utm_source=github .. _专业级本地终端RQPro: https://www.ricequant.com/rqpro_propaganda/?utm_source=github

.. _RQAlpha 介绍: http://rqalpha.readthedocs.io/zh_CN/latest/intro/overview.html .. _安装指南: http://rqalpha.readthedocs.io/zh_CN/latest/intro/install.html .. _10分钟学会 RQAlpha: http://rqalpha.readthedocs.io/zh_CN/latest/intro/tutorial.html .. _策略示例: http://rqalpha.readthedocs.io/zh_CN/latest/intro/examples.html

.. _API: http://rqalpha.readthedocs.io/zh_CN/latest/api/base_api.html

.. _如何贡献代码: http://rqalpha.readthedocs.io/zh_CN/latest/development/make_contribute.html .. _基本概念: http://rqalpha.readthedocs.io/zh_CN/latest/development/basic_concept.html .. _RQAlpha 基于 Mod 进行扩展: http://rqalpha.readthedocs.io/zh_CN/latest/development/mod.html .. _History: http://rqalpha.readthedocs.io/zh_CN/latest/history.html .. _TODO: https://github.com/ricequant/rqalpha/blob/master/TODO.md .. _develop 分支: https://github.com/ricequant/rqalpha/tree/develop .. _master 分支: https://github.com/ricequant/rqalpha .. _rqalpha_mod_tushare: https://github.com/ricequant/rqalpha-mod-tushare .. _通过 Mod 扩展 RQAlpha: http://rqalpha.io/zh_CN/latest/development/mod.html .. _sys_analyser: https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_analyser/README.rst .. _sys_scheduler: https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_scheduler/README.rst .. _sys_progress: https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_progress/README.rst .. _sys_risk: https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_risk/README.rst .. _sys_simulation: https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_simulation/README.rst .. _sys_accounts: https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_accounts/README.rst .. _sys_transaction_cost: https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_transaction_cost/README.rst .. _RQData数据本地化服务: https://www.ricequant.com/doc/rqdata-institutional .. _点击链接免费开通: https://ricequant.mikecrm.com/h7ZFJnT .. _RQData: https://www.ricequant.com/welcome/rqdata .. _CHANGELOG: https://rqalpha.readthedocs.io/zh_CN/latest/history.html