Git Product home page Git Product logo

tbase's Introduction

Test PyPI version Coverage Status

tbase

基于强化学习的交易算法Baselines(Pytorch&Tensorflow2实现). 旨在提供一个在**股市上可复现的算法Baselines.

支持环境: Python 3.5–3.7

安装

设置 tushare token(Tushare token注册申请):

export TUSHARE_TOKEN=YOUR_TOKEN

pip install tbase --upgrade

Example

python3 -m tbase.run --alg ddpg

Features(In progress)

  • 可以完美复现训练过程, How?

  • 加速

    • 多进程CPU并行: 多进程运行独立的Enviroment进行探索
    • 多进程单GPU并行
    • 多进程多GPU并行
    • 分布式多机多卡并行: 参考 menger
  • 支持算法:

    • 单Agent

      • (Double Dueling)DQN
      • DDPG
      • TD3(Twin Delayed Deep Deterministic Policy Gradients)
      • A2C-ACKTR
        • A2C
        • ACKTR
      • PPO
      • PPO2
      • GAIL
      • OPD
    • 多Agent

      • MADDPG
  • 自定义Net

    • LSTM-MLP
    • LSTM_Merge_MLP
    • MLP
    • LSTM
    • CNN
    • CNN-MLP

训练

例如 ddpg

python3 -m tbase.run --alg ddpg --num_env 1 --gamma 0.53 --seed 9 --print_action

默认参数:

  • scenario: "average", 平均分仓操作
  • codes: "000001.SZ", 平安银行
  • indexs: "000001.SH,399001.SZ", 000001.SH:沪指, 399001.SZ: 深指
  • start: "20190101", 训练开始时间
  • end: "201901231", 训练结束时间
  • max_iter_num: "500", 训练轮数
  • num_env: "1", 并行进程数, 当num_env=1时,不使用多进程方式运行
  • seed: "None", 系统随机种子
  • print_action: "False", 随机打印action的值,方便查看action分布状况
  • reward_fn: "daily_return_with_chl_penalty", env reward function
  • run_id: "1", 运行序号, 方便查看相同参数多次运行结果差异
  • debug: "False", debug模式,打印具体的交易信息
  • eval: "False", 回测评估,为True时,不训练,只加载模型进行回测
  • 其他参数

Defalut policy net setting(actor)

actor

Defalut value net setting(critic)

critic

运行tensorboard

tensorboard --logdir=/tmp/tbase/tensorboard

可以在http://localhost:6006中查看训练的loss, reward ,portfolio, time等指标

loss

评估&&回测

  • 训练周期内的评估指标
  • 回测: 模型在训练周期外(评估周期)的评估指标
    # lookback=10, 若测试从20200101开始, 则eval_start往前推10个交易日
    python -m tbase.run --alg ddpg --eval --eval_start 20191218 --eval_end 20200223 --seed 9
    
  • 滑动窗口更新模型, 在评估周期内,每隔一个窗口T,重新训练一次模型,当T>评估周期时,等价于固定模型

评估指标

  • 绝对收益率(Absolute Return)

  • 额外收益率(Excess Return)

    • 相对于"买入持有"策略
    • 相对于基线策略比如"沪深300"
  • 最大回撤: 在选定周期内任一历史时点往后推,净值走到最低点时的收益率回撤幅度的最大值

  • 夏普比率: 投资组合每承受一单位总风险,会产生多少的超额报酬

  • 年化收益率

TODO

  • 考虑到部署到线上的方便性, 增加tensorflow实现
  • 参数配置管理: yml
  • 使用cpprb作业replay buffer, 提升buffer性能
  • 模型分享,评估,部署(wandb)
    • 模型榜单排名
    • performance: 参考评估指标(去除绝对收益率)
  • 大规模训练: 参考 menger
    • 分布式, vector evnviroment explorer, 方便无限scale
    • 多个buffer
      • sample
      • model update & inference
    • 监控: explorer, buffer, learner, 吞吐量,以方便定位bottle neck
    • stock embedding

由于计算资源有限,为所有的算法跑完A股中所有的股票,需要花费大量的时间,希望有空闲计算资源的朋友,可以跑一下模型,将实验结果分享出来

线上交流方式

  • QQ群: 477860214

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.