public int Handle(DataParser dataParser) { List <UFXWithdrawFailedResponse> responseItems = new List <UFXWithdrawFailedResponse>(); var errorResponse = T2ErrorHandler.Handle(dataParser); if (T2ErrorHandler.Success(errorResponse.ErrorCode)) { responseItems = responseItems = UFXDataSetHelper.ParseSubscribeData <UFXWithdrawFailedResponse>(dataParser); } //update the database if (responseItems != null && responseItems.Count > 0) { foreach (var responseItem in responseItems) { int commandId; int submitId; int requestId; //TODO: add log if (EntrustRequestHelper.ParseThirdReff(responseItem.ThirdReff, out commandId, out submitId, out requestId)) { _entrustSecurityBLL.UpdateEntrustStatus(submitId, commandId, responseItem.StockCode, Model.EnumType.EntrustStatus.CancelFail); } else { string msg = string.Format("Fail to parse the third_reff: [{0}], entrust_no: [{1}].", responseItem.ThirdReff, responseItem.EntrustNo); logger.Error(msg); } } } return(responseItems.Count); }
public int Handle(DataParser dataParser) { List <UFXEntrustDealResponse> responseItems = new List <UFXEntrustDealResponse>(); var errorResponse = T2ErrorHandler.Handle(dataParser); if (T2ErrorHandler.Success(errorResponse.ErrorCode)) { //TODO: check the count of dataset. responseItems = UFXDataSetHelper.ParseSubscribeData <UFXEntrustDealResponse>(dataParser); } //update the database if (responseItems != null && responseItems.Count > 0) { List <EntrustSecurity> entrustSecuItems = new List <EntrustSecurity>(); foreach (var responseItem in responseItems) { int commandId; int submitId; int requestId; //TODO: add log if (EntrustRequestHelper.ParseThirdReff(responseItem.ThirdReff, out commandId, out submitId, out requestId)) { var dealItem = Convert(responseItem); //If it was stored in the database, then ignore //TODO: save into database if (_dealSecurityBLL.IsExist(commandId, submitId, requestId, dealItem.DealNo)) { string msg = string.Format("Duplicate to parse the third_reff: {0}, DealNo:{1}", responseItem.ThirdReff, dealItem.DealNo); logger.Error(msg); } else { //save the deal record in dealsecurity table _dealSecurityBLL.Create(dealItem); //update the entrustsecurity table _entrustSecurityBLL.UpdateDeal(submitId, commandId, responseItem.StockCode, responseItem.DealAmount, responseItem.DealBalance, responseItem.DealFee); //Update the TradingInstanceSecurity _tradeInstanceSecuBLL.UpdateToday(dealItem.EntrustDirection, commandId, dealItem.SecuCode, dealItem.DealAmount, dealItem.DealBalance, dealItem.DealFee); } } else { string msg = string.Format("Fail to parse the third_reff: {0}", responseItem.ThirdReff); logger.Error(msg); } } } return(responseItems.Count); }
private DealSecurity Convert(UFXEntrustDealResponse responseItem) { DealSecurity dealItem = new DealSecurity { SecuCode = responseItem.StockCode, DealNo = responseItem.DealNo, BatchNo = responseItem.BatchNo, EntrustNo = responseItem.EntrustNo, AccountCode = responseItem.AccountCode, PortfolioCode = responseItem.CombiNo, StockHolderId = responseItem.StockHolderId, ReportSeat = responseItem.ReportSeat, DealDate = responseItem.DealDate, DealTime = responseItem.DealTime, EntrustAmount = responseItem.EntrustAmount, DealAmount = responseItem.DealAmount, DealPrice = responseItem.DealPrice, DealBalance = responseItem.DealBalance, DealFee = responseItem.DealFee, TotalDealAmount = responseItem.TotalDealAmount, TotalDealBalance = responseItem.TotalDealBalance, CancelAmount = responseItem.CancelAmount, }; int commandId; int submitId; int requestId; if (EntrustRequestHelper.ParseThirdReff(responseItem.ThirdReff, out commandId, out submitId, out requestId)) { dealItem.CommandId = commandId; dealItem.SubmitId = submitId; dealItem.RequestId = requestId; } dealItem.ExchangeCode = EntrustRequestHelper.GetExchangeCode(responseItem.MarketNo); dealItem.EntrustDirection = EntrustRequestHelper.GetEntrustDirectionType(responseItem.EntrustDirection, dealItem.ExchangeCode); dealItem.EntrustState = Model.EnumType.EntrustStatus.Completed; //dealItem.EntrustState = UFXTypeConverter.GetEntrustState(responseItem.EntrustState); return(dealItem); }
private List <EntrustFlowItem> GetFlowItems(CallerToken token, List <UFXQueryFuturesEntrustResponse> responseItems) { var entrustFlowItems = new List <EntrustFlowItem>(); if (responseItems == null || responseItems.Count == 0) { return(entrustFlowItems); } Portfolio portfolio = (Portfolio)token.InArgs; string portfolioCode = string.Empty; string portfolioName = string.Empty; string fundCode = string.Empty; string fundName = string.Empty; if (portfolio != null) { portfolioCode = portfolio.PortfolioNo; portfolioName = portfolio.PortfolioName; fundCode = portfolio.FundCode; fundName = portfolio.FundName; } Dictionary <int, Model.UI.TradeInstance> cmdInstMap = new Dictionary <int, Model.UI.TradeInstance>(); foreach (var responseItem in responseItems) { int commandId = 0; int submitId = 0; int requestId = 0; int temp1, temp2, temp3; if (EntrustRequestHelper.TryParseThirdReff(responseItem.ThirdReff, out temp1, out temp2, out temp3)) { commandId = temp1; submitId = temp2; requestId = temp3; } int instanceId = 0; string instanceCode = string.Empty; if (!cmdInstMap.ContainsKey(commandId)) { var tradeInstance = _tradeCommandBLL.GetTradeInstance(commandId); if (tradeInstance != null) { instanceId = tradeInstance.InstanceId; instanceCode = tradeInstance.InstanceCode; cmdInstMap.Add(commandId, tradeInstance); } } else { instanceId = cmdInstMap[commandId].InstanceId; instanceCode = cmdInstMap[commandId].InstanceCode; } var entrustDirection = UFXTypeConverter.GetEntrustDirection(responseItem.EntrustDirection); var futuresDirection = UFXTypeConverter.GetFuturesDirection(responseItem.FuturesDirection); EntrustDirection eDirection = EntrustDirectionConverter.GetFuturesEntrustDirection(entrustDirection, futuresDirection); EntrustFlowItem efItem = new EntrustFlowItem { CommandNo = token.CommandId, SubmitId = token.SubmitId, //Market = responseItem.MarketNo, SecuCode = responseItem.StockCode, //EntrustDirection = responseItem.EntrustDirection, EEntrustPriceType = EntrustPriceTypeConverter.GetPriceType(responseItem.PriceType), EntrustPrice = responseItem.EntrustPrice, EntrustAmount = responseItem.EntrustAmount, //EntrustStatus = responseItem.EntrustState, DealAmount = responseItem.DealAmount, DealMoney = responseItem.DealBalance, DealTimes = responseItem.DealTimes, DEntrustDate = DateUtil.GetDateTimeFromInt(responseItem.EntrustDate, responseItem.EntrustTime), EntrustBatchNo = responseItem.BatchNo, EntrustNo = responseItem.EntrustNo, DeclareSeat = responseItem.ReportSeat, DeclareNo = Convert.ToInt32(responseItem.ReportNo), RequestId = responseItem.ExtSystemId, FundCode = responseItem.AccountCode, FundName = fundName, PortfolioCode = portfolioCode, PortfolioName = portfolioName, EDirection = eDirection, //EEntrustDirection = UFXTypeConverter.GetEntrustDirection(responseItem.EntrustDirection), EMarketCode = UFXTypeConverter.GetMarketCode(responseItem.MarketNo), EEntrustState = UFXTypeConverter.GetEntrustState(responseItem.EntrustState), WithdrawCause = responseItem.WithdrawCause, InstanceId = instanceId, InstanceNo = instanceCode, }; if (responseItem.FirstDealTime > 0) { efItem.DFirstDealDate = DateUtil.GetDateTimeFromInt(responseItem.EntrustDate, responseItem.FirstDealTime); } efItem.ExchangeCode = UFXTypeConverter.GetMarketCode(efItem.EMarketCode); efItem.CommandNo = commandId; efItem.SubmitId = submitId; efItem.RequestId = requestId; entrustFlowItems.Add(efItem); } return(entrustFlowItems); }
private List <DealFlowItem> GetDealItems(CallerToken token, List <UFXQueryDealResponse> responseItems) { List <DealFlowItem> dealItems = new List <DealFlowItem>(); if (responseItems == null || responseItems.Count == 0) { return(dealItems); } Portfolio portfolio = (Portfolio)token.InArgs; string portfolioCode = string.Empty; string portfolioName = string.Empty; string fundCode = string.Empty; string fundName = string.Empty; if (portfolio != null) { portfolioCode = portfolio.PortfolioNo; portfolioName = portfolio.PortfolioName; fundCode = portfolio.FundCode; fundName = portfolio.FundName; } Dictionary <int, Model.UI.TradeInstance> cmdInstMap = new Dictionary <int, Model.UI.TradeInstance>(); foreach (var responseItem in responseItems) { int commandId = 0; int submitId = 0; int requestId = 0; int temp1, temp2, temp3; if (EntrustRequestHelper.TryParseThirdReff(responseItem.ThirdReff, out temp1, out temp2, out temp3)) { commandId = temp1; submitId = temp2; requestId = temp3; } int instanceId = 0; string instanceCode = string.Empty; if (!cmdInstMap.ContainsKey(commandId)) { var tradeInstance = _tradeCommandBLL.GetTradeInstance(commandId); if (tradeInstance != null) { instanceId = tradeInstance.InstanceId; instanceCode = tradeInstance.InstanceCode; cmdInstMap.Add(commandId, tradeInstance); } } else { instanceId = cmdInstMap[commandId].InstanceId; instanceCode = cmdInstMap[commandId].InstanceCode; } var marketCode = UFXTypeConverter.GetMarketCode(responseItem.MarketNo); var entrustDirection = UFXTypeConverter.GetEntrustDirection(responseItem.EntrustDirection); DealFlowItem efItem = new DealFlowItem { CommandNo = commandId, SecuCode = responseItem.StockCode, FundNo = responseItem.AccountCode, FundName = fundName, PortfolioCode = responseItem.CombiNo, PortfolioName = portfolioName, EDirection = EntrustDirectionConverter.GetSecurityEntrustDirection(entrustDirection), DealPrice = responseItem.DealPrice, DealAmount = responseItem.DealAmount, DealMoney = responseItem.DealBalance, DealTime = string.Format("{0}", responseItem.DealTime), ShareHolderCode = responseItem.StockHolderId, EntrustNo = string.Format("{0}", responseItem.EntrustNo), DealNo = string.Format("{0}", responseItem.DealNo), ExchangeCode = UFXTypeConverter.GetMarketCode(marketCode), InstanceId = instanceId.ToString(), InstanceNo = instanceCode, }; dealItems.Add(efItem); } return(dealItems); }
public BLLResponse Submit(Model.Database.EntrustCommand cmdItem, List <EntrustSecurity> entrustItems, CallerCallback callerCallback) { var cmdEntrustItems = entrustItems.Where(p => p.CommandId == cmdItem.CommandId && p.SubmitId == cmdItem.SubmitId).ToList(); if (cmdEntrustItems == null || cmdEntrustItems.Count == 0) { return(new BLLResponse(ConnectionCode.EmptyEntrustItem, "Empty EntrustCommandItem or EntrustSecurityItem.")); } UpdateUFXSetting(); var tradeCommandItem = _tradeCommandBLL.GetTradeCommand(cmdItem.CommandId); var portfolio = LoginManager.Instance.GetPortfolio(tradeCommandItem.PortfolioCode); //var stockholder = LoginManager.Instance.GetHolder(tradeCommandItem. var ufxRequests = new List <UFXBasketEntrustRequest>(); var futuItem = cmdEntrustItems.Find(p => p.SecuType == Model.SecurityInfo.SecurityType.Futures); foreach (var secuItem in cmdEntrustItems) { UFXBasketEntrustRequest request = new UFXBasketEntrustRequest { StockCode = secuItem.SecuCode, EntrustPrice = secuItem.EntrustPrice, EntrustAmount = secuItem.EntrustAmount, PriceType = EntrustRequestHelper.GetEntrustPriceType(secuItem.EntrustPriceType), ExtSystemId = secuItem.RequestId, ThirdReff = EntrustRequestHelper.GenerateThirdReff(secuItem.CommandId, secuItem.SubmitId, secuItem.RequestId), LimitEntrustRatio = _limitEntrustRatio, FutuLimitEntrustRatio = _futuLimitEntrustRatio, OptLimitEntrustRatio = _optLimitEntrustRatio, }; if (secuItem.SecuType == Model.SecurityInfo.SecurityType.Stock) { request.EntrustDirection = EntrustRequestHelper.GetEntrustDirection(secuItem.EntrustDirection); request.FuturesDirection = string.Empty; } else if (secuItem.SecuType == Model.SecurityInfo.SecurityType.Futures) { request.EntrustDirection = EntrustRequestHelper.GetEntrustDirection(secuItem.EntrustDirection); request.FuturesDirection = EntrustRequestHelper.GetFuturesDirection(secuItem.EntrustDirection); } var secuInfo = SecurityInfoManager.Instance.Get(secuItem.SecuCode, secuItem.SecuType); if (secuInfo != null) { request.MarketNo = EntrustRequestHelper.GetMarketNo(secuInfo.ExchangeCode); } if (tradeCommandItem != null) { request.AccountCode = tradeCommandItem.AccountCode; request.CombiNo = tradeCommandItem.PortfolioCode; } ufxRequests.Add(request); } Callbacker callbacker = new Callbacker { Token = new CallerToken { SubmitId = cmdItem.SubmitId, CommandId = cmdItem.CommandId, Caller = callerCallback, WaitEvent = new AutoResetEvent(false), }, DataHandler = EntrustDataHandler, }; var result = _securityBLL.EntrustBasket(ufxRequests, callbacker); BLLResponse bllResponse = new BLLResponse(); if (result == Model.ConnectionCode.Success) { if (callbacker.Token.WaitEvent.WaitOne(_timeOut)) { var errorResponse = callbacker.Token.ErrorResponse as UFXErrorResponse; if (errorResponse != null && T2ErrorHandler.Success(errorResponse.ErrorCode)) { bllResponse.Code = ConnectionCode.Success; bllResponse.Message = "Success Entrust"; } else { bllResponse.Code = ConnectionCode.FailEntrust; bllResponse.Message = "Fail Entrust: " + errorResponse.ErrorCode + " " + errorResponse.ErrorMessage + " " + errorResponse.MessageDetail; } } else { bllResponse.Code = ConnectionCode.FailTimeoutEntrust; bllResponse.Message = "Timeout to entrust."; } } else { bllResponse.Code = result; bllResponse.Message = "Fail to submit in ufx."; } return(bllResponse); }
//TODO: async to sumbit the command, it can emit the latency. public BLLResponse SubmitAsync(Model.Database.EntrustCommand cmdItem, List <EntrustSecurity> entrustItems, CallerCallback callerCallback, EventWaitHandle waitHandle) { var cmdEntrustItems = entrustItems.Where(p => p.CommandId == cmdItem.CommandId && p.SubmitId == cmdItem.SubmitId).ToList(); if (cmdEntrustItems == null || cmdEntrustItems.Count == 0) { return(new BLLResponse(ConnectionCode.EmptyEntrustItem, "Empty EntrustCommandItem or EntrustSecurityItem.")); } UpdateUFXSetting(); var tradeCommandItem = _tradeCommandBLL.GetTradeCommand(cmdItem.CommandId); var portfolio = LoginManager.Instance.GetPortfolio(tradeCommandItem.PortfolioCode); //var stockholder = LoginManager.Instance.GetHolder(tradeCommandItem. var ufxRequests = new List <UFXBasketEntrustRequest>(); var futuItem = cmdEntrustItems.Find(p => p.SecuType == Model.SecurityInfo.SecurityType.Futures); foreach (var secuItem in cmdEntrustItems) { UFXBasketEntrustRequest request = new UFXBasketEntrustRequest { StockCode = secuItem.SecuCode, EntrustPrice = secuItem.EntrustPrice, EntrustAmount = secuItem.EntrustAmount, PriceType = EntrustRequestHelper.GetEntrustPriceType(secuItem.EntrustPriceType), ExtSystemId = secuItem.RequestId, ThirdReff = EntrustRequestHelper.GenerateThirdReff(secuItem.CommandId, secuItem.SubmitId, secuItem.RequestId), LimitEntrustRatio = _limitEntrustRatio, FutuLimitEntrustRatio = _futuLimitEntrustRatio, OptLimitEntrustRatio = _optLimitEntrustRatio, }; if (secuItem.SecuType == Model.SecurityInfo.SecurityType.Stock) { request.EntrustDirection = EntrustRequestHelper.GetEntrustDirection(secuItem.EntrustDirection); request.FuturesDirection = string.Empty; } else if (secuItem.SecuType == Model.SecurityInfo.SecurityType.Futures) { request.EntrustDirection = EntrustRequestHelper.GetEntrustDirection(secuItem.EntrustDirection); request.FuturesDirection = EntrustRequestHelper.GetFuturesDirection(secuItem.EntrustDirection); } var secuInfo = SecurityInfoManager.Instance.Get(secuItem.SecuCode, secuItem.SecuType); if (secuInfo != null) { request.MarketNo = EntrustRequestHelper.GetMarketNo(secuInfo.ExchangeCode); } if (tradeCommandItem != null) { request.AccountCode = tradeCommandItem.AccountCode; request.CombiNo = tradeCommandItem.PortfolioCode; } ufxRequests.Add(request); } Callbacker callbacker = new Callbacker { Token = new CallerToken { SubmitId = cmdItem.SubmitId, CommandId = cmdItem.CommandId, Caller = callerCallback, WaitEvent = waitHandle, }, DataHandler = EntrustDataHandler, }; var result = _securityBLL.EntrustBasket(ufxRequests, callbacker); return(new BLLResponse(result, "Finish to entrust.")); }
public int Submit(EntrustCommandItem cmdItem, List <EntrustSecurityItem> entrustItems) { int ret = -1; var cmdEntrustItems = entrustItems.Where(p => p.CommandId == cmdItem.CommandId && p.SubmitId == cmdItem.SubmitId).ToList(); if (cmdEntrustItems == null || cmdEntrustItems.Count == 0) { return(ret); } var tradeCommandItem = _tradeCommandBLL.GetTradeCommandItem(cmdItem.CommandId); var portfolio = LoginManager.Instance.GetPortfolio(tradeCommandItem.PortfolioCode); //var stockholder = LoginManager.Instance.GetHolder(tradeCommandItem. var ufxRequests = new List <UFXBasketEntrustRequest>(); var futuItem = cmdEntrustItems.Find(p => p.SecuType == Model.SecurityInfo.SecurityType.Futures); foreach (var secuItem in cmdEntrustItems) { UFXBasketEntrustRequest request = new UFXBasketEntrustRequest { StockCode = secuItem.SecuCode, EntrustPrice = secuItem.EntrustPrice, EntrustAmount = secuItem.EntrustAmount, PriceType = EntrustRequestHelper.GetEntrustPriceType(secuItem.EntrustPriceType), ExtSystemId = secuItem.RequestId, ThirdReff = EntrustRequestHelper.GenerateThirdReff(secuItem.CommandId, secuItem.SubmitId, secuItem.RequestId), LimitEntrustRatio = 100, FutuLimitEntrustRatio = 100, OptLimitEntrustRatio = 100, }; if (secuItem.SecuType == Model.SecurityInfo.SecurityType.Stock) { request.EntrustDirection = EntrustRequestHelper.GetEntrustDirection(secuItem.EntrustDirection); request.FuturesDirection = string.Empty; } else if (secuItem.SecuType == Model.SecurityInfo.SecurityType.Futures) { request.EntrustDirection = EntrustRequestHelper.GetEntrustDirection(secuItem.EntrustDirection); request.FuturesDirection = EntrustRequestHelper.GetFuturesDirection(secuItem.EntrustDirection); } var secuInfo = SecurityInfoManager.Instance.Get(secuItem.SecuCode, secuItem.SecuType); if (secuInfo != null) { request.MarketNo = EntrustRequestHelper.GetMarketNo(secuInfo.ExchangeCode); } if (tradeCommandItem != null) { request.AccountCode = tradeCommandItem.FundCode; request.CombiNo = tradeCommandItem.PortfolioCode; } ufxRequests.Add(request); } Callbacker callbacker = new Callbacker { Token = new CallerToken { SubmitId = cmdItem.SubmitId, CommandId = cmdItem.CommandId, InArgs = cmdItem, WaitEvent = new AutoResetEvent(false), }, DataHandler = EntrustBasketCallback, }; var result = _securityBLL.EntrustBasket(ufxRequests, callbacker); if (result == Model.ConnectionCode.Success) { callbacker.Token.WaitEvent.WaitOne(); var errorResponse = callbacker.Token.OutArgs as UFXErrorResponse; if (errorResponse != null && T2ErrorHandler.Success(errorResponse.ErrorCode)) { ret = 1; } } return(ret); }