Esempio n. 1
0
    def createLimitOrder(self,
                         action,
                         instrument,
                         limitPrice,
                         quantity,
                         extra={}):
        if instrument != common.btc_symbol:
            raise Exception("Only BTC instrument is supported")

        if action == broker.Order.Action.BUY_TO_COVER:
            action = broker.Order.Action.BUY
        elif action == broker.Order.Action.SELL_SHORT:
            action = broker.Order.Action.SELL

        if action not in [broker.Order.Action.BUY, broker.Order.Action.SELL]:
            raise Exception("Only BUY/SELL orders are supported")

        instrumentTraits = self.getInstrumentTraits(instrument)
        limitPrice = round(limitPrice, 2)
        quantity = instrumentTraits.roundQuantity(quantity)
        return broker.LimitOrder(action,
                                 instrument,
                                 limitPrice,
                                 quantity,
                                 instrumentTraits,
                                 extra=extra)
Esempio n. 2
0
 def __buildAcceptedLimitOrder(self, action, limitPrice, quantity):
     ret = broker.LimitOrder(action, "orcl", limitPrice, quantity, DefaultTraits())
     self.assertEquals(ret.getSubmitDateTime(), None)
     ret.switchState(broker.Order.State.SUBMITTED)
     ret.setSubmitted(1, datetime.datetime.now())
     self.assertNotEquals(ret.getSubmitDateTime(), None)
     ret.switchState(broker.Order.State.ACCEPTED)
     return ret
Esempio n. 3
0
    def createLimitOrder(self, action, instrument, 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.LimitOrder(action, instrument, limitPrice, quantity, instrumentTraits)
Esempio n. 4
0
def build_order_from_open_order(openOrder, instrumentTraits):
    if openOrder.isBuy():
        action = broker.Order.Action.BUY
    elif openOrder.isSell():
        action = broker.Order.Action.SELL
    else:
        raise Exception("Invalid order type")

    ret = broker.LimitOrder(action, common.btc_symbol, openOrder.getPrice(), openOrder.getAmount(), instrumentTraits)
    ret.setSubmitted(openOrder.getId(), openOrder.getDateTime())
    ret.setState(broker.Order.State.ACCEPTED)
    return ret
Esempio n. 5
0
 def createLimitOrder(self,
                      action,
                      instrument,
                      limitPrice,
                      quantity,
                      extra={}):
     instrumentTraits = self.getInstrumentTraits(instrument)
     quantity = instrumentTraits.roundQuantity(quantity)
     return broker.LimitOrder(action,
                              instrument,
                              limitPrice,
                              quantity,
                              instrumentTraits,
                              extra=extra)
Esempio n. 6
0
    def createLimitOrder(self, action, instrument, limitPrice, quantity):
        if instrument != common.CoinSymbol.BTC:
            raise Exception('仅支持 BTC 交易')

        if action == broker.Order.Action.BUY_TO_COVER:
            action = broker.Order.Action.BUY
        elif action == broker.Order.Action.SELL_SHORT:
            action = broker.Order.Action.SELL

        if action not in [broker.Order.Action.BUY, broker.Order.Action.SELL]:
            raise Exception('仅支持 买/卖 交易')

        limitPrice = round(limitPrice, 2)
        return broker.LimitOrder(action, instrument, limitPrice, quantity,
                                 common.BTCTraits())
Esempio n. 7
0
def build_order_from_open_order(openOrder, instrumentTraits):
    if openOrder.is_buy():
        action = broker.Order.Action.BUY
    elif openOrder.is_sell():
        action = broker.Order.Action.SELL
    else:
        raise Exception("Invalid order type")

    ret = broker.LimitOrder(action,
                            common.CoinSymbol.BTC, openOrder.get_price(),
                            openOrder.get_amount(), instrumentTraits)
    ret.setSubmitted(openOrder.get_id(), openOrder.get_datetime())
    ret.setState(broker.Order.State.ACCEPTED)

    return ret
Esempio n. 8
0
    def createLimitOrder(self, action, instrument, limitPrice, quantity):
        if instrument != common.INSTRUMENT_TOKEN:
            raise Exception("Only BTC instrument is supported")

        if action == broker.Order.Action.BUY_TO_COVER:
            action = broker.Order.Action.BUY
        elif action == broker.Order.Action.SELL_SHORT:
            action = broker.Order.Action.SELL

        if action not in [broker.Order.Action.BUY, broker.Order.Action.SELL]:
            raise Exception("Only BUY/SELL orders are supported")

        instrumentTraits = self.getInstrumentTraits(instrument)
        limitPrice = round(limitPrice, common.CASH_TOKEN_PRECISION)
        quantity = instrumentTraits.roundQuantity(quantity)
        return broker.LimitOrder(action, instrument, limitPrice, quantity, instrumentTraits)
Esempio n. 9
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
Esempio n. 10
0
    def createLimitOrder(self, action, instrument, limitPrice, quantity):
        if instrument not in common.supported_symbols:
            raise Exception("Unknown instrument.")

        if action == broker.Order.Action.BUY_TO_COVER:
            action = broker.Order.Action.BUY
        elif action == broker.Order.Action.SELL_SHORT:
            action = broker.Order.Action.SELL

        if action not in [broker.Order.Action.BUY, broker.Order.Action.SELL]:
            raise Exception("Only BUY/SELL orders are supported")

        instrumentTraits = self.getInstrumentTraits(instrument)
        limitPrice = round(limitPrice, 2)
        quantity = instrumentTraits.roundQuantity(instrument, quantity)
        return broker.LimitOrder(action, instrument, limitPrice, quantity,
                                 instrumentTraits)
Esempio n. 11
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
Esempio n. 12
0
    def createLimitOrder(self, action, instrument, limitPrice, quantity):
        instrument = build_instrument(instrument)

        if instrument not in common.SUPPORTED_INSTRUMENTS:
            raise Exception("Unsupported instrument %s" % instrument)

        if action == broker.Order.Action.BUY_TO_COVER:
            action = broker.Order.Action.BUY
        elif action == broker.Order.Action.SELL_SHORT:
            action = broker.Order.Action.SELL

        if action not in [broker.Order.Action.BUY, broker.Order.Action.SELL]:
            raise Exception("Only BUY/SELL orders are supported")

        instrumentTraits = self.getInstrumentTraits()
        limitPrice = instrumentTraits.round(limitPrice,
                                            instrument.priceCurrency)
        quantity = instrumentTraits.round(quantity, instrument.symbol)
        return broker.LimitOrder(action, instrument, limitPrice, quantity,
                                 instrumentTraits)
Esempio n. 13
0
 def createLimitOrder(self, action, instrument, limitPrice, quantity):
     instrumentTraits = self.getInstrumentTraits()
     limitPrice = round(limitPrice, 2)
     quantity = instrumentTraits.roundQuantity(quantity)
     return broker.LimitOrder(action, instrument, limitPrice, quantity, instrumentTraits)
Esempio n. 14
0
 def __buildAcceptedLimitOrder(self, action, limitPrice, quantity):
     ret = broker.LimitOrder(1, action, "orcl", limitPrice, quantity)
     ret.switchState(broker.Order.State.SUBMITTED)
     ret.switchState(broker.Order.State.ACCEPTED)
     return ret