R-Breaker 是一种短线日内交易策略,它结合了趋势和反转两种交易方式。该策略也长期被Future Thruth 杂志评为最赚钱的策略之一,尤其在标普500 股指期货上效果最佳。该策略的主要特点如下:
第一、根据前一个交易日的收盘价、最高价和最低价数据通过一定方式计算出六个价位,从大到小依次为突破买入价、观察卖出价、反转卖出价、反转买入价、观察买入价和突破卖出价,以此来形成当前交易日盘中交易的触发条件。通过对计算方式的调整,可以调节六个价格间的距离,进一步改变触发条件。
第二、根据盘中价格走势,实时判断触发条件,具体条件如下: 1) 当日内最高价超过观察卖出价后,盘中价格出现回落,且进一步跌破反转卖出价构成的支撑线时,采取反转策略,即在该点位(反手、开仓)做空; 2) 当日内最低价低于观察买入价后,盘中价格出现反弹,且进一步超过反转买入价构成的阻力线时,采取反转策略,即在该点位(反手、开仓)做多; 3) 在空仓的情况下,如果盘中价格超过突破买入价,则采取趋势策略,即在该点位开仓做多; 4) 在空仓的情况下,如果盘中价格跌破突破卖出价,则采取趋势策略,即在该点位开仓做空。
第三、设定止损以及止盈条件;
第四、设定过滤条件;
第五、在每日收盘前,对所持合约进行平仓。
具体来看,这六个价位形成的阻力和支撑位计算过程如下:
观察卖出价 = High + 0.35 * (Close – Low)
观察买入价 = Low – 0.35 * (High – Close)
反转卖出价 = 1.07 / 2 * (High + Low) – 0.07 * Low
反转买入价 = 1.07 / 2 * (High + Low) – 0.07 * High
突破买入价 = 观察卖出价 + 0.25 * (观察卖出价 – 观察买入价)
突破卖出价 = 观察买入价 – 0.25 * (观察卖出价 – 观察买入价)
其中,High、Close、Low 分别为昨日最高价、昨日收盘价和昨日最低价。这六个价位从大到小一次是,突破买入价、观察爱出价、反转卖出价、反转买入价、观察买入价和突破卖出价。
import DataAPI import numpy as np import pandas as pd import talib as ta from collections import deque from itertools import product import datetime import matplotlib import matplotlib.pyplot as plt import matplotlib.cm as cm import seaborn as sns import quartz_futures as qf from quartz_futures.api import * from CAL import * cal = Calendar(‘China.SSE’) class DataCenter(): keys = [u’openPrice’, u’highPrice’, u’lowPrice’, u’closePrice’, u’tradeDate’, u’volume’] def
init
(self, refresh_rate, pipe_length): self.data = {key:deque([], maxlen=pipe_length) for key in self.keys}#用双向队列保存数据 self.refresh_rate = refresh_rate # 算法调用周期 self.pipe_length = pipe_length #队列长度 def update_data(self, symbol): if len(self.data[‘openPrice’]) < self.pipe_length: hist = get_symbol_history(symbol=symbol, field=self.keys, time_range=self.refresh_rate*self.pipe_length)[symbol] else: hist = get_symbol_history(symbol=symbol, field=self.keys, time_range=self.refresh_rate)[symbol] # hist.index = range(len(hist)) current_data = {key:[] for key in self.keys} 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[‘volume’].append(current_bar[‘volume’].sum()) current_data[‘tradeDate’].append(current_bar.ix[len(current_bar)-1, ‘tradeDate’]) for i in self.keys: for j in current_data[i]: self.data[i].append(j) return self.data ### 策略初始化函数 universe = ‘RBM0’ # 策略交易的期货合约,此处选择IH1609 start = “2010-01-01” # 回测开始时间 end = “2016-12-28” # 回测结束时间 capital_base = 1e6 # 初始可用资金 refresh_rate = 5 # 算法调用周期 freq = ‘m’ # 算法调用频率:m-> 分钟;d-> 日; commissions = {‘RB’: (0.000025, ‘perValue’)} slippage = Slippage(0, ‘perValue’) amount = 20 def initialize(futures_account): global data_pool data_pool = DataCenter(refresh_rate, 2) futures_account.high = np.NAN futures_account.low = np.NAN futures_account.close = np.NAN futures_account.count1 = 0 futures_account.count2 = 0 def handle_data(futures_account): symbol = get_symbol(universe) long_position = futures_account.position.get(symbol, dict()).get(‘long_position’, 0) short_position = futures_account.position.get(symbol, dict()).get(‘short_position’, 0) if futures_account.current_time == ‘09:30:00’: yester_data = DataAPI.MktFutdGet(tradeDate=futures_account.previous_date, ticker=symbol, field=[u’closePrice’, u’highestPrice’,u’lowestPrice’], pandas=”1”) futures_account.high = yester_data[‘highestPrice’].iat[0] futures_account.low = yester_data[‘lowestPrice’].iat[0] futures_account.close = yester_data[‘closePrice’].iat[0] if futures_account.current_time > ‘09:30:00’ and futures_account.current_time < ‘14:55:00’: data = data_pool.update_data(symbol) before_2_close = data[‘closePrice’][-2] before_2_high = data[‘highPrice’][-2] before_1_close = data[‘closePrice’][-1] before_1_high = data[‘highPrice’][-1] before_1_low = data[‘lowPrice’][-1] before_1_open = data[‘openPrice’][-1] # 观察卖出价 ssetup=futures_account.high+0.35*(futures_account.close-futures_account.low) # 观察买入价 bsetup=futures_account.low-0.35*(futures_account.high-futures_account.close) # 反转卖出价 senter=(1+0.07)/2*(futures_account.high+futures_account.low)-0.07*futures_account.low # 反转买入价 benter = (1+0.07)/2*(futures_account.high+futures_account.low)-0.07*futures_account.high # 突破买入价 bbreak = ssetup+0.25*(ssetup-bsetup) # 突破卖出价 sbreak = bsetup-0.25*(ssetup-bsetup) ## 趋势 if before_2_close <= bbreak and before_1_close > bbreak: if long_position == 0: order(symbol, amount, ‘open’) if short_position != 0: order(symbol, short_position, ‘close’) if before_2_close >= sbreak and before_1_close < sbreak: if short_position == 0: order(symbol, -amount, ‘open’) if long_position != 0: order(symbol, -long_position, ‘close’) ## 反转 ### 多单反转,当日内最高价超过观察卖出价后,盘中价格出现回落,且进一步跌破反转卖出价构成的支撑线时,采取反转策略,即在该点位(反手、开仓)做空 if before_1_high > ssetup and before_1_close > senter: futures_account.count1 = 1 if futures_account.count1 == 1 and before_1_close < senter: if long_position > 0: order(symbol, -long_position, ‘close’) order(symbol, -amount, ‘open’) ### 空单反转,当日内最低价低于观察买入价后,盘中价格出现反弹,且进一步超过反转买入价构成的阻力线时,采取反转策略,即在该点位(反手、开仓)做多 if before_1_low < bsetup: futures_account.count2 = 1 if futures_account.count2 == 1 and before_1_close > benter: if short_position != 0: order(symbol, short_position, ‘close’) order(symbol, amount, ‘open’) elif futures_account.current_time >= ‘14:55:00’:#在每日收盘前,对所持合约进行平仓 if short_position > 0: order(symbol, short_position, ‘close’) if long_position > 0: order(symbol, -long_position, ‘close’) futures_account.high = np.NAN futures_account.low = np.NAN futures_account.close = np.NAN futures_account.count1 = 0 futures_account.count2 = 0