Пример #1
0
def bollinger_band_trading_rule(contract_group, i, timestamps, indicators, signal, account, strategy_context):
    timestamp = timestamps[i]
    curr_pos = account.position(contract_group, timestamp)
    signal_value = signal[i]
    risk_percent = 0.1
    close_price = indicators.c[i]
    
    contract = contract_group.get_contract('PEP')
    if contract is None:
        contract = pq.Contract.create(symbol = 'PEP', contract_group = contract_group)
    
    # if we don't already have a position, check if we should enter a trade
    if math.isclose(curr_pos, 0):
        if signal_value == 2 or signal_value == -2:
            curr_equity = account.equity(timestamp)
            order_qty = np.round(curr_equity * risk_percent / close_price * np.sign(signal_value))
            trigger_price = close_price
            reason_code = pq.ReasonCode.ENTER_LONG if order_qty > 0 else pq.ReasonCode.ENTER_SHORT
            return [pq.StopLimitOrder(contract, timestamp, order_qty, trigger_price, reason_code = reason_code)]
        
    else: # We have a current position, so check if we should exit
        if (curr_pos > 0 and signal_value == -1) or (curr_pos < 0 and signal_value == 1):
            order_qty = -curr_pos
            reason_code = pq.ReasonCode.EXIT_LONG if order_qty < 0 else pq.ReasonCode.EXIT_SHORT
            return [pq.MarketOrder(contract, timestamp, order_qty, reason_code = reason_code)]
    return []
def bollinger_band_trading_rule(strategy, symbol, i, date, marketdata,
                                indicator_values, signal_values, account):
    curr_pos = account.position(symbol, date)
    signal_value = signal_values[i]
    risk_percent = 0.05

    # if we don't already have a position, check if we should enter a trade
    if math.isclose(curr_pos, 0):
        if signal_value == 2 or signal_value == -2:
            curr_equity = account.equity(date)
            order_qty = np.round(curr_equity * risk_percent / marketdata.c[i] *
                                 np.sign(signal_value))
            trigger_price = marketdata.c[i]
            reason_code = pq.ReasonCode.ENTER_LONG if order_qty > 0 else pq.ReasonCode.ENTER_SHORT
            return [
                pq.StopLimitOrder(symbol,
                                  date,
                                  order_qty,
                                  trigger_price,
                                  reason_code=reason_code)
            ]
    else:  # We have a current position, so check if we should exit
        if (curr_pos > 0 and signal_value == -1) or (curr_pos < 0
                                                     and signal_value == 1):
            order_qty = -curr_pos
            reason_code = pq.ReasonCode.EXIT_LONG if order_qty < 0 else pq.ReasonCode.EXIT_SHORT
            return [
                pq.MarketOrder(symbol,
                               date,
                               order_qty,
                               reason_code=reason_code)
            ]
    return []