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);
        }
        private int DataHandlerCallback(CallerToken token, DataParser dataParser)
        {
            int ret = -1;

            var errorResponse = T2ErrorHandler.Handle(dataParser);

            token.ErrorResponse = errorResponse;

            List <UFXMultipleHoldingResponse> responseItems = new List <UFXMultipleHoldingResponse>();

            if (T2ErrorHandler.Success(errorResponse.ErrorCode))
            {
                responseItems = UFXDataSetHelper.ParseData <UFXMultipleHoldingResponse>(dataParser);

                var futures = responseItems.Where(p => p.MarketNo == "7").ToList();

                var validItems = responseItems.Where(p => p.CurrentAmount > 0).ToList();
            }
            else
            {
                ret = -1;
            }

            if (token.WaitEvent != null)
            {
                token.WaitEvent.Set();
            }

            return(ret);
        }
Example #3
0
        private int WithdrawBasketCallback(CallerToken token, DataParser dataParser)
        {
            var errorResponse = T2ErrorHandler.Handle(dataParser);

            List <UFXBasketWithdrawResponse> responseItems = new List <UFXBasketWithdrawResponse>();

            var dataFieldMap = UFXDataBindingHelper.GetProperty <UFXBasketWithdrawResponse>();

            for (int i = 1, count = dataParser.DataSets.Count; i < count; i++)
            {
                var dataSet = dataParser.DataSets[i];
                foreach (var dataRow in dataSet.Rows)
                {
                    UFXBasketWithdrawResponse p = new UFXBasketWithdrawResponse();
                    UFXDataSetHelper.SetValue <UFXBasketWithdrawResponse>(ref p, dataRow.Columns, dataFieldMap);
                    responseItems.Add(p);
                }
            }

            List <EntrustSecurityItem> entrustSecuItems = new List <EntrustSecurityItem>();

            int ret = -1;

            if (token.SubmitId > 0)
            {
                if (T2ErrorHandler.Success(errorResponse.ErrorCode))
                {
                    foreach (var responseItem in responseItems)
                    {
                        var entrustItem = new EntrustSecurityItem
                        {
                            SubmitId  = token.SubmitId,
                            CommandId = token.CommandId,
                            SecuCode  = responseItem.StockCode,
                            EntrustNo = responseItem.EntrustNo,
                        };

                        entrustSecuItems.Add(entrustItem);
                    }

                    ret = _entrustdao.UpdateSecurityEntrustStatus(entrustSecuItems, Model.EnumType.EntrustStatus.CancelSuccess);
                    ret = _entrustcmddao.UpdateEntrustCommandStatus(token.SubmitId, Model.EnumType.EntrustStatus.CancelSuccess);
                    ret = _tradecmddao.UpdateTargetNumBySubmitId(token.SubmitId, token.CommandId);
                }
            }

            if (token.Caller != null)
            {
                token.Caller(token, entrustSecuItems, errorResponse);
            }

            if (token.WaitEvent != null)
            {
                token.WaitEvent.Set();
            }

            return(ret);
        }
Example #4
0
        private int WithdrawDataHandler(CallerToken token, DataParser dataParser)
        {
            List <UFXBasketWithdrawResponse> responseItems = new List <UFXBasketWithdrawResponse>();

            var errorResponse = T2ErrorHandler.Handle(dataParser);

            token.ErrorResponse = errorResponse;

            //Verify the dataParser before reading the data.
            if (T2ErrorHandler.Success(errorResponse.ErrorCode))
            {
                responseItems = UFXDataSetHelper.ParseData <UFXBasketWithdrawResponse>(dataParser);
            }

            int ret = -1;
            List <EntrustSecurity> entrustSecuItems = new List <EntrustSecurity>();

            if (token.SubmitId > 0)
            {
                //TODO: check the withdraw status
                foreach (var responseItem in responseItems)
                {
                    var entrustItem = new EntrustSecurity
                    {
                        SubmitId  = token.SubmitId,
                        CommandId = token.CommandId,
                        SecuCode  = responseItem.StockCode,
                        EntrustNo = responseItem.EntrustNo,
                    };

                    entrustSecuItems.Add(entrustItem);
                }

                if (entrustSecuItems.Count > 0)
                {
                    ret = _entrustCombineBLL.UpdateSecurityEntrustStatus(entrustSecuItems, Model.EnumType.EntrustStatus.CancelSuccess);
                }
            }

            try
            {
                if (token.Caller != null)
                {
                    token.Caller(token, entrustSecuItems, errorResponse);
                }
            }
            finally
            {
                if (token.WaitEvent != null)
                {
                    token.WaitEvent.Set();
                }
            }

            return(ret);
        }
        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);
        }
