def read_rtn(): dnj['tt']['pReadRTN'] = dnj_trade_common.get_currentTT() # global dnj['cfg'] read_rtn_args = ["tail", "-f", "-n1", dnj['cfg']['filenamee_read_rtn']] read_rtn = subprocess.Popen(read_rtn_args, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: #buff1 = read_rtn.stdout.readline().decode("utf-8") buff1 = read_rtn.stdout.readline() tt_1 = dnj_trade_common.get_currentTT() time_1 = tt_1['timestamp_micro'] if buff1 == '' and read_rtn.poll() != None: continue else: parse_rtn(buff1)
def read_in_irr(): dnj['tt']['pReadInIrr'] = dnj_trade_common.get_currentTT() # global dnj['cfg'] read_in_irr_args = ["tail", "-f", "-n1", dnj['cfg']['filenamee_write_result']] sub_read_in_irr = subprocess.Popen(read_in_irr_args, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: buff1 = sub_read_in_irr.stdout.readline().decode("utf-8").strip() #buff1 = sub_read_in_irr.stdout.readline() tt_1 = dnj_trade_common.get_currentTT() time_1 = tt_1['timestamp_micro'] if buff1 == '' and sub_read_in_irr.poll() != None: continue else: parse_in_irr(buff1)
def parse_rsp(line_this): fields = str(line_this).split(",") if len(fields) < 20: # print("rsp <20") return False if int(fields[ 1]) < int(dnj['tt']['Dstart']["timestamp_micro"]): # print ("rsp < Dstart") return False # trr12 OnRspOrderInsert 报单录入应答 if fields[2] == "trr12": InputOrder_one = parse_trr12(fields) if InputOrder_one: dnj_trade_common.InputOrder[InputOrder_one['UserOrderLocalID']] = InputOrder_one.copy() # trr13 OnRspOrderAction() 报单操作应答 if fields[2] == "trr13": OrderAction_one = parse_trr13(fields) if OrderAction_one: dnj_trade_common.OrderAction[OrderAction_one['UserOrderActionLocalID']] = OrderAction_one.copy() # trr35 OnRspQryOrder() 报单查询应答 if fields[2] == "trr35": QueryOrder_one = parse_trr35(fields) #pprint.pprint(fields) if QueryOrder_one: dnj_trade_common.QueryOrder[QueryOrder_one['UserOrderLocalID']] = QueryOrder_one.copy() # trr39 OnRspQryInvestorAccount() 投资者资金账户查询应答 // much info if fields[2] == "trr39": InvestorAccount_one = parse_trr39(fields) #pprint.pprint(fields) if InvestorAccount_one: for key, value in InvestorAccount_one.items(): dnj_trade_common.InvestorAccount[key] = value # trr40 OnRspQryInstrument 合约查询应答 if fields[2] == "trr40": Instrument_one = parse_trr40(fields) #pprint.pprint(fields) if Instrument_one: dnj_trade_common.Instrument[Instrument_one['InstrumentID']] = Instrument_one.copy() tt_last = dnj_trade_common.get_currentTT() dnj_trade_common.rr_lastupdate['trr40'] = tt_last["timestamp_micro"] # trr42 OnRspQryInvestorPosition 投资者持仓查询应答 if fields[2] == "trr42": InvestorPosition_one = parse_trr42(fields) #pprint.pprint(fields) if InvestorPosition_one: dnj_trade_common.InvestorPosition[InvestorPosition_one['InstrumentID']+","+InvestorPosition_one['Direction']] = InvestorPosition_one.copy() tt_last = dnj_trade_common.get_currentTT() dnj_trade_common.rr_lastupdate['trr42'] = tt_last["timestamp_micro"]
def form_tqq05(OrderSysID, UserOrderLocalID): tt_now = dnj_trade_common.get_currentTT() fields = [] # first 10 reserved fields fields.append( tt_now["datentime_micro"] ) #0 fields.append( str(tt_now["timestamp_micro"]) ) #1, UserOrderLocalID (char [21]) (if needed) fields.append( "tqq05" ) #2 fields.append( "ReqOrderAction()" ) #3 fields.append( "tcmd" ) #4 fields.append( "in" ) #5, co(console manual)/au(auto)/in(in.txt) fields.append( "in001" ) #4 fields.append( tt_now["nRequestID"] ) #7 nRequestID (int), 1553[826774366]354 for x in range(12): # 12times (#8 - #19) fields.append( "" ) # data start from [#20], 21st fields fields.append( dnj['cfg']['gExchangeID'] ) # "CFFEX" ExchangeID ///交易所代码,必填字段(支持中金所、上期所、大商所、郑商所) fields.append( OrderSysID ) # OrderSysID ///交易所系统报单编号(不为空按该字段操作,为空按本地报单编号操作) TUstpFtdcOrderSysIDType fields.append( dnj['cfg']['gBrokerID'] ) # "0059" BrokerID ///经纪公司编号,必填字段 fields.append( dnj['cfg']['gInvestorID'] ) # "990407" InvestorID ///投资者编号,必填字段 (客户资金账户) fields.append( dnj['cfg']['gUserID'] ) # "990407" UserID //用户代码,必填字段(飞马用户代码) fields.append( str(tt_now["timestamp_micro"]) ) # UserOrderActionLocalID ///本次撤单Req的本地编号,必填字段 (需要保证按字典序递增) TUstpFtdcUserOrderLocalIDType # (直接用 timestamp, 一定比原來的 UserOrderLocalID 大) fields.append( UserOrderLocalID ) # UserOrderLocalID ///被撤订单的本地报单编号 TUstpFtdcUserOrderLocalIDType fields.append( '0' ) # "0" ActionFlag ///报单操作标志,必填字段(只支持删除) TUstpFtdcActionFlagType fields.append( "~X~" ) # LimitPrice ///价格 (暂不支持,保留域) TUstpFtdcPriceType fields.append( "~X~" ) # VolumeChange ///数量变化(暂不支持,保留域) TUstpFtdcVolumeType fields.append( "~X~" ) # BusinessLocalID ///本地业务标识 TUstpFtdcBusinessLocalIDType fields.append( dnj['cfg']['gClientID'] ) # "01568909" ClientID ///客户编码 return fields
def form_tqq19(InstrumentID): # should clear InvestorPosition first if InstrumentID == "": dnj_trade_common.InvestorPosition.clear() else: key0 = InstrumentID + ",0" key1 = InstrumentID + ",1" if key0 in dnj_trade_common.InvestorPosition: del dnj_trade_common.InvestorPosition[key0] if key1 in dnj_trade_common.InvestorPosition: del dnj_trade_common.InvestorPosition[key1] tt_now = dnj_trade_common.get_currentTT() fields = [] # first 10 reserved fields fields.append( tt_now["datentime_micro"] ) #0 fields.append( str(tt_now["timestamp_micro"]) ) #1, UserOrderLocalID (char [21]) (if needed) fields.append( "tqq19" ) #2 fields.append( "ReqQryInvestorPosition()" ) #3 fields.append( "tcmd" ) #4 fields.append( "in" ) #5, co(console manual)/au(auto)/in(in.txt) fields.append( "in001" ) #4 fields.append( tt_now["nRequestID"] ) #7 nRequestID (int), 1553[826774366]354 for x in range(12): # 12times (#8 - #19) fields.append( "" ) # data start from [#20], 21st fields fields.append( dnj['cfg']['gBrokerID'] ) # "0059" BrokerID ///经纪公司编号,必填字段 fields.append( dnj['cfg']['gUserID'] ) # "990407" UserID //用户代码,必填字段(飞马用户代码) fields.append( dnj['cfg']['gExchangeID'] ) # "CFFEX" ExchangeID ///交易所代码,必填字段(支持中金所、上期所、大商所、郑商所) fields.append( dnj['cfg']['gInvestorID'] ) # "990407" InvestorID ///投资者编号,必填字段 (客户资金账户) fields.append( InstrumentID ) # "" InstrumentID ///合约代码, 可以填寫 空字串 (回傳全部) return fields
def form_tqq12(OrderSysID, InstrumentID, OrderStatus): tt_now = dnj_trade_common.get_currentTT() fields = [] # first 10 reserved fields fields.append( tt_now["datentime_micro"] ) #0 fields.append( str(tt_now["timestamp_micro"]) ) #1, UserOrderLocalID (char [21]) (if needed) fields.append( "tqq12" ) #2 fields.append( "ReqQryOrder()" ) #3 fields.append( "tcmd" ) #4 fields.append( "in" ) #5, co(console manual)/au(auto)/in(in.txt) fields.append( "in001" ) #4 fields.append( tt_now["nRequestID"] ) #7 nRequestID (int), 1553[826774366]354 for x in range(12): # 12times (#8 - #19) fields.append( "" ) # data start from [#20], 21st fields fields.append( dnj['cfg']['gBrokerID'] ) # "0059" BrokerID ///经纪公司编号,必填字段 fields.append( dnj['cfg']['gUserID'] ) # "990407" UserID //用户代码,必填字段(飞马用户代码) fields.append( "~X~" ) # ExchangeID ///交易所代码 TUstpFtdcExchangeIDType fields.append( dnj['cfg']['gInvestorID'] ) # "990407" InvestorID ///投资者编号,必填字段 (客户资金账户) fields.append( OrderSysID ) # "~X~" OrderSysID ///系统报单编号 TUstpFtdcOrderSysIDType fields.append( InstrumentID ) # "~X~" InstrumentID ///合约代码 TUstpFtdcInstrumentIDType fields.append( OrderStatus ) # "" OrderStatus ///报单状态 TUstpFtdcOrderStatusType fields.append( "~X~" ) # OrderType ///委托类型 TUstpFtdcOrderTypeType fields.append( "~X~" ) # ClientID ///客户编码 TUstpFtdcClientIDType return fields
def read_rtn_mrr11(): dnj['tt']['pReadRTNmr11'] = dnj_trade_common.get_currentTT() #global dnj['cfg'] read_rtn_mrr11_args = ["tail", "-f", "-n1", dnj['cfg']['filenamee_read_rtn_mrr11']] read_rtn_mrr11 = subprocess.Popen(read_rtn_mrr11_args, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: buff2 = read_rtn_mrr11.stdout.readline().decode("utf-8") #buff2 = read_rtn_mrr11.stdout.readline() if buff2 == '' and read_rtn_mrr11.poll() != None: continue else: depdata_one = parse_mrr11(buff2) if depdata_one: dnj_trade_common.deptmarketdata[depdata_one['InstrumentID']] = depdata_one.copy() tt_last = dnj_trade_common.get_currentTT() dnj_trade_common.rr_lastupdate['mrr11'] = tt_last["timestamp_micro"] terminal_update_mrr11(depdata_one);
def write_in(fields): q_11 = dnj_trade_common.get_currentTT() fields[11] = str(q_11["timestamp_micro"]); str_app = "" for idx in range(0, len(fields)): #print (">%d:%s<" %(idx,fields[idx])) str_app += fields[idx] + "," #0 # print(str_app) if dnj_trade_common.fh_filenamee_write.closed: dnj_trade_common.fh_filenamee_write = open(dnj['cfg']['filenamee_write'], "a") # print (">>>>>>>>>>>>>>>>do a open: %s" %(dnj['cfg']['filenamee_write'])) dnj_trade_common.fh_filenamee_write.write(str_app + "\n") dnj_trade_common.fh_filenamee_write.flush() #dnj_trade_common.fh_filenamee_write.close() q_12 = dnj_trade_common.get_currentTT() #print ("%s (編號:%s) 全部p:%6d (mq前:%6d, mq t12-t11:%5d) (q12:%d [網路])" %(fields[2], fields[1], q_12["timestamp_micro"]-int(fields[1]), q_11["timestamp_micro"] - int(fields[1]), q_12["timestamp_micro"] - q_11["timestamp_micro"], q_12["timestamp_micro"]))
def read_rtn_trr22(): dnj['tt']['pReadRTNtrr22'] = dnj_trade_common.get_currentTT() #global dnj['cfg'] read_rtn_trr22_args = ["tail", "-f", "-n1", dnj['cfg']['filenamee_read_rtn_trr22']] read_rtn_trr22 = subprocess.Popen(read_rtn_trr22_args, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: buff2 = read_rtn_trr22.stdout.readline().decode("utf-8") #buff2 = read_rtn_trr22.stdout.readline() if buff2 == '' and read_rtn_trr22.poll() != None: continue else: xxx_one = parse_trr22(buff2)
def parse_in_irr (line_this): #global tqq16_irr fields = str(line_this).split(",") #pprint.pprint(fields) if fields[2] == "tqq16": dnj_trade_common.tqq16_irr = int(fields[8]) tt_last = dnj_trade_common.get_currentTT() dnj_trade_common.rr_lastupdate['in_irr'] = tt_last["timestamp_micro"] dnj['check']['isJConnected'] = True dnj['check']['isFConnected'] = True if dnj_trade_common.tqq16_irr == -1: dnj['check']['isFConnected'] = False
def terminal_main(): dnj['tt']['pTerminalMain'] = dnj_trade_common.get_currentTT() print(chr(27) + "[2J") # clear the screen # update option # update trading # updat balance #print ("%d %d %d %d" %(len(Instrument.keys()), len(deptmarketdata.keys()), len(OrderAction.keys()), len(InputOrder.keys()))) #time.sleep(1) # the inpu while True: line = get_input_str(28, 1) sys.stdout.write("\x1b7\x1b[%d;%df%-30s\x1b8" % (27, 1, "pre: " + line)) sys.stdout.write("\x1b7\x1b[%d;%df%-30s\x1b8" % (28, 1, "cmd: ")) sys.stdout.flush() line = line.strip() if line == "i": msg = "b:bool s:dict c:path 1:IF1905 2:IH 3:IO 4:tqq19" terminal_update_msg(29, 1, msg) if line == "b": msg = ( "Femas:%s J:%s mrr11:%s " % (str(dnj['check']['isFConnected']), str(dnj['check']['isJConnected']), str(dnj['check']['isMrr11Coming']))) terminal_update_msg(29, 1, msg) if line == "s": msg = ("%d %d %d %d %d" % (len(dnj_trade_common.Instrument.keys()), len(dnj_trade_common.deptmarketdata.keys()), len(dnj_trade_common.OrderAction.keys()), len(dnj_trade_common.InputOrder.keys()), len(dnj_trade_common.InvestorAccount.keys()))) terminal_update_msg(29, 1, msg) if line == "1": write_in(form_tqq17("", "IF1905")) if line == "2": write_in(form_tqq17("", "IH")) if line == "3": write_in(form_tqq17("", "IO")) if line == "4": write_in(form_tqq19("")) if line == "c": msg = ("%s/%s" % (dnj['cfg']['basenamee'], dnj['cfg']['datenamee'])) terminal_update_msg(29, 1, msg)
def check_connectivity(): write_in(form_tqq16()) # tqq16 ReqQryInvestorAccount() tt_last = dnj_trade_common.get_currentTT() if (tt_last["timestamp_micro"] - dnj_trade_common.rr_lastupdate['in_irr']) > 10 * 1000000: dnj['check']['isJConnected'] = False else: dnj['check']['isJConnected'] = True if (tt_last["timestamp_micro"] - dnj_trade_common.rr_lastupdate['mrr11']) > 5 * 1000000: dnj['check']['isMrr11Coming'] = False else: dnj['check']['isMrr11Coming'] = True
def form_tqq04(InstrumentID, OrderPriceType, Direction, OffsetFlag, LimitPrice, Volume, TimeCondition, VolumeCondition): tt_now = dnj_trade_common.get_currentTT() fields = [] # first 10 reserved fields fields.append( tt_now["datentime_micro"] ) #0 fields.append( str(tt_now["timestamp_micro"]) ) #1, UserOrderLocalID (char [21]) (if needed) fields.append( "tqq04" ) #2 fields.append( "ReqOrderInsert()" ) #3 fields.append( "tcmd" ) #4 fields.append( "in" ) #5, co(console manual)/au(auto)/in(in.txt) fields.append( "in001" ) #4 fields.append( tt_now["nRequestID"] ) #7 nRequestID (int), 1553[826774366]354 for x in range(12): # 12times (#8 - #19) fields.append( "" ) # data start from [#20], 21st fields fields.append( dnj['cfg']['gBrokerID'] ) # "0059" BrokerID ///经纪公司编号,必填字段 fields.append( dnj['cfg']['gExchangeID'] ) # "CFFEX" ExchangeID ///交易所代码,必填字段(支持中金所、上期所、大商所、郑商所) fields.append( "~X~" ) # "" OrderSysID ///系统报单编号 fields.append( dnj['cfg']['gInvestorID'] ) # "990407" InvestorID ///投资者编号,必填字段 (客户资金账户) fields.append( dnj['cfg']['gUserID'] ) # "990407" UserID //用户代码,必填字段(飞马用户代码) fields.append( "~X~" ) # "" SeatNo ///指定下单席位编号(取值范围[1-N],N为可用席位数目,超出范围的随机分配席位) fields.append( InstrumentID ) # "IF1904" InstrumentID ///合约代码,必填字段 fields.append( str(tt_now["timestamp_micro"])) # "00001" UserOrderLocalID ///用户本地报单号,必填字段 fields.append( OrderPriceType ) # '3' OrderPriceType ///(option 只能 限价2, 不能 任意價格市价1) 报单价格条件,必填字段(支持市价1、限价2、五档价4 和最优价3) fields.append( Direction ) # '0' Direction ///(# 0 买, 1 卖, 9 All) 买卖方向,必填字段 fields.append( OffsetFlag ) # '0' OffsetFlag ///开平标志,必填字段(中金所支持开仓0和平仓1,上期所支持开仓、平今和平昨) fields.append( '1' ) # '1' HedgeFlag ///投机套保标志,必填字段(支持投机、套保、套利、做市商编码) fields.append( LimitPrice ) # "500" LimitPrice ///价格,必填字段 fields.append( Volume ) # '1' Volume ///数量,必填字段 fields.append( TimeCondition ) # '3' TimeCondition ///有效期类型,必填字段(“立即完成,否则撤销”1 OR“当日有效”3) fields.append( "~X~" ) # "" GTDDate ///GTD日期(暂不支持,保留域) fields.append( VolumeCondition ) # '1' VolumeCondition ///成交量类型,必填字段(支持“任意数量 1” 和 “全部数量 3”) fields.append( "~X~" ) # "" MinVolume ///最小成交量(暂不支持,保留域) fields.append( "~X~" ) # "" StopPrice ///止损价(暂不支持,保留域) fields.append( '0' ) # '0' ForceCloseReason ///强平原因,只支持”非强平” ? (暂不支持,保留域) fields.append( "~X~" ) # "" IsAutoSuspend ///自动挂起标志(暂不支持,保留域) fields.append( "~X~" ) # "" BusinessUnit ///业务单元(暂不支持,保留域) fields.append( "self" ) # "self" UserCustom ///用户自定义域 64字节 fields.append( "~X~" ) # "" BusinessLocalID ///本地业务标识(暂不支持,保留域) fields.append( "~X~" ) # "" ActionDay ///业务发生日期 fields.append( "~X~" ) # "" ArbiType ///策略类别(套利组合业务必填字段) fields.append( dnj['cfg']['gClientID'] ) # "01568909" ClientID ///客户编码 return fields
def form_tqq16(): tt_now = dnj_trade_common.get_currentTT() fields = [] # first 10 reserved fields fields.append( tt_now["datentime_micro"] ) #0 fields.append( str(tt_now["timestamp_micro"]) ) #1, UserOrderLocalID (char [21]) (if needed) fields.append( "tqq16" ) #2 fields.append( "ReqQryInvestorAccount()" ) #3 fields.append( "tcmd" ) #4 fields.append( "in" ) #5, co(console manual)/au(auto)/in(in.txt) fields.append( "in001" ) #4 fields.append( tt_now["nRequestID"] ) #7 nRequestID (int), 1553[826774366]354 for x in range(12): # 12times (#8 - #19) fields.append( "" ) # data start from [#20], 21st fields fields.append( dnj['cfg']['gBrokerID'] ) # "0059" BrokerID ///经纪公司编号,必填字段 fields.append( dnj['cfg']['gUserID'] ) # "990407" UserID //用户代码,必填字段(飞马用户代码) fields.append( dnj['cfg']['gInvestorID'] ) # "990407" InvestorID ///投资者编号,必填字段 (客户资金账户) return fields
def form_tqq17(ProductID, InstrumentID): tt_now = dnj_trade_common.get_currentTT() fields = [] # first 10 reserved fields fields.append( tt_now["datentime_micro"] ) #0 fields.append( str(tt_now["timestamp_micro"]) ) #1, UserOrderLocalID (char [21]) (if needed) fields.append( "tqq17" ) #2 fields.append( "ReqQryInstrument()" ) #3 fields.append( "tcmd" ) #4 fields.append( "in" ) #5, co(console manual)/au(auto)/in(in.txt) fields.append( "in001" ) #4 fields.append( tt_now["nRequestID"] ) #7 nRequestID (int), 1553[826774366]354 for x in range(12): # 12times (#8 - #19) fields.append( "" ) # data start from [#20], 21st fields fields.append( dnj['cfg']['gExchangeID'] )# 交易所代码,必填字段(支持中金所、上期所、大商所、郑商所) fields.append( ProductID )# 产品代码 fields.append( InstrumentID )# 合约代码 return fields
""" import time import threading import dnj_trade_common from dnj_trade_read_spi import read_in_irr, read_rsp, read_rtn, read_rtn_trr22, read_rtn_mrr11 from dnj_trade_write_api import write_in, form_tqq12, form_tqq16, form_tqq19 from dnj_trade_terminal import terminal_main, terminal_update_msg, terminal_update_act_info, terminal_update_connectivity, print_time from dnj_trade_strategy import strategy_main dnj = dnj_trade_common.dnj # program start dnj['tt']['Dstart'] = dnj_trade_common.get_currentTT() # should be load config isLoadCFG = dnj_trade_common.load_cfg_init() if not isLoadCFG: print("error reading cfg") sys.exit(0) # print_time 子執行緒 (只是 print time, 和 keep connection) t_print_time = threading.Thread(target=print_time) t_print_time.start() # read_in_irr 子執行緒 (這個可以當作 qq 的 log) t_read_in_irr = threading.Thread(target=read_in_irr) t_read_in_irr.start()
def strategy_main(): dnj['tt']['pStrategylMain'] = dnj_trade_common.get_currentTT() global ta_IF, ta_IO_C, ta_IO_P, list_IO_aaa_C, list_IO_aaa_P # make sure connection # get IF* contracts, find the most recently (IF1095) (1095) # from IF1095, today's quote (mrr11), find the "pivot", e.g. 4098 # get IO1095-*, find up/down 5 contracts near 4098 (for print) t_update_ta_IF = 0 t_update_position = 0 while True: tt_now = dnj_trade_common.get_currentTT() # update_position_list() # tqq19 ReqQryInvestorPosition() 投资者持仓查询请求 # expect: trr42 OnRspQryInvestorPosition 投资者持仓查询应答 if (tt_now["timestamp_micro"] - t_update_position) > 9 * 1000000: YY_now = 18 for key, value in dnj_trade_common.InvestorPosition.items(): if not value["Position"] == "0": msg = value[ "r_datentime_micro"] + " " + key + " Position:" + value[ "Position"] terminal_update_msg(YY_now, 1, msg) YY_now = YY_now + 1 write_in(form_tqq19("")) t_update_position = tt_now["timestamp_micro"] # time.sleep(1) # update_IF_IO_list() # update if/io target update_IF_IO_list() msg = ("%s IF:%s, IOC:%s, IOP:%s, listIOC:%d, listIOP:%d" % (tt_now["datentime_micro"], ta_IF, ta_IO_C, ta_IO_P, len(list_IO_aaa_C), len(list_IO_aaa_P))) terminal_update_msg(16, 1, msg) if (tt_now["timestamp_micro"] - t_update_ta_IF) > 10 * 59 * 1000000: if dnj_trade_common.dnj['check']['isMrr11Coming']: # tqq12 ReqQryOrder() 报单查询请求 # expect: trr35 OnRspQryOrder() 报单查询应答 write_in(form_tqq12("~X~", "~X~", "1")) time.sleep(1) write_in(form_tqq12("~X~", "~X~", "3")) time.sleep(5) # cancel all pending order # YY_now = 10 for key, value in dnj_trade_common.QueryOrder.items(): # #define USTP_FTDC_OS_AllTraded '0' ///全部成交 # #define USTP_FTDC_OS_PartTradedQueueing '1' ///部分成交还在队列中 # #define USTP_FTDC_OS_PartTradedNotQueueing '2' ///部分成交不在队列中 # #define USTP_FTDC_OS_NoTradeQueueing '3' ///未成交还在队列中 # #define USTP_FTDC_OS_NoTradeNotQueueing '4' ///未成交不在队列中 (錯單) # #define USTP_FTDC_OS_Canceled '5' ///撤单 # #define USTP_FTDC_OS_AcceptedNoReply '6' ///订单已报入交易所未应答 # msg = key + " OrderStatus:" + value['OrderStatus'] # terminal_update_msg(YY_now, 1, msg) # YY_now = YY_now + 1 if value['OrderStatus'] == "1" or value[ 'OrderStatus'] == "3": write_in(form_tqq05("~X~", key)) ## sell all for key, value in dnj_trade_common.InvestorPosition.items(): # form_tqq04(InstrumentID, OrderPriceType, Direction, OffsetFlag, LimitPrice, Volume, TimeCondition, VolumeCondition): # InstrumentID ///合约代码,必填字段 # OrderPriceType ///(option 只能 限价2, 不能 任意價格市价1) 报单价格条件,必填字段(支持市价1、限价2、五档价4 和最优价3) # Direction ///(# 0 买, 1 卖, 9 All) 买卖方向,必填字段 # OffsetFlag ///开平标志,必填字段(中金所支持开仓0和平仓1,上期所支持开仓、平今和平昨) # LimitPrice ///价格,必填字段 # Volume ///数量,必填字段 # TimeCondition ///有效期类型,必填字段(“立即完成,否则撤销”1 OR“当日有效”3) # VolumeCondition ///成交量类型,必填字段(支持“任意数量 1” 和 “全部数量 3”) if not value["Position"] == "0": InstrumentID_pos = value['InstrumentID'] Direction_pos = value['Direction'] Position_pos = value['Position'] FrozenClosing_pos = value['FrozenClosing'] FrozenPosition_pos = value['FrozenPosition'] InstrumentID_order = InstrumentID_pos OrderPriceType_order = "2" Direction_order = "0" if Direction_pos == "1" else "1" OffsetFlag_order = "1" # LimitPrice_order if InstrumentID_order.startswith("IO"): # option if Direction_order == "1": # 1 卖 (這裡故意賣 低價) LimitPrice_order = dnj_trade_common.Instrument[ InstrumentID_order]['LowerLimitPrice'] else: # 0 买 (這裡故意買 高價) LimitPrice_order = dnj_trade_common.Instrument[ InstrumentID_order]['UpperLimitPrice'] else: # future, 市價 +/- 30.0 保護帶 (use 29.0) if Direction_order == "1": # 1 卖 (這裡故意賣 低價) LimitPrice_order = str( float(dnj_trade_common. deptmarketdata[InstrumentID_order] ['LastPrice']) - 29) else: # 0 买 (這裡故意買 高價) LimitPrice_order = str( float(dnj_trade_common. deptmarketdata[InstrumentID_order] ['LastPrice']) + 29) Volume_order = str( int(Position_pos) - int(FrozenClosing_pos) - int(FrozenPosition_pos)) TimeCondition_order = "3" VolumeCondition_order = "1" write_in( form_tqq04(InstrumentID_order, OrderPriceType_order, Direction_order, OffsetFlag_order, LimitPrice_order, Volume_order, TimeCondition_order, VolumeCondition_order)) # 上面的 sell 和 下面的 buy 會造成 # trr12,OnRspOrderInsert(),trsp,138,交易所返回:[订单触发自成交] time.sleep(8 * 60) # update if/io target update_IF_IO_list() msg = ("%s IF:%s, IOC:%s, IOP:%s, listIOC:%d, listIOP:%d" % (tt_now["datentime_micro"], ta_IF, ta_IO_C, ta_IO_P, len(list_IO_aaa_C), len(list_IO_aaa_P))) terminal_update_msg(16, 1, msg) # buy 2 lots, for: ta_IF, ta_IO_C, ta_IO_P #for key in [ta_IF, ta_IO_C, ta_IO_P]: # future 損失比較多, 先不要 for key in [ta_IO_C, ta_IO_P]: # form_tqq04(InstrumentID, OrderPriceType, Direction, OffsetFlag, LimitPrice, Volume, TimeCondition, VolumeCondition): # InstrumentID ///合约代码,必填字段 # OrderPriceType ///(option 只能 限价2, 不能 任意價格市价1) 报单价格条件,必填字段(支持市价1、限价2、五档价4 和最优价3) # Direction ///(# 0 买, 1 卖, 9 All) 买卖方向,必填字段 # OffsetFlag ///开平标志,必填字段(中金所支持开仓0和平仓1,上期所支持开仓、平今和平昨) # LimitPrice ///价格,必填字段 # Volume ///数量,必填字段 # TimeCondition ///有效期类型,必填字段(“立即完成,否则撤销”1 OR“当日有效”3) # VolumeCondition ///成交量类型,必填字段(支持“任意数量 1” 和 “全部数量 3”) InstrumentID_order = key OrderPriceType_order = "2" Direction_order = "0" OffsetFlag_order = "0" # LimitPrice_order if InstrumentID_order.startswith("IO"): # option if Direction_order == "1": # 1 卖 (這裡故意賣 低價) LimitPrice_order = dnj_trade_common.Instrument[ InstrumentID_order]['LowerLimitPrice'] else: # 0 买 (這裡故意買 高價) LimitPrice_order = dnj_trade_common.Instrument[ InstrumentID_order]['UpperLimitPrice'] else: # future, 市價 +/- 30.0 保護帶 (use 29.0) if Direction_order == "1": # 1 卖 (這裡故意賣 低價) LimitPrice_order = str( float(dnj_trade_common. deptmarketdata[InstrumentID_order] ['LastPrice']) - 29) else: # 0 买 (這裡故意買 高價) LimitPrice_order = str( float(dnj_trade_common. deptmarketdata[InstrumentID_order] ['LastPrice']) + 29) Volume_order = "2" TimeCondition_order = "3" VolumeCondition_order = "1" write_in( form_tqq04(InstrumentID_order, OrderPriceType_order, Direction_order, OffsetFlag_order, LimitPrice_order, Volume_order, TimeCondition_order, VolumeCondition_order)) # time t_update_ta_IF = tt_now["timestamp_micro"]