@Channelchan
2018-01-16T03:08:08.000000Z
字数 1499
阅读 1585
股票代号: 600050
指标: MA(,timeperiod=60)
通道百分比: 3%
Buy: 突破上轨满仓买入
Sell: 突破下轨满仓卖出
图表: 画出指标的图表
# Bollinger Bandimport rqalphafrom rqalpha.api import *import talib as tadef init(context):context.s1 = "600050.XSHG"context.PERIOD = 60context.UP = 1.03context.LOW = 0.97def handle_bar(context, bar_dict):# record(context, bar_dict)record_Chart(context, bar_dict)prices = history_bars(context.s1, context.PERIOD+2, '1d', 'close')MA_middle = ta.EMA(prices,context.PERIOD)MA_upper = MA_middle*context.UPMA_lower = MA_middle*context.LOWcur_position = context.portfolio.positions[context.s1].quantityshares = context.portfolio.cash/bar_dict[context.s1].closeif prices[-1]<MA_lower[-2] and cur_position > 0:order_target_value(context.s1, 0)if prices[-1]>MA_upper[-2]:order_shares(context.s1, shares)def record(context, bar_dict):pos_s1 = context.portfolio.positions[context.s1].quantityprice_s1 = context.portfolio.positions[context.s1].avg_pricecapital = pos_s1*price_s1plot("capital", capital)def record_Chart(context, bar_dict):prices = history_bars(context.s1, context.PERIOD+2, '1d', 'close')MA_middle = ta.EMA(prices,context.PERIOD)MA_upper = MA_middle*context.UPMA_lower = MA_middle*context.LOWplot("prices", prices[-1])plot("MA_middle", MA_middle[-1])plot("MA_upper", MA_upper[-1])plot("MA_lower", MA_lower[-1])config = {"base": {"start_date": "2014-06-01","end_date": "2018-01-15","accounts": {'stock':1000000},"benchmark": "600050.XSHG"},"extra": {"log_level": "error",},"mod": {"sys_analyser": {"enabled": True,"plot": True}}}rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)

