qsq加密货币量化系统:实现简单择时策略的回测

引言

在《数据获取和分析》一文中,我演示了qsq是如何获取数据和进行简单分析的,在文末我也提到,下一个目标便是实现一些简单策略的回测。只有完成了基础的回测案例,才能进一步挑战和实现更加复杂的策略。所以,这篇研究中,我会演示我是如何在qsq中设计和实现一个择时策略的回测的。

项目地址

https://github.com/qiushui777/qsq

本文位于qs_papers目录下,建议使用jupter notebook打开学习。

abu和小白量化回测框架代码分析

正所谓工欲善其事,必先利其器。为了实现自己的回测框架,我阅读了abu和小白量化这两个项目的回测源码,两者的项目链接我列在了文末的参考文献中,向两个项目致敬。

abu

这里分析下abu中ABuPickTimeExecute的相关源码。可以看下其调用流程。
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
#...
buy_factors = [{'xd': 60, 'class': AbuFactorBuyBreak},
{'xd': 42, 'class': AbuFactorBuyBreak}]
#...
orders_pd, action_pd, _ = ABuPickTimeExecute.do_symbols_with_same_factors(['usTSLA'],
benchmark,
buy_factors,
None,
capital, show=True) ```

使用者自己设置一个买入和卖出因子,然后通过do_symbols_with_same_factors一天天递进,看是否触发因子,完成交易。通过跟踪do_symbols_with_same_factors会发现这里一直往下调用到ABuPickTimeWorker.py中AbuPickTimeWorker类。可以看到这个类中定义了:

```python
def _week_task(self, today):
"""
周任务:使用self.week_buy_factors,self.week_sell_factors进行迭代
不需再使用hasattr进行是否支持判断fit_week
"""

def _month_task(self, today):
"""
月任务:使用self.month_buy_factors,self.month_sell_factors进行迭代
不需再使用hasattr进行是否支持判断fit_month
"""

def _day_task(self, today):
"""
日任务:迭代买入卖出因子序列进行择时
:param today: 今日的交易数据
:return:
"""

也就是根据月、周、日来进行一个任务驱动的交易。如果仔细看下日任务这里。

1
2
3
4
5
6
7
8
9
10
11
for sell_factor in self.sell_factors:
# 迭代卖出因子,每个卖出因子针对今日交易数据,已经所以交易单进行择时
sell_factor.read_fit_day(today, self.orders)

for buy_factor in self.buy_factors:
# 如果择时买入因子没有被封锁执行任务
if not buy_factor.lock_factor:
# 迭代买入因子,每个因子都对今天进行择时,如果生成order加入self.orders
order = buy_factor.read_fit_day(today)
if order and order.order_deal:
self.orders.append(order)

实际上,这里通过的是factor类的read_fit_day方法来生成订单。

实际的abu系统实现较为复杂,有很多类之间的相互继承,这里只是简单分析了下。

小白量化

回测框架部分主要代码位于HP_sys.py中。这里关键的两个函数是Order和Trade_testing函数。

Order函数中有两个if判断,分别对应于买入和卖出的操作。

1
2
3
4
if mode==1:   #买入
# ...
if mode==2 and ln>0: #卖出
# ...

因而在调用order函数的时候需要设定mode参数,决定是卖出还是买入操作。
当买入操作时,先计算所花金额se,然后在资金中减去这部分消费。

1
2
se=amount*price*(1+self.trading_Commission)
self.money=self.money-se

接下来会把这笔交易加入交易历史中。

1
2
df_new = pd.DataFrame({'date':date,'time':time,'mode':mode,'code':code,'amount':amount,'price':price,'money':self.money},index=[ln])
self.trade_df=self.trade_df.append( df_new,ignore_index=True)

同时,判断自己是否已经持仓这只股票,对持仓,也就是security_df进行一个操作。

Trade_testing这个函数是对策略的回测,函数参数tp1为买点,参数tp2为卖点。在最初给出的链接中,作者使用了CROSS函数来生成买卖点。

函数首先判断是否是买点。

1
if (df[tp1].at[i] >0 and self.position==False and self.trade==True) :  #买点

如果是,则开始买入。这里买入的量是将自己的资金全部花掉。

1
2
x=int(myMoney/(self.priceBuy*(1+self.trading_Commission))/100)
self.amount=x*100.00

这之后,会设置一个止损价格,也就是跌到这个价格就得卖掉了。

1
self.priceStopLoss=self.priceBuy*(1-self.stop_loss_range)

相对于的,这里还会有设置对应于卖点和止损点的判断和操作,大体过程类似。

