def new_order(sender_comp_id, target_comp_id, symbol, quantity, price, side, order_type): if side.lower() == "buy": side = fix.Side_BUY else: side = fix.Side_SELL message = Message() header = message.getHeader() header.setField(fix.BeginString("FIX.4.2")) header.setField(fix.SenderCompID(sender_comp_id)) header.setField(fix.TargetCompID(target_comp_id)) header.setField(fix.MsgType("D")) ord_id = get_order_id(sender_comp_id, symbol) message.setField(fix.ClOrdID(ord_id)) message.setField(fix.Symbol(symbol)) message.setField(fix.Side(side)) message.setField(fix.Price(float(price))) if order_type.lower() == "market": message.setField(fix.OrdType(fix.OrdType_MARKET)) else: message.setField(fix.OrdType(fix.OrdType_LIMIT)) message.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION)) message.setField(fix.TransactTime()) message.setField(fix.OrderQty(float(quantity))) message.setField(fix.Text(f"{side} {symbol} {quantity}@{price}")) return message
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 queryOrdType(self): print("1) Market\n2) Limit\n3) Stop\n4) Stop Limit") value = input("OrdType: ") if value == '1': return fix.OrdType(fix.OrdType_MARKET) elif value == '2': return fix.OrdType(fix.OrdType_LIMIT) elif value == '3': return fix.OrdType(fix.OrdType_STOP) elif value == '4': return fix.OrdType(fix.OrdType_STOP_LIMIT) else: pass
def queryNewOrderSingle44(self): ordType = fix.OrdType() message = fix44.NewOrderSingle() message.setField(self.queryClOrdID()) message.setField(fix.HandlInst('1')) message.setField(self.querySymbol()) message.setField(self.querySide()) message.setField(fix.TransactTime()) ordType = self.queryOrdType() message.setField(ordType) message.setField(self.queryOrderQty()) message.setField(self.queryTimeInForce()) message.setField(self.querySecurityExchange()) message.setField(self.queryAccount()) if ordType.getValue() == fix.OrdType_LIMIT or ordType.getValue( ) == fix.OrdType_STOP_LIMIT: message.setField(self.queryPrice()) if ordType.getValue() == fix.OrdType_STOP or ordType.getValue( ) == fix.OrdType_STOP_LIMIT: message.setField(self.queryStopPx()) header = message.getHeader() self.queryHeader(header) return message
def new_order(self): print("Creating the following order: ") trade = fix.Message() trade.getHeader().setField(fix.BeginString(fix.BeginString_FIX42)) # trade.getHeader().setField(fix.MsgType( fix.MsgType_NewOrderSingle)) #35=D trade.setField(fix.ClOrdID(self.genOrderID())) #11=Unique order id trade.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION) ) #21=3 (Manual order, best executiona) trade.setField(fix.Symbol("ethbtc")) #55=ethbtc trade.setField(fix.Side(fix.Side_BUY)) #54=1 Buy trade.setField(fix.OrdType(fix.OrdType_LIMIT)) #40=2 Limit order trade.setField(fix.OrderQty(9)) #38=9 trade.setField(fix.Price(1.5)) #44=1.5 trade.setField( fix.StringField( 60, (datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f"))[:-3]) ) #60 TransactTime, not supported in python, so use tag number trade.setField(fix.Text("New Order")) #58 text print(trade.toString()) try: fix.Session.sendToTarget(trade, self.sessionID) except (fix.ConfigError, fix.RuntimeError) as e: print(e)
def queryNewOrderSingle42(self): ordType = fix.OrdType() message = fix.Message() message.setField(self.queryClOrdID()) message.setField(fix.HandlInst('1')) message.setField(self.querySymbol()) message.setField(self.querySide()) message.setField(fix.TransactTime()) ordType = self.queryOrdType() message.setField(ordType) message.setField(self.queryOrderQty()) message.setField(self.queryTimeInForce()) if abc.getValue() == fix.OrdType_LIMIT or abc.getValue( ) == fix.OrdType_STOP_LIMIT: message.setField(self.queryPrice()) if abc.getValue() == fix.OrdType_STOP or abc.getValue( ) == fix.OrdType_STOP_LIMIT: message.setField(self.queryStopPx()) header = message.getHeader() self.queryHeader(header) newOrderSingle = fix42.NewOrderSingle(message) return newOrderSingle
def newOrderSingle(self): try: LOG_EVENT("New Order Single") orderMsg = fix.Message() orderMsg.getHeader().setField(self.sessionID.getBeginString()) orderMsg.getHeader().setField( fix.MsgType(fix.MsgType_NewOrderSingle)) orderMsg.getHeader().setField(self.sessionID.getSenderCompID()) orderMsg.getHeader().setField(self.sessionID.getTargetCompID()) orderMsg.getHeader().setField(fix.MsgSeqNum(self.genOrderID())) sendingTime = fix.SendingTime() sendingTime.setString( datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")) orderMsg.getHeader().setField(sendingTime) orderMsg.setField(fix.Account("17018382")) orderMsg.setField(fix.ClOrdID(self.genExecID())) orderMsg.setField(fix.OrderQty(100)) orderMsg.setField(fix.OrdType(fix.TriggerOrderType_LIMIT)) orderMsg.setField(fix.Price(1.216)) orderMsg.setField(fix.Symbol("X.US.OREURUSD")) orderMsg.setField(fix.Side(fix.Side_BUY)) tranactionTime = fix.TransactTime() tranactionTime.setString( datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")) orderMsg.setField(tranactionTime) orderMsg.setField(fix.OpenClose(fix.OpenClose_OPEN)) LOG_PACKET(orderMsg.toString()) fix.Session.sendToTarget(orderMsg, self.sessionID) except Exception as e: print(e)
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 getExecutionReportForNewOrder(self, message): beginString = fix.BeginString() message.getHeader().getField(beginString) symbol = fix.Symbol() side = fix.Side() ordType = fix.OrdType() orderQty = fix.OrderQty() price = fix.Price() clOrdID = fix.ClOrdID() message.getField(ordType) if ordType.getValue() != fix.OrdType_LIMIT: raise fix.IncorrectTagValue(ordType.getField()) message.getField(symbol) message.getField(side) message.getField(orderQty) message.getField(price) message.getField(clOrdID) executionReport = fix.Message() executionReport.getHeader().setField(beginString) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport)) executionReport.setField(fix.OrderID(self.genOrderID())) executionReport.setField(fix.ExecID(self.genExecID())) executionReport.setField(fix.OrdStatus(fix.OrdStatus_FILLED)) executionReport.setField(symbol) executionReport.setField(side) executionReport.setField(fix.CumQty(orderQty.getValue())) executionReport.setField(fix.AvgPx(price.getValue())) executionReport.setField(fix.LastShares(orderQty.getValue())) executionReport.setField(fix.LastPx(price.getValue())) executionReport.setField(clOrdID) executionReport.setField(orderQty) executionReport.setField(fix.Text("New order accepted!")) # Since FIX 4.3, ExecTransType is killed and the values are moved to ExecType 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)) # ExecType and LeavesQty fields only existsince FIX 4.1 if beginString.getValue() >= fix.BeginString_FIX41: if beginString.getValue() <= fix.BeginString_FIX42: executionReport.setField(fix.ExecType( fix.ExecType_FILL)) #150=2 FILL (or 1 PARTIAL_FILL) else: # FILL and PARTIAL_FILL are removed and replaced by TRADE (F) since FIX 4.3 as these info can be retrieved from OrdStatus field executionReport.setField(fix.ExecType( fix.ExecType_TRADE)) #150=F TRADE executionReport.setField(fix.LeavesQty(0)) return executionReport
def fromApp(self, message, sessionID): beginString = fix.BeginString() msgType = fix.MsgType() message.getHeader().getField(beginString) message.getHeader().getField(msgType) symbol = fix.Symbol() side = fix.Side() ordType = fix.OrdType() orderQty = fix.OrderQty() price = fix.Price() clOrdID = fix.ClOrdID() message.getField(ordType) print(ordType) if ordType.getValue() != fix.OrdType_LIMIT: raise fix.IncorrectTagValue(ordType.getField()) message.getField(symbol) message.getField(side) message.getField(orderQty) message.getField(price) message.getField(clOrdID) executionReport = fix.Message() executionReport.getHeader().setField(beginString) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport)) executionReport.setField(fix.OrderID(self.genOrderID())) executionReport.setField(fix.ExecID(self.genExecID())) executionReport.setField(fix.OrdStatus(fix.OrdStatus_FILLED)) executionReport.setField(symbol) executionReport.setField(side) executionReport.setField(fix.CumQty(orderQty.getValue())) executionReport.setField(fix.AvgPx(price.getValue())) executionReport.setField(fix.LastShares(orderQty.getValue())) executionReport.setField(fix.LastPx(price.getValue())) executionReport.setField(clOrdID) 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_NEW)) if beginString.getValue() >= fix.BeginString_FIX41: executionReport.setField(fix.ExecType(fix.ExecType_FILL)) executionReport.setField(fix.LeavesQty(0)) try: fix.Session.sendToTarget(executionReport, sessionID) except (SessionNotFound, e): return
def make_single_order(self, order_id, symbol, qty): account = "YIZHE0" order = fix44.NewOrderSingle() order.setField(fix.Account(account)) order.setField(fix.ClOrdID(order_id)) order.setField(fix.Symbol(symbol)) order.setField(fix.Side("1")) order.setField(fix.OrdType("U")) order.setField(fix.OrderQty(qty)) order.setField(fix.Price(49)) return order
def put_order(self): print("Creating the following order: ") trade = fix.Message() trade.setField(fix.ClOrdID(self.genExecID())) # 11=Unique order trade.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION)) trade.setField(fix.Symbol('SMBL')) trade.setField(fix.Side(fix.Side_BUY)) trade.setField(fix.OrdType(fix.OrdType_LIMIT)) trade.setField(fix.OrderQty(100)) trade.setField(fix.Price(10)) print trade.toString() fix.Session.sendToTarget(trade, self.sessionID)
def sendHelloWorldMessage(sessionID): message = fix.Message() header = message.getHeader() header.setField(fix.BeginString("FIX.4.1")) header.setField(fix.SenderCompID("HELLOWORLD_TRADER")) header.setField(fix.TargetCompID("HELLOWORLD_EXCHANGE")) header.setField(fix.MsgType(fix.MsgType_NewOrderSingle)) message.setField(fix.ClOrdID('1')) message.setField(fix.HandlInst('1')) message.setField(fix.Symbol('BMLL')) message.setField(fix.Side(fix.Side_BUY)) message.setField(fix.OrdType(fix.OrdType_MARKET)) message.setField(fix.Text("Hello Exchange! How are you?")) fix.Session.sendToTarget(message, sessionID)
def __get_attributes(self, message): price = fix.Price() quantity = fix.OrderQty() symbol = fix.Symbol() side = fix.Side() order_type = fix.OrdType() client_order_id = fix.ClOrdID() message.getField(client_order_id) message.getField(side) message.getField(order_type) message.getField(symbol) message.getField(price) message.getField(quantity) return (symbol, price, quantity, side, order_type, client_order_id)
def getExecutionReportForStatusRequest(self, message): beginString = fix.BeginString() message.getHeader().getField(beginString) clOrdID = fix.ClOrdID() message.getField(clOrdID) executionReport = fix.Message() executionReport.getHeader().setField(beginString) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport)) executionReport.setField(fix.Symbol("ABCD")) executionReport.setField(fix.Side(fix.Side_BUY)) #43=1 Buy executionReport.setField(fix.OrderID(self.genOrderID())) executionReport.setField(fix.ExecID(self.genExecID())) executionReport.setField(fix.OrdType( fix.OrdType_LIMIT)) #40=2 Limit order executionReport.setField(fix.OrderQty(100)) #38=100 executionReport.setField(fix.Price(10)) #44=10 executionReport.setField(fix.OrdStatus(fix.OrdStatus_FILLED)) executionReport.setField(fix.AvgPx(10)) #6=10 executionReport.setField(fix.CumQty(100)) #14=100 executionReport.setField(clOrdID) executionReport.setField(fix.Text("Order status retrieved!")) # Since FIX 4.3, ExecTransType values are moved to ExecType if beginString.getValue( ) == fix.BeginString_FIX40 or beginString.getValue( ) == fix.BeginString_FIX41 or beginString.getValue( ) == fix.BeginString_FIX42: executionReport.setField( fix.ExecTransType(fix.ExecTransType_STATUS)) # ExecType and LeavesQty fields only existsince FIX 4.1 if beginString.getValue() >= fix.BeginString_FIX41: if beginString.getValue() <= fix.BeginString_FIX42: executionReport.setField(fix.ExecType( fix.ExecType_FILL)) #150=2 FILL (or 1 PARTIAL_FILL) else: # FILL and PARTIAL_FILL are removed and replaced by TRADE (F) since FIX 4.3 as these info can be retrieved from OrdStatus field executionReport.setField(fix.ExecType( fix.ExecType_TRADE)) #150=F TRADE executionReport.setField(fix.LeavesQty(0)) return executionReport
def getExecutionReportForCancelOrder(self, message): beginString = fix.BeginString() message.getHeader().getField(beginString) symbol = fix.Symbol() side = fix.Side() clOrdID = fix.ClOrdID() message.getField(symbol) message.getField(side) message.getField(clOrdID) executionReport = fix.Message() executionReport.getHeader().setField(beginString) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport)) executionReport.setField(fix.OrderID(self.genOrderID())) executionReport.setField(fix.ExecID(self.genExecID())) executionReport.setField(fix.OrdType( fix.OrdType_LIMIT)) #40=2 Limit order executionReport.setField(fix.OrderQty(100)) #38=100 executionReport.setField(fix.Price(10)) #44=10 executionReport.setField(fix.OrdStatus(fix.OrdStatus_FILLED)) executionReport.setField(symbol) executionReport.setField(side) executionReport.setField(fix.AvgPx(10)) #6=10 executionReport.setField(fix.CumQty(100)) #14=100 executionReport.setField(clOrdID) executionReport.setField(fix.Text("Order cancelled!")) # Since FIX 4.3, ExecTransType values are moved to ExecType 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)) # ExecType and LeavesQty fields only existsince FIX 4.1 if beginString.getValue() >= fix.BeginString_FIX41: executionReport.setField(fix.ExecType( fix.ExecType_CANCELED)) #150=4 CANCELED executionReport.setField(fix.LeavesQty(0)) #151=0 return executionReport
def new_order(self, message, beginString, sessionID): symbol = fix.Symbol() side = fix.Side() ordType = fix.OrdType() orderQty = fix.OrderQty() price = fix.Price(50) clOrdID = fix.ClOrdID() message.getField(ordType) message.getField(symbol) message.getField(side) message.getField(orderQty) message.getField(price) message.getField(clOrdID) log.info("new_order:\nordType:{},symbol:{},side:{},orderQty:{},price:{},clOrdID:{} -----"\ .format(ordType, symbol, side, orderQty, price, clOrdID)) executionReport = fix.Message() executionReport.getHeader().setField(beginString) executionReport.getHeader().setField( fix.MsgType(fix.MsgType_ExecutionReport)) executionReport.setField(fix.TransactTime()) executionReport.setField(fix.OrderID(self.genOrderID())) executionReport.setField(fix.ExecID(self.genExecID())) executionReport.setField(fix.OrdStatus(fix.OrdStatus_NEW)) executionReport.setField(symbol) executionReport.setField(side) executionReport.setField(fix.CumQty(orderQty.getValue())) executionReport.setField(fix.AvgPx(price.getValue())) executionReport.setField(fix.LastShares(orderQty.getValue())) executionReport.setField(fix.LastPx(price.getValue())) executionReport.setField(clOrdID) executionReport.setField(orderQty) executionReport.setField(fix.ExecType(fix.ExecType_NEW)) executionReport.setField(fix.LeavesQty(orderQty.getValue())) try: fix.Session.sendToTarget(executionReport, sessionID) time.sleep(1) if ordType.getValue() == fix.OrdType_MARKET: executionReport.setField(fix.OrdStatus(fix.OrdStatus_FILLED)) executionReport.setField(fix.ExecType(fix.ExecType_TRADE)) fix.Session.sendToTarget(executionReport, sessionID) except fix.SessionNotFound as e: return
def put_order(self): print("Creating the following order: ") trade = fix.Message() trade.getHeader().setField(fix.BeginString(fix.BeginString_FIX50)) # trade.getHeader().setField(fix.MsgType( fix.MsgType_NewOrderSingle)) #39=D trade.setField(fix.ClOrdID(self.genExecID())) #11=Unique order trade.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION) ) #21=3 (Manual order, best executiona) trade.setField(fix.Symbol('SMBL')) #55=SMBL ? trade.setField(fix.Side(fix.Side_BUY)) #43=1 Buy trade.setField(fix.OrdType(fix.OrdType_LIMIT)) #40=2 Limit order trade.setField(fix.OrderQty(100)) #38=100 trade.setField(fix.Price(10)) print trade.toString() fix.Session.sendToTarget(trade, self.sessionID)
def generate_message_fields(self, message_type): message_fields = { 'client_orderID': fix.ClOrdID(), 'symbol': fix.Symbol(), 'side': fix.Side(), 'quantity': fix.OrderQty(), } if message_type in ['new', 'replace']: extra_message_fields = { 'type': fix.OrdType(), 'price': fix.Price(), 'handlInst': fix.HandlInst(), } message_fields.update(extra_message_fields) if message_type == 'replace': message_fields['original_orderID'] = fix.OrigClOrdID() return message_fields
def fromApp(self, message, sessionID): msgtype = fix.MsgType() symbol = fix.Symbol() side = fix.Side() ordtype = fix.OrdType() text = fix.Text() message.getHeader().getField(msgtype) message.getField(symbol) message.getField(side) message.getField(ordtype) message.getField(text) print print 'Message received! It reads:' print ' = '.join(self.parse(msgtype)) print self.getFieldName(symbol), '=', symbol.getString() print ' = '.join(self.parse(side)) print ' = '.join(self.parse(ordtype)) print self.getFieldName(text), '=', text.getString()
def on_replace_request(self, message): print('replace!') message_fields = { 'client_orderID': fix.ClOrdID(), 'original_clientID': fix.OrigClOrdID(), 'symbol': fix.Symbol(), 'type': fix.OrdType(), 'side': fix.Side(), 'quantity': fix.OrderQty(), 'price': fix.Price(), 'handlInst': fix.HandlInst(), } message_details = {} for k, v in message_fields.items(): message_details[k] = self.get_field_value(message, v) orderID = self.gen_orderID() self.orders[orderID] = message_details # testing self.test_replies(orderID, fix.OrdStatus_PENDING_REPLACE, fix.ExecType_REPLACE, message_details)
def order_cancel_replace(self,account,symbol,side,quantity,order_type,price): print("Creating order_cancel_replace message... ") cancel_replace_message = fix.Message() cancel_replace_message.getHeader().setField(fix.BeginString(fix.BeginString_FIX42)) # cancel_replace_message.getHeader().setField(fix.MsgType('G')) #39=D cancel_replace_message.getHeader().setField(fix.SendingTime(1)) cancel_replace_message.setField(fix.Account(str(account))) #1 cancel_replace_message.setField(fix.HandlInst(fix.HandlInst_AUTOMATED_EXECUTION_ORDER_PUBLIC_BROKER_INTERVENTION_OK)) #21=3 (Manual order), 21=2 automated execution only supported value cancel_replace_message.setField(fix.ClOrdID(str('order_cancel_replace'+self.genOrderID()))) #11 cancel_replace_message.setField(fix.OrigClOrdID(str(self.orders[0]))) #41 cancel_replace_message.setField(fix.Symbol(str(symbol))) #55 cancel_replace_message.setField(fix.Side(str(side))) #54 cancel_replace_message.setField(fix.OrderQty(quantity)) #38 cancel_replace_message.setField(fix.OrdType(str(order_type))) #40 cancel_replace_message.setField(fix.Price(price)) #44 print('sending order_cancel_replace message...') print(f'order_cancel_replace message: {cancel_replace_message.toString()}') fix.Session.sendToTarget(cancel_replace_message, self.sessionID) print('order_cancel_replace message sent!')
def on_new_order(self, message): print('New Order!') message_fields = { 'client_orderID': fix.ClOrdID(), 'symbol': fix.Symbol(), 'type': fix.OrdType(), 'side': fix.Side(), 'quantity': fix.OrderQty(), 'price': fix.Price(), 'handlInst': fix.HandlInst(), } message_details = {} for k, v in message_fields.items(): message_details[k] = self.get_field_value(message, v) orderID = self.gen_orderID() self.orders[orderID] = message_details # add error checking here # testing self.test_replies(orderID, fix.OrdStatus_NEW, fix.ExecType_NEW, message_details)
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 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 replace_order(sender_comp_id, target_comp_id, quantity, price, orig_client_order_id): symbol = ORDERS[orig_client_order_id][0].getValue() side = ORDERS[orig_client_order_id][3].getValue() message = fix42.OrderCancelReplaceRequest() header = message.getHeader() header.setField(fix.SenderCompID(sender_comp_id)) header.setField(fix.TargetCompID(target_comp_id)) ord_id = get_order_id(sender_comp_id, symbol) message.setField(fix.OrigClOrdID(orig_client_order_id)) message.setField(fix.ClOrdID(ord_id)) message.setField(fix.Symbol(symbol)) message.setField(fix.Side(side)) message.setField(fix.Price(float(price))) message.setField(fix.OrdType(fix.OrdType_LIMIT)) message.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION)) message.setField(fix.TransactTime()) message.setField(fix.TransactTime()) message.setField(fix.OrderQty(float(quantity))) message.setField(fix.Text(f"{side} {symbol} {quantity}@{price}")) return message
def new_child_order(self, message: fix.Message): """""" child_order = GenusChildOrder( cl_ord_id=get_field_value(message, fix.ClOrdID()), symbol=get_field_value(message, fix.Symbol()), side=get_field_value(message, fix.Side()), order_qty=get_field_value(message, fix.OrderQty()), price=get_field_value(message, fix.Price()), order_type=get_field_value(message, fix.OrdType()), ord_status="A", parent_orderid=get_field_value(message, fix.StringField(526))) # If parent offset not exist, then this child order should be ignored offset = self.client.get_parent_offset(child_order.parent_orderid) if not offset: return symbol, genus_exchange = child_order.symbol.split(".") exchange = EXCHANGE_GNS2VT[genus_exchange] direction = DIRECTION_GNS2VT[child_order.side] order_type = ORDERTYPE_GNS2VT[child_order.order_type] vt_symbol = generate_vt_symbol(symbol, exchange) child_order.order_id = self.client.send_order( vt_symbol, direction, offset, order_type, child_order.price, child_order.order_qty, ) self.child_orders[child_order.order_id] = child_order self.genus_vt_map[child_order.cl_ord_id] = child_order.order_id msg = f"委托{direction.value}{vt_symbol}:{child_order.order_qty}@{child_order.price}" self.client.write_log(msg, algo_name=child_order.parent_orderid)
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)
header.setField(fix.MsgType(fix.MsgType_NewOrderSingle)) header.setField(fix.SendingTime()) header.setField(fix.SenderSubID("SG1")) header.setField(fix.TargetSubID("BARCA")) header.setField(fix.MsgSeqNum()) header.setField(fix.DefaultApplVerID(fix.ApplVerID_FIX42)) message.setField(fix.ClOrdID("ClOrdID")) message.setField( fix.AccountType( fix. AccountType_ACCOUNT_IS_CARRIED_ON_CUSTOMER_SIDE_OF_BOOKS)) message.setField(fix.Symbol("APPL")) message.setField( fix.HandlInst( fix. HandlInst_AUTOMATED_EXECUTION_ORDER_PRIVATE_NO_BROKER_INTERVENTION )) message.setField(fix.ReceivedDeptID("I")) message.setField(fix.Side(fix.Side_BUY)) message.setField(fix.TransactTime()) message.setField(fix.OrderQty(1000)) message.setField(fix.OrdType(fix.OrdType_MARKET)) message.setField(fix.Price(2000)) trailer.setField(fix.CheckSum()) application.send(message) message_flag = False except (fix.ConfigError, fix.RuntimeError) as e: print(e)