ATR通道涉及一个真实振幅TR(Ture-Range)
先上公式

High是指当日最高价,Low为当日最低价,pre_close是指前一日收盘价。

取三个中的最大值的绝对值 就是某一天的TR 。
(ATR)其实就是真实波幅的一个移动平均值,直接看公式:


唐安奇通道 布林带通道 ATR通道其实都是三条线 只是上下轨的计算公式有所不同。
传统通道突破策略是建立在通道突破思想上:
突破上轨做多;
下破下轨做空。
但是通道突破系统主要的问题在于假突破。假突破中,通道的突破并没有带来趋势的确定,反而是价格动能的衰竭,会迅速回落并反向运动
金肯纳特交易系统针对假突破问题进行了改进。金肯纳特交易系统中在移动平均线附近设置了止损,以限制假突破造成的损失。
具体而言,三价(最高价、最低价和收盘价)均线向上,并且价格上破通道上轨,买入股票,
价格下破三价均线,卖出股票。其中上轨=三价均线+真实振幅的移动平均值
策略:三价均线向上(多头排列),价格上破通道上轨,买入股票・
出场条件:价格下破三价均线,卖出股票
代码回测 第一步 准备工作
import numpy as npimport pandas as pdimport talib as taimport datetimestart = '2015-01-01' # 回测起始时间end = '2015-12-30' # 回测结束时间universe = DynamicUniverse('A') # 证券池,支持股票、基金、期货、指数四种资产benchmark = 'HS300' # 策略参考标准freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测refresh_rate = 10 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟# 配置账户信息,支持多资产多账户accounts = {'fantasy_account': AccountConfig(account_type='security', capital_base=10000000)}Max_position_per = 0.1Max_history_window = 250Max_time_range = 30atrlength = 20avglength =20def initialize(context):passdef handle_data(context):tradingDic = timing_CK(context)trading(tradingDic,context)
第二步 选股 三价均线向上 价格突破上轨 买入 备选
有持仓 价格跌破三价均线 卖出备选
import talib as tadef timing_CK(context):account = context.get_account('fantasy_account')current_universe = context.get_universe(exclude_halt=True)security_position = account.get_positions()history = context.history(current_universe,['closePrice','lowPrice','highPrice'],Max_time_range,rtype= 'array')buy_list = []sell_list = []for sec in current_universe:close = history[sec]['closePrice']low = history[sec]['lowPrice']high = history[sec]['highPrice']#数据处理 计算ATR通道上轨和止损点 计算三价均线atr = ta.ATR(high,low,close,atrlength)[-1]movavgval = ta.MA((high+low+close)/3,avglength)upband = movavgval[-1] + atrliquidpoint = movavgval # 三价均线向上 突破上轨做多 止损点为3价均线if movavgval[-1] >movavgval[-2] and high[-1]>=upband and sec not in security_position:buy_list.append(sec)elif low[-1]<= liquidpoint[-1] and sec in security_position:sell_list.append(sec)TradingDic = {'buy_list':buy_list,'sell_list':sell_list}return TradingDic
第三步 交易
def trading(TradingDic,context):account = context.get_account('fantasy_account')current_universe = context.get_universe( exclude_halt=True)security_position = account.get_positions()cash = account.cashbuy_list = TradingDic['buy_list']sell_list = TradingDic['sell_list']for sec in current_universe:if sec in sell_list and sec in security_position:account.order_to(sec,0)cash += security_position[sec].amount * context.current_price(sec)if len(buy_list) >0:weight = min(Max_position_per, 1.0/len(buy_list))else:weight = 0for sec in buy_list:if sec not in security_position:account.order_pct_to(sec,weight)

跑了一下15年牛市和股灾的收益 跑赢大盘20个点 还不错 就是持仓太多太多


从今天开始 我准备实盘记录一下最近的操作 我的操作策略 一般是 选择 8-12只股票 每只股票仓位最多不超过20% 目前仓位 80% 近期会减 没粉丝 就实盘了

文章转载自量化分析之路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




