Ejemplo n.º 1
0
	def place_new_IB_order(self, ibcontract, trade, px, orderType, orderid=None, startTime=None, endTime=None):
		"""
		Places an order

		Returns brokerorderid

		raises exception if fails
		"""
		iborder = IBOrder()
		iborder.action = bs_resolve(trade)
		
		if orderType == 'STOP':
			iborder.auxPrice = px
		else:
			iborder.lmtPrice = px
		
		iborder.orderType = orderType
		iborder.totalQuantity = abs(trade)
		iborder.tif='DAY'
		iborder.transmit=True

		#compute time zone info
		# ib doesn't work if you have startTime and endTime
		# if startTime:
		# 	startTime_tz = local_tz.localize(startTime)
		# 	startTime_est = startTime_tz.astimezone(EST)
		# 	iborder.goodAfterTime = startTime_est.strftime("%Y%m%d %H:%M:%S EST")

		if endTime and orderType is not 'MKT':
			print 'here'
			iborder.tif = 'GTD'
			endTime_tz = local_tz.localize(endTime)
			endTime_est = endTime_tz.astimezone(EST)

			iborder.goodTillDate = endTime_est.strftime("%Y%m%d %H:%M:%S EST")

		## We can eithier supply our own ID or ask IB to give us the next valid one
		if orderid is None:
			print "Getting orderid from IB"
			orderid=self.get_next_brokerorderid()
			
		print "Using order id of %d" % orderid
	
		 # Place the order
		self.tws.placeOrder(
				orderid,									# orderId,
				ibcontract,								   # contract,
				iborder									   # order
			)
	
		return orderid
Ejemplo n.º 2
0
contract.symbol = "DELL"
contract.secType = "STK"
contract.currency = "USD"

if orderId is None:
    print 'Waiting for valid order id'
    sleep(1)
    while orderId is None:
        print 'Still waiting for valid order id...'
        sleep(1)

# Order details
order = Order()
order.action = 'BUY'
order.lmtPrice = 0
order.auxPrice = 0
order.orderType = 'MTL'
order.totalQuantity = 1

print "Placing order for %d %s's (id: %d)" % (order.totalQuantity,
                                              contract.symbol, orderId)

# Place the order
tws.placeOrder(
    orderId,  # orderId,
    contract,  # contract,
    order  # order
)

print "\n====================================================================="
print " Order placed, waiting for TWS responses"
Ejemplo n.º 3
0
contract.symbol = "DELL"
contract.secType = "STK"
contract.currency = "USD"

if orderId is None:
    print 'Waiting for valid order id'
    sleep(1)
    while orderId is None:
        print 'Still waiting for valid order id...'
        sleep(1)

# Order details
order = Order()
order.action = 'BUY'
order.lmtPrice = 0
order.auxPrice = 0
order.orderType = 'MTL'
order.totalQuantity = 1


print "Placing order for %d %s's (id: %d)" % (order.totalQuantity,
        contract.symbol, orderId)

# Place the order
tws.placeOrder(
        orderId,                                    # orderId,
        contract,                                   # contract,
        order                                       # order
    )

print "\n====================================================================="
Ejemplo n.º 4
0
    def place_new_IB_order(self,
                           ibcontract,
                           trade,
                           lmtPrice,
                           orderType,
                           orderid=None,
                           stopPrice=None,
                           trailStopPrice=None):
        """
        Places an order
        
        Returns brokerorderid
    
        raises exception if fails
        """
        iborder = IBOrder()
        iborder.account = self.accountid
        iborder.action = bs_resolve(trade)

        self.logger.debug(
            "Placing an order for %s:%s:%s" %
            (ibcontract.exchange, ibcontract.symbol, ibcontract.secType))

        #if orderType == "LMT":
        #    iborder.lmtPrice = lmtPrice

        iborder.orderType = orderType
        iborder.totalQuantity = abs(trade)
        iborder.tif = 'GTC'
        iborder.transmit = False

        ## We can eithier supply our own ID or ask IB to give us the next valid one
        if orderid is None:
            #print "Getting orderid from IB"
            orderid = self.get_next_brokerorderid()

        if stopPrice:
            stoporder = IBOrder()
            stoporder.account = self.accountid
            stoporder.action = bs_resolve((-1) * trade)
            stoporder.orderType = "STP"
            stoporder.totalQuantity = abs(trade)
            stoporder.tif = 'GTC'
            stoporder.transmit = False
            stoporder.auxPrice = stopPrice
            stoporder.parentId = orderid
            self.logger.debug("Setup StopPrice %s" % stoporder.auxPrice)

        if trailStopPrice:
            limitorder = IBOrder()
            limitorder.account = self.accountid
            limitorder.action = bs_resolve((-1) * trade)
            limitorder.orderType = "LMT"
            limitorder.totalQuantity = abs(trade)
            limitorder.tif = 'GTC'
            limitorder.transmit = False
            limitorder.lmtPrice = trailStopPrice
            limitorder.parentId = orderid
            self.logger.debug("Setup TrailPrice %s" % limitorder.lmtPrice)

        # Place the order
        #self.tws.placeOrder( orderid, ibcontract,iborder )

        if stopPrice and trailStopPrice:
            #limitorder.transmit=True
            self.logger.debug("Place Order Stop and TrailPrice")
            self.tws.placeOrder(orderid, ibcontract, iborder)
            self.tws.placeOrder(orderid + 1, ibcontract, stoporder)
            self.tws.placeOrder(orderid + 2, ibcontract, limitorder)
        elif stopPrice:
            #stoporder.transmit=True
            self.logger.debug("Place Order Stop")
            self.tws.placeOrder(orderid, ibcontract, iborder)
            self.tws.placeOrder(orderid + 1, ibcontract, stoporder)
        elif trailStopPrice:
            #limitorder.transmit=True
            self.logger.debug("Place Order trailStop")
            self.tws.placeOrder(orderid, ibcontract, iborder)
            self.tws.placeOrder(orderid + 2, ibcontract, limitorder)
        else:
            #iborder.transmit=True
            self.logger.debug("Place Single Order")
            self.tws.placeOrder(orderid, ibcontract, iborder)

        return orderid