|
|
1. 均值回归理论
均值回归:“跌下去的迟早要涨上来”
均值回归的理论基于以下观测:价格的波动一般会以它的均线为中心,也就是说,当表的价格由于波动而偏离移动均线时,它将调整并重新归于均线。
定义偏离程度:(MA - P) / MA
2. 布林带策略
布林带/布林线/ 保利加通道(Bollinger Band):由三条轨道线组成,其中上下两条线分别可以看成是价格的压力线和支撑线,在两条线之间是一条价格平均线。
计算公式:
中间线 = 20日均线
Up线 = 20日均线 + N*SD(20日收盘价)
down线 = 20日均线 - N*SD(20日收盘价)
就像 (a.mean() - 2*a.std(), a.mean() + 2*a.std()
(a.mean() - 1.5*a.std(), a.mean() + 1.5*a.std()
布林带策略:择时
当股价突破阻力线时,清仓
当股价跌破支撑线时,全仓买入
布林带策略研究:N的取值问题,布林带宽度等。
def initialize(context):
set_option('use_real_price', True)
set_order_cost(OrderCost(open_tax=0, close_tax=0.001,open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
set_benchmark('000300.XSHG')
g.security = '600036.XSHG'
g.M = 20
g.k = 2
def handle(context, data):
sr = attribute_history(g.security, g.M)['close']
ma = sr.mean()
up = ma + g.k * sr.std()
down = ma - g.k * sr.std()
p = get_current_data()[g.security].day_open
cash = context.portfolio.available_cash
if p < down and g.security not in context.portfolio.positions:
order_value(g.security, cash)
elif p > up and g.security in context.portfolio.positions:
order_target(g.security, 0)
|
-
|