def execCmdCancelOrderReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: srvSendTimestamp = getTimeStamp() # Message to clients (pub/sub) sendForward = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['from'], _fromto_to=recv.fromto['to'], _timestamp=recv.timestamp, _cmdtype=recv.cmdtype, # must be: BinanceCTProto.CT_TYPE_REQUEST _resp_timestamp= srvSendTimestamp, # Client will receive the SrvSend timestamp _data=recv.data) # Message response to CopyTrade sendResponse = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE(BinanceCTProto.CT_PROTO_RESP_OK, "Ok")) return ([True, "Ok", sendForward, sendResponse])
def execCmdPingReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: sendBadResponse = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=getTimeStamp(), _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE( BinanceCTProto.CT_PROTO_RESP_PING, "Ping resp")) return ([True, "Ok", recv, sendResponse])
def execCmdCopytradeReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO]: sendForward = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['from'], _fromto_to=recv.fromto['to'], _timestamp=recv.timestamp, # CopyTrade timestamp _cmdtype=recv.cmdtype, _resp_timestamp=recv.resp_timestamp, # should be empty (a request..) _data=recv.data) BinanceCTProto.dumpCmdToLog(sendForward, logging.info) return ([True, "Ok", sendForward])
def execCmdGetOpenOrdersReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: # TODO: send get open orders to pubsub sendResponse = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=getTimeStamp(), _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE(BinanceCTProto.CT_PROTO_RESP_OK, "Ok")) return ([True, "Ok", None, sendResponse])
exit(1) del cfgFile # --- SAMPLE 1 ------------------- print("--- SAMPLE 1 -------------------") con = envelop_sendRecv.connection() ret, msgret = con.connectToServer(ctm_signalSource_address, int(ctm_signalSource_port), socket.AF_INET, socket.SOCK_STREAM) if ret == False: print(msgret, file=stderr) exit(1) copytrade_sample1 = BinanceCTProto.CT_PROTO() copytrade_sample1.cmd = BinanceCTProto.CT_CMD_COPYTRADE copytrade_sample1.fromto = {'from': ctm_name, 'to': 'SrvSend'} copytrade_sample1.timestamp = getTimeStamp() copytrade_sample1.cmdtype = "REQ" copytrade_sample1.response_timestamp = "" copytrade_sample1.data = BinanceCTProto.CT_PROTO_COPYTRADE_DATA( _symbol="BTCUSDT", _side="BUY", _ordid="1234567890", _ordtype="LIMIT", _price="10.10101010", _qtd=10, _priceStop="123.12",
ping_log = cfgFile['GENERAL']['log'] ping_signalSource_port = cfgFile['SIGNAL_SOURCE']['port'] ping_signalSource_address = cfgFile['SIGNAL_SOURCE']['address'] ping_interval = cfgFile['SIGNAL_SOURCE']['interval'] ping_qtd = cfgFile['SIGNAL_SOURCE']['qtd'] except Exception as e: print(f"Invalid cfg file! [{e}]") exit(1) del cfgFile # --- SOCKET ------------------------------ ping_sample1 = BinanceCTProto.CT_PROTO() ping_sample1.cmd = BinanceCTProto.CT_CMD_PING ping_sample1.fromto = {'from': ping_name, 'to': "ALL"} ping_sample1.cmdtype = "REQ" ping_sample1.response_timestamp = "" ping_qtdNum = int(ping_qtd) continuePing = True qtdSent = 0 while continuePing == True: con = envelop_sendRecv.connection() ret, msgret = con.connectToServer(ping_signalSource_address, int(ping_signalSource_port), socket.AF_INET, socket.SOCK_STREAM)
def sendOrderToSrvSend(symb: str = "", side: str = "", qtd: str = "", price: str = "", priceStop: str = "", priceLimit: str = "", typeOrd: str = "", orderCTId: str = "") -> [bool, str]: global GUIUtilcfg orderToSendData = BinanceCTProto.CT_PROTO_COPYTRADE_DATA( _symbol=symb, _side=side, _qtd=qtd, _ordid=orderCTId, _ordtype=typeOrd, _price=price, _priceStop=priceStop, _priceLimit=priceLimit) orderToSend = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=GUIUtilcfg.meName, _fromto_to="SrvSend", _timestamp=BinanceCTUtil.getTimeStamp(), _cmdtype=BinanceCTProto.CT_TYPE_REQUEST, _resp_timestamp="", _data=orderToSendData) ret, msg = orderToSend.formatToNet() if ret == False: return ([False, msg]) con = envelop_sendRecv.connection() logging.debug( f"Connecting to SrvSend: {GUIUtilcfg.srvSendAddress} {GUIUtilcfg.srvSendPort}" ) ret, retmsg = con.connectToServer(GUIUtilcfg.srvSendAddress, GUIUtilcfg.srvSendPort, socket.AF_INET, socket.SOCK_STREAM) if ret == False: return ([False, f"Connect to server error: {retmsg}"]) ret, retmsg = con.sendMsg(msg, len(msg)) if ret == False: return ([False, f"Send to server error: {retmsg}"]) ret, retmsg, msgRecv = con.recvMsg() if ret == False: return ([False, f"Error msgRecv: [{retmsg}]"]) orderRecv = BinanceCTProto.CT_PROTO() orderRecv.loadFromNet(msgRecv) logging.info(f'Sent: [{msg}]') BinanceCTProto.dumpCmdToLog(orderToSend, logging.info) logging.info(f'Received: [{msgRecv}]') BinanceCTProto.dumpCmdToLog(orderRecv, logging.info) return ([True, "Ok"])
def execCmdCopytradeReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: ret, retmsg = srvDB.DB.insertCmd(recv) if ret == False: return ([ False, f"Database error inserting recv cmd [{retmsg}]", None, None ]) srvSendTimestamp = getTimeStamp() # Message to clients (pub/sub) sendForward = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['from'], _fromto_to=recv.fromto['to'], _timestamp=recv.timestamp, _cmdtype=recv.cmdtype, # must be: BinanceCTProto.CT_TYPE_REQUEST _resp_timestamp= srvSendTimestamp, # Client will receive the SrvSend timestamp _data=recv.data) ret, retmsg = srvDB.DB.insertCmd(sendForward) if ret == False: sendResponseErr = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE( BinanceCTProto.CT_PROTO_COPYTRADE_ERROR1, "Insert sendForward error")) return ([ False, f"Database error inserting forward cmd [{retmsg}]", None, sendResponse ]) # Message response to CopyTrade sendResponse = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE(BinanceCTProto.CT_PROTO_RESP_OK, "Ok")) ret, retmsg = srvDB.DB.insertCmd(sendResponse) if ret == False: sendResponseErr = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE( BinanceCTProto.CT_PROTO_COPYTRADE_ERROR2, "Insert sendResponse error")) return ([ False, f"Database error inserting response cmd [{retmsg}]", None, sendResponseErr ]) return ([True, "Ok", sendForward, sendResponse])
while True: logging.info('Waiting connections..') ret, msgret, client = conn.serverAccept() if ret == False: logging.info(f'Connection error: [{msgret}].') safeExit(1) ret, retmsg, msgRecv = conn.recvMsg() if ret == False: print(f"Error: [{retmsg}]") safeExit(1) recv = BinanceCTProto.CT_PROTO() recv.loadFromNet(msgRecv) logging.debug(f'Msg: [{msgRecv}]') logging.info(f"-------------------------------") logging.info(f"RECEIVED FROM SOCKET [{client[0]}:{client[1]}]:") BinanceCTProto.dumpCmdToLog(recv, logging.info) # here we may place a recv.fromto['from'] validation through a "valid CopyTrade clients" list from config file. at first we will execute from ALL incoming. if recv.cmd == BinanceCTProto.CT_CMD_COPYTRADE: # a CopyTrade client sent a trade. if recv.cmdtype == BinanceCTProto.CT_TYPE_REQUEST: ret, retmsg, sendForward, sendResponse = execCmdCopytradeReq(recv)
except Exception as e: logging.info("0Mq Exception: [{e}]") exit(1) except BaseException as e: logging.info(f"BaseException {str(e)}") exit(1) except: logging.info("Unknow exception") exit(1) dt = msg[:len(sub_topic)] ds = msg[len(sub_topic) + 1:] logging.info("----------------------------------\nReceived:") logging.debug(f"TOPIC: [{dt}]\nMSG: [{ds}]") recv = BinanceCTProto.CT_PROTO() recv.loadFromNet(ds) if recv.cmd == BinanceCTProto.CT_CMD_COPYTRADE: logging.info("Received a COPYTRAPE cmd") ret, retmsg, sendForward = execCmdCopytradeReq(recv) elif recv.cmd == BinanceCTProto.CT_CMD_PING: logging.info("Received a PING cmd") ret, retmsg, sendForward = execCmdPingReq(recv) elif recv.cmd == BinanceCTProto.CT_CMD_CANCELORDER: logging.info("Received a CANCELORDER cmd") ret, retmsg, sendForward = execCmdCancelOrderReq(recv) elif recv.cmd == BinanceCTProto.CT_CMD_GETOPENORDERS: