• 开源支持东财自动交易的简单量化交易框架pytrader


    pytrader是基于 easytradereasyquotation 的量化交易框架,支持东方财富自动交易,支持进行策略回测。

    开源地址:https://github.com/jadepeng/pytrader

    策略文件

    在strategies目录,可以参考已有的编写。

    策略需要继承StrategyTemplate类,实现int和onbar等函数。

    init 设置关注的股票,行情引擎就会推动股票行情。

    
        def init(self):
            for stock_code in self.watch_stocks:
                self.quotation_engine.watch(stock_code)
    
    
    

    行情数据到来时,触发on_bar函数:

    
    
    def on_bar(self, context: Context, data: Dict[str, DataFrame]):
        pass
    
    
    • Context 是一个工具类,可以获取其他bar或者计算cci、rsi等指标
    • data是推动的行情字典,可以用股票代码获取DataFrame类型的行情数据

    在线交易

    参见 tradertest.py ,会加载所有策略,稍微改动下也能支持制定策略

    
    import easyquant
    from easyquant import DefaultLogHandler
    
    print('测试 DEMO')
    
    # 东财
    broker = 'eastmoney'
    
    # 自己准备
    # {
    #     "user": "",
    #     "password": ""# }
    need_data = 'account.json'
    
    log_type = 'file'
    
    log_handler = DefaultLogHandler(name='测试', log_type=log_type, filepath='logs.log')
    
    m = easyquant.MainEngine(broker,
                             need_data,
                             quotation='online',
                             # 1分钟K线
                             bar_type="1m",
                             log_handler=log_handler)
    m.is_watch_strategy = True  # 策略文件出现改动时,自动重载,不建议在生产环境下使用
    m.load_strategy()
    m.start()
    
    

    运行结果:

    测试 DEMO
    already logined in
    Balance(asset_balance=59686.86, current_balance=6834.62, enable_balance=56977.76, frozen_balance=0.0, market_value=2709.0999999999985, money_type='人民币', pre_interest=0)
    Balance(asset_balance=59686.86, current_balance=6834.62, enable_balance=56977.76, frozen_balance=0.0, market_value=2709.0999999999985, money_type='人民币', pre_interest=0)
    [2021-11-11 11:59:05.663694] INFO: 测试策略1: 164906 名称:中国互联 市值:2709.0 盈亏: -814.7999999999997
    1.29
    [2021-11-11 11:59:05.667705] INFO: 测试策略1: 164906 CCI:[200.           5.07246377  34.82587065]
    [2021-11-11 11:59:05.668707] INFO: 测试策略1: 164906 RSI6:[75.21876239 52.10112261 52.10112261]
    [2021-11-11 11:59:05.668707] INFO: 测试策略1: cci enter high space
    [2021-11-11 11:59:05.668707] INFO: 测试策略1: 上升中,继续持仓
    [2021-11-11 11:59:05.668707] INFO: 测试策略1: 
    Balance(asset_balance=59686.86, current_balance=6834.62, enable_balance=56977.76, frozen_balance=0.0, market_value=2709.0999999999985, money_type='人民币', pre_interest=0)
    Balance(asset_balance=59686.86, current_balance=6834.62, enable_balance=56977.76, frozen_balance=0.0, market_value=2709.0999999999985, money_type='人民币', pre_interest=0)
    [2021-11-11 12:00:07.303305] INFO: 测试策略1: 164906 名称:中国互联 市值:2709.0 盈亏: -814.7999999999997
    1.29
    [2021-11-11 12:00:07.314334] INFO: 测试策略1: 164906 CCI:[200.           5.07246377  34.82587065]
    [2021-11-11 12:00:07.315336] INFO: 测试策略1: 164906 RSI6:[75.21876239 52.10112261 52.10112261]
    [2021-11-11 12:00:07.316338] INFO: 测试策略1: cci enter high space
    [2021-11-11 12:00:07.316338] INFO: 测试策略1: 上升中,继续持仓
    [2021-11-11 12:00:07.317342] INFO: 测试策略1: 
    Balance(asset_balance=59686.86, current_balance=6834.62, enable_balance=56977.76, frozen_balance=0.0, market_value=2709.0999999999985, money_type='人民币', pre_interest=0)
    Balance(asset_balance=59686.86, current_balance=6834.62, enable_balance=56977.76, frozen_balance=0.0, market_value=2709.0999999999985, money_type='人民币', pre_interest=0)
    [2021-11-11 12:01:18.392363] INFO: 测试策略1: 164906 名称:中国互联 市值:2709.0 盈亏: -814.7999999999997
    1.29
    

    回测

    参考backtest.py,设置回测的时间和策略,注意使用quotation需要为tushare或者jqdata,可以自己申请

    import easyquotation
    
    import easyquant
    from easyquant import  DefaultLogHandler, PushBaseEngine
    from easyquant.log_handler.default_handler import MockLogHandler
    from strategies.CCI import Strategy
    
    print('backtest 回测 测试 ')
    
    broker = 'mock'
    need_data = 'account.json'
    
    #
    mock_start_dt = "2020-01-01"
    mock_end_dt= "2021-11-11"
    
    
    m = easyquant.MainEngine(broker, need_data,
                             quotation='tushare',
                             # quotation='jqdata',
                             bar_type="1d")
    
    log_handler = MockLogHandler(context=m.context)
    
    # 选择策略
    strategy = Strategy(user=m.user, log_handler=log_handler, main_engine=m)
    
    m.start_mock(mock_start_dt, mock_end_dt, strategy)
    
    print('mock end')
    
    print(m.user.get_balance())
    
    for deal in m.user.get_current_deal():
        print(deal.deal_time, deal.bs_type, deal.deal_price, deal.deal_amount)
    

    运行结果举例:

    
    backtest 回测 测试 
    [2021-11-11 12:09:44.962805] INFO: default: 启动主引擎
    [2020-02-04 15:00:00] INFO: 002230 RSI6:[48.76350746 20.16124773 38.11308036]
    [2020-02-04 15:00:00] INFO: RSI上穿,抄底买入 35.15元 28300股
    [2020-03-17 15:00:00] INFO: 没钱买入
    [2020-03-19 15:00:00] INFO: 没钱买入
    [2020-03-23 15:00:00] INFO: 跌破20日线,止损 34.74元 
    [2020-03-23 15:00:00] INFO: 002230 盈亏: -14587.234999999835 
    [2020-03-24 15:00:00] INFO: 002230 RSI6:[34.67789665 25.20904214 33.31120403]
    [2020-03-24 15:00:00] INFO: RSI上穿,抄底买入 35.48元 27500股
    [2020-03-25 15:00:00] INFO: 没钱买入
    [2020-03-30 15:00:00] INFO: 跌破20日线,止损 34.53元 
    [2020-03-30 15:00:00] INFO: 002230 盈亏: -26124.999999999884 
    [2020-04-01 15:00:00] INFO: 002230 RSI6:[29.6608755  28.83954667 29.33914896]
    [2020-04-01 15:00:00] INFO: RSI上穿,抄底买入 34.5元 27400股
    [2020-04-02 15:00:00] INFO: 没钱买入
    [2020-04-16 15:00:00] INFO: 跌破20日线,止损 34.42元 
    [2020-04-16 15:00:00] INFO: 002230 盈亏: -2191.999999999953 
    [2020-04-30 15:00:00] INFO: 002230 RSI6:[42.27921854 21.00976129 24.6876384 ]
    [2020-04-30 15:00:00] INFO: RSI上穿,抄底买入 33.28元 28100股
    [2020-05-18 15:00:00] INFO: 跌破20日线,止损 33.22元 
    [2020-05-18 15:00:00] INFO: 002230 盈亏: -1686.000000000064 
    [2020-05-19 15:00:00] INFO: 002230 RSI6:[63.47371421 33.38711899 36.98845238]
    [2020-05-19 15:00:00] INFO: RSI上穿,抄底买入 34.63元 26800股
    [2020-05-21 15:00:00] INFO: 跌破20日线,止损 32.87元 
    [2020-05-21 15:00:00] INFO: 002230 盈亏: -47168.00000000014 
    [2020-05-26 15:00:00] INFO: 002230 RSI6:[36.84886995 24.31515989 31.02544277]
    [2020-05-26 15:00:00] INFO: RSI上穿,抄底买入 32.2元 27100股
    [2020-05-27 15:00:00] INFO: 跌破20日线,止损 31.7元 
    [2020-05-27 15:00:00] INFO: 002230 盈亏: -13550.000000000096 
    [2020-05-28 15:00:00] INFO: 002230 RSI6:[33.23760915 31.96286073 36.84886995]
    [2020-05-28 15:00:00] INFO: RSI上穿,抄底买入 31.76元 26900股
    [2020-05-29 15:00:00] INFO: 没钱买入
    [2020-06-11 15:00:00] INFO: 002230 RSI6:[68.15891577 81.93030038 62.4026211 ]
    [2020-06-11 15:00:00] INFO: RSI拐点,卖出 34.86元 26900股
    [2020-06-11 15:00:00] INFO: 002230 盈亏: 83389.99999999994 
    [2020-07-17 15:00:00] INFO: 002230 RSI6:[33.74292572 31.23748151 42.53459562]
    [2020-07-17 15:00:00] INFO: RSI上穿,抄底买入 37.24元 25000股
    [2020-07-20 15:00:00] INFO: 没钱买入
    [2020-07-23 15:00:00] INFO: 跌破20日线,止损 37.23元 
    [2020-07-23 15:00:00] INFO: 002230 盈亏: -250.0000000001279 
    [2020-07-28 15:00:00] INFO: 002230 RSI6:[26.57979316 25.44498903 25.44498903]
    [2020-07-28 15:00:00] INFO: RSI上穿,抄底买入 36.0元 25700股
    [2020-07-29 15:00:00] INFO: 没钱买入
    [2020-09-08 15:00:00] INFO: 没钱买入
    [2020-09-09 15:00:00] INFO: 跌破20日线,止损 34.87元 
    [2020-09-09 15:00:00] INFO: 002230 盈亏: -29041.000000000065 
    [2020-09-11 15:00:00] INFO: 002230 RSI6:[23.21420985 19.75351242 21.54530544]
    [2020-09-11 15:00:00] INFO: RSI上穿,抄底买入 34.7元 25600股
    [2020-09-14 15:00:00] INFO: 没钱买入
    [2020-09-15 15:00:00] INFO: 没钱买入
    [2020-09-16 15:00:00] INFO: 没钱买入
    [2020-09-18 15:00:00] INFO: 没钱买入
    [2020-09-24 15:00:00] INFO: 跌破20日线,止损 34.45元 
    [2020-09-24 15:00:00] INFO: 002230 盈亏: -6400.0 
    [2020-09-25 15:00:00] INFO: 002230 RSI6:[31.28480547 28.80417236 37.72008547]
    [2020-09-25 15:00:00] INFO: RSI上穿,抄底买入 34.52元 25400股
    [2020-09-28 15:00:00] INFO: 跌破20日线,止损 34.05元 
    [2020-09-28 15:00:00] INFO: 002230 盈亏: -11938.000000000151 
    [2020-09-29 15:00:00] INFO: 002230 RSI6:[39.35969455 24.42732022 31.28480547]
    [2020-09-29 15:00:00] INFO: RSI上穿,抄底买入 34.49元 24900股
    [2020-09-30 15:00:00] INFO: 跌破20日线,止损 34.27元 
    [2020-09-30 15:00:00] INFO: 002230 盈亏: -5477.999999999972 
    [2020-11-27 15:00:00] INFO: 002230 RSI6:[28.44005514 25.0207767  26.70993727]
    [2020-11-27 15:00:00] INFO: RSI上穿,抄底买入 38.3元 22100股
    [2020-11-30 15:00:00] INFO: 跌破20日线,止损 38.22元 
    [2020-11-30 15:00:00] INFO: 002230 盈亏: -1767.9999999999623 
    [2020-12-01 15:00:00] INFO: 002230 RSI6:[44.13552902 27.72604598 28.44005514]
    [2020-12-01 15:00:00] INFO: RSI上穿,抄底买入 39.0元 21500股
    [2020-12-02 15:00:00] INFO: 跌破20日线,止损 38.92元 
    [2020-12-02 15:00:00] INFO: 002230 盈亏: -1719.9999999999634 
    [2020-12-10 15:00:00] INFO: 002230 RSI6:[35.60943671 27.75226461 39.14467353]
    [2020-12-10 15:00:00] INFO: RSI上穿,抄底买入 38.0元 21900股
    [2020-12-28 15:00:00] INFO: 跌破20日线,止损 36.83元 
    [2020-12-28 15:00:00] INFO: 002230 盈亏: -25623.000000000036 
    [2020-12-29 15:00:00] INFO: 002230 RSI6:[32.07174913 27.74978633 39.49470813]
    [2020-12-29 15:00:00] INFO: RSI上穿,抄底买入 37.06元 21600股
    [2020-12-30 15:00:00] INFO: 没钱买入
    [2021-01-14 15:00:00] INFO: 002230 RSI6:[65.37887186 87.65459084 81.3661945 ]
    [2021-01-14 15:00:00] INFO: RSI拐点,卖出 44.65元 21600股
    [2021-01-14 15:00:00] INFO: 002230 盈亏: 163943.9999999999 
    [2021-02-04 15:00:00] INFO: 002230 RSI6:[40.45563373 34.85232425 61.618979  ]
    [2021-02-04 15:00:00] INFO: RSI上穿,抄底买入 45.3元 21100股
    [2021-02-05 15:00:00] INFO: 跌破20日线,止损 43.68元 
    [2021-02-05 15:00:00] INFO: 002230 盈亏: -34181.99999999995 
    [2021-02-08 15:00:00] INFO: 002230 RSI6:[38.25296049 34.05735873 40.45563373]
    [2021-02-08 15:00:00] INFO: RSI上穿,抄底买入 44.26元 20700股
    [2021-03-09 15:00:00] INFO: 没钱买入
    [2021-03-17 15:00:00] INFO: 没钱买入
    [2021-03-22 15:00:00] INFO: 没钱买入
    [2021-04-13 15:00:00] INFO: 没钱买入
    [2021-04-14 15:00:00] INFO: 没钱买入
    [2021-05-10 15:00:00] INFO: 没钱买入
    [2021-05-26 15:00:00] INFO: 002230 RSI6:[76.69600435 84.43471078 72.43816252]
    [2021-05-26 15:00:00] INFO: RSI拐点,卖出 61.26元 20700股
    [2021-05-26 15:00:00] INFO: 002230 盈亏: 351900.0 
    [2021-07-15 15:00:00] INFO: 002230 RSI6:[39.48466829 33.9016054  39.71800096]
    [2021-07-15 15:00:00] INFO: RSI上穿,抄底买入 61.23元 20600股
    [2021-07-16 15:00:00] INFO: 跌破20日线,止损 60.8元 
    [2021-07-16 15:00:00] INFO: 002230 盈亏: -8857.999999999995 
    [2021-07-21 15:00:00] INFO: 002230 RSI6:[48.9588887  32.03685393 44.09694546]
    [2021-07-21 15:00:00] INFO: RSI上穿,抄底买入 61.53元 20200股
    [2021-07-27 15:00:00] INFO: 跌破20日线,止损 58.02元 
    [2021-07-27 15:00:00] INFO: 002230 盈亏: -70901.99999999996 
    [2021-07-29 15:00:00] INFO: 002230 RSI6:[42.58407346 32.68979414 32.93389648]
    [2021-07-29 15:00:00] INFO: RSI上穿,抄底买入 59.49元 19500股
    [2021-07-30 15:00:00] INFO: 跌破20日线,止损 58.6元 
    [2021-07-30 15:00:00] INFO: 002230 盈亏: -17355.00000000001 
    [2021-08-10 15:00:00] INFO: 002230 RSI6:[37.95398922 30.4741093  32.0076735 ]
    [2021-08-10 15:00:00] INFO: RSI上穿,抄底买入 57.34元 19800股
    [2021-08-11 15:00:00] INFO: 跌破20日线,止损 56.46元 
    [2021-08-11 15:00:00] INFO: 002230 盈亏: -17424.00000000005 
    [2021-08-18 15:00:00] INFO: 002230 RSI6:[14.58881343 13.39682666 17.62430499]
    [2021-08-18 15:00:00] INFO: RSI上穿,抄底买入 51.6元 21600股
    [2021-08-19 15:00:00] INFO: 没钱买入
    [2021-08-23 15:00:00] INFO: 没钱买入
    [2021-08-31 15:00:00] INFO: 没钱买入
    [2021-09-02 15:00:00] INFO: 跌破20日线,止损 50.05元 
    [2021-09-02 15:00:00] INFO: 002230 盈亏: -33480.000000000095 
    [2021-09-06 15:00:00] INFO: 002230 RSI6:[50.10713849 21.29516507 23.76252327]
    [2021-09-06 15:00:00] INFO: RSI上穿,抄底买入 51.94元 20600股
    [2021-09-07 15:00:00] INFO: 跌破20日线,止损 51.93元 
    [2021-09-07 15:00:00] INFO: 002230 盈亏: -205.99999999995902 
    [2021-09-17 15:00:00] INFO: 002230 RSI6:[44.42750488 29.78456305 37.51313217]
    [2021-09-17 15:00:00] INFO: RSI上穿,抄底买入 50.95元 20900股
    [2021-10-22 15:00:00] INFO: 没钱买入
    mock end
    [Balance(asset_balance=1231152.1139999998, current_balance=1187.1139999998268, enable_balance=1187.1139999998268, frozen_balance=0, market_value=1229965.0, money_type='人民币', pre_interest=0.25)]
    2020-02-04 00:00:00 B 35.15 28300
    2020-03-23 00:00:00 S 34.74 28300
    2020-03-24 00:00:00 B 35.48 27500
    2020-03-30 00:00:00 S 34.53 27500
    2020-04-01 00:00:00 B 34.5 27400
    2020-04-16 00:00:00 S 34.42 27400
    2020-04-30 00:00:00 B 33.28 28100
    2020-05-18 00:00:00 S 33.22 28100
    2020-05-19 00:00:00 B 34.63 26800
    2020-05-21 00:00:00 S 32.87 26800
    2020-05-26 00:00:00 B 32.2 27100
    2020-05-27 00:00:00 S 31.7 27100
    2020-05-28 00:00:00 B 31.76 26900
    2020-06-11 00:00:00 S 34.86 26900
    2020-07-17 00:00:00 B 37.24 25000
    2020-07-23 00:00:00 S 37.23 25000
    2020-07-28 00:00:00 B 36.0 25700
    2020-09-09 00:00:00 S 34.87 25700
    2020-09-11 00:00:00 B 34.7 25600
    2020-09-24 00:00:00 S 34.45 25600
    2020-09-25 00:00:00 B 34.52 25400
    2020-09-28 00:00:00 S 34.05 25400
    2020-09-29 00:00:00 B 34.49 24900
    2020-09-30 00:00:00 S 34.27 24900
    2020-11-27 00:00:00 B 38.3 22100
    2020-11-30 00:00:00 S 38.22 22100
    2020-12-01 00:00:00 B 39.0 21500
    2020-12-02 00:00:00 S 38.92 21500
    2020-12-10 00:00:00 B 38.0 21900
    2020-12-28 00:00:00 S 36.83 21900
    2020-12-29 00:00:00 B 37.06 21600
    2021-01-14 00:00:00 S 44.65 21600
    2021-02-04 00:00:00 B 45.3 21100
    2021-02-05 00:00:00 S 43.68 21100
    2021-02-08 00:00:00 B 44.26 20700
    2021-05-26 00:00:00 S 61.26 20700
    2021-07-15 00:00:00 B 61.23 20600
    2021-07-16 00:00:00 S 60.8 20600
    2021-07-21 00:00:00 B 61.53 20200
    2021-07-27 00:00:00 S 58.02 20200
    2021-07-29 00:00:00 B 59.49 19500
    2021-07-30 00:00:00 S 58.6 19500
    2021-08-10 00:00:00 B 57.34 19800
    2021-08-11 00:00:00 S 56.46 19800
    2021-08-18 00:00:00 B 51.6 21600
    2021-09-02 00:00:00 S 50.05 21600
    2021-09-06 00:00:00 B 51.94 20600
    2021-09-07 00:00:00 S 51.93 20600
    2021-09-17 00:00:00 B 50.95 20900
    
  • 相关阅读:
    Vscode 隐藏 工作区中的目录
    java 中 静态泛型方法书写
    Vscode 配置 maven debug
    vscode 配置 java utf-8 编码
    node.js 设置 淘宝 镜像
    vscode 注册表
    ESET Smart Security 6 – 免费60天(SG)
    WIN-8“内置管理员无法激活此应用”问题
    怎样更新PE内的工具
    使用Setup安装Windows8 RTM方法
  • 原文地址:https://www.cnblogs.com/xiaoqi/p/pytrader.html
Copyright © 2020-2023  润新知