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])
exit(1) con.sendMsg(msg, len(msg)) ret, retmsg, msgRecv = con.recvMsg() if ret == False: print(f"Error: [{retmsg}]") exit(1) con.endClient() copytrade_sample1_resp = BinanceCTProto.CT_PROTO() copytrade_sample1_resp.loadFromNet(msgRecv) print('Sent:') BinanceCTProto.dumpCmdToLog(copytrade_sample1, print) print('\nReceived:') BinanceCTProto.dumpCmdToLog(copytrade_sample1_resp, print) # --- SAMPLE 2 ------------------- print("\n--- SAMPLE 2 -------------------") 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) cancelorder_sample1 = BinanceCTProto.CT_PROTO()
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"])
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) if ret == False: logging.info(f"Error execCmdCopytradeReq() [{retmsg}]!") srvDB.DB.rollback() safeExit(1) # TODO: exit proc? srvDB.DB.commit()