qsq回测框架设计

通过上面的代码分析,我发现这两个项目和我想要完成的系统有如下区别:
  1. abu系统过于复杂,而且很多代码对于我来说并不需要
  2. 小白量化面向初级用户,过于简单
所以我想要设计一个功能完善,便于我后期扩展的框架。为此,我给出了如下的架构。

QsAccount 类

设定帐户资金等初始参数,完成买卖操作。

QsSignal 类

这个类针对择时交易,可以被用户继承和修改,定义给出交易信号的函数,produce_signal。回测可以利用这个类来判断是否买入和卖出。

QsCrypto 类

这个类包含bitcoin,ethereum等,含有数据DataFrame格式。
提供一个接口,可以对dataframe进行操作,例如,添加n天内最高价最低价的一列,同理可以扩展更多类型的接口。

TacticsQs目录

提供回测的接口,目前实现signal_day_back_test函数,针对日线进行回测。

择时策略回测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 基础库导入

from __future__ import print_function
from __future__ import division

import warnings
warnings.filterwarnings('ignore')
warnings.simplefilter('ignore')

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline

import os
import sys
# 使用insert 0即只使用github,避免交叉使用了pip安装的abupy,导致的版本不一致问题
sys.path.insert(0, os.path.abspath('../'))
1
from qsq import QsData, QsCrypto, QsAccount, QsSignal, QsPickSignal, QsDrawUtil

获取数据

利用上一篇文章中实现的QsData类来获取我们想要的数据。这里以bitcoin为例。
1
2
3
4
5
data = QsData()
bitcoin = QsCrypto('bitcoin', data.get_coin_df(coin='bitcoin'))
bitcoin.add_period_max(12)
bitcoin.add_period_min(15)
bitcoin.crypto_df

