Beispiel #1
0
def handle_bar(context, bar_dict):
    # 开始编写你的主要的算法逻辑

    # bar_dict[order_book_id] 可以拿到某个证券的bar信息
    # context.portfolio 可以拿到现在的投资组合状态信息

    # 使用order_shares(id_or_ins, amount)方法进行落单

    # TODO: 开始编写你的算法吧!

    # 对我们选中的股票集合进行loop,运算每一只股票的RSI数值
    for stock in context.stocks:
        # 读取历史数据
        prices = history(context.TIME_PERIOD + 1, '1d', 'close')[stock].values

        # 用Talib计算RSI值
        rsi_data = talib.RSI(prices, timeperiod=context.TIME_PERIOD)[-1]

        curPosition = context.portfolio.positions[stock].quantity
        #用剩余现金的30%来购买新的股票
        target_available_cash = context.portfolio.cash * context.ORDER_PERCENT

        #当RSI大于设置的上限阀值,清仓该股票
        if rsi_data > context.HIGH_RSI and curPosition > 0:
            order_target_value(stock, 0)

        #当RSI小于设置的下限阀值,用剩余cash的一定比例补仓该股
        if rsi_data < context.LOW_RSI:
            # logger.info("target available cash caled: " + str(target_available_cash))
            #如果剩余的现金不够一手 - 100shares,那么会被ricequant 的order management system reject掉
            order_value(stock, target_available_cash)
Beispiel #2
0
 def handle_bar(context, bar_dict):
     order_id = order_value(context.s1, 1000, style=LimitOrder(context.limitprice))
     order = get_order(order_id)
     order_side = SIDE.BUY if order.quantity > 0 else SIDE.SELL
     assert order.side == order_side
     assert order.order_book_id == context.s1
     assert order.quantity == 100
     assert order.unfilled_quantity + order.filled_quantity == order.quantity
     assert order.price == context.limitprice
Beispiel #3
0
def position(context, bar_dict):
    stocks = set(context.to_buy)
    holdings = set(get_holdings(context))
    to_buy = stocks - holdings
    holdings = set(get_holdings(context))
    to_sell = holdings - stocks
    for stock in to_sell:
        if bar_dict[stock].is_trading:
            order_target_percent(stock, 0)
    to_buy = get_trading_stocks(to_buy, context, bar_dict)
    cash = context.portfolio.cash
    average_value = 0
    if len(to_buy) > 0:
        average_value = 0.98 * cash / len(to_buy)
    for stock in to_buy:
        if bar_dict[stock].is_trading:
            order_value(stock, average_value)
    context.marketval = context.portfolio.market_value
Beispiel #4
0
 def handle_bar(context, bar_dict):
     order_id = order_value(context.s1, 1000, style=LimitOrder(context.limitprice))
     order = get_order(order_id)
     order_side = SIDE.BUY if order.quantity > 0 else SIDE.SELL
     assert order.side == order_side
     assert order.order_book_id == context.s1
     assert order.quantity == 100
     assert order.unfilled_quantity + order.filled_quantity == order.quantity
     assert order.price == context.limitprice
def handle_bar(context, bar_dict):
    #count = context.buyStkCount
    #context.today = context.now.date()
    #context.today = str(context.today)
    #context.prev_calendar_date = get_prev_trade_date(context,context.today)
    #for stk in context.A_H_price_compare_result.A_code:
    #    df = context.A_H_price_compare_result[(context.A_H_price_compare_result.A_code == stk)]
    #    plot(stk, df.iloc[0,7])
    df = context.A_H_price_compare_result[(context.A_H_price_compare_result.A_code == '000002.XSHE')]
    plot('000002.XSHE', df.iloc[0,8])


        # 买入新股
    A_stk = '000002.XSHE'
    H_stk = ''
    if not context.holding_A:
        order_target_percent('000002.XSHE', 1)
        context.holding_A = True
    if bar_dict[A_stk].is_trading:
        if df.iloc[0,8] <= -10:
            order_value('000002.XSHE', 20000)
        if df.iloc[0,8] >= -5 and A_stk in context.portfolio.positions:
            order_value('000002.XSHE', -20000)