Example #6
0
        public BLLResponse Withdraw(Model.Database.EntrustCommand cmdItem, CallerCallback callerCallback)
        {
            BLLResponse bllResponse = new BLLResponse();

            UFXBasketWithdrawRequest request = new UFXBasketWithdrawRequest
            {
                BatchNo = cmdItem.BatchNo,
            };

            Callbacker callbacker = new Callbacker
            {
                Token = new CallerToken
                {
                    SubmitId  = cmdItem.SubmitId,
                    CommandId = cmdItem.CommandId,
                    WaitEvent = new AutoResetEvent(false),
                    Caller    = callerCallback,
                },

                DataHandler = WithdrawDataHandler,
            };

            var result = _securityBLL.WithdrawBasket(request, callbacker);

            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    = Model.ConnectionCode.Success;
                        bllResponse.Message = "Success Withdraw";
                    }
                    else
                    {
                        bllResponse.Code    = Model.ConnectionCode.FailWithdraw;
                        bllResponse.Message = errorResponse.ErrorMessage;
                    }
                }
                else
                {
                    bllResponse.Code    = Model.ConnectionCode.FailTimeoutWithdraw;
                    bllResponse.Message = "Fail to submit the basket withdraw to UFX!";
                }
            }
            else
            {
                bllResponse.Code    = Model.ConnectionCode.FailSubmit;
                bllResponse.Message = "Fail to submit the basket withdraw to UFX!";
            }

            return(bllResponse);
        }
        public int Query()
        {
            int ret = -1;

            //var portfolio = LoginManager.Instance.GetPortfolio(tradeCommandItem.PortfolioCode);
            //var stockholder = LoginManager.Instance.GetHolder(tradeCommandItem.

            var ufxRequests = new List <UFXHoldingRequest>();
            var ufxRequest  = new UFXHoldingRequest
            {
                CombiNo = "30",
            };

            ufxRequests.Add(ufxRequest);

            Callbacker callbacker = new Callbacker
            {
                Token = new CallerToken
                {
                    SubmitId  = 90000,
                    CommandId = 90001,
                    WaitEvent = new AutoResetEvent(false),
                },

                DataHandler = DataHandlerCallback,
            };

            var result = _securityBLL.QueryMultipleHolding(ufxRequests, callbacker);

            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))
                    {
                        ret = 1;
                    }
                }
                else
                {
                    ret = -1;
                }
            }

            return(ret);
        }
Example #8
0
        private List <UFXBasketWithdrawResponse> GetResponse(DataParser parser)
        {
            var errorResponse = T2ErrorHandler.Handle(parser);

            if (T2ErrorHandler.Success(errorResponse.ErrorCode))
            {
                return(UFXDataSetHelper.ParseData <UFXBasketWithdrawResponse>(parser));
            }
            else
            {
                string msg = string.Format("Fail to withdraw - error code: {0}, message: {1}, detail: {2}",
                                           errorResponse.ErrorCode, errorResponse.ErrorMessage, errorResponse.MessageDetail);
                logger.Error(msg);

                return(new List <UFXBasketWithdrawResponse>());
            }
        }
Example #9
0
        private int DataHandlerCallback(CallerToken token, DataParser dataParser)
        {
            int ret = -1;

            var errorResponse = T2ErrorHandler.Handle(dataParser);

            token.ErrorResponse = errorResponse;

            List <UFXHoldingResponse> responseItems = new List <UFXHoldingResponse>();

            if (T2ErrorHandler.Success(errorResponse.ErrorCode))
            {
                responseItems = UFXDataSetHelper.ParseData <UFXHoldingResponse>(dataParser);

                var futures = responseItems.Where(p => p.MarketNo == "7").ToList();

                var validItems = responseItems.Where(p => p.CurrentAmount > 0).ToList();

                if (validItems != null && validItems.Count > 0 &&
                    token.OutArgs != null && token.OutArgs is List <UFXHoldingResponse> )
                {
                    ((List <UFXHoldingResponse>)token.OutArgs).AddRange(validItems);
                }
            }
            else
            {
                ret = -1;
            }

            if (token.Caller != null)
            {
                token.Caller(token, null, errorResponse);
            }

            if (token.WaitEvent != null)
            {
                token.WaitEvent.Set();
            }

            return(ret);
        }