open close high low volume date MarketCap pre_close date_week p_change 12_period_max 15_period_min
2013-04-28 135.30 134.21 135.98 132.10 NaN 20130428 1,488,566,728 NaN 6 NaN NaN NaN
2013-04-29 134.44 144.54 147.49 134.00 NaN 20130429 1,603,768,865 134.21 0 7.697 NaN NaN
2013-04-30 144.00 139.00 146.93 134.05 NaN 20130430 1,542,813,125 144.54 1 -3.833 NaN NaN
2013-05-01 139.00 116.99 139.89 107.72 NaN 20130501 1,298,954,594 139.00 2 -15.835 NaN NaN
2013-05-02 116.38 105.21 125.60 92.28 NaN 20130502 1,168,517,495 116.99 3 -10.069 NaN NaN
2013-05-03 106.25 97.75 108.13 79.10 NaN 20130503 1,085,995,169 105.21 4 -7.091 NaN NaN
2013-05-04 98.10 112.50 115.00 92.50 NaN 20130504 1,250,316,563 97.75 5 15.090 NaN NaN
2013-05-05 112.90 115.91 118.80 107.14 NaN 20130505 1,288,693,176 112.50 6 3.031 NaN NaN
2013-05-06 115.98 112.30 124.66 106.64 NaN 20130506 1,249,023,060 115.91 0 -3.114 NaN NaN
2013-05-07 112.25 111.50 113.44 97.70 NaN 20130507 1,240,593,600 112.30 1 -0.712 NaN NaN
2013-05-08 109.60 113.57 115.78 109.60 NaN 20130508 1,264,049,202 111.50 2 1.857 NaN NaN
2013-05-09 113.20 112.67 113.46 109.26 NaN 20130509 1,254,535,382 113.57 3 -0.792 NaN NaN
2013-05-10 112.80 117.20 122.00 111.55 NaN 20130510 1,305,479,080 112.67 4 4.021 144.54 NaN
2013-05-11 117.70 115.24 118.68 113.01 NaN 20130511 1,284,207,489 117.20 5 -1.672 144.54 NaN
2013-05-12 115.64 115.00 117.45 113.43 NaN 20130512 1,281,982,625 115.24 6 -0.208 139.00 NaN
2013-05-13 114.82 117.98 118.70 114.50 NaN 20130513 1,315,710,011 115.00 0 2.591 117.20 97.75
2013-05-14 117.98 111.50 119.80 110.25 NaN 20130514 1,243,874,488 117.98 1 -5.492 117.98 97.75
2013-05-15 111.40 114.22 115.81 103.50 NaN 20130515 1,274,623,813 111.50 2 2.439 117.98 97.75
2013-05-16 114.22 118.76 118.76 112.20 NaN 20130516 1,325,726,787 114.22 3 3.975 117.98 97.75
2013-05-17 118.21 123.01 125.30 116.57 NaN 20130517 1,373,723,882 118.76 4 3.579 118.76 97.75
2013-05-18 123.50 123.50 125.25 122.30 NaN 20130518 1,379,574,546 123.01 5 0.398 123.01 97.75
2013-05-19 123.21 121.99 124.50 119.57 NaN 20130519 1,363,204,703 123.50 6 -1.223 123.50 111.50
2013-05-20 122.50 122.00 123.62 120.12 NaN 20130520 1,363,709,900 121.99 0 0.008 123.50 111.50
2013-05-21 122.02 122.88 123.00 121.21 NaN 20130521 1,374,013,440 122.00 1 0.721 123.50 111.50
2013-05-22 122.89 123.89 124.00 122.00 NaN 20130522 1,385,778,993 122.88 2 0.822 123.50 111.50
2013-05-23 123.80 126.70 126.93 123.10 NaN 20130523 1,417,769,833 123.89 3 2.268 123.89 111.50
2013-05-24 126.30 133.20 133.85 125.72 NaN 20130524 1,491,070,770 126.70 4 5.130 126.70 111.50
2013-05-25 133.10 131.98 133.22 128.90 NaN 20130525 1,477,958,233 133.20 5 -0.916 133.20 111.50
2013-05-26 131.99 133.48 136.00 130.62 NaN 20130526 1,495,293,015 131.98 6 1.137 133.20 111.50
2013-05-27 133.50 129.74 135.46 124.70 NaN 20130527 1,454,029,510 133.48 0 -2.802 133.48 111.50
... ... ... ... ... ... ... ... ... ... ... ... ...
2019-07-28 9491.63 9552.86 9575.55 9252.30 13,738,687,093 20190728 170,461,958,074 9477.68 6 0.793 10767.14 9477.64
2019-07-29 9548.18 9519.15 9681.65 9472.95 13,791,445,323 20190729 169,880,343,827 9552.86 0 -0.353 10767.14 9477.64
2019-07-30 9522.33 9607.42 9701.76 9437.34 13,829,811,132 20190730 171,472,452,506 9519.15 1 0.927 10767.14 9477.64
2019-07-31 9604.05 10085.63 10085.63 9598.10 16,631,520,648 20190731 180,028,959,603 9607.42 2 4.978 10767.14 9477.64
2019-08-01 10077.44 10399.67 10446.92 9922.02 17,165,337,858 20190801 185,653,203,391 10085.63 3 3.114 10767.14 9477.68
2019-08-02 10402.04 10518.17 10657.95 10371.01 17,489,094,082 20190802 187,791,090,996 10399.67 4 1.139 10599.11 9477.68
2019-08-03 10519.28 10821.73 10946.78 10503.50 15,352,685,061 20190803 193,233,960,601 10518.17 5 2.886 10518.17 9477.68
2019-08-04 10821.63 10970.18 11009.21 10620.28 16,530,894,787 20190804 195,907,875,403 10821.73 6 1.372 10821.73 9477.68
2019-08-05 10960.74 11805.65 11895.09 10960.74 23,875,988,832 20190805 210,848,822,060 10970.18 0 7.616 10970.18 9477.68
2019-08-06 11811.55 11478.17 12273.82 11290.73 23,635,107,660 20190806 205,023,347,814 11805.65 1 -2.774 11805.65 9477.68
2019-08-07 11476.19 11941.97 12036.99 11433.70 22,194,988,641 20190807 213,330,426,789 11478.17 2 4.041 11805.65 9477.68
2019-08-08 11954.04 11966.41 11979.42 11556.17 19,481,591,730 20190808 213,788,089,212 11941.97 3 0.205 11941.97 9477.68
2019-08-09 11953.47 11862.94 11970.46 11709.75 18,339,989,960 20190809 211,961,319,133 11966.41 4 -0.865 11966.41 9477.68
2019-08-10 11861.56 11354.02 11915.66 11323.90 18,125,355,447 20190810 202,890,020,455 11862.94 5 -4.290 11966.41 9477.68
2019-08-11 11349.74 11523.58 11523.58 11248.29 15,774,371,518 20190811 205,941,632,235 11354.02 6 1.493 11966.41 9477.68
2019-08-12 11528.19 11382.62 11528.19 11320.95 13,647,198,229 20190812 203,441,494,985 11523.58 0 -1.223 11966.41 9519.15
2019-08-13 11385.05 10895.83 11420.05 10830.33 16,681,503,537 20190813 194,762,696,644 11382.62 1 -4.277 11966.41 9519.15
2019-08-14 10889.49 10051.70 10889.56 10028.14 19,990,838,300 20190814 179,692,803,424 10895.83 2 -7.747 11966.41 9607.42
2019-08-15 10038.42 10311.55 10437.41 9675.32 22,899,115,082 20190815 184,357,666,577 10051.70 3 2.585 11966.41 10051.70
2019-08-16 10319.42 10374.34 10524.35 9855.48 20,228,207,096 20190816 185,500,055,339 10311.55 4 0.609 11966.41 10051.70
2019-08-17 10358.72 10231.74 10452.62 10086.70 13,778,035,685 20190817 182,966,857,173 10374.34 5 -1.375 11966.41 10051.70
2019-08-18 10233.01 10345.81 10487.07 10119.09 12,999,813,869 20190818 185,022,920,955 10231.74 6 1.115 11966.41 10051.70
2019-08-19 10350.28 10916.05 10916.05 10313.20 16,038,264,603 20190819 195,243,306,008 10345.81 0 5.512 11966.41 10051.70
2019-08-20 10916.35 10763.23 10947.04 10618.96 15,053,082,175 20190820 192,530,283,565 10916.05 1 -1.400 11966.41 10051.70
2019-08-21 10764.57 10138.05 10798.73 9962.72 19,473,084,768 20190821 181,364,502,142 10763.23 2 -5.808 11862.94 10051.70
2019-08-22 10142.52 10131.06 10233.00 9831.46 17,097,508,856 20190822 181,257,125,783 10138.05 3 -0.069 11523.58 10051.70
2019-08-23 10136.31 10407.97 10442.44 10078.19 15,627,023,886 20190823 186,231,409,722 10131.06 4 2.733 11523.58 10051.70
2019-08-24 10407.64 10159.96 10418.02 9982.30 15,451,030,650 20190824 181,813,631,752 10407.97 5 -2.383 11382.62 10051.70
2019-08-25 10160.74 10138.52 10304.62 10008.79 14,153,856,610 20190825 181,450,186,164 10159.96 6 -0.211 10916.05 10051.70
2019-08-26 10126.30 10370.82 10512.33 10126.30 18,438,654,080 20190826 185,625,235,889 10138.52 0 2.291 10916.05 10051.70

