暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

基于ATR通道的金肯纳特交易系统

量化分析之路 2020-04-09
3481

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

先上公式




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


取三个中的最大值的绝对值 就是某一天的TR 。

(ATR)其实就是真实波幅的一个移动平均值,直接看公式:


 唐安奇通道  布林带通道   ATR通道其实都是三条线 只是上下轨的计算公式有所不同。


传统通道突破策略是建立在通道突破思想上:


突破上轨做多;

下破下轨做空。

但是通道突破系统主要的问题在于假突破。假突破中,通道的突破并没有带来趋势的确定,反而是价格动能的衰竭,会迅速回落并反向运动

 

金肯纳特交易系统针对假突破问题进行了改进。金肯纳特交易系统中在移动平均线附近设置了止损,以限制假突破造成的损失。

 

具体而言,三价(最高价、最低价和收盘价)均线向上,并且价格上破通道上轨,买入股票,

 

价格下破三价均线,卖出股票。其中上轨=三价均线+真实振幅的移动平均值

 

策略:三价均线向上多头排列,价格上破通道上轨,买入股票・

出场条件:价格下破三价均线,卖出股票


代码回测 第一步 准备工作

import numpy as np
import pandas as pd
import talib as ta
import datetime


start = '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.1
Max_history_window = 250
Max_time_range = 30


atrlength = 20
avglength =20




def initialize(context):
pass


def handle_data(context):
tradingDic = timing_CK(context)
trading(tradingDic,context)
复制

第二步 选股   三价均线向上  价格突破上轨 买入 备选

                     有持仓  价格跌破三价均线   卖出备选


import talib  as ta


def 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] + atr
liquidpoint = 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.cash
buy_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 = 0
for 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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论