Note

【策略回测】布林带之轨道逆势策略

· Views 2,206

书接上一回,我们简单介绍了布林带的一些基础知识。

今天话不多说,直接开策略跑回测!

逆势策略

规则简单说明一下:

品种沥青、回测一个月、基于分钟线做日内、参数20布林带。

低于下轨做多,高于上轨做空,回归均线平仓:

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'chengzhi'

from datetime import date

import numpy as np
from tqsdk import TargetPosTask, TqApi, TqBacktest, TqSim
from tqsdk.ta import MA
from tqsdk.tafunc import std

api = TqApi(TqSim(init_balance=100000),web_gui=True,backtest=TqBacktest(start_dt=date(2020,4, 1), end_dt=date(2020, 4, 30)))

# 获得 bu2006 60秒K线的引用
klines = api.get_kline_serial("SHFE.bu2006", 60)
# 创建 bu2006 的目标持仓 task,该 task 负责调整 bu2006 的仓位到指定的目标仓位
target_pos = TargetPosTask(api, "SHFE.bu2006")


while True:
    api.wait_update()
    #均线
    ma = MA(klines, 20)  # 使用 tqsdk 自带指标函数计算均线
    klines["ma_MAIN"] =  ma.ma  # 在主图中画一根默认颜色(红色)的 ma 指标线
    #上轨
    up_line=ma.ma+2*std(klines.close,20)
    klines["up_line"]=up_line
    klines["up_line.color"]="rgb(47,94,224)"
    #下轨
    down_line=ma.ma-2*std(klines.close,20)
    klines["down_line"]=down_line
    klines["down_line.color"]="rgb(255,0,255)"
    if klines.close.iloc[-1]>up_line.tail(1).values :
        target_pos.set_target_volume(-5)
    elif klines.close.iloc[-1]<down_line.tail(1).values:
        target_pos.set_target_volume(5)
    
    position = api.get_position("SHFE.bu2006")
    
    if position.volume_long>0 and klines.close.iloc[-1]>ma.ma.tail(1).values :
        target_pos.set_target_volume(0)
        print("到均线,平多仓")
    if position.volume_short>0 and klines.close.iloc[-1]<ma.ma.tail(1).values :
        target_pos.set_target_volume(0)
        print("到均线,平空仓")
   

收益如何?

【策略回测】布林带之轨道逆势策略

总体来看表现不佳,亏掉了三分之一,曲线上来看也不是非常理想:

【策略回测】布林带之轨道逆势策略
就目前的参数和测试环境下,似乎不可行。

顺势策略

逆势不可行的话那顺势抓突破可以么?

规则切换为:

低于下轨做空,高于上轨做多,回归均线平仓:

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'chengzhi'

from datetime import date

import numpy as np
from tqsdk import TargetPosTask, TqApi, TqBacktest, TqSim
from tqsdk.ta import MA
from tqsdk.tafunc import std

api = TqApi(TqSim(init_balance=100000),web_gui=":9875",backtest=TqBacktest(start_dt=date(2020,4, 1), end_dt=date(2020, 4, 30)))

# 获得 bu2006 60秒K线的引用
klines = api.get_kline_serial("SHFE.bu2006", 60)
# 创建 bu2006 的目标持仓 task,该 task 负责调整 bu2006 的仓位到指定的目标仓位
target_pos = TargetPosTask(api, "SHFE.bu2006")


while True:
    api.wait_update()
    #均线
    ma = MA(klines, 20)  # 使用 tqsdk 自带指标函数计算均线
    klines["ma_MAIN"] =  ma.ma  # 在主图中画一根默认颜色(红色)的 ma 指标线
    #上轨
    up_line=ma.ma+2*std(klines.close,20)
    klines["up_line"]=up_line
    klines["up_line.color"]="rgb(47,94,224)"
    #下轨
    down_line=ma.ma-2*std(klines.close,20)
    klines["down_line"]=down_line
    klines["down_line.color"]="rgb(255,0,255)"
    if klines.close.iloc[-1]>up_line.tail(1).values :
        target_pos.set_target_volume(5)
    elif klines.close.iloc[-1]0 and klines.close.iloc[-1]0 and klines.close.iloc[-1]>ma.ma.tail(1).values :
        target_pos.set_target_volume(0)
        print("到均线,平空仓")
   
【策略回测】布林带之轨道逆势策略
表现依旧不佳
【策略回测】布林带之轨道逆势策略

这里就有些观众老爷们看出问题了,

突破轨道后不管做多做空,基于穿均线平仓,好像都是亏损的?

没有错,基于手续费的影响

有时候无论做什么方向,都难以盈利。

失效的策略,并不是逆着方向做,就能盈利。

过高的交易频率往往意味着亏损。

 

好的,今天的分享就到这里,

策略仅用于抛砖引玉,

参数调一调可能还是能盈利的。

Disclaimer: The content above represents only the views of the author or guest. It does not represent any views or positions of FOLLOWME and does not mean that FOLLOWME agrees with its statement or description, nor does it constitute any investment advice. For all actions taken by visitors based on information provided by the FOLLOWME community, the community does not assume any form of liability unless otherwise expressly promised in writing.

FOLLOWME Trading Community Website: https://www.followme.com

If you like, reward to support.
avatar

Hot

No comment on record. Start new comment.