2312 rows × 12 columns

这里我们加入了12日最高价和15日最低价,这是因为择时策略中我选择了当价格高于12日最高时买入,低于15日最低时卖出。

定义信号

我们要调用的signal_day_back_test函数需要调用买入和卖出信号。所以这里我们使用QsSignal类来自定义信号。这里需要注意的时其中的mode时买卖方向,0为不操作,1为买入,2为卖出。symbol是买卖的币种。percent是使用多少资金进行交易。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class buysignal(QsSignal):
def produce_signal(self,param):
if self.crypto.crypto_df.loc[param]['12_period_max'] == np.NaN:
return {'mode':0, 'symbol':self.crypto.symbol,'percent':1}
if self.crypto.crypto_df.loc[param]['close'] > self.crypto.crypto_df.loc[param]['12_period_max']:
return {'mode':1, 'symbol':self.crypto.symbol,'percent':1}
else:
return {'mode':0, 'symbol':self.crypto.symbol,'percent':1}

class sellsignal(QsSignal):
def produce_signal(self,param):
if self.crypto.crypto_df.loc[param]['15_period_min'] == np.NaN:
return {'mode':0, 'symbol':self.crypto.symbol,'percent':1}
if self.crypto.crypto_df.loc[param]['close'] < self.crypto.crypto_df.loc[param]['15_period_min']:
return {'mode':2, 'symbol':self.crypto.symbol,'percent':1}
else:
return {'mode':0, 'symbol':self.crypto.symbol,'percent':1}

buy_sig = buysignal(bitcoin)
sell_sig = sellsignal(bitcoin)

开始回测

