首页
数据字典 (opens new window)
  • 内置Python
  • 原生Python
  • VBA
投研服务平台 (opens new window)
迅投官网 (opens new window)

    暂无数据

    策略服务 (opens new window) 迅投知识库 迅投知识库
    首页
    数据字典 (opens new window)
    • 内置Python
    • 原生Python
    • VBA
    投研服务平台 (opens new window)
    迅投官网 (opens new window)
      • 内置Python

        • 快速上手
        • 开始使用
        • 新建一个Python策略
        • Python API 手册

        • 财务数据接口
        • 附录

        • 常见问题Q&A
        • 行情示例
        • 交易示例
        • 回测示例
          • 1.双均线回测示例Demo
        • 策略迁移

      ×
      当前文档查询 “ ” 关键字 0 个
      0/0
      • 内置Python
      • 内置Python
      RZRK
      2022-09-08
      目录

      回测示例

      # 回测示例

      # 1.双均线回测示例Demo

      本示例用于演示双均线模式进行回测的样例。

      #coding:gbk
      
      
      #导入常用库
      import pandas as pd
      import numpy as np
      import talib
      #示例说明:本策略,通过计算快慢双均线,在金叉时买入,死叉时做卖出
      
      def init(C):
      	#init handlebar函数的入参是ContextInfo对象 可以缩写为C
      	#设置测试标的为主图品种
      	C.stock= C.stockcode + '.' +C.market
      	#line1和line2分别为两条均线期数
      	C.line1=10   #快线参数
      	C.line2=20   #慢线参数
      	#accountid为测试的ID 回测模式资金账号可以填任意字符串
      	C.accountid = "testS"  
      
      def handlebar(C):
      	#当前k线日期
      	bar_date = timetag_to_datetime(C.get_bar_timetag(C.barpos), '%Y%m%d%H%M%S')
      	#回测不需要订阅最新行情 使用本地数据速度更快. 如果回测多个品种 需要手动下载对应周期历史数据 主图品种显示在界面时已经订阅了行情 直接取就可以
      	local_data = C.get_local_data(C.stock, end_time = bar_date, period = C.period, divid_type = 'front_ratio', count = max(C.line1, C.line2))
      	sorted_keys = sorted(local_data.keys())
      	close_list = [local_data[i]['close'] for i in sorted_keys]
      	#将获取的历史数据转换为DataFrame格式方便计算
      	#如果目前未持仓,同时快线穿过慢线,则买入8成仓位
      	if len(close_list) <1:
      		print(bar_date, '行情不足 跳过')
      	line1_mean = round(np.mean(close_list[-C.line1:]),2)
      	line2_mean = round(np.mean(close_list[-C.line2:]),2)
      	print(f"{bar_date} 短均线{line1_mean} 长均线{line2_mean}")
      	account = get_trade_detail_data('test', 'stock', 'account')
      	account = account[0]
      	available_cash = int(account.m_dAvailable)
      	holdings = get_trade_detail_data('test', 'stock', 'position')
      	holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nVolume for i in holdings}
      	holding_vol = holdings[C.stock] if C.stock in holdings else 0
      	if holding_vol == 0 and line1_mean > line2_mean:
      		vol = int(available_cash / close_list[-1] / 100) * 100
      		#下单开仓
      		passorder(23, 1101,C.accountid, C.stock, 5, -1, vol, C)
      		print(f"{bar_date} 开仓")
      		C.draw_text(1, 1, '开')
      	#如果目前持仓中,同时快线下穿慢线,则全部平仓
      	elif holding_vol > 0 and line1_mean < line2_mean:
      		#状态变更为未持仓
      		C.holding=False
      		#下单平仓
      		passorder(24, 1101, C.accountid, C.stock, 5, -1, holding_vol, C)
      		print(f"{bar_date} 平仓")
      		C.draw_text(1, 1, '平')
      
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      上次更新: 2023/10/12, 10:45:51
      上一章-交易示例
      下一章-聚宽策略迁移至QMT

      ← 交易示例 聚宽策略迁移至QMT→

      Copyright © 2022-2024 北京睿智融科控股股份有限公司 | 迅投官网
      请使用微信扫码联系客服
      请使用微信扫码联系客服
      点击这里给我发消息
      • 跟随系统
      • 浅色模式
      • 深色模式
      • 阅读模式