Пример #1
0
    def createStopLimitOrder(self, action, instrument, stopPrice, limitPrice, quantity):
        #IB doesn't support buy to cover
        if action == broker.Order.Action.BUY_TO_COVER:
            action = broker.Order.Action.BUY

        instrumentTraits = self.getInstrumentTraits(instrument)

        return broker.StopLimitOrder(action, instrument, stopPrice,limitPrice, quantity, instrumentTraits)        
Пример #2
0
 def createStopLimitOrder(self,
                          action,
                          instrument,
                          stopPrice,
                          limitPrice,
                          quantity,
                          extra={}):
     instrumentTraits = self.getInstrumentTraits(instrument)
     quantity = instrumentTraits.roundQuantity(quantity)
     return broker.StopLimitOrder(action,
                                  instrument,
                                  stopPrice,
                                  limitPrice,
                                  quantity,
                                  instrumentTraits,
                                  extra=extra)
Пример #3
0
def build_order_from_open_order(openOrder, instrumentTraits):
    #order_id = openOrder.order.m_permId   #we use the TWS id for the order rather than our id - not sure this is a good idea but its apparently consistent across sessions - https://www.interactivebrokers.com/en/software/api/apiguide/java/order.htm
    order_id = openOrder.order.m_orderId
    #doesn't seem to be a useable date/time for orders so going to use current time
    order_time = dt.as_utc(datetime.datetime.now())

    order_type = openOrder.order.m_orderType  #stop, limit, stoplimit, market

    order_action = openOrder.order.m_action

    order_amount = openOrder.order.m_totalQuantity
    order_limprice = openOrder.order.m_lmtPrice
    order_auxprice = openOrder.order.m_auxPrice
    contract_symbol = openOrder.contract.m_symbol

    if order_action == 'BUY':
        action = broker.Order.Action.BUY
    elif order_action == 'SELL':
        action = broker.Order.Action.SELL
    elif order_action == 'SSHORT':
        action = broker.Order.Action.SELL_SHORT
    else:
        raise Exception("Invalid order action")

    if order_type == 'LMT':  #Limit
        ret = broker.LimitOrder(action, contract_symbol, order_limprice,
                                order_amount, instrumentTraits)
    elif order_type == 'MKT':  #Market
        ret = broker.MarketOrder(action, contract_symbol, order_amount, False,
                                 instrumentTraits)
    elif order_type == 'MOC':  #Market On Close
        ret = broker.MarketOrder(action, contract_symbol, order_amount, True,
                                 instrumentTraits)
    elif order_type == 'STP':  #Stop order
        ret = broker.StopOrder(action, contract_symbol, order_auxprice,
                               order_amount, instrumentTraits)
    elif order_type == 'STP LMT':
        ret = broker.StopLimitOrder(action, contract_symbol, order_auxprice,
                                    order_limprice, order_amount,
                                    instrumentTraits)
    else:
        #Totally possible if you use pyalgotrade and TWS to manage the same account which is not really a good idea
        raise Exception("Unsupported order type - %s" % order_type)

    ret.setSubmitted(order_id, order_time)
    ret.setState(broker.Order.State.ACCEPTED)
    return ret
Пример #4
0
def build_order_from_open_order(openOrder,
                                instrumentTraits,
                                state=broker.Order.State.ACCEPTED):
    if openOrder.isBuy():
        if 'Close' in openOrder.getData().get(
                'execInst', '') or openOrder.getExtra().get('close'):
            action = broker.Order.Action.BUY_TO_COVER
        else:
            action = broker.Order.Action.BUY
    elif openOrder.isSell():
        if 'Close' in openOrder.getData().get(
                'execInst', '') or openOrder.getExtra().get('close'):
            action = broker.Order.Action.SELL
        else:
            action = broker.Order.Action.SELL_SHORT
    else:
        raise Exception("Invalid order side")

    if openOrder.getType() in [
            'Market', 'MarketIfTouched', 'MarketWithLeftOverAsLimit'
    ]:
        ret = broker.MarketOrder(action, common.btc_symbol,
                                 openOrder.getAmount(), True, instrumentTraits,
                                 openOrder.getExtra())
    elif openOrder.getType() in ['Limit', 'LimitIfTouched']:
        ret = broker.LimitOrder(action,
                                common.btc_symbol, openOrder.getPrice(),
                                openOrder.getAmount(), instrumentTraits,
                                openOrder.getExtra())
    elif openOrder.getType() == 'StopLimit':
        ret = broker.StopLimitOrder(action, common.btc_symbol,
                                    openOrder.getStopPrice(),
                                    openOrder.getPrice(),
                                    openOrder.getAmount(), instrumentTraits,
                                    openOrder.getExtra())
    elif openOrder.getType() == 'Stop':
        ret = broker.StopOrder(action, common.btc_symbol,
                               openOrder.getStopPrice(), openOrder.getAmount(),
                               instrumentTraits, openOrder.getExtra())
    else:
        raise Exception("Invalid order type")

    ret.setSubmitted(openOrder.getId(), openOrder.getDateTime())
    ret.setState(state)
    return ret