Example #10
0
        private int QueryDataHandler(CallerToken token, DataParser dataParser)
        {
            List <UFXQueryFuturesEntrustResponse> responseItems = new List <UFXQueryFuturesEntrustResponse>();
            var errorResponse = T2ErrorHandler.Handle(dataParser);

            token.ErrorResponse = errorResponse;

            if (T2ErrorHandler.Success(errorResponse.ErrorCode))
            {
                responseItems = UFXDataSetHelper.ParseData <UFXQueryFuturesEntrustResponse>(dataParser);
            }

            try
            {
                if (token.Caller != null)
                {
                    var entrustFlowItems = GetFlowItems(token, responseItems);

                    if (token.OutArgs != null &&
                        token.OutArgs is List <EntrustFlowItem> &&
                        entrustFlowItems != null &&
                        entrustFlowItems.Count > 0
                        )
                    {
                        ((List <EntrustFlowItem>)token.OutArgs).AddRange(entrustFlowItems);
                    }

                    token.Caller(token, entrustFlowItems, errorResponse);
                }
            }
            finally
            {
                if (token.WaitEvent != null)
                {
                    token.WaitEvent.Set();
                }
            }

            return(responseItems.Count());
        }
Example #11
0
        public ConnectionCode QueryHolder()
        {
            UFXFunctionCode functionCode = UFXFunctionCode.QueryHolder;
            FunctionItem    functionItem = ConfigManager.Instance.GetFunctionConfig().GetFunctionItem(functionCode);

            if (functionItem == null || functionItem.RequestFields == null || functionItem.RequestFields.Count == 0)
            {
                return(ConnectionCode.ErrorLogin);
            }

            CT2BizMessage bizMessage = new CT2BizMessage();

            //初始化
            bizMessage.SetFunction((int)functionCode);
            bizMessage.SetPacketType(CT2tag_def.REQUEST_PACKET);

            //业务包
            CT2Packer packer = new CT2Packer(2);

            packer.BeginPack();
            foreach (FieldItem item in functionItem.RequestFields)
            {
                packer.AddField(item.Name, item.Type, item.Width, item.Scale);
            }

            foreach (FieldItem item in functionItem.RequestFields)
            {
                switch (item.Name)
                {
                case "user_token":
                    packer.AddStr(LoginManager.Instance.LoginUser.Token);
                    break;

                case "account_code":
                    packer.AddStr("");
                    break;

                case "asset_no":
                    packer.AddStr("");
                    break;

                case "combi_no":
                    packer.AddStr("");
                    break;

                case "market_no":
                    packer.AddStr("");
                    break;

                default:
                    break;
                }
            }

            packer.EndPack();

            unsafe
            {
                bizMessage.SetContent(packer.GetPackBuf(), packer.GetPackLen());
            }

            var parser = _t2SDKWrap.SendSync2(bizMessage);

            packer.Dispose();
            bizMessage.Dispose();

            if (parser.ErrorCode != ConnectionCode.Success)
            {
                UFXLogger.Error(logger, functionCode, "交易股东查询失败");
                return(parser.ErrorCode);
            }

            var response = T2ErrorHandler.Handle(parser);

            if (T2ErrorHandler.Success(response.ErrorCode))
            {
                if (parser.DataSets.Count > 1)
                {
                    var dataSet = parser.DataSets[1];
                    foreach (var dataRow in dataSet.Rows)
                    {
                        HolderItem p = new HolderItem();
                        p.AccountCode   = dataRow.Columns["account_code"].GetStr();
                        p.AssetNo       = dataRow.Columns["asset_no"].GetStr();
                        p.CombiNo       = dataRow.Columns["combi_no"].GetStr();
                        p.StockHolderId = dataRow.Columns["stockholder_id"].GetStr();
                        p.MarketNo      = dataRow.Columns["market_no"].GetStr();

                        LoginManager.Instance.AddHolder(p);
                    }
                }

                return(ConnectionCode.Success);
            }
            else
            {
                UFXLogger.Error(logger, functionCode, response);
                return(ConnectionCode.ErrorFailContent);
            }
        }
