@Channelchan
2018-01-16T11:08:08.000000Z
字数 1499
阅读 1425
股票代号: 600050
指标: MA(,timeperiod=60)
通道百分比: 3%
Buy: 突破上轨满仓买入
Sell: 突破下轨满仓卖出
图表: 画出指标的图表
# Bollinger Band
import rqalpha
from rqalpha.api import *
import talib as ta
def init(context):
context.s1 = "600050.XSHG"
context.PERIOD = 60
context.UP = 1.03
context.LOW = 0.97
def 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.UP
MA_lower = MA_middle*context.LOW
cur_position = context.portfolio.positions[context.s1].quantity
shares = context.portfolio.cash/bar_dict[context.s1].close
if 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].quantity
price_s1 = context.portfolio.positions[context.s1].avg_price
capital = pos_s1*price_s1
plot("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.UP
MA_lower = MA_middle*context.LOW
plot("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)