Exemple #1
0
        /// <summary>
        /// 执行TASK更新市场深度
        /// </summary>
        /// <param name="model"></param>
        public void TaskExecution(VmpConfigModel model)
        {
            //Log.Info($"执行更新市场深度TaskExecution,platformCode={platformCode};coinCode={coinCode};currencyCode={currencyCode}");
            try
            {
                ExchangeType   eType      = (ExchangeType)Enum.Parse(typeof(ExchangeType), model.PlatformCode);
                ExChangeBase   eb         = ExchangeFactory.InstanExchange(eType);
                LatePriceModel priceModel = eb.GetLatestRecord(model.CurrencyCode, model.ExCurrencyCode);
                if (priceModel.Asks == null)
                {
                    Log.Error($"执行查询集合为空,platformCode={model.PlatformCode};coinCode={model.CurrencyCode};currencyCode={model.ExCurrencyCode}");
                    return;
                }

                //更新数据库市场深度
                //UpdateLatePriceFacade latePriceFacade =new UpdateLatePriceFacade();
                //latePriceFacade.InsertLatestRecord(model, priceModel);
                //更新缓存市场深度
                UpdateDepthFacade depthFacade = new UpdateDepthFacade();
                if (!depthFacade.UpdateLatePrice(model, priceModel))
                {
                    Log.Error($"Error,更新缓存市场深度失败,platformCode={model.PlatformCode};coinCode={model.CurrencyCode};currencyCode={model.ExCurrencyCode}");
                }

                GC.Collect();
            }
            catch (Exception ex)
            {
                Log.Error("执行查询插入数据出错" + ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// 装载MODEL
        /// </summary>
        /// <param name="configModel"></param>
        /// <param name="priceModel"></param>
        /// <returns></returns>
        private LatePriceCacheBase InsertPrice(VmpConfigModel configModel, LatePriceModel priceModel)
        {
            ///默认交易深度50条
            int specifiedQuantity = 50;
            int askscount         = priceModel.Asks.Count;
            int bidscount         = priceModel.Bids.Count;

            if (askscount > specifiedQuantity)
            {
                askscount = specifiedQuantity;
            }
            if (bidscount > specifiedQuantity)
            {
                askscount = specifiedQuantity;
            }

            List <PriceModel> ListAsks = priceModel.Asks.OrderBy(p => p.price).Take(specifiedQuantity).ToList();
            List <PriceModel> ListBids = priceModel.Bids.OrderByDescending(p => p.price).Take(specifiedQuantity).ToList();

            List <TickModel> bidTick = new List <TickModel>();

            for (int i = 0; i < bidscount; i++)
            {
                TickModel bidMode = new TickModel();
                bidMode.Amount = ListBids[i].amount;
                bidMode.Price  = ListBids[i].price;
                bidMode.Sort   = i;
                bidTick.Add(bidMode);
            }

            List <TickModel> askTick = new List <TickModel>();

            for (int k = 0; k < askscount; k++)
            {
                TickModel askModel = new TickModel();
                askModel.Amount = ListAsks[k].amount;
                askModel.Price  = ListAsks[k].price;
                askModel.Sort   = k;
                askTick.Add(askModel);
            }
            LatePriceCacheBase priceCache = new LatePriceCacheBase();
            LatePriceCacheBase tempPrice  = MapProvider.Map(priceCache, configModel);

            tempPrice.LateTime  = DateTime.Now;
            tempPrice.BuyPrice  = ListBids.FirstOrDefault().price;
            tempPrice.BuyCount  = ListBids.FirstOrDefault().amount;
            tempPrice.SellPrice = ListAsks.FirstOrDefault().price;
            tempPrice.SellCount = ListAsks.FirstOrDefault().amount;
            tempPrice.Asks      = askTick;
            tempPrice.Bids      = bidTick;

            return(tempPrice);
        }
        /// <summary>
        /// 更新当前市场深度
        /// </summary>
        /// <param name="configModel">数据库配置Model</param>
        /// <param name="priceModel">市场深度MODEL</param>
        /// <returns></returns>
        public bool InsertLatestRecord(VmpConfigModel configModel, LatePriceModel priceModel)
        {
            //Log.Info($"执行InsertLatestRecord:{platformId}_{pairId}");
            Tmp_Late_Price tlatePrice  = new Tmp_Late_Price();
            int            isExisrence = tlatePrice.IsExistencePrice(configModel.PlatformId, configModel.PairId).Safe().ToInt32();

            if (isExisrence > 0)
            {
                Tmp_Late_PriceCollection latestPriceColl = new Tmp_Late_PriceCollection();
                if (!latestPriceColl.DelByPlatform(configModel.PlatformId, configModel.PairId))
                {
                    Log.Error("删除交易信息失败" + configModel.PairCode);
                }
            }

            int askscount = priceModel.Asks.Count;
            int bidscount = priceModel.Bids.Count;

            ///默认交易深度5条
            int specifiedQuantity = 5;

            if (bidscount < askscount || askscount < specifiedQuantity)
            {
                return(false);
            }

            List <PriceModel> ListAsks = priceModel.Asks.OrderBy(p => p.price).Take(specifiedQuantity).ToList();
            List <PriceModel> ListBids = priceModel.Bids.OrderByDescending(p => p.price).Take(specifiedQuantity).ToList();

            for (int i = 0; i < specifiedQuantity; i++)
            {
                Tmp_Late_Price latePrice = new Tmp_Late_Price();
                latePrice.BuyPrice   = ListBids[i].price;
                latePrice.BuyCount   = ListBids[i].amount;
                latePrice.PairId     = configModel.PairId;;
                latePrice.SellPrice  = ListAsks[i].price;
                latePrice.SellCount  = ListAsks[i].amount;
                latePrice.Sort       = i;
                latePrice.LateTime   = DateTime.Now;
                latePrice.PlatformId = configModel.PlatformId;
                if (!latePrice.Insert())
                {
                    Log.Error("新增交易价格失败");
                    continue;
                }
            }
            //Log.Info($"执行完成InsertLatestRecord:{platformId}_{pairId}");
            return(true);
        }
Exemple #4
0
        /// <summary>
        /// 更新缓存交易深度记录
        /// </summary>
        /// <param name="configModel">数据配置</param>
        /// <param name="priceModel">价格深度</param>
        /// <returns></returns>
        public bool UpdateLatePrice(VmpConfigModel configModel, LatePriceModel priceModel)
        {
            Log.Info($"更新市场深度缓存UpdateLatePrice:{configModel.PlatformCode}_{configModel.PairCode}");
            string             cacheKey   = string.Format("LatePrice_{0}_{1}", configModel.PlatformId, configModel.PairId);
            LatePriceCacheBase priceCache = InsertPrice(configModel, priceModel);
            var cache = CacheManage.GetInstance();

            if (!cache.Add(cacheKey, priceCache))
            {
                Alert("更新缓存失败");
                Log.Error("更新缓存失败" + cacheKey);
                return(false);
            }
            return(true);
        }
Exemple #5
0
        /// <summary>
        /// 获取市场深度
        /// </summary>
        /// <param name="coin"></param>
        /// <param name="currency"></param>
        /// <returns></returns>
        public override LatePriceModel GetLatestRecord(string coin, string currency)
        {
            LatePriceModel latePrice = new LatePriceModel();
            string         Symbol    = ConvertSymbolTool.HBConvertSymbol(coin, currency);

            Symbol += "&type=step0";
            var result = api.SendRequestContent <DepthRequest>(ApiUrlList.Api_Depth, Symbol);

            if (result == null)
            {
                Log.Error("火币数据为空" + coin);
                return(latePrice);
            }
            List <PriceModel> asksList = new List <PriceModel>();

            foreach (var asksPrice in result.tick.asks)
            {
                PriceModel asks = new PriceModel();
                asks.price  = asksPrice[0];
                asks.amount = asksPrice[1];
                asksList.Add(asks);
            }

            List <PriceModel> bidsList = new List <PriceModel>();

            foreach (var bidsPrice in result.tick.bids)
            {
                PriceModel bids = new PriceModel();
                bids.price  = bidsPrice[0];
                bids.amount = bidsPrice[1];
                bidsList.Add(bids);
            }

            latePrice.Asks = asksList;
            latePrice.Bids = bidsList;
            return(latePrice);
        }
Exemple #6
0
        public override LatePriceModel GetLatestRecord(string coin, string currency)
        {
            LatePriceModel latePrice = new LatePriceModel();
            string         Symbol    = ConvertSymbolTool.BiAnConvertSymbol(coin, currency);
            var            result    = api.SendRequestContent <DepthRequest>(ApiUrlList.API_Depth, Symbol);

            if (result == null)
            {
                Log.Error("币安数据为空" + coin);
                return(latePrice);
            }

            List <PriceModel> asksList = new List <PriceModel>();

            foreach (var asksPrice in result.asks)
            {
                PriceModel asks = new PriceModel();
                asks.price  = Convert.ToDecimal(asksPrice[0]);
                asks.amount = Convert.ToDecimal(asksPrice[1]);
                asksList.Add(asks);
            }

            List <PriceModel> bidsList = new List <PriceModel>();

            foreach (var bidsPrice in result.bids)
            {
                PriceModel bids = new PriceModel();
                bids.price  = Convert.ToDecimal(bidsPrice[0]);
                bids.amount = Convert.ToDecimal(bidsPrice[1]);
                bidsList.Add(bids);
            }

            latePrice.Asks = asksList;
            latePrice.Bids = bidsList;
            return(latePrice);
        }
        /// <summary>
        /// 处理火币返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message"></param>
        /// <returns></returns>
        public void MsgPackage(string message)
        {
            try
            {
                //Log.Info("接收消息2"+message);
                int index = message.IndexOf("subbed");
                if (index >= 0)
                {
                    Log.Info("调用火币监听成功:" + message);
                    return;
                }

                DepthWSRequest depth = JsonConvert.DeserializeObject <DepthWSRequest>(message);
                //查询所属平台及交易对
                string pairMark = StringProcess.HuoBiStringSplit(depth.ch);

                VpmConfigCache        vpmConfigCache = new VpmConfigCache();
                List <VmpConfigModel> listModel      = vpmConfigCache.ListConfig();
                var model = listModel.Where(p => p.PlatformCode.Equals("HuoBi") && p.Mark.Equals(pairMark)).FirstOrDefault();

                /*此处用于取数据库数据,最新的改为取缓存*/
                //Vmp_Config vmp_Config = new Vmp_Config();
                //if (!vmp_Config.GetByPlatformPair("HuoBi", pairMark))
                //{
                //    Log.Error("未找到相应的配置:"+ pairMark);
                //    return;
                //}
                //var model = MapProvider.Map<VmpConfigModel>(vmp_Config.DataRow);

                List <PriceModel> asksList = new List <PriceModel>();
                foreach (var asksPrice in depth.tick.asks)
                {
                    PriceModel asks = new PriceModel();
                    asks.price  = asksPrice[0];
                    asks.amount = asksPrice[1];
                    asksList.Add(asks);
                }

                List <PriceModel> bidsList = new List <PriceModel>();
                foreach (var bidsPrice in depth.tick.bids)
                {
                    PriceModel bids = new PriceModel();
                    bids.price  = bidsPrice[0];
                    bids.amount = bidsPrice[1];
                    bidsList.Add(bids);
                }
                LatePriceModel latePrice = new LatePriceModel();
                latePrice.Asks = asksList;
                latePrice.Bids = bidsList;

                //更新数据库市场深度
                //UpdateLatePriceFacade latePriceFacade =new UpdateLatePriceFacade();
                //latePriceFacade.InsertLatestRecord(model, latePrice);
                //更新缓存市场深度
                UpdateDepthFacade depthFacade = new UpdateDepthFacade();
                if (!depthFacade.UpdateLatePrice(model, latePrice))
                {
                    Log.Error($"Error,更新缓存市场深度失败,platformCode={model.PlatformCode};coinCode={model.CurrencyCode};currencyCode={model.ExCurrencyCode}");
                }
            }
            catch (Exception ex)
            {
                Log.Error("更新市场深度失败:" + ex);
            }
        }