Example #12
0
        public ConnectionCode QueryAccount()
        {
            UFXFunctionCode functionCode = UFXFunctionCode.QueryAccount;
            FunctionItem    functionItem = ConfigManager.Instance.GetFunctionConfig().GetFunctionItem(functionCode);

            if (functionItem == null || functionItem.RequestFields == null || functionItem.RequestFields.Count == 0)
            {
                return(ConnectionCode.ErrorLogin);
            }

            CT2BizMessage bizMessage = new CT2BizMessage();

            //初始化
            bizMessage.SetFunction((int)functionCode);
            bizMessage.SetPacketType(CT2tag_def.REQUEST_PACKET);

            //业务包
            CT2Packer packer = new CT2Packer(2);

            packer.BeginPack();
            foreach (FieldItem item in functionItem.RequestFields)
            {
                packer.AddField(item.Name, item.Type, item.Width, item.Scale);
            }

            foreach (FieldItem item in functionItem.RequestFields)
            {
                switch (item.Name)
                {
                case "user_token":
                    packer.AddStr(LoginManager.Instance.LoginUser.Token);
                    break;

                case "account_code":
                    packer.AddStr("");
                    break;

                default:
                    break;
                }
            }

            packer.EndPack();

            unsafe
            {
                bizMessage.SetContent(packer.GetPackBuf(), packer.GetPackLen());
            }

            var parser = _t2SDKWrap.SendSync2(bizMessage);

            packer.Dispose();
            bizMessage.Dispose();

            if (parser.ErrorCode != ConnectionCode.Success)
            {
                UFXLogger.Error(logger, functionCode, "账户查询失败");
                return(ConnectionCode.ErrorConn);
            }

            var response = T2ErrorHandler.Handle(parser);

            if (T2ErrorHandler.Success(response.ErrorCode))
            {
                if (parser.DataSets.Count > 1)
                {
                    var dataSet = parser.DataSets[1];
                    foreach (var dataRow in dataSet.Rows)
                    {
                        AccountItem acc = new AccountItem();
                        acc.AccountCode = dataRow.Columns["account_code"].GetStr();
                        acc.AccountName = dataRow.Columns["account_name"].GetStr();

                        string accType = dataRow.Columns["account_type"].GetStr();
                        int    temp;
                        if (int.TryParse(accType, out temp))
                        {
                            acc.AccountType = (FundAccountType)temp;
                        }

                        LoginManager.Instance.AddAccount(acc);
                    }
                }

                return(ConnectionCode.Success);
            }
            else
            {
                UFXLogger.Error(logger, functionCode, response);
                return(ConnectionCode.ErrorFailContent);
            }
        }
        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);
        }
Example #14
0
        public ConnectionCode Login(LoginUser user)
        {
            UFXFunctionCode functionCode = UFXFunctionCode.Login;
            FunctionItem    functionItem = ConfigManager.Instance.GetFunctionConfig().GetFunctionItem(functionCode);

            if (functionItem == null || functionItem.RequestFields == null || functionItem.RequestFields.Count == 0)
            {
                return(ConnectionCode.ErrorLogin);
            }

            LoginManager.Instance.LoginUser = user;
            CT2BizMessage bizMessage = new CT2BizMessage();

            //初始化
            bizMessage.SetFunction((int)functionCode);
            bizMessage.SetPacketType(CT2tag_def.REQUEST_PACKET);

            //业务包
            CT2Packer packer = new CT2Packer(2);

            packer.BeginPack();
            foreach (FieldItem item in functionItem.RequestFields)
            {
                packer.AddField(item.Name, item.Type, item.Width, item.Scale);
            }

            foreach (FieldItem item in functionItem.RequestFields)
            {
                switch (item.Name)
                {
                case "operator_no":
                    packer.AddStr(user.Operator);
                    break;

                case "password":
                    packer.AddStr(user.Password);
                    break;

                case "mac_address":
                {
                    packer.AddStr(ConfigManager.Instance.GetTerminalConfig().MacAddress);
                }
                break;

                case "op_station":
                {
                    packer.AddStr("www.htsec.com");
                }
                break;

                case "ip_address":
                {
                    packer.AddStr(ConfigManager.Instance.GetTerminalConfig().IPAddress);
                }
                break;

                case "hd_volserial":
                {
                    packer.AddStr("");
                }
                break;

                case "authorization_id":
                {
                    //TODO:
                    packer.AddStr("authorization_id");
                }
                break;

                default:
                    if (item.Type == UFXPackFieldType.IntType)
                    {
                        packer.AddInt(-1);
                    }
                    else if (item.Type == UFXPackFieldType.StringType || item.Type == UFXPackFieldType.CharType)
                    {
                        packer.AddStr(item.Name);
                    }
                    else
                    {
                        packer.AddStr(item.Name);
                    }
                    break;
                }
            }
            packer.EndPack();

            unsafe
            {
                bizMessage.SetContent(packer.GetPackBuf(), packer.GetPackLen());
            }

            var parser = _t2SDKWrap.SendSync2(bizMessage);

            packer.Dispose();
            bizMessage.Dispose();

            var ret = parser.ErrorCode;

            if (ret == ConnectionCode.Success)
            {
                string token    = string.Empty;
                string version  = string.Empty;
                var    response = T2ErrorHandler.Handle(parser);
                if (T2ErrorHandler.Success(response.ErrorCode))
                {
                    if (parser.DataSets[1].Rows[0].Columns.ContainsKey("user_token"))
                    {
                        token = parser.DataSets[1].Rows[0].Columns["user_token"].GetStr();
                    }

                    if (parser.DataSets[1].Rows[0].Columns.ContainsKey("version_no"))
                    {
                        version = parser.DataSets[1].Rows[0].Columns["version_no"].GetStr();
                    }

                    string msg = string.Format("Login success - token: [{0}], version: [{1}]", token, version);
                    UFXLogger.Info(logger, functionCode, msg);
                }
                else
                {
                    UFXLogger.Error(logger, functionCode, response);
                }

                if (!string.IsNullOrEmpty(token))
                {
                    LoginManager.Instance.LoginUser.Token = token;
                    ret = ConnectionCode.Success;
                }
                else
                {
                    ret = ConnectionCode.ErrorLogin;
                }
            }

            return(ret);
        }
Example #15
0
        public List <UFXHoldingResponse> Query(CallerCallback callback)
        {
            List <UFXHoldingResponse> holdingItems = new List <UFXHoldingResponse>();
            var portfolios = _productBLL.GetAll();

            foreach (var portfolio in portfolios)
            {
                var ufxRequests = new List <UFXHoldingRequest>();
                var ufxRequest  = new UFXHoldingRequest
                {
                    CombiNo = portfolio.PortfolioNo,
                };

                ufxRequests.Add(ufxRequest);

                Callbacker callbacker = new Callbacker
                {
                    Token = new CallerToken
                    {
                        SubmitId  = 90000,
                        CommandId = 90001,
                        InArgs    = portfolio.PortfolioNo,
                        OutArgs   = holdingItems,
                        WaitEvent = new AutoResetEvent(false),
                        Caller    = callback,
                    },

                    DataHandler = DataHandlerCallback,
                };

                var result = _securityBLL.QueryHolding(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 QueryHolding";
                        }
                        else
                        {
                            bllResponse.Code    = ConnectionCode.FailQueryHolding;
                            bllResponse.Message = "Fail QueryHolding: " + errorResponse.ErrorMessage;
                        }
                    }
                    else
                    {
                        bllResponse.Code    = ConnectionCode.FailTimeoutQueryHolding;
                        bllResponse.Message = "Fail QueryHolding: Timeout";
                    }
                }
                else
                {
                    bllResponse.Code    = result;
                    bllResponse.Message = "Fail to QueryHolding in ufx.";
                }
            }

            return(holdingItems);
        }
