Note

【量化回测】日内追涨杀跌是否稳妥?

· Views 2,353

在别人贪婪的时候恐惧,在别人恐惧的时候贪婪。——巴菲特

 

曾经的著名交易员朋友告诉我,交易如做人。

在一根根K线中保持理性,

在大幅浮盈中谨慎从容,

在屡屡亏损中果断离开,

如同人间百态,让人迷失,也让人着迷。

 

是日,在文档中看到一则有趣的日内策略,叫:

“简单趋势策略”

规则:

如果连续三根线收阳,则进多,持有至出现阴线离场;

如果连续三根线收阴,则进空,持有至出现阳线离场。

K线基于10秒,想必是一个正宗的日内策略了。

30秒内朝着一个方向行进则顺势追涨杀跌,像极了散户。

我也时不时干一下这种事情,可惜亏多盈少。

 

接着我们就来看看这个策略跑起来怎么说:

【量化回测】日内追涨杀跌是否稳妥?

4月1日到4月10日,短短十天,本金都快亏光了。

原来计划回测一个月的,结果才跑了几天资金不够了。

【量化回测】日内追涨杀跌是否稳妥?

好的,经过对这个策略的摸索,可以帮大家摸索出一个大家都懂的结论:

不可盲目追涨杀跌!

 

最后,放上熟悉的代码,有兴趣调试或者创新创新的可以参考:

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

from datetime import date

from tqsdk import TqApi, TqBacktest,TargetPosTask,TqSim


api = TqApi(TqSim(init_balance=100000),web_gui=True,backtest=TqBacktest(start_dt=date(2020, 4, 1), end_dt=date(2020, 4, 10)))
# 设定连续多少根阳线/阴线
length = 3
# 获得 bu2006 10秒K线的引用, 长度为 length+1
klines = api.get_kline_serial("SHFE.bu2006", 10, data_length=length + 1)
# 创建 bu2006 的目标持仓 task,该 task 负责调整 bu2006 的仓位到指定的目标仓位, offset_priority的用法详见文档
target_pos = TargetPosTask(api, "SHFE.bu2006", offset_priority="今昨开")

while True:
    api.wait_update()
    # 只有在新创建出K线时才判断开平仓条件
    if api.is_changing(klines.iloc[-1], "datetime"):
        # 跳过最后一根刚生成的K线
        df = klines.iloc[:-1]
        # 比较收盘价和开盘价,判断是阳线还是阴线
        # df.close 为收盘价序列, df.open 为开盘价序列, ">"(pandas.Series.gt) 返回收盘价是否大于开盘价的一个新序列
        up = df.close > df.open
        down = df.close < df.open
        if all(up):
            print("连续阳线: 目标持仓 多头10手")
            # 设置目标持仓为正数表示多头,负数表示空头,0表示空仓
            target_pos.set_target_volume(10)
        elif all(down):
            print("连续阴线: 目标持仓 空头10手")
            target_pos.set_target_volume(-10)
        else:
            print("目标持仓: 空仓")
            target_pos.set_target_volume(0)

 

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

你用10秒K肯定亏死你啊,你知道是亏什么么?就是手续费 反过来做一样亏死
反过来是一个机会,急涨卖空,急跌买多

-THE END-