完成上述操作后,可以进行回测。
1
2
myaccount = QsAccount()
QsPickSignal.signal_day_back_test(myaccount,buy_sig,sell_sig)
2019-09-04 15:12:54,614 - INFO - start back test...
2019-09-04 15:12:54,716 - INFO - 2013-05-13 buy bitcoin 8471.776995042062
2019-09-04 15:12:54,720 - INFO - The asset is 999500.2498750625
2019-09-04 15:12:54,743 - INFO - 2013-05-14 stop loss bitcoin 8471.776995042062
2019-09-04 15:12:54,747 - INFO - The asset is 944130.8333797163
2019-09-04 15:12:54,768 - INFO - 2013-05-16 buy bitcoin 7945.93300671756
......
2019-09-04 15:13:05,253 - INFO - The asset is 74024044.48192468
2019-09-04 15:13:05,370 - INFO - 2019-02-08 buy bitcoin 20177.662951266906
2019-09-04 15:13:05,370 - INFO - The asset is 73987050.95644647
2019-09-04 15:13:05,829 - INFO - 2019-06-09 sell bitcoin 20177.662951266906
2019-09-04 15:13:05,829 - INFO - The asset is 155049923.2388849
2019-09-04 15:13:05,873 - INFO - 2019-06-15 buy bitcoin 17534.031917656263
2019-09-04 15:13:05,874 - INFO - The asset is 154972437.02037472
2019-09-04 15:13:05,985 - INFO - 2019-07-14 sell bitcoin 17534.031917656263
2019-09-04 15:13:05,985 - INFO - The asset is 179740168.34770295
2019-09-04 15:13:06,086 - INFO - 2019-08-03 buy bitcoin 16600.889430443644
2019-09-04 15:13:06,086 - INFO - The asset is 179650343.17611492
2019-09-04 15:13:06,153 - INFO - 2019-08-14 stop loss bitcoin 16600.889430443644
2019-09-04 15:13:06,154 - INFO - The asset is 166783726.70784643
1
myaccount.balance
166783726.70784643

由于初始资金我设置了100万美金,通过上述可以看出,我们的回报率是160倍。

1
QsDrawUtil.plot_dfs({'bitcoin':bitcoin.crypto_df.close,'asset':myaccount.asset.asset})

可以看出,这个策略在17年大涨的时候没有跑过大盘,但是在19年大涨的过程中获得了不错的收益。为此,我决定尝试下单独在过去的一年时间段内进行回测。

1
2
bitcoin.crypto_df = bitcoin.crypto_df[-365:]
bitcoin.crypto_df