Example #16
0
        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);
        }
        public List <DealFlowItem> QueryToday(List <Portfolio> portfolios, int timeOut, CallerCallback callback)
        {
            List <DealFlowItem> dealItems = new List <DealFlowItem>();

            foreach (var portfolio in portfolios)
            {
                List <UFXQueryDealRequest> requests = new List <UFXQueryDealRequest>();

                UFXQueryDealRequest request = new UFXQueryDealRequest();

                request.AccountCode = portfolio.FundCode;
                request.CombiNo     = portfolio.PortfolioNo;
                requests.Add(request);

                Callbacker callbacker = new Callbacker
                {
                    Token = new CallerToken
                    {
                        SubmitId  = -1,
                        CommandId = -1,
                        InArgs    = portfolio,
                        OutArgs   = dealItems,
                        WaitEvent = new AutoResetEvent(false),
                        Caller    = callback,
                    },

                    DataHandler = QueryDataHandler,
                };

                var result = _securityBLL.QueryDeal(requests, 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 QueryDeal";
                        }
                        else
                        {
                            bllResponse.Code    = ConnectionCode.FailQueryDeal;
                            bllResponse.Message = "Fail QueryDeal: " + errorResponse.ErrorMessage;
                        }
                    }
                    else
                    {
                        bllResponse.Code    = ConnectionCode.FailTimeoutQueryDeal;
                        bllResponse.Message = "Fail QueryDeal: Timeout!";
                    }
                }
                else
                {
                    bllResponse.Code    = result;
                    bllResponse.Message = "Fail to QueryDeal in ufx.";
                }
            }

            return(dealItems);
        }
Example #18
0
        /// <summary>
        /// Withdraw the entrusted securites.
        /// NOTE: the entrust_no is  necessary.
        /// </summary>
        /// <param name="submitId">The entrustsecurity SubmitId.</param>
        /// <param name="commandId">The tradingcommand CommandId.</param>
        /// <param name="entrustItems">The entrustsecurity item.</param>
        /// <param name="callerCallback"></param>
        /// <returns></returns>
        public BLLResponse Withdraw(int submitId, int commandId, List <EntrustSecurity> entrustItems, CallerCallback callerCallback)
        {
            BLLResponse bllResponse = new BLLResponse();

            List <UFXWithdrawRequest> requests = new List <UFXWithdrawRequest>();

            foreach (var entrustItem in entrustItems)
            {
                UFXWithdrawRequest request = new UFXWithdrawRequest
                {
                    EntrustNo = entrustItem.EntrustNo,
                };

                requests.Add(request);
            }

            Callbacker callbacker = new Callbacker
            {
                Token = new CallerToken
                {
                    SubmitId  = submitId,
                    CommandId = commandId,
                    WaitEvent = new AutoResetEvent(false),
                    Caller    = callerCallback,
                },

                DataHandler = WithdrawDataHandler,
            };

            var result = _securityBLL.Withdraw(requests, callbacker);

            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    = Model.ConnectionCode.Success;
                        bllResponse.Message = "Success Withdraw";
                    }
                    else
                    {
                        bllResponse.Code    = Model.ConnectionCode.FailWithdraw;
                        bllResponse.Message = errorResponse.ErrorMessage;
                    }
                }
                else
                {
                    bllResponse.Code    = Model.ConnectionCode.FailTimeoutWithdraw;
                    bllResponse.Message = "Fail to submit the basket withdraw to UFX: Timeout";
                }
            }
            else
            {
                bllResponse.Code    = Model.ConnectionCode.FailSubmit;
                bllResponse.Message = "Fail to submit the basket withdraw to UFX!";
            }

            return(bllResponse);
        }
Example #19
0
        public ConnectionCode HeartBeat()
        {
            UFXFunctionCode functionCode = UFXFunctionCode.HeartBeat;
            FunctionItem    functionItem = ConfigManager.Instance.GetFunctionConfig().GetFunctionItem(functionCode);

            if (functionItem == null || functionItem.RequestFields == null || functionItem.RequestFields.Count == 0)
            {
                return(ConnectionCode.ErrorLogin);
            }

            CT2BizMessage bizMessage = new CT2BizMessage();

            //初始化
            bizMessage.SetFunction((int)functionCode);
            bizMessage.SetPacketType(CT2tag_def.REQUEST_PACKET);

            //业务包
            CT2Packer packer = new CT2Packer(2);

            packer.BeginPack();
            foreach (FieldItem item in functionItem.RequestFields)
            {
                packer.AddField(item.Name, item.Type, item.Width, item.Scale);
            }

            foreach (FieldItem item in functionItem.RequestFields)
            {
                switch (item.Name)
                {
                case "user_token":
                    packer.AddStr(LoginManager.Instance.LoginUser.Token);
                    break;

                default:
                    break;
                }
            }

            packer.EndPack();

            unsafe
            {
                bizMessage.SetContent(packer.GetPackBuf(), packer.GetPackLen());
            }

            var parser = _t2SDKWrap.SendSync2(bizMessage);

            packer.Dispose();
            bizMessage.Dispose();

            if (parser.ErrorCode == ConnectionCode.Success)
            {
                var response = T2ErrorHandler.Handle(parser);
                if (!T2ErrorHandler.Success(response.ErrorCode))
                {
                    UFXLogger.Error(logger, functionCode, response);
                    return(ConnectionCode.ErrorFailContent);
                }
            }
            else
            {
                UFXLogger.Error(logger, functionCode, "心跳检测失败");
                return(ConnectionCode.ErrorConn);
            }

            return(ConnectionCode.Success);
        }
        private int EntrustDataHandler(CallerToken token, DataParser dataParser)
        {
            var errorResponse = T2ErrorHandler.Handle(dataParser);

            token.ErrorResponse = errorResponse;

            List <UFXBasketEntrustResponse> responseItems = UFXDataSetHelper.ParseData <UFXBasketEntrustResponse>(dataParser);

            int ret = -1;
            List <EntrustSecurity> entrustSecuItems = new List <EntrustSecurity>();

            foreach (var responseItem in responseItems)
            {
                var entrustItem = new EntrustSecurity
                {
                    SubmitId         = token.SubmitId,
                    RequestId        = responseItem.ExtSystemId,
                    SecuCode         = responseItem.StockCode,
                    EntrustNo        = responseItem.EntrustNo,
                    BatchNo          = responseItem.BatchNo,
                    EntrustFailCode  = responseItem.EntrustFailCode,
                    EntrustFailCause = responseItem.FailCause,
                };

                entrustSecuItems.Add(entrustItem);
            }

            ret = _entrustCombineBLL.UpdateSecurityEntrustResponseByRequestId(entrustSecuItems);

            if (T2ErrorHandler.Success(errorResponse.ErrorCode))
            {
                int batchNo  = 0;
                var batchNos = responseItems.Select(p => p.BatchNo).Distinct().ToList();
                if (batchNos.Count == 1)
                {
                    batchNo = batchNos[0];
                }

                ret = _entrustCommandBLL.UpdateEntrustCommandBatchNo(token.SubmitId, batchNo, Model.EnumType.EntrustStatus.Completed, errorResponse.ErrorCode, errorResponse.ErrorMessage);
            }
            else
            {
                ret = _entrustCommandBLL.UpdateEntrustCommandBatchNo(token.SubmitId, 0, Model.EnumType.EntrustStatus.EntrustFailed, errorResponse.ErrorCode, errorResponse.ErrorMessage);

                //TODO:
                string msg = string.Format("The SubmitId [{0}] fail to entrust. ErrorCode: {1}, ErrorMessage: {2}, MessageDetail: {3}", token.SubmitId, errorResponse.ErrorCode, errorResponse.ErrorMessage, errorResponse.MessageDetail);
                logger.Error(msg);
            }

            try
            {
                if (token.Caller != null)
                {
                    token.Caller(token, entrustSecuItems, errorResponse);
                }
            }
            finally
            {
                if (token.WaitEvent != null)
                {
                    token.WaitEvent.Set();
                }
            }

            return(ret);
        }
