金肯特纳交易系统

策略说明: 基于肯特纳通道的突破系统

系统构成:

基于最高价、最低价、收盘价三者平均值计算而来的三价均线
基于三价均线加减真实波幅计算而来的通道上下轨
入场条件:

三价均线向上,并且价格上破通道上轨,开多单
三价均线向下,并且价格下破通道下轨,开空单
出场条件:

持有多单时,价格下破三价均线,平多单
持有空单时,价格上破三价均线,平空单
import DataAPI
import numpy as np
import pandas as pd
import talib as ta
from collections import deque
from itertools import product

class DataCenter():
keys = [u’openPrice’, u’highPrice’, u’lowPrice’, u’closePrice’, u’turnoverVol’]
def init(self, refresh_rate, pipe_length):
self.data = {key:deque([], maxlen=pipe_length) for key in (self.keys + [‘tradeTime’])}
self.refresh_rate = refresh_rate
self.pipe_length = pipe_length

def update_data(self, symbol):
    if len(self.data['openPrice']) < self.pipe_length:
        hist = history(symbol=symbol, attribute=self.keys, time_range=self.refresh_rate*self.pipe_length+1, freq='m', rtype='frame')[symbol].ix[:-1, :]
    else:
        hist = history(symbol=symbol, attribute=self.keys, time_range=self.refresh_rate+1, freq='m', rtype='frame')[symbol].ix[:-1, :]
    # hist.index = range(len(hist))
    current_data = {key:[] for key in (self.keys + ['tradeTime'])}

    for i in range(len(hist)/self.refresh_rate):
        current_bar = hist[self.refresh_rate*i:self.refresh_rate*(i+1)]
        current_data['closePrice'].append(current_bar.ix[len(current_bar)-1, 'closePrice'])
        current_data['openPrice'].append(current_bar.ix[0, 'openPrice'])
        current_data['highPrice'].append(current_bar['highPrice'].max())
        current_data['lowPrice'].append(current_bar['lowPrice'].min())
        current_data['turnoverVol'].append(current_bar['turnoverVol'].sum())
        current_data['tradeTime'].append(current_bar.index[len(current_bar)-1])
    for i in self.keys:
        for j in current_data[i]:
            self.data[i].append(j) 
    return self.data

universe = [‘RBM0’] # 策略期货合约
start = ‘2016-01-01’ # 回测开始时间
end = ‘2016-03-25’ # 回测结束时间
refresh_rate = 5 # 调仓周期
freq = ‘m’ # 调仓频率:m-> 分钟;d-> 日
minpoint = 10

avglength = 40
atrlength = 40
lots = 10

accounts = {
‘futures_account’: AccountConfig(account_type=’futures’, capital_base=1000000)
}

def initialize(context): # 初始化虚拟期货账户,一般用于设置计数器,回测辅助变量等。
global data_pool
data_pool = DataCenter(refresh_rate, atrlength+1)
context.symbol = ‘RB1605’

def handle_data(context): # 回测调仓逻辑,每个调仓周期运行一次,可在此函数内实现信号生产,生成调仓指令。
futures_account = context.get_account(‘futures_account’)
symbol = context.symbol
long_position = futures_account.get_positions().get(symbol, dict()).get(‘long_amount’, 0)
if context.get_symbol(universe[0]) != symbol:
if long_position != 0:
print futures_account.current_date, futures_account.current_time, ‘主力更换, 平仓’
print context.get_symbol(universe[0])
order(futures_account.symbol, -long_position, ‘close’)
futures_account.symbol = context.get_symbol(universe[0])
else:
data = data_pool.update_data(symbol)

    high = np.array(data['highPrice'])
    low = np.array(data['lowPrice'])
    close = np.array(data['closePrice'])
    open_ = np.array(data['openPrice'])

    atr = ta.ATR(high, low, close, atrlength)[-1]

    # 三价均线
    movavgval = ta.MA((high+low+close)/3, avglength)
    # 通道上轨
    upband = movavgval[-1] + atr
    # 出场条件
    liquidpoint = movavgval

    # 三价均线向上,并且价格向上突破上轨,开多单
    if long_position == 0 and movavgval[-1] > movavgval[-2] and high[-1] >= upband:
        futures_account.order(symbol, 10, 'open')
    # 持有多单时,并且价格向下突破三价均线,平多单
    if long_position != 0 and low[-1] <= liquidpoint[-1]:
        futures_account.order(symbol, -long_position, 'close')
print "=============="
print context.current_date
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值