Hikyuu 交互式工具示例

1、引入交互式工具

需从hikyuu.interactive引入,而不是直接从hikyuu库中引入(hikyuu是一个库,可用于编制其他的工具,而hikyuu.interactive.interactive是基于hikyuu库实现的交互式探索工具)

In [1]:
%matplotlib inline
%time from hikyuu.interactive import *
#use_draw_engine('echarts') #use_draw_engine('matplotlib')  #默认为'matplotlib'绘图
std::cout are redirected to python::stdout
std::cerr are redirected to python::stderr
2021-02-10 22:51:37.238 [HKU-I] - Using SQLITE3 BaseInfoDriver (BaseInfoDriver.cpp:58)
2021-02-10 22:51:37.242 [HKU-I] - Loading market information... (StockManager.cpp:473)
2021-02-10 22:51:37.244 [HKU-I] - Loading stock type information... (StockManager.cpp:486)
2021-02-10 22:51:37.245 [HKU-I] - Loading stock information... (StockManager.cpp:422)
2021-02-10 22:51:37.339 [HKU-I] - Loading stock weight... (StockManager.cpp:503)
2021-02-10 22:51:38.674 [HKU-I] - Loading KData... (StockManager.cpp:138)
2021-02-10 22:51:38.688 [HKU-I] - Preloading all day kdata to buffer! (StockManager.cpp:161)
2021-02-10 22:51:38.713 [HKU-I] - 0.04s Loaded Data. (StockManager.cpp:149)
Wall time: 3.27 s

2、创建交易系统并运行

In [3]:
#创建模拟交易账户进行回测,初始资金30万
my_tm = crtTM(init_cash = 300000)

#创建信号指示器(以5日EMA为快线,5日EMA自身的10日EMA最为慢线,快线向上穿越慢线时买入,反之卖出)
my_sg = SG_Flex(EMA(n=5), slow_n=10)

#固定每次买入1000股
my_mm = MM_FixedCount(1000)

#创建交易系统并运行
sys = SYS_Simple(tm = my_tm, sg = my_sg, mm = my_mm)
sys.run(sm['sz000001'], Query(-150))

3、绘制曲线观察

In [4]:
#绘制系统信号
sys.plot()

k = sm['sz000001'].get_kdata(Query(-150))
c = CLOSE(k)
fast = EMA(c, 5)
slow = EMA(fast, 10)

#绘制信号指示器使用两个指标
fast.plot(new=False)
slow.plot(new=False)

4、绘制资金收益曲线

In [5]:
#绘制资金收益曲线
x = my_tm.get_profit_curve(k.get_datetime_list(), Query.DAY)
x = PRICELIST(x)
x.plot()

5、回测统计报告

In [6]:
#回测统计
from datetime import datetime

per = Performance()
print(per.report(my_tm, Datetime(datetime.today())))
帐户初始金额: 300000.00
累计投入本金: 300000.00
累计投入资产: 0.00
累计借入现金: 0.00
累计借入资产: 0.00
累计红利: 0.00
现金余额: 282160.00
未平仓头寸净值: 24930.00
当前总资产: 307090.00
已平仓交易总成本: 0.00
已平仓净利润总额: 1680.00
单笔交易最大占用现金比例%: 6.47
交易平均占用现金比例%: 5.77
已平仓帐户收益率%: 0.56
帐户年复合收益率%: 4.77
帐户平均年收益率%: 4.71
赢利交易赢利总额: 2600.00
亏损交易亏损总额: -920.00
已平仓交易总数: 4.00
赢利交易数: 3.00
亏损交易数: 1.00
赢利交易比例%: 75.00
赢利期望值: 420.00
赢利交易平均赢利: 866.67
亏损交易平均亏损: -920.00
平均赢利/平均亏损比例: 0.94
净赢利/亏损比例: 2.83
最大单笔赢利: 1220.00
最大单笔亏损: -920.00
赢利交易平均持仓时间: 34.00
赢利交易最大持仓时间: 48.00
亏损交易平均持仓时间: 7.00
亏损交易最大持仓时间: 7.00
空仓总时间: 75.00
空仓时间/总时间%: 40.00
平均空仓时间: 18.00
最长空仓时间: 35.00
最大连续赢利笔数: 3.00
最大连续亏损笔数: 1.00
最大连续赢利金额: 2600.00
最大连续亏损金额: -920.00
R乘数期望值: 0.03
交易机会频率/年: 7.98
年度期望R乘数: 0.24
赢利交易平均R乘数: 0.06
亏损交易平均R乘数: -0.05
最大单笔赢利R乘数: 0.09
最大单笔亏损R乘数: -0.05
最大连续赢利R乘数: 0.06
最大连续亏损R乘数: -0.05

6、关于性能

经常有人问到性能问题,下面这段的代码使用之前的系统示例,遍历指定板块的所有股票,计算他们的“盈利交易比例%”(即胜率)。

In [7]:
def test_func(stock, query):
    """计算指定stock的系统策略胜率,系统策略为之前的简单双均线交叉系统(每次固定买入100股)
    """
    #创建模拟交易账户进行回测,初始资金30万
    my_tm = crtTM(init_cash = 1000000)

    #创建信号指示器(以5日EMA为快线,5日EMA自身的10日EMA最为慢线,快线向上穿越慢线时买入,反之卖出)
    my_sg = SG_Flex(EMA(n=5), slow_n=10)

    #固定每次买入1000股
    my_mm = MM_FixedCount(100)

    #创建交易系统并运行
    sys = SYS_Simple(tm = my_tm, sg = my_sg, mm = my_mm)
    sys.run(stock, query)
    
    per = Performance()
    per.statistics(my_tm, Datetime(datetime.today()))
    return per["赢利交易比例%".encode('gb2312')]

def total_func(blk, query):
    """遍历指定板块的所有的股票,计算系统胜率"""
    result = {}
    for s in blk:
        if s.valid and s.type != constant.STOCKTYPE_INDEX:
            result[s.name] = test_func(s, query)
    return result

遍历所有当前有效且并非指数的证券。下面是我的机器执行结果,共计算4151支证券,最近500个交易日,共耗时2.89秒。机器配置:Intel i7-4700HQ 2.G。

In [8]:
%time a = total_func(sm, Query(-500))
len(a)
2021-02-10 22:52:22.410 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:22.490 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:22.546 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:23.213 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:23.547 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:23.549 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:23.580 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:23.732 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:23.761 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:24.365 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:24.548 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:24.707 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:24.818 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:24.880 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.013 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.060 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.067 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.068 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.068 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.069 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.070 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.070 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.071 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.079 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.087 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.088 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.089 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.090 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.091 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.092 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.092 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.126 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.127 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.128 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.131 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.132 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.134 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.143 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.145 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.146 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.147 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.149 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.150 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.151 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.151 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.152 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.153 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.153 [HKU-I] - KData is empty! (System.cpp:275)
2021-02-10 22:52:25.154 [HKU-I] - KData is empty! (System.cpp:275)
Wall time: 3.17 s
Out[8]:
4677
In [ ]: