diff --git a/src/data_files/files/audit_ilink3.py b/src/data_files/files/audit_ilink3.py new file mode 100644 index 000000000..67acd8d0a --- /dev/null +++ b/src/data_files/files/audit_ilink3.py @@ -0,0 +1,405 @@ +import json +import csv +import datetime, time +import argparse, os + + +def valid_file(f): + if not os.path.exists(f): + # Argparse uses the ArgumentTypeError to give a rejection message like: + # error: argument input: x does not exist + raise argparse.ArgumentTypeError("{0} does not exist".format(f)) + return f + +def valid_date(s): + try: + return datetime.datetime.strptime(s, "%Y%m%d") + except ValueError: + raise argparse.ArgumentTypeError("Not a valid date: '{0}'.".format(s)) + +def print_headers(): + # From https://www.cmegroup.com/confluence/display/EPICSANDBOX/iLink+3+-+Minimum+Acceptable+Audit+Trail+Elements+-+Data+Definitions+and+Validation+Rules + line_to_write = "Sending Timestamps,Message Direction,Operator ID,Session ID,Manual Order Indicator,Message Type,Instrument,Order Flow ID,\ +CME Globex Order ID,Client Order ID,Order Request ID,Buy/Sell Indicator,Quantity,Limit Price,Stop Price,Order Type,Order Qualifier,IFM Flag,\ +Display Quantity,Minimum Quantity,Country of Origin,Fill Price,Fill Quantity,Cumulative Quantity,Remaining Quantity,Aggressor Flag,Source of Cancellation,\ +Reject Reason,CME Globex Message ID,Cross ID,Quote Request ID,Message Quote ID,Quote Set ID,Quote Entry ID,Bid Price,Bid Size,Offer Price,Offer Size,\ +Processed Entries,Party Details List Request ID,ListUpdateAction,Self-Matching Prevention ID,Self-Match Prevention Instr,Customer Type Indicator,\ +AvgPxGroupID,Average Px Ind,CmtaGiveUpCD,Origin,Clearing Trade Price Type,Customer Handling Instr,Executing Firm ID,Account Number,Take Up Firm,Take up Account" + + with open (args.output_file, 'w') as the_file: + the_file.write(line_to_write) + the_file.write("\n") + +def set_blanks(): + return "" + +def get_value(json_key): + prices = ["Price", "LastPx", "StopPx"] + if json_key in json_data: + if json_key in prices: + x = json_data[json_key] + return x['mantissa']/100000000000 + else: + return json_data[json_key] + else: + return "" + + +# Column 1 - Sending Timestamps +def SendingTimeEpoch(epochtime): #1 + #TIME = time.strftime('%Y%m%d-%H:%M:%S.{}'.format(epochtime/1000000000), time.gmtime(epochtime/1000000000)) + TIME = datetime.datetime.fromtimestamp(epochtime/1000000000).strftime('%Y%m%d-%H:%M:%S.%f') + return TIME + +# Column 2 - Message Direction +def direction(whichway): + if whichway == '<': + DIRECTION = "FROM CME" + else: + DIRECTION = "TO CME" + return DIRECTION + +# Column 3 - Operator ID +def SenderID(sender): + if "SenderID" in json_data: + SENDER = json_data["SenderID"] + return SENDER + else: + return SESSIONID + + +# Column 4 - Session ID + +# Column 5 - Manual Order Indicator +def ManualOrderIndicator(manual): + if "ManualOrderIndicator" in json_data: + if manual == "Manual": + return "1" + else: + return "0" + +# Column 6 - Message Type +def MessageType(msg_type,orderstatus): + if msg_type == "NewOrderSingle514": + return "D" + elif msg_type == "ExecutionReportNew522": + return "8--0" + if "ExecutionReportTrade" in msg_type: + if orderstatus == "PartiallyFilled": + return "8--1" + else: + return "8--2" + elif msg_type == "ExecutionReportCancel534": + return "8--4" + elif msg_type == "ExecutionReportModify531": + return "8--5" + elif msg_type == "ExecutionReportCancel534": + return "8--H" + elif msg_type == "ExecutionReportReject523": + return "8--8" + elif msg_type == "OrderCancelReplaceReject536": + return "9--2" + elif msg_type == "OrderCancelReject535": + return "9--1" + elif msg_type == "PartyDetailsDefinitionRequest518": + return "CX" + elif msg_type == "PartyDetailsDefinitionRequestAck519": + return "CY" + elif msg_type == "OrderCancelRequest516": + return "F" + elif msg_type == "OrderCancelReplaceRequest515": + return "G" + + + +# Column 12 - Buy-Sell Indicator/Side +def Side(indicator): + if "Side" in json_data: + if indicator == "Buy": + return "1" + else: + return "2" + +# Column 16 - Order Type/OrdType +def OrdType(ordertype): + if "OrdType" in json_data: + if ordertype == "MarketWithProtection" or ordertype == "MarketwithProtection": + return "1" + elif ordertype == "Limit": + return "2" + elif ordertype == "StopWithProtection" or ordertype == "StopwithProtection": + return "3" + elif ordertype == "StopLimit": + return "4" + elif ordertype == "MarketLimit": + return "K" + else: + return "" + +# Column 17 - Order Qualifier/TimeInForce +def TimeInForce(orderqual): + if "TimeInForce" in json_data: + if orderqual == "Day": + return "0" + elif orderqual == "GTC": + return "1" + elif orderqual == "FAK": + return "3" + elif orderqual == "GTD": + return "6" + else: + return "" + +# Column 18 IFM Flag/OFMOverride +def OFMOverride(imfflag): + if "OFMOverride" in json_data: + if imfflag == "Enabled": + return "1" + else: + return "0" + +# Column 19 Display Quantity/DisplayQty +def DisplayQty(qty): + if "DisplayQty" in json_data: + if str(qty) == "0": + return "" + elif str(qty) == "4294967295": + return "" + else: + return qty + + +# Column 26 - Aggressor Flag/AggressorIndicator +def AggressorIndicator(aggressor): + if "AggressorIndicator" in json_data: + if aggressor == "True": + return "1" + else: + return "0" + +# Column 27 - Source of Cancellation/ExecRestatementReason +def ExecRestatementReason(sourcecancel): + if "ExecRestatementReason" in json_data: + if sourcecancel == "Exchange": + return "8" + elif sourcecancel == "CancelledNotBest": + return "9" + elif sourcecancel == "CancelOnDisconnect": + return "100" + elif sourcecancel == "CancelRestingDueToSelfMatchPrevention": + return "103" + elif sourcecancel == "CancelFromCMEGlobexCreditControlsViolation": + return "104" + elif sourcecancel == "CancelFromCMEOne": + return "105" + elif sourcecancel == "CancelFromRiskManagementAPI": + return "106" + elif sourcecancel == "CancelAggressingDueToSelfMatchPrevention": + return "107" + elif sourcecancel == "CancelDueToVolatilityQuoteOptionLessThanMinOrderSize": + return "108" + else: + return "" + +# Column 43 - Self-Match Prevention Instr/SelfMatchPreventionInstruction +def SelfMatchPreventionInstruction(instr): + if "SelfMatchPreventionInstruction" in json_data: + if instr == "CancelOldest": + return "O" + elif instr == "CancelNewest": + return "N" + else: + return "" + +# Column 44 - Customer Type Indicator/CustOrderCapacity +def CustOrderCapacity(custord): + if "CustOrderCapacity" in json_data: + if custord == "NULL_VALUE": + return "" + elif custord == "Membertradingfortheirownaccount": + return "1" + elif custord == "ClearingFirmtradingforitsproprietaryaccount": + return "2" + elif custord == "Membertradingforanothermember": + return "3" + else: + return "4" + +# Column 46 - Average Px Ind/AvgPxInd +def AvgPxInd(avgpxindicator): + if "AvgPxInd" in json_data: + if avgpxindicator == "NULL_VALUE": + return "" + else: + return avgpxindicator + +# Column 48 - Origin/ClearingAccountType +def ClearingAccountType(accounttype): + if "ClearingAccountType" in json_data: + if accounttype == "Customer": + return "0" + elif accounttype == "Firm": + return "1" + else: + return "" + +# Column 49 - Clearing Trade Price Type/ClearingTradePriceType +def ClearingTradePriceType(clrtrdprice): + if "ClearingTradePriceType" in json_data: + if clrtrdprice == "NULL_VALUE": + return "0" + else: + return "3" + +# Column 50 - Customer Handling Instr/CustOrderHandlingInst +def CustOrderHandlingInst(custorderinst): + if "CustOrderHandlingInst" in json_data: + if custorderinst == "ClientElectronic": + return "C" + elif custorderinst == "TEST": + return "D" + else: + return "" + +# Column 51 and 52 - Getting the Firm and Account Number +def partyid(partydetailid,column): + for x in partydetailid: + role = x['PartyDetailRole'] + id = x['PartyDetailID'] + if column == "51" and role == "ExecutingFirm": + return id + if column == "52" and role == "CustomerAccount": + return id + + +parser = argparse.ArgumentParser() +parser.add_argument("json_log", help="the json log[.gz] you wish to process", type=valid_file) +parser.add_argument("output_file", help="the destination csv file you wish to write") +parser.add_argument("-d", "--date", help="the date you wish to parse from the log, specified as YYYYmmdd", + default=datetime.datetime.today().strftime("%Y%m%d"), type=valid_date) +args = parser.parse_args() + +print_headers() + +list_types = ['NewOrderSingle514', 'OrderCancelReplaceRequest515', 'OrderCancelRequest516', 'PartyDetailsDefinitionRequest518', 'PartyDetailsDefinitionRequestAck519', \ + 'ExecutionReportNew522', 'ExecutionReportReject523', 'ExecutionReportTradeOutright525', 'ExecutionReportTradeSpread526', 'ExecutionReportTradeSpreadLeg527', \ + 'ExecutionReportModify531', 'ExecutionReportCancel534', 'OrderCancelReject535', 'OrderCancelReplaceReject536', ] + +# Note that the timedeltas specificed in trade_day_start and trade_day_end +# are based on Chicago times; as such, this script should run on a machine +# set to a Chicago timezone. A future impromovement would be to determine +# the current machine's timezone and determine the appropriate offset +# versus Chicago time, but for now we assume this will run on a Chicago +# tz machine. +input_as_datetime = args.date +trade_day_start = input_as_datetime - datetime.timedelta(days=1) + datetime.timedelta(hours=16, minutes=30) +trade_day_utc_start = time.gmtime((time.mktime(trade_day_start.timetuple()))) +trade_day_end = input_as_datetime + datetime.timedelta(hours=16) +trade_day_utc_end = time.gmtime((time.mktime(trade_day_end.timetuple()))) + +file_opener = None +if args.json_log.endswith(".gz"): + import gzip + file_opener = gzip.open +else: + file_opener = open + + +#inputFile = open(fileInput) #open json file +#outputFile = open(fileOutput, 'w') #load csv file +# Get Session +with file_opener(args.json_log) as json_file: + for line in json_file: + data = json.loads(line) + json_data = data['msg_json'] + if "Session" in json_data: + SESSIONID = json_data["Session"] + +#inputFile = open(fileInput) #open json file +#outputFile = open(fileOutput, 'w') #load csv file +with file_opener(args.json_log) as json_file: + for line in json_file: + data = json.loads(line) + line_timestamp = time.strptime(data['time'], "%Y%m%d-%H:%M:%S.%f") + if trade_day_utc_start <= line_timestamp <= trade_day_utc_end: + json_data = data['msg_json'] + NAME = json_data["Name"] + if NAME in list_types: + EXECUTING_FIRM_ID = "" + ACCOUNT_NUMBER = "" + DIRECTION = direction(data['direction']) + # Set Blanks + blank_json = {'QuoteReqID': '', 'QuoteID': '', 'QuoteSetID': '', 'QuoteEntryID': '', 'BidPx': '', 'BidSize': '', 'OfferPx': '', \ + 'OfferSize': '', 'NoProcessedEntries': '', 'ListUpdateAction': '', 'MinQty': '', 'AvgPxGroupID': '', \ + 'CmtaGiveUpCD': '', 'TAKE_UP_FIRM': '', 'TAKE_UP_ACCOUNT': '',} + + # No Data Processing + straight_json = {'SecurityID': '', 'ClOrdID': '', 'OrderID': '', 'OrderRequestID': '', 'OrderQty': '', \ + 'Location': '', 'LastQty': '', 'CumQty': '', 'LeavesQty': '', 'Text': '', 'SideTradeID': '', 'CrossID': '', 'SelfMatchPreventionID': '', \ + 'Price': '', 'StopPx': '', 'LastPx': '', 'PartyDetailsListReqID': '', 'OrdStatus': ''} + + # Processing Data + process_json = {'SendingTimeEpoch': '', 'ManualOrderIndicator': '', 'SenderID': '', 'Side': '', 'OrdType': '', 'TimeInForce': '', \ + 'OFMOverride': '', 'AggressorIndicator': '', 'ExecRestatementReason': '', 'SelfMatchPreventionInstruction': '', 'CustOrderCapacity': '', \ + 'AvgPxInd': '', 'ClearingAccountType': '', 'ClearingTradePriceType': '', 'CustOrderHandlingInst': '', 'DisplayQty': ''} + + for d in straight_json: + test = get_value(d) + if test != "None" or test != "": + if "," in str(test): + test = test.replace(",",";") + if "4294967295" in str(test): + test = "" + if "us;" in str(test): + test = test.upper() + if d == "SelfMatchPreventionID" and len(str(test)) > 8: + test = "" + straight_json[d] = test + else: + straight_json[d] = '' + + for d in process_json: + if d in json_data: + function = locals()[d] + test = function(json_data[d]) + if test != "None": + process_json[d] = test + else: + process_json[d] = '' + elif d == "SenderID": + function = locals()[d] + test = function(SenderID) + if test != "None": + process_json[d] = test + + # Party Message Fields + # Column 51 - Executing Firm ID + if "NoPartyDetails" in json_data: + EXECUTING_FIRM_ID = partyid(json_data['NoPartyDetails'], "51") + # Column 52 - Party Account Number + ACCOUNT_NUMBER = partyid(json_data['NoPartyDetails'], "52") + + line_to_write = process_json['SendingTimeEpoch'] + "," + DIRECTION + "," + process_json['SenderID'] + "," + SESSIONID + "," + process_json['ManualOrderIndicator'] + "," + \ + MessageType(NAME,straight_json['OrdStatus']) + "," + str(straight_json['SecurityID']) + "," + str(straight_json['ClOrdID']) + "," + str(straight_json['OrderID']) + "," + str(straight_json['ClOrdID']) + "," + \ + str(straight_json['OrderRequestID']) + "," + process_json['Side'] + "," + str(straight_json['OrderQty']) + "," + str(straight_json['Price']) + "," + \ + str(straight_json['StopPx']) + "," + process_json['OrdType'] + "," + process_json['TimeInForce'] + "," + process_json['OFMOverride'] + "," + str(process_json['DisplayQty']) + "," + \ + blank_json['MinQty'] + "," + straight_json['Location'] + "," + str(straight_json['LastPx']) + "," + str(straight_json['LastQty']) + "," + str(straight_json['CumQty']) + "," + \ + str(straight_json['LeavesQty']) + "," + process_json['AggressorIndicator'] + "," + process_json['ExecRestatementReason'] + "," + str(straight_json['Text']) + "," + \ + str(straight_json['SideTradeID']) + "," + str(straight_json['CrossID']) + "," + blank_json['QuoteReqID'] + "," + blank_json['QuoteID'] + "," + blank_json['QuoteSetID'] + "," + \ + blank_json['QuoteEntryID'] + "," + blank_json['BidPx'] + "," + blank_json['BidSize'] + "," + blank_json['OfferPx'] + "," + blank_json['OfferSize'] + "," + \ + blank_json['NoProcessedEntries'] + "," + str(straight_json['PartyDetailsListReqID']) + "," + blank_json['ListUpdateAction'] + "," + str(straight_json['SelfMatchPreventionID']) + "," + \ + process_json['SelfMatchPreventionInstruction'] + "," + process_json['CustOrderCapacity'] + "," + blank_json['AvgPxGroupID'] + "," + process_json['AvgPxInd'] + "," + \ + blank_json['CmtaGiveUpCD'] + "," + str(process_json['ClearingAccountType']) + "," + process_json['ClearingTradePriceType'] + "," + process_json['CustOrderHandlingInst'] + "," + \ + EXECUTING_FIRM_ID + "," + str(ACCOUNT_NUMBER) + "," + blank_json['TAKE_UP_FIRM'] + "," + blank_json['TAKE_UP_ACCOUNT'] + + + #print(line_to_write) + + with open (args.output_file, 'a') as the_file: + the_file.write(line_to_write) + the_file.write("\n") + + + + + \ No newline at end of file diff --git a/src/data_files/files/audit_upload.sh b/src/data_files/files/audit_upload.sh new file mode 100755 index 000000000..2aad9604d --- /dev/null +++ b/src/data_files/files/audit_upload.sh @@ -0,0 +1,73 @@ +#/bin/bash + +function help_and_exit() +{ + echo + echo Usage: $0 [option]... config_file + echo + echo Can also be used in cron like the following + echo 40 16 * * 1-5 /usr/bin/docker exec spark_10000 bash /sparkbin/data_files/audit_upload.sh /sparkdata/data/audit_uploads/example.conf + echo example.conf included. The location of the conf file should be relative to the docker container, not the host + echo + echo Options are as follows: + echo " -d|--date YYYYMMDD Example is 20180731" + echo " Help: " + echo " -h|--help Display this message." + echo + exit 1 +} + +DATE=$(date +%Y%m%d) +ORIG_DIR=$(pwd) +ERROR_LEVEL="0" + +while getopts "d:h" option; do + case $option in + d) DATE=$OPTARG ;; + h) help_and_exit;; + \?) echo "Invalid option: -$OPTARG" >&2; + exit 1;; + esac +done + +shift $((OPTIND-1)) + +CONFIG_FILE=$1 + +declare -A conf_vars; while read line; do conf_vars[${line%=*}]=${line#*=}; done <$CONFIG_FILE + +SESSION_ID=${conf_vars[session_id]} +FCM=${conf_vars[fcm]} +CLIENT=${conf_vars[client]} + +cd /sparkbin/data_files + +for SESSION in $(ls /sparkdata/data/ilink3/); do + if [[ $SESSION == *$SESSION_ID* ]]; then + echo "Session is $SESSION" + SESSION_DAY=$(date +%Y-%m-%d) + if [[ $SESSION == "cme_"* ]]; then + SESSION_DAY=$(date -d "$DATE -$(date -d $DATE +%u) days" +"%Y-%m-%d") + AUDIT_SCRIPT="audit_ilink3.py" + fi + echo $SESSION_DAY + if [[ ! -d /sparkdata/data/ilink3/$SESSION/audit_logs ]]; then + mkdir -p /sparkdata/data/ilink3/$SESSION/audit_logs + fi + if [[ -d /sparkdata/data/ilink3/$SESSION/$SESSION_DAY ]]; then + echo "Starting the creation of the audit log" + python $AUDIT_SCRIPT -d $DATE /sparkdata/data/ilink3/$SESSION/$SESSION_DAY/log /sparkdata/data/ilink3/$SESSION/audit_logs/"$FCM"_audit_"$SESSION"_"$DATE".csv + fi + if [[ ! -d /sparkdata/data/md_audit ]]; then + mkdir /sparkdata/data/md_audit + fi + if [[ -d /sparkdata/data/md_audit ]]; then + # Running the MD Audit csv generation + cd /sparkbin + ./sparkd adminreport md > /sparkdata/data/md_audit/"$FCM"_md_"$CLIENT"_"$DATE".csv + cd /sparkbin/data_files + fi + echo "Uploading to the (s)FTP Server" + python audit_uploader.py --config_file="$CONFIG_FILE" --datestamp $DATE --session="$SESSION" --client="$CLIENT" --fcm="$FCM" + fi +done diff --git a/src/data_files/files/audit_uploader.py b/src/data_files/files/audit_uploader.py new file mode 100755 index 000000000..51dcbc5ca --- /dev/null +++ b/src/data_files/files/audit_uploader.py @@ -0,0 +1,150 @@ +import getopt, sys, datetime +import paramiko, ftplib + +######################################################### +# Commandline Args # +######################################################### + +def usage(): + print "-------------------------------------------------------" + print "-Usage: -" + print "-------------------------------------------------------" + print "- --config_file= : Path to upload config file -" + print "- -if no path is specified look -" + print "- in the working directory. -" + print "- -" + print "- --help : Print this message -" + print "- -" + print "- --datestamp= : Optional, defaults to today -" + print "- -replaces *datestamp in the -" + print "- the upload config file -" + print "- -" + print "- --fcm : Optional, spark input -" + print "- -" + print "- --session : Optional, spark jf session -" + print "-------------------------------------------------------" + +fcm = '' +session = '' + +try: + options, arguments = getopt.getopt(sys.argv[1:],"h",['config_file=', 'datestamp=', 'help', 'fcm=', 'client=', 'session=']) +except getopt.GetoptError, err: + print str(err) + usage() + sys.exit(2) + +for option, value in options: + if option in ("-h","--h","-help","--help"): + usage() + sys.exit() + elif option in ("-config_file","--config_file"): + config_file = value + elif option in ("-datestamp","--datestamp"): + datestamp = value + elif option in ("-fcm","--fcm"): + fcm = value + elif option in ("-client","--client"): + client = value + elif option in ("-session","--session"): + session = value + +for argument in arguments: + if argument in ("h", "help"): + usage() + sys.exit() + else: + print "Unrecognized commandline argument. Type help for usage." + sys.exit() + +if len(options) <= 0: + print "Requires a config file to run." + usage() + sys.exit() + + +######################################################### +# Read Config Function # +######################################################### + +def read_config_file(config_file, datestamp=datetime.datetime.today().strftime('%Y%m%d'), fcm=fcm, session=session): + ''' + Parse our config file that gets passed in as a commandline parameter. + All config keys must be of the form key=value. White space is ignored. + All config keys must be seperated by a newline character. + Called during initialization. + ''' + upload_config = {} + config_file = open(config_file, 'r') + config_file = config_file.read() + for config in config_file.split("\n"): + if '=' in config: + cleaned_key = config.split('=')[0].strip().upper() + cleaned_value = config.split('=')[1].strip() + if '*datestamp*' in cleaned_value: + cleaned_value = cleaned_value.replace('*datestamp*',datestamp) + if '*fcm*' in cleaned_value: + cleaned_value = cleaned_value.replace('*fcm*',fcm) + if '*client*' in cleaned_value: + cleaned_value = cleaned_value.replace('*client*',client) + if '*session*' in cleaned_value: + cleaned_value = cleaned_value.replace('*session*',session) + upload_config[cleaned_key] = cleaned_value + else: + pass + return upload_config + +######################################################### +# FTP File Transfer Function # +######################################################### +def putfile_ftp(file, put_path, site, user, password, port=21, verbose=True): + if verbose: print 'Uploading', file + local = open(file, 'rb') + remote = ftplib.FTP(site) + remote.login(user, password) + remote.storbinary(put_path, local, 1024) + remote.quit() + local.close() + if verbose: print 'Upload done.' + +######################################################### +# SFTP File Transfer Function # +######################################################### +def putfile_sftp(file, put_path, site, username, password, port=22, verbose=True): + transport = paramiko.Transport((site, port)) + transport.connect(username = username, password = password) + sftp = paramiko.SFTPClient.from_transport(transport) + sftp.put(file, put_path) + sftp.close() + transport.close() + + +######################################################### +# Main # +######################################################### +#if datestamp != None: +# upload_config = read_config_file(config_file, datestamp) +#else: +# upload_config = read_config_file(config_file) + +try: + datestamp +except NameError: + upload_config = read_config_file(config_file) +else: + upload_config = read_config_file(config_file, datestamp) + + + +print upload_config + +if upload_config["PROTOCOL_PUT"] in ['FTP','ftp']: + print "Protocol = FTP" + putfile_ftp(upload_config["GET_ILINK3_FILE_PATH"] + upload_config["GET_ILINK3_FILE_NAME"], upload_config["PUT_ILINK3_FILE_PATH"] + upload_config["PUT_ILINK3_FILE_NAME"], upload_config["PUT_HOST"], upload_config["USERNAME_PUT"], upload_config["PASSWORD_PUT"], upload_config["PORT_PUT"] ) + putfile_ftp(upload_config["GET_MD_FILE_PATH"] + upload_config["GET_MD_FILE_NAME"], upload_config["PUT_MD_FILE_PATH"] + upload_config["PUT_MD_FILE_NAME"], upload_config["PUT_HOST"], upload_config["USERNAME_PUT"], upload_config["PASSWORD_PUT"], upload_config["PORT_PUT"] ) +elif upload_config["PROTOCOL_PUT"] in ['SFTP','sftp']: + print "Protocol = SFTP" + putfile_sftp(upload_config["GET_ILINK3_FILE_PATH"] + upload_config["GET_ILINK3_FILE_NAME"], upload_config["PUT_ILINK3_FILE_PATH"] + upload_config["PUT_ILINK3_FILE_NAME"], upload_config["PUT_HOST"], upload_config["USERNAME_PUT"], upload_config["PASSWORD_PUT"], int(upload_config["PORT_PUT"]) ) + putfile_sftp(upload_config["GET_MD_FILE_PATH"] + upload_config["GET_MD_FILE_NAME"], upload_config["PUT_MD_FILE_PATH"] + upload_config["PUT_MD_FILE_NAME"], upload_config["PUT_HOST"], upload_config["USERNAME_PUT"], upload_config["PASSWORD_PUT"], int(upload_config["PORT_PUT"]) ) + + diff --git a/src/data_files/files/example.conf b/src/data_files/files/example.conf new file mode 100644 index 000000000..12886ba6b --- /dev/null +++ b/src/data_files/files/example.conf @@ -0,0 +1,16 @@ +fcm=FCM +client=NAME +session_id=ses +put_host=ftpc.example.com +port_put=2222 +username_put=user +password_put=password +protocol_put=sftp +get_ilink3_file_path=/sparkdata/data/ilink3/*session*/audit_logs/ +put_ilink3_file_path=/ +get_ilink3_file_name=*fcm*_audit_*session*_*datestamp*.csv +put_ilink3_file_name=*fcm*_audit_*session*_*datestamp*.csv +get_md_file_path=/sparkdata/data/md_audit/ +put_md_file_path=/ +get_md_file_name=*fcm*_md_*client*_*datestamp*.csv +put_md_file_name=*fcm*_md_*client*_*datestamp*.csv diff --git a/src/lib/logging/Sink.cpp b/src/lib/logging/Sink.cpp index 826e7cdc0..d95c96f37 100644 --- a/src/lib/logging/Sink.cpp +++ b/src/lib/logging/Sink.cpp @@ -57,7 +57,7 @@ auto formatter(LoggingConfig const &cfg) { Sink console_sink(LoggingConfig const &) { return [](LogMsg const &m) { - std::cout << fmt::format("{:<8} | {}", m.subsystem_name, m.msg) + std::cout << fmt::format("{:<8} | {:<5} | {}", m.subsystem_name, m.level_name,m.msg) << std::endl; }; } diff --git a/src/lib/logging/logging.cpp b/src/lib/logging/logging.cpp index 12a187a07..eb8729ba3 100644 --- a/src/lib/logging/logging.cpp +++ b/src/lib/logging/logging.cpp @@ -121,7 +121,7 @@ void configure_logging(bts::config::Logging const&cfg, bts::config::ThreadGroupC {log::Sdk,"Sdk"}, {log::Http,"Http"}, {log::Spartan,"Spartan"}, - {log::iLink3,"ilink3"}, + {log::iLink3,"iLink3"}, }; real_cfg.sinks["console"] = diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.cpp index 0cd1c34ad..573266213 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.cpp @@ -28,6 +28,48 @@ void FifoPriceLevelMatcher::_add_order(MDP3OrderKey key, MDP3Quantity quantity){ _orders.emplace_back(key, quantity); } +void FifoPriceLevelMatcher::_reload_icebergs(){ + _aux_usage.clear(); + auto mv_ibs = [&](const Order& o){ + if(o.ready_to_load_ib()){ + _aux_usage.push_back(o); + return true; + } + return false; + }; + _orders.erase(std::remove_if(_orders.begin(),_orders.end(),mv_ibs), + _orders.end()); + // add it back + for (auto& o : _aux_usage){ + o.reload_ib(); + _orders.push_back(o); + } + _aux_usage.clear(); +} +std::tuple + FifoPriceLevelMatcher::_one_pass_cross_order(MDP3Quantity cross_qty){ + // go through all the orders and see which ones get filled + // (orders are sorted by priority) + for(auto& o : _orders){ + if(o.is_active){ + auto[o_rem, cross_rem] = o.cross(cross_qty.value()); + cross_qty = MDP3Quantity(cross_rem); + if(cross_rem > 0) + bts_assert(o_rem==0); // visible qty should be completely crossed + else + break; + } + } + if(cross_qty.value() > 0) + bts_assert(quantity().value()==0); + // reload the filled ib's and put them at the back + _reload_icebergs(); + _compress(); + return std::make_tuple(total_quantity(), cross_qty); +} + +// constructors /////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// FifoPriceLevelMatcher::FifoPriceLevelMatcher ( md::IntPrice price, api::Side side) @@ -36,9 +78,6 @@ _price(price), _side(side), _orders() {} - -// constructors /////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// FifoPriceLevelMatcher::FifoPriceLevelMatcher ( const FifoPriceLevelMatcher& o){ *this = o; @@ -98,23 +137,12 @@ void FifoPriceLevelMatcher::on_order_remove(MDP3OrderKey k){ _compress(); } MDP3Quantity FifoPriceLevelMatcher::on_new_opp_order(MDP3Quantity cross_qty){ - auto rem = cross_qty.value(); - // go through all the orders and see which ones get filled - // (orders are sorted by priority) - for(auto& o : _orders){ - if(o.is_active){ - auto[o_rem, cross_rem] = o.cross(rem); - rem = cross_rem; - if(rem > o.quantity.value()) - bts_assert(o_rem==0); - } - if(rem == 0) - // cross order has been consumed completely - break; - } - _compress(); - bts_assert(rem <= cross_qty.value()); - return MDP3Quantity(rem); + auto rv = _one_pass_cross_order(cross_qty); + // continue until either the crossing order is consumed + // or the price level is consumed + while (std::get<0>(rv).value()>0 && std::get<1>(rv).value()>0) + rv = _one_pass_cross_order(std::get<1>(rv)); + return std::get<1>(rv); } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.h index 42a5cc514..c52fda397 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/FifoPriceLevelMatcher.h @@ -23,16 +23,39 @@ public: } return std::make_tuple(resting_qty,cross_qty); } + struct IB{ + bool is_ib; + MDP3Quantity show_qty; + MDP3Quantity total_qty; + IB (bool is_ib, + MDP3Quantity show_qty, + MDP3Quantity total_qty) + : + is_ib(is_ib), + show_qty(show_qty), + total_qty(total_qty) + {} + }; struct Order{ MDP3OrderKey key; MDP3Quantity quantity; - bool is_active=true; - + bool is_active=true; + IB ib; Order (MDP3OrderKey key, - MDP3Quantity quantity) + MDP3Quantity qty, // working qty out there + MDP3Quantity show_qty, + MDP3Quantity total_qty) : key(key), - quantity(quantity) + quantity(qty), + ib(show_qty.value()>0,show_qty,total_qty){ + if(qty < show_qty) + bts_assert(total_qty == qty); + } + Order (MDP3OrderKey key, + MDP3Quantity qty) + : + Order(key,qty,MDP3Quantity(0),MDP3Quantity(qty)) {} // returns the remainder of this order, remainder of opp order std::tuple cross (uint32_t cross_qty){ @@ -41,11 +64,28 @@ public: is_active = quantity.value() > 0; return std::make_tuple(r,c); } + MDP3Quantity total_qty() const{ + return ib.is_ib ? ib.total_qty : quantity; + } + MDP3Quantity working_qty() const{ + return quantity; + } + bool ready_to_load_ib() const{ + return ib.is_ib && quantity.value() == 0 && ib.total_qty.value()>0; + } + void reload_ib(){ + if (ready_to_load_ib()){ + auto load_qty = std::min(ib.show_qty.value(),ib.total_qty.value()); + quantity = MDP3Quantity(load_qty); + ib.total_qty = MDP3Quantity(ib.total_qty.value()-ib.show_qty.value()); + } + } }; private: md::IntPrice _price; api::Side _side; std::vector _orders; + std::vector _aux_usage; private: // helpers ///////////////////////////////////////////////////////////////// void _compress(); @@ -53,6 +93,11 @@ private: void _logically_delete(Order& o); void _add_order(MDP3OrderKey key, MDP3Quantity quantity); + // crosses the cross_order with existing orders. returns + // the remaining qty in the level and the cross order + void _reload_icebergs(); + std::tuple _one_pass_cross_order( + MDP3Quantity cross_order); public: // constructors //////////////////////////////////////////////////////////// FifoPriceLevelMatcher (md::IntPrice price, @@ -72,18 +117,21 @@ public: } bool operator<(const FifoPriceLevelMatcher& o) const; // simple accessors //////////////////////////////////////////////////////// - std::tuple n_orders_qty() const{ - uint32_t n_orders = 0, tot_qty = 0; + std::tuple n_orders_qty(bool visible_only = true) const{ + auto [c,q] = std::make_tuple(0,0); for(const auto& o : _orders) if(o.is_active){ - n_orders++; - tot_qty += o.quantity.value(); + c++; + q += visible_only ? o.working_qty().value() : o.total_qty().value(); } - return std::make_tuple(n_orders,tot_qty); + return std::make_tuple(c,q); } MDP3Quantity quantity() const{ return MDP3Quantity(std::get<1>(n_orders_qty())); } + MDP3Quantity total_quantity() const{ + return MDP3Quantity(std::get<1>(n_orders_qty(false))); + } uint32_t n_orders() const{ return std::get<0>(n_orders_qty()); } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h index 2575d3e6d..ebc8e58e3 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h @@ -75,6 +75,16 @@ struct MDP3Order { MDP3OrderPriority priority; md::IntPrice price; MDP3Quantity quantity; + // (verbose) placeholder to get started. + // we could have just added total_quantity + // total_quantity != quantity could be used + // to detect an ib + struct IB{ + bool is_ib; + MDP3Quantity show_quantity; + MDP3Quantity total_quantity; + }; + IB ib; }; struct OrderChange { diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp index 5ddfad55d..d3e522dc5 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/recovery/RecoveryHandler.cpp @@ -424,7 +424,9 @@ void RecoveryHandler::update_book_chunk_in_snapshot( MDP3OrderPriority{md_entry.mDOrderPriority()}, md::IntPrice{md_entry.mDEntryPx().mantissa(), md_entry.mDEntryPx().exponent()}, - MDP3Quantity{md_entry.mDDisplayQty()}}); + MDP3Quantity{md_entry.mDDisplayQty()}, + MDP3Order::IB{false,MDP3Quantity{0},MDP3Quantity{0}} + }); }); } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp index e64d64378..3faaa4598 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/tests/spartan_md_tests.cpp @@ -272,7 +272,8 @@ MDP3Order create_order(uint64_t key, side, MDP3OrderPriority(priority), md::IntPrice_from_double<7>(price), - MDP3Quantity(qty) + MDP3Quantity(qty), + MDP3Order::IB{false,MDP3Quantity{0},MDP3Quantity{0}} }; }