open close high low volume date MarketCap pre_close date_week p_change 12_period_max 15_period_min
2018-08-27 6710.80 6884.64 6884.64 6689.71 4,019,000,000 20180827 118,657,885,712 6707.26 0 2.645 6763.19 6199.71
2018-08-28 6891.08 7096.28 7109.56 6882.34 4,659,940,000 20180828 122,319,195,736 6884.64 1 3.074 6884.64 6199.71
2018-08-29 7091.71 7047.16 7113.30 6970.82 4,145,880,000 20180829 121,484,666,374 7096.28 2 -0.692 7096.28 6199.71
2018-08-30 7043.76 6978.23 7072.69 6834.69 4,463,250,000 20180830 120,309,828,156 7047.16 3 -0.978 7096.28 6308.52
2018-08-31 6973.97 7037.58 7057.17 6920.16 4,495,650,000 20180831 121,346,613,238 6978.23 4 0.851 7096.28 6308.53
2018-09-01 7044.81 7193.25 7242.29 7038.05 4,116,050,000 20180901 124,044,625,438 7037.58 5 2.212 7096.28 6308.53
2018-09-02 7189.58 7272.72 7306.31 7132.16 4,329,540,000 20180902 125,427,780,027 7193.25 6 1.105 7193.25 6308.53
2018-09-03 7279.03 7260.06 7317.94 7208.15 4,087,760,000 20180903 125,222,785,390 7272.72 0 -0.174 7272.72 6308.53
2018-09-04 7263.00 7361.66 7388.26 7255.44 4,273,640,000 20180904 126,986,882,925 7260.06 1 1.399 7272.72 6308.53
2018-09-05 7361.46 6792.83 7388.43 6792.83 5,800,460,000 20180905 117,185,657,641 7361.66 2 -7.727 7361.66 6376.71
2018-09-06 6755.14 6529.17 6755.14 6404.72 5,523,470,000 20180906 112,649,565,532 6792.83 3 -3.881 7361.66 6376.71
2018-09-07 6528.92 6467.07 6555.29 6396.87 4,264,680,000 20180907 111,590,424,587 6529.17 4 -0.951 7361.66 6529.17
2018-09-08 6460.17 6225.98 6534.25 6197.52 3,835,060,000 20180908 107,442,122,871 6467.07 5 -3.728 7361.66 6467.07
2018-09-09 6223.38 6300.86 6446.26 6201.22 3,671,890,000 20180909 108,747,090,915 6225.98 6 1.203 7361.66 6225.98
2018-09-10 6301.57 6329.70 6374.98 6292.76 3,714,100,000 20180910 109,255,603,474 6300.86 0 0.458 7361.66 6225.98
2018-09-11 6331.88 6321.20 6398.92 6260.21 3,849,910,000 20180911 109,119,948,884 6329.70 1 -0.134 7361.66 6225.98
2018-09-12 6317.01 6351.80 6363.87 6265.09 4,064,230,000 20180912 109,661,521,297 6321.20 2 0.484 7361.66 6225.98
2018-09-13 6354.24 6517.31 6535.41 6354.24 4,210,910,000 20180913 112,530,970,182 6351.80 3 2.606 7361.66 6225.98
2018-09-14 6515.41 6512.71 6596.10 6456.17 4,076,220,000 20180914 112,462,453,186 6517.31 4 -0.071 7361.66 6225.98
2018-09-15 6509.40 6543.20 6561.72 6493.55 3,216,300,000 20180915 113,000,324,618 6512.71 5 0.468 7361.66 6225.98
2018-09-16 6536.68 6517.18 6544.33 6460.10 3,273,730,000 20180916 112,562,367,224 6543.20 6 -0.398 7361.66 6225.98
2018-09-17 6514.06 6281.20 6540.21 6257.52 3,910,780,000 20180917 108,497,127,334 6517.18 0 -3.621 6792.83 6225.98
2018-09-18 6280.91 6371.30 6384.18 6265.71 4,180,090,000 20180918 110,064,685,348 6281.20 1 1.434 6543.20 6225.98
2018-09-19 6371.85 6398.54 6448.46 6208.34 4,431,340,000 20180919 110,547,735,544 6371.30 2 0.428 6543.20 6225.98
2018-09-20 6398.85 6519.67 6529.26 6395.95 4,348,110,000 20180920 112,653,130,183 6398.54 3 1.893 6543.20 6225.98
2018-09-21 6513.87 6734.95 6794.33 6496.36 6,531,940,000 20180921 116,385,068,032 6519.67 4 3.302 6543.20 6225.98
2018-09-22 6735.05 6721.98 6814.56 6616.80 4,509,660,000 20180922 116,173,876,360 6734.95 5 -0.193 6734.95 6225.98
2018-09-23 6715.32 6710.63 6766.15 6679.42 4,197,500,000 20180923 115,990,387,532 6721.98 6 -0.169 6734.95 6225.98
2018-09-24 6704.77 6595.41 6713.56 6580.90 4,177,310,000 20180924 114,011,060,309 6710.63 0 -1.717 6734.95 6281.20
2018-09-25 6603.64 6446.47 6603.64 6381.86 4,726,180,000 20180925 111,450,035,114 6595.41 1 -2.258 6734.95 6281.20
... ... ... ... ... ... ... ... ... ... ... ... ...
2019-07-28 9491.63 9552.86 9575.55 9252.30 13,738,687,093 20190728 170,461,958,074 9477.68 6 0.793 10767.14 9477.64
2019-07-29 9548.18 9519.15 9681.65 9472.95 13,791,445,323 20190729 169,880,343,827 9552.86 0 -0.353 10767.14 9477.64
2019-07-30 9522.33 9607.42 9701.76 9437.34 13,829,811,132 20190730 171,472,452,506 9519.15 1 0.927 10767.14 9477.64
2019-07-31 9604.05 10085.63 10085.63 9598.10 16,631,520,648 20190731 180,028,959,603 9607.42 2 4.978 10767.14 9477.64
2019-08-01 10077.44 10399.67 10446.92 9922.02 17,165,337,858 20190801 185,653,203,391 10085.63 3 3.114 10767.14 9477.68
2019-08-02 10402.04 10518.17 10657.95 10371.01 17,489,094,082 20190802 187,791,090,996 10399.67 4 1.139 10599.11 9477.68
2019-08-03 10519.28 10821.73 10946.78 10503.50 15,352,685,061 20190803 193,233,960,601 10518.17 5 2.886 10518.17 9477.68
2019-08-04 10821.63 10970.18 11009.21 10620.28 16,530,894,787 20190804 195,907,875,403 10821.73 6 1.372 10821.73 9477.68
2019-08-05 10960.74 11805.65 11895.09 10960.74 23,875,988,832 20190805 210,848,822,060 10970.18 0 7.616 10970.18 9477.68
2019-08-06 11811.55 11478.17 12273.82 11290.73 23,635,107,660 20190806 205,023,347,814 11805.65 1 -2.774 11805.65 9477.68
2019-08-07 11476.19 11941.97 12036.99 11433.70 22,194,988,641 20190807 213,330,426,789 11478.17 2 4.041 11805.65 9477.68
2019-08-08 11954.04 11966.41 11979.42 11556.17 19,481,591,730 20190808 213,788,089,212 11941.97 3 0.205 11941.97 9477.68
2019-08-09 11953.47 11862.94 11970.46 11709.75 18,339,989,960 20190809 211,961,319,133 11966.41 4 -0.865 11966.41 9477.68
2019-08-10 11861.56 11354.02 11915.66 11323.90 18,125,355,447 20190810 202,890,020,455 11862.94 5 -4.290 11966.41 9477.68
2019-08-11 11349.74 11523.58 11523.58 11248.29 15,774,371,518 20190811 205,941,632,235 11354.02 6 1.493 11966.41 9477.68
2019-08-12 11528.19 11382.62 11528.19 11320.95 13,647,198,229 20190812 203,441,494,985 11523.58 0 -1.223 11966.41 9519.15
2019-08-13 11385.05 10895.83 11420.05 10830.33 16,681,503,537 20190813 194,762,696,644 11382.62 1 -4.277 11966.41 9519.15
2019-08-14 10889.49 10051.70 10889.56 10028.14 19,990,838,300 20190814 179,692,803,424 10895.83 2 -7.747 11966.41 9607.42
2019-08-15 10038.42 10311.55 10437.41 9675.32 22,899,115,082 20190815 184,357,666,577 10051.70 3 2.585 11966.41 10051.70
2019-08-16 10319.42 10374.34 10524.35 9855.48 20,228,207,096 20190816 185,500,055,339 10311.55 4 0.609 11966.41 10051.70
2019-08-17 10358.72 10231.74 10452.62 10086.70 13,778,035,685 20190817 182,966,857,173 10374.34 5 -1.375 11966.41 10051.70
2019-08-18 10233.01 10345.81 10487.07 10119.09 12,999,813,869 20190818 185,022,920,955 10231.74 6 1.115 11966.41 10051.70
2019-08-19 10350.28 10916.05 10916.05 10313.20 16,038,264,603 20190819 195,243,306,008 10345.81 0 5.512 11966.41 10051.70
2019-08-20 10916.35 10763.23 10947.04 10618.96 15,053,082,175 20190820 192,530,283,565 10916.05 1 -1.400 11966.41 10051.70
2019-08-21 10764.57 10138.05 10798.73 9962.72 19,473,084,768 20190821 181,364,502,142 10763.23 2 -5.808 11862.94 10051.70
2019-08-22 10142.52 10131.06 10233.00 9831.46 17,097,508,856 20190822 181,257,125,783 10138.05 3 -0.069 11523.58 10051.70
2019-08-23 10136.31 10407.97 10442.44 10078.19 15,627,023,886 20190823 186,231,409,722 10131.06 4 2.733 11523.58 10051.70
2019-08-24 10407.64 10159.96 10418.02 9982.30 15,451,030,650 20190824 181,813,631,752 10407.97 5 -2.383 11382.62 10051.70
2019-08-25 10160.74 10138.52 10304.62 10008.79 14,153,856,610 20190825 181,450,186,164 10159.96 6 -0.211 10916.05 10051.70
2019-08-26 10126.30 10370.82 10512.33 10126.30 18,438,654,080 20190826 185,625,235,889 10138.52 0 2.291 10916.05 10051.70

