@Channelchan
2017-06-06T13:44:38.000000Z
字数 850
阅读 1868
未分类
import rqalpha
from rqalpha.api import *
import talib
def init(context):
context.s1 = "000001.XSHE"
context.SHORTPERIOD = 10
context.LONGPERIOD = 30
def handle_bar(context, bar_dict):
prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')
short_avg = talib.SMA(prices, context.SHORTPERIOD)
long_avg = talib.SMA(prices, context.LONGPERIOD)
plot("short avg", short_avg[-1])
plot("long avg", long_avg[-1])
# 计算现在portfolio中股票的仓位
cur_position = context.portfolio.positions[context.s1].quantity
# 计算现在portfolio中的现金可以购买多少股票
shares = context.portfolio.cash/bar_dict[context.s1].close
# 如果短均线从上往下跌破长均线,也就是在目前的bar短线平均值低于长线平均值,而上一个bar的短线平均值高于长线平均值
if short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:
# 进行清仓
order_target_value(context.s1, 0)
# 如果短均线从下往上突破长均线,为入场信号
if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0:
# 满仓入股
order_shares(context.s1, shares)