def run(self): # Simple contract for GOOG contract = Contract() #contract.conId = 114376112 contract.exchange = "SMART" contract.symbol = "ATK" contract.secType = "STK" #contract.right = "PUT" contract.currency = "USD" #contract.secType = 'OPT' #contract.strike = 24 #contract.expiry = '20121116' today = datetime.today() order = Order() order.orderId = 89 order.clientId = 44 order.action = "BUY" order.totalQuantity = 1 # may have to calculate a smarter number order.orderType = "MKT" order.tif = "DAY" order.transmit = True order.sweepToFill = True order.outsideRth = True contract.symbol = "alkjdf" self.callback.askPrice = None self.tws.reqMktData(2, contract, "", 1) max_wait = timedelta(seconds=30) + datetime.now() while self.callback.askPrice is None: if datetime.now() > max_wait: print "max wait giving up" break print self.callback.askPrice
def get_market_order(self, sid, amt): order = Order() order.action = self.order_action(amt) order.totalQuantity = abs(amt) order.orderType = 'MKT' order.tif = 'DAY' order.outsideRth = False return order
def get_limit_order(self, sid, amt, lmtPrice): order = Order() order.action = self.order_action(amt) order.totalQuantity = abs(amt) order.orderType = 'LMT' order.tif = 'DAY' order.outsideRth = False order.lmtPrice = lmtPrice return order
def createOrderBuy(self): order = Order() order.orderId = self.orderIdCount self.orderIdCount += 1 order.clientId = self.socketnum order.action = "BUY" order.orderType = "MKT" order.tif = "DAY" order.transmit = True order.sweepToFill = True order.outsideRth = True return order
def place_order(self, contract): print('Waiting for valid order id') order_id = self.order_ids.get(timeout=WAIT_TIME) if not order_id: raise RuntimeError('Failed to receive order id after %ds' % WAIT_TIME) # Order details algoParams = TagValueList() algoParams.append(TagValue("componentSize", "3")) algoParams.append(TagValue("timeBetweenOrders", "60")) algoParams.append(TagValue("randomizeTime20", "1")) algoParams.append(TagValue("randomizeSize55", "1")) algoParams.append(TagValue("giveUp", "1")) algoParams.append(TagValue("catchUp", "1")) algoParams.append(TagValue("waitForFill", "1")) algoParams.append(TagValue("startTime", "20110302-14:30:00 GMT")) algoParams.append(TagValue("endTime", "20110302-21:00:00 GMT")) order = Order() order.action = 'BUY' order.lmtPrice = 140 order.orderType = 'LMT' order.totalQuantity = 10 order.algoStrategy = "AD" order.tif = 'DAT' order.algoParams = algoParams # order.transmit = False print("Placing order for %d %s's (id: %d)" % (order.totalQuantity, contract.symbol, order_id)) # Place the order self.tws.placeOrder( order_id, # orderId, contract, # contract, order # order ) print("\n====================================================================") print(" Order placed, waiting %ds for TWS responses" % WAIT_TIME) print("====================================================================\n") print("Waiting for order to be filled..") try: self.order_filled.wait(WAIT_TIME) except KeyboardInterrupt: pass finally: if not self.order_filled.is_set(): print('Failed to fill order')
def execute_order(self, order): contract = self._ewrapper.symbol_contract_dict[order.symbol] ib_order = Order() ib_order.orderType = order.type ib_order.action = order.direction ib_order.totalQuantity = order.quantity ib_order.transmit = True ib_order.tif = 'DAY' if order.type == 'LMT': ib_order.lmtPrice = order.limit_price self.placeOrder(self._ewrapper.next_valid_order_id, contract, ib_order) self._ewrapper.order_dict[ self._ewrapper.next_valid_order_id] = ib_order self._ewrapper.next_valid_order_id += 1
# Simple contract for GOOG contract = Contract() contract.exchange = "SMART" 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 )