Example #21
0
        public List <EntrustFlowItem> QueryHistory(List <Portfolio> portfolios, DateTime startDate, DateTime endDate, int timeOut, CallerCallback callback)
        {
            List <EntrustFlowItem> entrustItems = new List <EntrustFlowItem>();
            int intStart = DateUtil.GetIntDate(startDate);
            int intEnd   = DateUtil.GetIntDate(endDate);

            foreach (var portfolio in portfolios)
            {
                List <UFXQueryFuturesHistEntrustRequest> requests = new List <UFXQueryFuturesHistEntrustRequest>();

                UFXQueryFuturesHistEntrustRequest request = new UFXQueryFuturesHistEntrustRequest();
                request.StartDate = intStart;
                request.EndDate   = intEnd;
                request.CombiNo   = portfolio.PortfolioNo;

                requests.Add(request);

                Callbacker callbacker = new Callbacker
                {
                    Token = new CallerToken
                    {
                        SubmitId  = -2,
                        CommandId = -2,
                        InArgs    = portfolio,
                        OutArgs   = entrustItems,
                        WaitEvent = new AutoResetEvent(false),
                        Caller    = callback,
                    },

                    DataHandler = QueryDataHandler,
                };

                var result = _securityBLL.QueryFuturesEntrustHistory(requests, 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.ErrorMessage;
                        }
                    }
                    else
                    {
                        bllResponse.Code    = ConnectionCode.FailEntrust;
                        bllResponse.Message = "Fail Entrust: Timeout";
                    }
                }
                else
                {
                    bllResponse.Code    = result;
                    bllResponse.Message = "Fail to submit in ufx.";
                }
            }

            return(entrustItems);
        }
Example #22
0
        private int EntrustBasketCallback(CallerToken token, DataParser dataParser)
        {
            int ret = -1;
            List <EntrustSecurityItem> entrustSecuItems = new List <EntrustSecurityItem>();

            var errorResponse = T2ErrorHandler.Handle(dataParser);

            token.OutArgs = errorResponse;
            if (T2ErrorHandler.Success(errorResponse.ErrorCode))
            {
                List <UFXBasketEntrustResponse> responseItems = new List <UFXBasketEntrustResponse>();
                var dataFieldMap = UFXDataBindingHelper.GetProperty <UFXBasketEntrustResponse>();
                for (int i = 1, count = dataParser.DataSets.Count; i < count; i++)
                {
                    var dataSet = dataParser.DataSets[i];
                    foreach (var dataRow in dataSet.Rows)
                    {
                        UFXBasketEntrustResponse p = new UFXBasketEntrustResponse();
                        UFXDataSetHelper.SetValue <UFXBasketEntrustResponse>(ref p, dataRow.Columns, dataFieldMap);
                        responseItems.Add(p);
                    }
                }

                foreach (var responseItem in responseItems)
                {
                    var entrustItem = new EntrustSecurityItem
                    {
                        SubmitId         = token.SubmitId,
                        RequestId        = responseItem.ExtSystemId,
                        SecuCode         = responseItem.StockCode,
                        EntrustNo        = responseItem.EntrustNo,
                        BatchNo          = responseItem.BatchNo,
                        EntrustFailCode  = responseItem.EntrustFailCode,
                        EntrustFailCause = responseItem.FailCause,
                    };

                    entrustSecuItems.Add(entrustItem);
                }

                ret = _entrustdao.UpdateSecurityEntrustResponseByRequestId(entrustSecuItems);

                if (token.InArgs != null && token.InArgs is EntrustCommandItem)
                {
                    var cmdItem = token.InArgs as EntrustCommandItem;

                    ret = _tradecmddao.UpdateTargetNum(cmdItem.CommandId, cmdItem.Copies);
                }

                var batchNos = responseItems.Select(p => p.BatchNo).Distinct().ToList();
                if (batchNos.Count == 1)
                {
                    int batchNo = batchNos[0];

                    ret = _entrustcmddao.UpdateEntrustCommandBatchNo(token.SubmitId, batchNo, Model.EnumType.EntrustStatus.Completed, errorResponse.ErrorCode, errorResponse.ErrorMessage);
                }
                else
                {
                    //TODO:
                    string msg = string.Format("The SubmitId [{0}] was split into several batch no", token.SubmitId);
                    logger.Warn(msg);
                }
            }
            else
            {
                ret = -1;
            }

            if (token.Caller != null)
            {
                token.Caller(token, entrustSecuItems, errorResponse);
            }

            if (token.WaitEvent != null)
            {
                token.WaitEvent.Set();
            }

            return(ret);
        }