用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
# 5.16 DualTrust 策略和布林强盗策略 > 来源:https://uqer.io/community/share/564737ddf9f06c4446b48133 谁能够帮忙实现DualTrust策略和布林强盗策略(BollingerBandit)?@薛昆Kelvin @lookis: DualTrust: ```py start = '2014-01-01' # 回测起始时间 end = '2015-01-01' # 回测结束时间 benchmark = 'HS300' # 策略参考标准 universe = set_universe("CYB") # 证券池,支持股票和基金 capital_base = 100000 # 起始资金 freq = 'm' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测 refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟 def initialize(account): # 初始化虚拟账户状态 account.k1 = 0.7 account.k2 = 0.7 account.cache = {} account.holding_max = 10 account.holding = 0 account.buy_sell_line = {} pass def handle_data(account): # 每个交易日的买入卖出指令 #准备数据 if not account.current_date.strftime('%Y%m%d') in account.cache: account.cache = {} account.cache[account.current_date.strftime('%Y%m%d')] = account.get_daily_history(1) if account.current_minute == "09:30": return #每天画一次线 if account.current_minute == "09:31": account.buy_sell_line = {} for stock in account.cache[account.current_date.strftime('%Y%m%d')]: if stock in account.universe: close = account.cache[account.current_date.strftime('%Y%m%d')][stock]["closePrice"][0] low = account.cache[account.current_date.strftime('%Y%m%d')][stock]["lowPrice"][0] high = account.cache[account.current_date.strftime('%Y%m%d')][stock]["highPrice"][0] o = account.referencePrice[stock] r = max(high - low, close - low) account.buy_sell_line[stock] = {"buy": o + account.k1 * r, "sell": o - account.k2 * r} else: #每天剩余的时间根据画线买卖 for stock in account.buy_sell_line: if stock in account.universe and stock in account.referencePrice and stock in account.valid_secpos: if account.referencePrice[stock] < account.buy_sell_line[stock]["sell"]: order_to(stock, 0) account.holding -= 1 for stock in account.buy_sell_line: if stock in account.universe and stock in account.referencePrice and not stock in account.valid_secpos: if account.holding < account.holding_max and account.referencePrice[stock] > account.buy_sell_line[stock]["buy"]: account.holding += 1 order_pct(stock, 1.0/account.holding_max) return ``` 回测看效果不是特别好…… LZ自己调一下参数吧 @JasonYichuan: BollingerBandit很一般,不过没怎么调参数,看着办吧 ```py import numpy as np import pandas as pd start = '2015-01-01' # 回测起始时间 end = '2015-11-26' # 回测结束时间 benchmark = 'HS300' # 策略参考标准 universe = set_universe('HS300') # 证券池,支持股票和基金 capital_base = 100000 # 起始资金 #commission = Commission(buycost=0.00025,sellcost=0.00025) # 佣金 freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测 refresh_rate = 1 # 调仓频率 # 全局参数 ## Boll线参数 N = 20 k = 2 ## ROC变动率参数 M = 20 ## 平仓参数 E = 20 def initialize(account): # 初始化虚拟账户状态 # 持股代码以及持股时间 account.duration = pd.DataFrame(np.array([0]*len(universe)), index=universe, columns=['duration']) account.amount = 400 def handle_data(account): # 每个交易日的买入卖出指令 hist = account.get_attribute_history('closePrice',50) ticker_name = [] # 符合买入要求股票代码 for stk in account.universe: # 遍历股票池内所有股票,选出符合要求的股票 if np.isnan(account.referencePrice[stk]) or account.referencePrice[stk] == 0: # 停牌或是还没有上市等原因不能交易 continue # 计算股票的BOLL线上下轨 ## 计算MA MA = np.mean(hist[stk][-N:]) ## 计算标准差MD MD = np.sqrt((sum(hist[stk][-N:] - MA)**2) / N) ## 计算MB、UP、DN线 MB =np.mean(hist[stk][-(N-1):]) UP = MB + k * MD DN = MB - k * MD # 计算股票的ROC ROC = float(hist[stk][-1] - hist[stk][-M])/float(hist[stk][-M]) # 开仓条件 if (hist[stk][-1] > UP) and (ROC > 0): ticker_name.append(stk) # 若股票符合开仓条件且尚未持有,则买入 for stk in ticker_name: if stk not in account.valid_secpos: order(stk,account.amount) account.duration.loc[stk]['duration'] = 1 # 对于持有的股票,若股票不符合平仓条件,则将持仓时间加1,否则卖出,并删除该持仓时间记录 for stk in account.valid_secpos: T = max(E - account.duration.loc[stk]['duration'],10) if hist[stk][-1] > np.mean(hist[stk][-T:]): account.duration.loc[stk]['duration'] = account.duration.loc[stk]['duration'] + 1 else: order_to(stk,0) account.duration.loc[stk]['duration'] = 0 return ```