def queryTimeInForce(self): print("1) Day\n2) IOC\n3) OPG\n4) GTC\n5) GTX") value = input("TimeInForce: ") if value == '1': return fix.TimeInForce(fix.TimeInForce_DAY) elif value == '2': return fix.TimeInForce(fix.TimeInForce_IMMEDIATE_OR_CANCEL) elif value == '3': return fix.TimeInForce(fix.TimeInForce_AT_THE_OPENING) elif value == '4': return fix.TimeInForce(fix.TimeInForce_GOOD_TILL_CANCEL) elif value == '5': return fix.TimeInForce(fix.TimeInForce_GOOD_TILL_CROSSING)
def makeOrder(self, snapshot): self.logger.info("FIXSIM-CLIENT Snapshot received %s", str(snapshot)) quote = snapshot.getRandomQuote() self.logger.info("FIXSIM-CLIENT make order for quote %s", str(quote)) order = self.fixVersion.NewOrderSingle() order.setField( quickfix.HandlInst( quickfix. HandlInst_AUTOMATED_EXECUTION_ORDER_PUBLIC_BROKER_INTERVENTION_OK )) order.setField( quickfix.SecurityType( quickfix.SecurityType_FOREIGN_EXCHANGE_CONTRACT)) order.setField(quickfix.OrdType(quickfix.OrdType_PREVIOUSLY_QUOTED)) order.setField(quickfix.ClOrdID(self.idGen.orderID())) order.setField(quickfix.QuoteID(quote.id)) order.setField(quickfix.SecurityDesc("SPOT")) order.setField(quickfix.Symbol(snapshot.symbol)) order.setField(quickfix.Currency(quote.currency)) order.setField(quickfix.Side(quote.side)) order.setField(quickfix.OrderQty(quote.size)) order.setField(quickfix.FutSettDate("SP")) order.setField(quickfix.Price(quote.price)) order.setField(quickfix.TransactTime()) order.setField( quickfix.TimeInForce(quickfix.TimeInForce_IMMEDIATE_OR_CANCEL)) self.sendToTarget(order, self.orderSession)
def put_new_order(self, instrument, side, price, size): """Request sample new order single""" message = quickfix44.NewOrderSingle() header = message.getHeader() print("Executing : " + str(instrument) + ":" + str(side) + ":" + str(price) + ":" + str(size)) message.setField(quickfix.ClOrdID(self.genExecID())) message.setField(quickfix.Side(side)) message.setField(quickfix.Symbol(instrument.symbol)) message.setField(quickfix.SecurityExchange(instrument.exchange)) message.setField(quickfix.OrderQty(size)) message.setField(quickfix.Price(int(price))) message.setField(quickfix.OrdType(quickfix.OrdType_LIMIT)) message.setField(quickfix.TimeInForce('0')) message.setField(quickfix.Text("NewOrderSingle")) trstime = quickfix.TransactTime() trstime.setString( datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")[:-3]) message.setField(trstime) msg = message.toString().replace(__SOH__, "|") print("New Order Single: " + msg) quickfix.Session.sendToTarget(message, self.sessionID)
def sendOrder(self, ticker, side, type, px, qty): nos = fix.Message() nos.getHeader().setField(fix.BeginString(fix.BeginString_FIX44)) nos.getHeader().setField(fix.MsgType(fix.MsgType_NewOrderSingle)) symbol = fix.Symbol(ticker) nos.setField(symbol) if side == "BUY": side = fix.Side(fix.Side_BUY) if side == "SELL": side = fix.Side(fix.Side_SELL) nos.setField(side) if type == "MARKET": ordType = fix.OrdType(fix.OrdType_MARKET) px = fix.Price(0) if type == "LIMIT": ordType = fix.OrdType(fix.OrdType_MARKET) px = fix.Price(px) nos.setField(ordType) nos.setField(px) orderQty = fix.OrderQty(qty) clOrdID = fix.ClOrdID(self.genOrderID()) nos.setField(orderQty) nos.setField(clOrdID) TimeInForce = fix.TimeInForce(fix.TimeInForce_GOOD_TILL_CANCEL) TransactTime = fix.TransactTime() nos.setField(TimeInForce) nos.setField(TransactTime) fix.Session.sendToTarget(nos, self.sessionID)
def send_replace(self, order): order.status = OrdStatus.PENDING_REPLACE message = fix.Message() message.getHeader().setField( fix.MsgType(fix.MsgType_OrderCancelReplaceRequest)) message.setField(fix.Symbol(order.symbol)) message.setField(fix.Side(order.side)) message.setField(fix.OrderQty(order.qty)) if order.type is not OrderType.MARKET: message.setField(fix.Price(order.price)) message.setField(fix.OrdType(order.type)) message.setField(fix.TimeInForce(order.time_in_force)) cl_ord_id = self.order_store.generate_next_cl_ord_id(order.order_id) message.setField(fix.ClOrdID(cl_ord_id)) self.order_store.update_order_maps(cl_ord_id, order) self._send_message(message)
def send_new(self, order): order.status = OrdStatus.PENDING_NEW message = fix.Message() message.getHeader().setField(fix.MsgType(fix.MsgType_NewOrderSingle)) message.setField(fix.Symbol(order.symbol)) message.setField(fix.Side(order.side)) message.setField(fix.OrderQty(order.qty)) if order.type is not OrderType.MARKET: message.setField(fix.Price(order.price)) message.setField(fix.Currency(order.currency)) message.setField(fix.OrdType(order.type)) message.setField(fix.TimeInForce(order.time_in_force)) cl_ord_id = self.order_store.generate_new_cl_ord_id(order.order_id) message.setField(fix.ClOrdID(cl_ord_id)) self.order_store.update_order_maps(cl_ord_id, order) self._send_message(message)
def put_new_order(self): """Request sample new order single""" message = fix.Message() header = message.getHeader() header.setField(fix.MsgType(fix.MsgType_NewOrderSingle)) #39 = D message.setField(fix.ClOrdID( self.genExecID())) #11 = Unique Sequence Number message.setField(fix.Side(fix.Side_BUY)) #43 = 1 BUY message.setField(fix.Symbol("MSFT")) #55 = MSFT message.setField(fix.OrderQty(10000)) #38 = 1000 message.setField(fix.Price(100)) message.setField(fix.OrdType(fix.OrdType_LIMIT)) #40=2 Limit Order message.setField( fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION)) #21 = 3 message.setField(fix.TimeInForce('0')) message.setField(fix.Text("NewOrderSingle")) trstime = fix.TransactTime() trstime.setString( datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")[:-3]) message.setField(trstime) fix.Session.sendToTarget(message, self.sessionID)
class Application(fix.Application): orderID = 0 execID = 0 bids = {} asks = {} #creates empty dictionaries loc = ("C:\\Bloomberg\\SampleMarketData.xlsx") wb = xlrd.open_workbook(loc) sheetMSFT = wb.sheet_by_index(0) sheetNFLX = wb.sheet_by_index(1) sheetNames = wb.sheet_names() xl_sheet1 = wb.sheet_by_index(0) xl_sheet2 = wb.sheet_by_index(1) sheet1Name = xl_sheet1.name.encode("utf-8") sheet2Name = xl_sheet2.name.encode("utf-8") excelbids = {} excelbids['BATS'] = {} excelbids['BATS'][sheet1Name] = {} excelbids['BATS'][sheet2Name] = {} excelbids['NASD'] = {} excelbids['NASD'][sheet1Name] = {} excelbids['NASD'][sheet2Name] = {} excelasks = {} excelasks['BATS'] = {} excelasks['BATS'][sheet1Name] = {} excelasks['BATS'][sheet2Name] = {} excelasks['NASD'] = {} excelasks['NASD'][sheet1Name] = {} excelasks['NASD'][sheet2Name] = {} for i in range (2, 7): for j in range (0, 1): excelbids['NASD'][sheet1Name][sheetMSFT.cell_value(i, j)] = sheetMSFT.cell_value(i, j+1) for i in range (2, 7): for j in range (2, 3): excelasks['NASD'][sheet1Name][sheetMSFT.cell_value(i, j)] = sheetMSFT.cell_value(i, j+1) for i in range (9, 14): for j in range (0, 1): excelbids['BATS'][sheet1Name][sheetMSFT.cell_value(i, j)] = sheetMSFT.cell_value(i, j+1) for i in range (9, 14): for j in range (2, 3): excelasks['BATS'][sheet1Name][sheetMSFT.cell_value(i, j)] = sheetMSFT.cell_value(i, j+1) for i in range (2, 7): for j in range (0, 1): excelbids['NASD'][sheet2Name][sheetNFLX.cell_value(i, j)] = sheetNFLX.cell_value(i, j+1) for i in range (2, 7): for j in range (2, 3): excelasks['NASD'][sheet2Name][sheetNFLX.cell_value(i, j)] = sheetNFLX.cell_value(i, j+1) for i in range (9, 14): for j in range (0, 1): excelbids['BATS'][sheet2Name][sheetNFLX.cell_value(i, j)] = sheetNFLX.cell_value(i, j+1) for i in range (9, 14): for j in range (2, 3): excelasks['BATS'][sheet2Name][sheetNFLX.cell_value(i, j)] = sheetNFLX.cell_value(i, j+1) #reads excel files, inputs data into nested dictionaries @echo def onCreate(self, sessionID): return @echo def onLogon(self, sessionID): return @echo def onLogout(self, sessionID): return @echo def toAdmin(self, sessionID, message): return @echo def fromAdmin(self, sessionID, message): return @echo def toApp(self, sessionID, message): return @echo def fromApp(self, message, sessionID): beginString = fix.BeginString() msgType = fix.MsgType() message.getHeader().getField( beginString ) message.getHeader().getField( msgType ) if msgType.getValue() == 'F': print 'Cancel Request' symbol = fix.Symbol() side = fix.Side() clOrdID = fix.ClOrdID() origClOrdID = fix.OrigClOrdID() orderQty = fix.OrderQty() message.getField( orderQty ) message.getField( clOrdID ) message.getField( origClOrdID ) message.getField( symbol ) message.getField( side ) executionReport = fix.Message() executionReport.getHeader().setField( beginString ) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport) ) executionReport.setField( symbol ) executionReport.setField( side ) executionReport.setField( clOrdID ) executionReport.setField( origClOrdID ) executionReport.setField( fix.ExecID(self.genExecID()) ) executionReport.setField( fix.OrderID(self.genOrderID()) ) executionReport.setField( fix.AvgPx(0)) executionReport.setField( fix.LastShares(0)) executionReport.setField( fix.CumQty(0)) if beginString.getValue() == fix.BeginString_FIX40 or beginString.getValue() == fix.BeginString_FIX41 or beginString.getValue() == fix.BeginString_FIX42: executionReport.setField( fix.ExecTransType(fix.ExecTransType_CANCEL) ) executionReport.setField( fix.OrdStatus(fix.OrdStatus_CANCELED) ) if beginString.getValue() >= fix.BeginString_FIX41: executionReport.setField( fix.ExecType(fix.ExecType_CANCELED) ) executionReport.setField( fix.LeavesQty(0) ) else: executionReport.setField( fix.ExecType(fix.ExecType_CANCELED) ) executionReport.setField( fix.LeavesQty(0) ) try: print executionReport fix.Session.sendToTarget( executionReport, sessionID ) except SessionNotFound, e: return if msgType.getValue() == 'D': print 'New Order Single' symbol = fix.Symbol() side = fix.Side() ordType = fix.OrdType() orderQty = fix.OrderQty() price = fix.Price() timeInForce = fix.TimeInForce() clOrdID = fix.ClOrdID() message.getField( ordType ) print message if ordType.getValue() == fix.OrdType_LIMIT: message.getField( price ) price = round(float(price.getValue()),2) message.getField( symbol ) message.getField( side ) message.getField( orderQty ) message.getField( clOrdID ) message.getField( timeInForce ) executionReport = fix.Message() executionReport.getHeader().setField( beginString ) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport) ) availableQty = 0 if side.getValue() == '2': #check the bids sym = symbol.getValue() mergedDict = sorted(set(self.excelbids['BATS'][sym].keys()) | set(self.excelbids['NASD'][sym].keys()), reverse = True) orderMergedDict = OrderedDict() for pricelevel in mergedDict: orderMergedDict[pricelevel] = OrderedDict() if pricelevel in self.excelbids['BATS'][sym].keys(): orderMergedDict[pricelevel]['BATS'] = self.excelbids['BATS'][sym][pricelevel] if pricelevel in self.excelbids['NASD'][sym].keys(): orderMergedDict[pricelevel]['NASD'] = self.excelbids['NASD'][sym][pricelevel] print '-------------Sell Order - Checking available Bids-------------' qty = orderQty.getValue() orderToSend = {} for bid_price, exch in orderMergedDict.iteritems(): if price <= bid_price or ordType.getValue() == fix.OrdType_MARKET : for exchange,bid_qty in exch.iteritems(): if exchange not in orderToSend: orderToSend[exchange] = {} if bid_qty >= qty and qty > 0: orderToSend[exchange][bid_price] = qty qty = 0 elif qty > 0: orderToSend[exchange][bid_price] = bid_qty qty = qty - bid_qty availableQty += bid_qty else: #check the asks sym = symbol.getValue() mergedDict = sorted(set(self.excelasks['BATS'][sym].keys()) | set(self.excelasks['NASD'][sym].keys())) orderMergedDict = OrderedDict() for pricelevel in mergedDict: orderMergedDict[pricelevel] = OrderedDict() if pricelevel in self.excelasks['BATS'][sym].keys(): orderMergedDict[pricelevel]['BATS'] = self.excelasks['BATS'][sym][pricelevel] if pricelevel in self.excelasks['NASD'][sym].keys(): orderMergedDict[pricelevel]['NASD'] = self.excelasks['NASD'][sym][pricelevel] print '--------------Buy Order - Checking available Asks-------------' qty = orderQty.getValue() orderToSend = {} for ask_price, exch in orderMergedDict.iteritems(): #if ask_price <= round(float(price.getValue()),2): if ask_price <= price or ordType.getValue() == fix.OrdType_MARKET: for exchange,ask_qty in exch.iteritems(): if exchange not in orderToSend: orderToSend[exchange] = {} if ask_qty >= qty and qty > 0: orderToSend[exchange][ask_price] = qty qty = 0 elif qty > 0: orderToSend[exchange][ask_price] = ask_qty qty = qty - ask_qty availableQty += ask_qty filledQty = 0 if len(orderToSend) > 0: for exchange, detailsOfOrders in orderToSend.iteritems(): for price, qty in detailsOfOrders.iteritems(): filledQty = filledQty + qty executionReport.setField( fix.OrderID(self.genOrderID()) ) executionReport.setField( fix.ExecID(self.genExecID()) ) if filledQty >= orderQty.getValue(): executionReport.setField( fix.OrdStatus(fix.OrdStatus_FILLED) ) else: executionReport.setField( fix.OrdStatus(fix.OrdStatus_PARTIALLY_FILLED) ) executionReport.setField( symbol ) executionReport.setField( side ) executionReport.setField( fix.CumQty(filledQty)) executionReport.setField( fix.AvgPx(price )) # to do - need to properly calculate average price executionReport.setField( fix.LastShares(qty)) executionReport.setField( fix.LastPx(price)) executionReport.setField( clOrdID ) executionReport.setField( orderQty ) executionReport.setField( fix.LastMkt(exchange)) if beginString.getValue() == fix.BeginString_FIX40 or beginString.getValue() == fix.BeginString_FIX41 or beginString.getValue() == fix.BeginString_FIX42: executionReport.setField( fix.ExecTransType(fix.ExecTransType_NEW) ) if beginString.getValue() >= fix.BeginString_FIX41: if filledQty >=0 : executionReport.setField( fix.ExecType(fix.ExecType_FILL) ) executionReport.setField( fix.LeavesQty(0) ) else: executionReport.setField( fix.ExecType(fix.ExecType_PARTIAL_FILL) ) executionReport.setField( fix.LeavesQty(orderQty.getValue() - filledQty) ) try: print executionReport fix.Session.sendToTarget( executionReport, sessionID ) except SessionNotFound, e: return if timeInForce.getValue() == fix.TimeInForce_IMMEDIATE_OR_CANCEL: print "TimeInForce is IOC - need to cancel the remaining balance" executionReport = fix.Message() executionReport.getHeader().setField( beginString ) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport) ) executionReport.setField( symbol ) executionReport.setField( side ) executionReport.setField( clOrdID ) executionReport.setField( fix.ExecID(self.genExecID()) ) executionReport.setField( fix.OrderID(self.genOrderID()) ) executionReport.setField( fix.AvgPx(price)) executionReport.setField( fix.LastShares(0)) executionReport.setField( fix.CumQty(filledQty)) executionReport.setField( fix.LastPx(price)) executionReport.setField( orderQty ) if beginString.getValue() == fix.BeginString_FIX40 or beginString.getValue() == fix.BeginString_FIX41 or beginString.getValue() == fix.BeginString_FIX42: executionReport.setField( fix.ExecTransType(fix.ExecTransType_CANCEL) ) executionReport.setField( fix.OrdStatus(fix.OrdStatus_CANCELED) ) if beginString.getValue() >= fix.BeginString_FIX41: executionReport.setField( fix.ExecType(fix.ExecType_CANCELED) ) executionReport.setField( fix.LeavesQty(0) ) else: executionReport.setField( fix.ExecType(fix.ExecType_CANCELED) ) executionReport.setField( fix.LeavesQty(0) ) try: print executionReport fix.Session.sendToTarget( executionReport, sessionID ) except SessionNotFound, e: return