[关闭]
@Channelchan 2018-01-06T15:13:55.000000Z 字数 1094 阅读 125207

5. 订单类型

order_shares买入一只股票的一定股份数

  1. import rqalpha
  2. from rqalpha.api import *
  3. def init(context):
  4. context.s1 = '000001.XSHE'
  5. def handle_bar(context, bar_dict):
  6. cur_position = context.portfolio.positions[context.s1].quantity
  7. shares = context.portfolio.cash/bar_dict[context.s1].close
  8. if cur_position==0:
  9. order_shares(context.s1, shares)

order_target_percent买入两只股票的一定百分比

  1. def init(context):
  2. context.s1 = '000001.XSHE'
  3. context.s2 = '600036.XSHG'
  4. def handle_bar(context, bar_dict):
  5. if context.s1 not in context.portfolio.positions:
  6. order_target_percent(context.s1, 0.5)
  7. if context.s2 not in context.portfolio.positions:
  8. order_target_percent(context.s2, 0.5)

sell_open卖空一手股指期货与buy_close买多平仓

连续两天MA下跌卖出一手股指期货,连续两天MA上涨买入平仓。

  1. def init(context):
  2. context.future = get_future_contracts("IF")[0]
  3. def handle_bar(context, bar_dict):
  4. prices = history_bars(context.future, 12, '1d', 'close')
  5. ma = talib.EMA(prices, 10)
  6. sell_qty = context.portfolio.positions[future].sell_quantity
  7. if ma[-1] < ma[-2] and ma[-2] < ma[-3] and sell_qty == 0:
  8. sell_open(context.future, 1)
  9. if ma[-1] > ma[-2] and ma[-2]>ma[-3] and sell_qty > 0:
  10. buy_close(context.future, 1)

更多下单接口使用方式,情参考: http://rqalpha.readthedocs.io/zh_CN/latest/index.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注