365 rows × 12 columns

1
2
3
4
buy_sig = buysignal(bitcoin)
sell_sig = sellsignal(bitcoin)
myaccount = QsAccount()
QsPickSignal.signal_day_back_test(myaccount,buy_sig,sell_sig)
2019-09-04 15:13:06,489 - INFO - start back test...
2019-09-04 15:13:06,498 - INFO - 2018-08-27 buy bitcoin 145.17828817121338
2019-09-04 15:13:06,500 - INFO - The asset is 999500.2498750625
2019-09-04 15:13:06,555 - INFO - 2018-09-06 stop loss bitcoin 145.17828817121338
2019-09-04 15:13:06,556 - INFO - The asset is 947419.776916952
2019-09-04 15:13:06,656 - INFO - 2018-09-21 buy bitcoin 140.60183130759242
2019-09-04 15:13:06,658 - INFO - The asset is 946946.3037650695
2019-09-04 15:13:06,756 - INFO - 2018-10-11 sell bitcoin 140.60183130759242
2019-09-04 15:13:06,758 - INFO - The asset is 879198.9816992619
2019-09-04 15:13:06,870 - INFO - 2018-11-07 buy bitcoin 134.56979511898868
2019-09-04 15:13:06,870 - INFO - The asset is 878759.6018983128
2019-09-04 15:13:06,938 - INFO - 2018-11-14 sell bitcoin 134.56979511898868
2019-09-04 15:13:06,940 - INFO - The asset is 771822.4795291382
2019-09-04 15:13:07,068 - INFO - 2018-12-18 buy bitcoin 208.7186791200803
2019-09-04 15:13:07,068 - INFO - The asset is 771436.7611485639
2019-09-04 15:13:07,185 - INFO - 2019-01-12 sell bitcoin 208.7186791200803
2019-09-04 15:13:07,185 - INFO - The asset is 763799.6090124189
2019-09-04 15:13:07,285 - INFO - 2019-02-08 buy bitcoin 208.19844661048324
2019-09-04 15:13:07,285 - INFO - The asset is 763417.9000623877
2019-09-04 15:13:07,769 - INFO - 2019-06-09 sell bitcoin 208.19844661048324
2019-09-04 15:13:07,770 - INFO - The asset is 1599845.9902604157
2019-09-04 15:13:07,816 - INFO - 2019-06-15 buy bitcoin 180.92076455491875
2019-09-04 15:13:07,818 - INFO - The asset is 1599046.4670269024
2019-09-04 15:13:07,940 - INFO - 2019-07-14 sell bitcoin 180.92076455491875
2019-09-04 15:13:07,941 - INFO - The asset is 1854606.4494128593
2019-09-04 15:13:08,020 - INFO - 2019-08-03 buy bitcoin 171.29235432856441
2019-09-04 15:13:08,020 - INFO - The asset is 1853679.6096080553
2019-09-04 15:13:08,087 - INFO - 2019-08-14 stop loss bitcoin 171.29235432856441
2019-09-04 15:13:08,088 - INFO - The asset is 1720918.468325429

