5.16 DualTrust 策略和布林强盗策略

来源:https://uqer.io/community/share/564737ddf9f06c4446b48133

谁能够帮忙实现DualTrust策略和布林强盗策略(BollingerBandit)?@薛昆Kelvin

@lookis:

DualTrust:

  1. start = '2014-01-01' # 回测起始时间
  2. end = '2015-01-01' # 回测结束时间
  3. benchmark = 'HS300' # 策略参考标准
  4. universe = set_universe("CYB") # 证券池,支持股票和基金
  5. capital_base = 100000 # 起始资金
  6. freq = 'm' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  7. refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  8. def initialize(account): # 初始化虚拟账户状态
  9. account.k1 = 0.7
  10. account.k2 = 0.7
  11. account.cache = {}
  12. account.holding_max = 10
  13. account.holding = 0
  14. account.buy_sell_line = {}
  15. pass
  16. def handle_data(account): # 每个交易日的买入卖出指令
  17. #准备数据
  18. if not account.current_date.strftime('%Y%m%d') in account.cache:
  19. account.cache = {}
  20. account.cache[account.current_date.strftime('%Y%m%d')] = account.get_daily_history(1)
  21. if account.current_minute == "09:30":
  22. return
  23. #每天画一次线
  24. if account.current_minute == "09:31":
  25. account.buy_sell_line = {}
  26. for stock in account.cache[account.current_date.strftime('%Y%m%d')]:
  27. if stock in account.universe:
  28. close = account.cache[account.current_date.strftime('%Y%m%d')][stock]["closePrice"][0]
  29. low = account.cache[account.current_date.strftime('%Y%m%d')][stock]["lowPrice"][0]
  30. high = account.cache[account.current_date.strftime('%Y%m%d')][stock]["highPrice"][0]
  31. o = account.referencePrice[stock]
  32. r = max(high - low, close - low)
  33. account.buy_sell_line[stock] = {"buy": o + account.k1 * r, "sell": o - account.k2 * r}
  34. else:
  35. #每天剩余的时间根据画线买卖
  36. for stock in account.buy_sell_line:
  37. if stock in account.universe and stock in account.referencePrice and stock in account.valid_secpos:
  38. if account.referencePrice[stock] < account.buy_sell_line[stock]["sell"]:
  39. order_to(stock, 0)
  40. account.holding -= 1
  41. for stock in account.buy_sell_line:
  42. if stock in account.universe and stock in account.referencePrice and not stock in account.valid_secpos:
  43. if account.holding < account.holding_max and account.referencePrice[stock] > account.buy_sell_line[stock]["buy"]:
  44. account.holding += 1
  45. order_pct(stock, 1.0/account.holding_max)
  46. return

回测看效果不是特别好…… LZ自己调一下参数吧

@JasonYichuan:

BollingerBandit很一般,不过没怎么调参数,看着办吧

  1. import numpy as np
  2. import pandas as pd
  3. start = '2015-01-01' # 回测起始时间
  4. end = '2015-11-26' # 回测结束时间
  5. benchmark = 'HS300' # 策略参考标准
  6. universe = set_universe('HS300') # 证券池,支持股票和基金
  7. capital_base = 100000 # 起始资金
  8. #commission = Commission(buycost=0.00025,sellcost=0.00025) # 佣金
  9. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  10. refresh_rate = 1 # 调仓频率
  11. # 全局参数
  12. ## Boll线参数
  13. N = 20
  14. k = 2
  15. ## ROC变动率参数
  16. M = 20
  17. ## 平仓参数
  18. E = 20
  19. def initialize(account): # 初始化虚拟账户状态
  20. # 持股代码以及持股时间
  21. account.duration = pd.DataFrame(np.array([0]*len(universe)), index=universe, columns=['duration'])
  22. account.amount = 400
  23. def handle_data(account): # 每个交易日的买入卖出指令
  24. hist = account.get_attribute_history('closePrice',50)
  25. ticker_name = [] # 符合买入要求股票代码
  26. for stk in account.universe: # 遍历股票池内所有股票,选出符合要求的股票
  27. if np.isnan(account.referencePrice[stk]) or account.referencePrice[stk] == 0: # 停牌或是还没有上市等原因不能交易
  28. continue
  29. # 计算股票的BOLL线上下轨
  30. ## 计算MA
  31. MA = np.mean(hist[stk][-N:])
  32. ## 计算标准差MD
  33. MD = np.sqrt((sum(hist[stk][-N:] - MA)**2) / N)
  34. ## 计算MB、UP、DN线
  35. MB =np.mean(hist[stk][-(N-1):])
  36. UP = MB + k * MD
  37. DN = MB - k * MD
  38. # 计算股票的ROC
  39. ROC = float(hist[stk][-1] - hist[stk][-M])/float(hist[stk][-M])
  40. # 开仓条件
  41. if (hist[stk][-1] > UP) and (ROC > 0):
  42. ticker_name.append(stk)
  43. # 若股票符合开仓条件且尚未持有,则买入
  44. for stk in ticker_name:
  45. if stk not in account.valid_secpos:
  46. order(stk,account.amount)
  47. account.duration.loc[stk]['duration'] = 1
  48. # 对于持有的股票,若股票不符合平仓条件,则将持仓时间加1,否则卖出,并删除该持仓时间记录
  49. for stk in account.valid_secpos:
  50. T = max(E - account.duration.loc[stk]['duration'],10)
  51. if hist[stk][-1] > np.mean(hist[stk][-T:]):
  52. account.duration.loc[stk]['duration'] = account.duration.loc[stk]['duration'] + 1
  53. else:
  54. order_to(stk,0)
  55. account.duration.loc[stk]['duration'] = 0
  56. return