/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/futuquant@3.2.6/src/futuquant.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const Socket=require("./socket"),sleep=async t=>new Promise((e=>{setTimeout(e,t)}));class FutuQuant{constructor(t){if("object"!=typeof t)throw new Error("传入参数类型错误");const{ip:e,port:r,userID:s,market:i,pwdMd5:o,env:a}=t;if(!e)throw new Error("必须指定FutuOpenD服务的ip");if(!r)throw new Error("必须指定FutuOpenD服务的port");if(!s)throw new Error("必须指定FutuOpenD服务的牛牛号");if(!o)throw new Error("必须指定FutuOpenD服务的解锁 MD5");this.market=i||1,this.userID=s,this.pwdMd5=o,this.params=t,this.env=a,"number"!=typeof this.env&&(this.env=1),this.socket=new Socket(e,r),this.inited=!1,this.trdHeader=null,this.timerKeepLive=null}async init(){if(this.inited)return;await this.initConnect(),await this.limitExecTimes(3e4,10,(async()=>{await this.trdUnlockTrade(!0,this.pwdMd5)}));const{accID:t}=(await this.trdGetAccList())[0];await this.setCommonTradeHeader(this.env,t,this.market),this.inited=!0}async initConnect(t){if(this.inited)throw new Error("请勿重复初始化连接");return new Promise((async e=>{this.socket.onConnect((async()=>{const r=await this.socket.send("InitConnect",Object.assign({clientVer:101,clientID:"yisbug",recvNotify:!0},t));this.connID=r.connID,this.connAESKey=r.connAESKey,this.keepAliveInterval=r.keepAliveInterval,this.timerKeepLive&&(clearInterval(this.timerKeepLive),this.timerKeepLive=null),this.timerKeepLive=setInterval((()=>this.keepAlive()),1e3*this.keepAliveInterval),e(r)})),await this.socket.init()}))}close(){this.timerKeepLive&&(clearInterval(this.timerKeepLive),this.socket.close(),this.inited=!1)}getGlobalState(){return this.socket.send("GetGlobalState",{userID:this.userID})}async keepAlive(){return await this.socket.send("KeepAlive",{time:Math.round(Date.now()/1e3)})}qotSub(t){return this.socket.send("Qot_Sub",Object.assign({securityList:[],subTypeList:[],isSubOrUnSub:!0,isRegOrUnRegPush:!0,regPushRehabTypeList:[],isFirstPush:!0},t))}qotRegQotPush(t){return this.socket.send("Qot_RegQotPush",Object.assign({securityList:[],subTypeList:[],rehabTypeList:[],isRegOrUnReg:!0,isFirstPush:!0},t))}qotGetSubInfo(t=!1){return this.socket.send("Qot_RegQotPush",{isReqAllConn:t})}async qotGetBasicQot(t){return(await this.socket.send("Qot_GetBasicQot",{securityList:t})).basicQotList||[]}subQotUpdateBasicQot(t){return this.socket.subNotify(3005,(e=>t(e.basicQotList||[])))}async qotGetKL(t){return(await this.socket.send("Qot_GetKL",Object.assign({rehabType:1,klType:1,security:{},reqNum:60},t))).klList||[]}subQotUpdateKL(t){return this.socket.subNotify(3007,t)}async qotGetRT(t){return(await this.socket.send("Qot_GetRT",{security:t})).rtList||[]}subQotUpdateRT(t){return this.socket.subNotify(3009,(e=>t(e.rtList||[])))}async qotGetTicker(t,e=100){return(await this.socket.send("Qot_GetTicker",{security:t,maxRetNum:e})).tickerList||[]}subQotUpdateTicker(t){return this.socket.subNotify(3011,t)}async qotGetOrderBook(t,e=10){const r=await this.socket.send("Qot_GetOrderBook",{security:t,num:e});return r.orderBookAskList=r.orderBookAskList||[],r.orderBookBidList=r.orderBookBidList||[],r.sellList=r.orderBookAskList,r.buyList=r.orderBookBidList,r.sellList.forEach((t=>{t.volume=Number(t.volume)})),r.buyList.forEach((t=>{t.volume=Number(t.volume)})),r}subQotUpdateOrderBook(t){return this.socket.subNotify(3013,(e=>{e.sellList=e.orderBookAskList||[],e.buyList=e.orderBookBidList||[],e.sellList.forEach((t=>{t.volume=Number(t.volume)})),e.buyList.forEach((t=>{t.volume=Number(t.volume)})),t(e)}))}async qotGetBroker(t){const e=await this.socket.send("Qot_GetBroker",{security:t});return e.brokerAskList=e.brokerAskList||[],e.brokerBidList=e.brokerBidList||[],e.sellList=e.brokerAskList,e.buyList=e.brokerBidList,e}subQotUpdateBroker(t){return this.socket.subNotify(3015,(e=>{e.brokerAskList=e.brokerAskList||[],e.brokerBidList=e.brokerBidList||[],e.sellList=e.brokerAskList,e.buyList=e.brokerBidList,t(e)}))}async qotGetHistoryKL(t){return(await this.socket.send("Qot_GetHistoryKL",Object.assign({rehabType:1,klType:1,security:{},beginTime:"",endTime:""},t))).klList||[]}qotGetHistoryKLPoints(t){return this.socket.send("Qot_GetHistoryKLPoints",Object.assign({rehabType:1,klType:1,noDataMode:0,securityList:[],timeList:[],maxReqSecurityNum:60,needKLFieldsFlag:512},t)).klPointList||[]}qotGetRehab(t){return this.socket.send("Qot_GetRehab",{securityList:t})}async qotGetTradeDate(t=1,e,r){return(await this.socket.send("Qot_GetTradeDate",{market:t,beginTime:e,endTime:r})).tradeDateList||[]}async qotGetStaticInfo(t=1,e){return(await this.socket.send("Qot_GetStaticInfo",{market:t,secType:e})).staticInfoList||[]}async qotGetSecuritySnapShot(t){const e=[].concat(t);let r=[];for(;e.length;){const t=await this.limitExecTimes(3e4,10,(async()=>(await this.socket.send("Qot_GetSecuritySnapshot",{securityList:e.splice(-200)})).snapshotList));r=r.concat(t)}return r}async limitExecTimes(t,e,r){const s=Date.now(),i=`${r.toString()}_exec_time_array`,o=this[i]||[];for(;o[0]&&s-o[0]>t;)o.shift();return o.length>e&&await sleep(t-(s-o[0])),o.push(Date.now()),this[i]=o,r()}async qotGetPlateSet(t=1,e){return(await this.socket.send("Qot_GetPlateSet",{market:t,plateSetType:e})).plateInfoList||[]}async qotGetPlateSecurity(t){return(await this.socket.send("Qot_GetPlateSecurity",{plate:t})).staticInfoList||[]}async qotGetReference(t,e=1){return(await this.socket.send("Qot_GetReference",{security:t,referenceType:e})).staticInfoList||[]}async trdGetAccList(){const{accList:t}=await this.socket.send("Trd_GetAccList",{userID:this.userID});return t.filter((t=>t.trdMarketAuthList.includes(this.market)&&t.trdEnv===this.env))}trdUnlockTrade(t=!0,e=""){return e&&(this.pwdMD5=e),this.socket.send("Trd_UnlockTrade",{unlock:t,pwdMD5:e||this.pwdMD5})}async trdSubAccPush(t){return this.socket.send("Trd_SubAccPush",{accIDList:t})}setCommonTradeHeader(t=1,e,r=1){this.market=r,this.trdHeader={trdEnv:t,accID:e,trdMarket:r}}async trdGetFunds(){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_GetFunds",{header:this.trdHeader})).funds}async trdGetPositionList(t,e,r){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_GetPositionList",{header:this.trdHeader,filterConditions:t,filterPLRatioMin:e,filterPLRatioMax:r})).positionList||[]}async trdGetMaxTrdQtys(t){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_GetMaxTrdQtys",Object.assign({header:this.trdHeader,orderType:1,code:"",price:0,adjustPrice:!1,adjustSideAndLimit:0},t))).maxTrdQtys}async trdGetOrderList(t,e){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_GetOrderList",{header:this.trdHeader,filterConditions:t,filterStatusList:e})).orderList||[]}async trdPlaceOrder(t){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_PlaceOrder",Object.assign({packetID:{connID:this.connID,serialNo:this.socket.requestId},header:this.trdHeader,trdSide:0,orderType:1,code:"",qty:0,price:0,adjustPrice:!1,adjustSideAndLimit:0},t))).orderID}async trdPlaceOrderMarket(t){const{trdSide:e,code:r,qty:s}=t;let i=s,o=0;for(;i>0;){let t=null,s=null;const a=await this.qotGetOrderBook({market:this.market,code:r}),n=1===e?a.sellList[0].price:a.buyList[0].price;for(t&&10===s.orderStatus?await this.trdModifyOrder({modifyOrderOp:4,orderID:t,price:n,qty:i}):t||(t=await this.trdPlaceOrder({trdSide:e,code:r,qty:i,price:n}));;){if(s=(await this.trdGetOrderList({},[])).filter((e=>e.orderID===t)),s){if(s.orderStatus>11){s=null,t=null;break}s.orderStatus<10?await sleep(50):s.fillQty>0&&(i-=s.fillQty,o+=s.price*s.fillQty,i>0&&10===s.orderStatus&&await this.trdModifyOrder({modifyOrderOp:3,orderID:t}))}else await sleep(60)}}return o}async trdModifyOrder(t){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_ModifyOrder",Object.assign({packetID:{connID:this.connID,serialNo:this.socket.requestId},header:this.trdHeader,orderID:0,modifyOrderOp:1,forAll:!1,qty:0,price:0,adjustPrice:!1,adjustSideAndLimit:0},t))).orderID}async subTrdUpdateOrder(t){return this.socket.subNotify(2208,(e=>t(e.order)))}async unsubTrdUpdateOrder(){return this.socket.unsubNotify(2208)}async trdGetOrderFillList(t){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_GetOrderFillList",{header:this.trdHeader,filterConditions:t})).orderFillList||[]}async subTrdUpdateOrderFill(t){return this.socket.subNotify(2218,(e=>t(e.orderFill||[])))}async trdGetHistoryOrderList(t,e){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_GetHistoryOrderList",{header:this.trdHeader,filterConditions:t,filterStatusList:e})).orderList||[]}async trdGetHistoryOrderFillList(t){if(!this.trdHeader)throw new Error("请先调用setCommonTradeHeader接口设置交易公共header");return(await this.socket.send("Trd_GetHistoryOrderFillList",{header:this.trdHeader,filterConditions:t})).orderFillList||[]}}module.exports=FutuQuant;
//# sourceMappingURL=/sm/4e06d2f63826ac3775f171485c06490865606620e864a7416db3b09223f69ef0.map