收益略好于大盘,这主要是因为19年整个大涨期间持有了btc一直没有抛。由于现在btc横盘已经有3个月了,不如再次缩小时间段看看收益。

1
2
3
4
5
bitcoin.crypto_df = bitcoin.crypto_df[-90:]
buy_sig = buysignal(bitcoin)
sell_sig = sellsignal(bitcoin)
myaccount = QsAccount()
QsPickSignal.signal_day_back_test(myaccount,buy_sig,sell_sig)
2019-09-04 15:13:08,185 - INFO - start back test...
2019-09-04 15:13:08,247 - INFO - 2019-06-15 buy bitcoin 113.08636309765622
2019-09-04 15:13:08,249 - INFO - The asset is 999500.2498750625
2019-09-04 15:13:08,366 - INFO - 2019-07-14 sell bitcoin 113.08636309765622
2019-09-04 15:13:08,366 - INFO - The asset is 1159240.614848792
2019-09-04 15:13:08,469 - INFO - 2019-08-03 buy bitcoin 107.06802740473924
2019-09-04 15:13:08,469 - INFO - The asset is 1158661.2842066889
2019-09-04 15:13:08,568 - INFO - 2019-08-14 stop loss bitcoin 107.06802740473924
2019-09-04 15:13:08,568 - INFO - The asset is 1075677.5832186858
1
QsDrawUtil.plot_dfs({'bitcoin':bitcoin.crypto_df.close,'asset':myaccount.asset.asset})

看到这里我们发现这个策略的这两拨操作输给了大盘,虽然获得了收益,但是输给大盘是我们的耻辱。而且做这种高危的加密货币,在大盘涨的时候,我们应该要远远甩下它才是。因此我们得出结论,应该买一个币然后过5年再看它的价格。好吧,这是玩笑。

结论

在这篇小文章中,我们完成了qsq中的回测框架,进行了一个简单策略的回测。最最重要的是,我们自己实现了框架,对源码比较熟悉,可以很方便地为其添加功能,实现新的策略。总体分析,这篇研究地优缺点如下。
优点:
  1. 实现了一个简单、易扩展的加密货币回测框架,尝试了简单的择时策略
缺点:
  1. 从框架上讲,目前只支持简单择时策略,还需添加更多的策略
  2. 从策略上讲,我们这个策略收益不理想

后续研究

后续研究我个人暂时有两个方向。
  1. 完成更多的策略支持和回测
  2. 深入分析每个策略,改变参数,分析比较

交流方式

email: xudong_shao#hotmail.com
qq群: 742593185

参考资料

1. abupy https://github.com/bbfamily/abu
2. 小白量化 https://blog.csdn.net/hepu8/article/details/93378626