示例#1
0
        public bool UpdateTradesStatistic(BitFinexTicker info, int depth) {
            string address = string.Format("https://bittrex.com/api/v1.1/public/getmarkethistory?market={0}", Uri.EscapeDataString(info.MarketName));
            byte[] bytes = null;
            try {
                bytes = GetDownloadBytes(address);
            }
            catch(Exception) {
                return false;
            }
            if(bytes == null)
                return false;

            int startIndex = 1;
            if(!JSonHelper.Default.SkipSymbol(bytes, ':', 3, ref startIndex))
                return false;

            string lastIdString = info.LastTradeId.ToString();
            List<string[]> res = JSonHelper.Default.DeserializeArrayOfObjects(bytes, ref startIndex, new string[] { "Id", "TimeStamp", "Quantity", "Price", "Total", "FillType", "OrderType" },
                (itemIndex, paramIndex, value) => {
                    return paramIndex != 0 || lastIdString != value;
                });
            if(res == null)
                return false;

            int lastTradeId = Convert.ToInt32(res[0][0]);
            if(lastTradeId == info.LastTradeId) {
                info.TradeStatistic.Add(new TradeStatisticsItem() { Time = DateTime.UtcNow });
                if(info.TradeStatistic.Count > 5000) {
                    for(int i = 0; i < 100; i++)
                        info.TradeStatistic.RemoveAt(0);
                }
                return true;
            }

            TradeStatisticsItem st = new TradeStatisticsItem();
            st.MinBuyPrice = double.MaxValue;
            st.MinSellPrice = double.MaxValue;
            lock(info) {
                for(int i = 0; i < res.Count; i++) {
                    string[] obj = res[i];
                    bool isBuy = obj[6].Length == 3;
                    double price = FastValueConverter.Convert(obj[3]);
                    double amount = FastValueConverter.Convert(obj[2]);
                    if(isBuy) {
                        st.BuyAmount += amount;
                        st.MinBuyPrice = Math.Min(st.MinBuyPrice, price);
                        st.MaxBuyPrice = Math.Max(st.MaxBuyPrice, price);
                        st.BuyVolume += amount * price;
                    }
                    else {
                        st.SellAmount += amount;
                        st.MinSellPrice = Math.Min(st.MinSellPrice, price);
                        st.MaxSellPrice = Math.Max(st.MaxSellPrice, price);
                        st.SellVolume += amount * price;
                    }
                }
            }
            if(st.MinSellPrice == double.MaxValue)
                st.MinSellPrice = 0;
            if(st.MinBuyPrice == double.MaxValue)
                st.MinBuyPrice = 0;
            info.LastTradeId = lastTradeId;
            info.TradeStatistic.Add(st);
            if(info.TradeStatistic.Count > 5000) {
                for(int i = 0; i < 100; i++)
                    info.TradeStatistic.RemoveAt(0);
            }
            return true;
        }
        public bool UpdateTradesStatistic(HitBtcTicker ticker, int count)
        {
            string text    = string.Empty;
            string address = string.Format("http://api.hitbtc.com//api/1/public/{0}/trades?from={1}&by=trade_id&sort=desc&start_index=0&max_results={2}&side=true", ticker.MarketName, ticker.LastTradeId, count);

            try {
                text = GetDownloadString(address);
                if (text == null)
                {
                    return(false);
                }
            }
            catch (Exception) {
                return(false);
            }

            Dictionary <string, object> res = null;

            lock (JsonParser) {
                res = (Dictionary <string, object>)JsonParser.Parse(text);
            }
            List <object> trades = (List <object>)res["trades"];

            if (trades.Count == 0)
            {
                ticker.TradeStatistic.Add(new TradeStatisticsItem()
                {
                    Time = DateTime.Now
                });
                if (ticker.TradeStatistic.Count > 5000)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        ticker.TradeStatistic.RemoveAt(0);
                    }
                }
                return(true);
            }

            TradeStatisticsItem st = new TradeStatisticsItem();

            st.MinBuyPrice  = decimal.MaxValue;
            st.MinSellPrice = decimal.MaxValue;
            st.Time         = DateTime.Now;

            foreach (List <object> obj in trades)
            {
                TradeHistoryItem item   = new TradeHistoryItem();
                decimal          amount = Convert.ToDecimal(obj[1]);
                decimal          price  = Convert.ToDecimal(obj[2]);
                bool             isBuy  = obj[4].ToString().Length == 3;
                if (isBuy)
                {
                    st.BuyAmount  += amount;
                    st.MinBuyPrice = Math.Min(st.MinBuyPrice, price);
                    st.MaxBuyPrice = Math.Max(st.MaxBuyPrice, price);
                    st.BuyVolume  += amount * price;
                }
                else
                {
                    st.SellAmount  += amount;
                    st.MinSellPrice = Math.Min(st.MinSellPrice, price);
                    st.MaxSellPrice = Math.Max(st.MaxSellPrice, price);
                    st.SellVolume  += amount * price;
                }
            }
            if (st.MinSellPrice == decimal.MaxValue)
            {
                st.MinSellPrice = 0;
            }
            if (st.MinBuyPrice == decimal.MaxValue)
            {
                st.MinBuyPrice = 0;
            }
            ticker.LastTradeId            = Convert.ToInt64(((List <object>)trades.First())[0]);
            ticker.LastTradeStatisticTime = DateTime.Now;
            ticker.TradeStatistic.Add(st);
            if (ticker.TradeStatistic.Count > 5000)
            {
                for (int i = 0; i < 100; i++)
                {
                    ticker.TradeStatistic.RemoveAt(0);
                }
            }
            return(true);
        }
示例#3
0
        public bool UpdateTradesStatistic(ExmoTicker ticker, int count)
        {
            string text    = string.Empty;
            string address = string.Format("https://api.exmo.me/v1/trades/?pair={0}", ticker.MarketName);

            try {
                text = GetDownloadString(address);
                if (text == null)
                {
                    return(false);
                }
            }
            catch (Exception) {
                return(false);
            }

            Dictionary <string, object> res = null;

            lock (JsonParser) {
                res = (Dictionary <string, object>)JsonParser.Parse(text);
            }
            List <object> trades = (List <object>)res[ticker.MarketName];

            if (trades.Count == 0)
            {
                ticker.TradeStatistic.Add(new TradeStatisticsItem()
                {
                    Time = DateTime.Now
                });
                if (ticker.TradeStatistic.Count > 5000)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        ticker.TradeStatistic.RemoveAt(0);
                    }
                }
                return(true);
            }

            TradeStatisticsItem st = new TradeStatisticsItem();

            st.MinBuyPrice  = decimal.MaxValue;
            st.MinSellPrice = decimal.MaxValue;
            st.Time         = DateTime.Now;

            foreach (Dictionary <string, object> obj in trades)
            {
                TradeHistoryItem item   = new TradeHistoryItem();
                decimal          amount = Convert.ToDecimal(obj["amount"]);
                decimal          price  = Convert.ToDecimal(obj["price"]);
                bool             isBuy  = obj["type"].ToString().Length == 3;
                if (isBuy)
                {
                    st.BuyAmount  += amount;
                    st.MinBuyPrice = Math.Min(st.MinBuyPrice, price);
                    st.MaxBuyPrice = Math.Max(st.MaxBuyPrice, price);
                    st.BuyVolume  += amount * price;
                }
                else
                {
                    st.SellAmount  += amount;
                    st.MinSellPrice = Math.Min(st.MinSellPrice, price);
                    st.MaxSellPrice = Math.Max(st.MaxSellPrice, price);
                    st.SellVolume  += amount * price;
                }
            }
            if (st.MinSellPrice == decimal.MaxValue)
            {
                st.MinSellPrice = 0;
            }
            if (st.MinBuyPrice == decimal.MaxValue)
            {
                st.MinBuyPrice = 0;
            }
            ticker.LastTradeId            = Convert.ToInt64(((Dictionary <string, object>)trades.First())["trade_id"]);
            ticker.LastTradeStatisticTime = DateTime.Now;
            ticker.TradeStatistic.Add(st);
            if (ticker.TradeStatistic.Count > 5000)
            {
                for (int i = 0; i < 100; i++)
                {
                    ticker.TradeStatistic.RemoveAt(0);
                }
            }
            return(true);
        }
        public override bool UpdateTrades(TickerBase ticker)
        {
            string address = string.Format("https://yobit.net/api/3/trades/{0}",
                                           Uri.EscapeDataString(ticker.CurrencyPair));
            string text = ((TickerBase)ticker).DownloadString(address);

            if (string.IsNullOrEmpty(text))
            {
                return(false);
            }

            JObject obj2   = (JObject)JsonConvert.DeserializeObject(text);
            JArray  trades = (JArray)obj2.Value <JArray>(ticker.CurrencyPair);

            if (trades.Count == 0)
            {
                return(true);
            }

            int  lastTradeId    = trades.First().Value <int>("tid");
            long lastGotTradeId = ticker.TradeHistory.Count > 0 ? ticker.TradeHistory.First().Id : 0;

            if (lastGotTradeId == lastTradeId)
            {
                ticker.TradeStatistic.Add(new TradeStatisticsItem()
                {
                    Time = DateTime.UtcNow
                });
                if (ticker.TradeStatistic.Count > 5000)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        ticker.TradeStatistic.RemoveAt(0);
                    }
                }
                return(true);
            }
            TradeStatisticsItem st = new TradeStatisticsItem();

            st.MinBuyPrice  = double.MaxValue;
            st.MinSellPrice = double.MaxValue;
            st.Time         = DateTime.UtcNow;

            int index = 0;

            foreach (JObject obj in trades)
            {
                DateTime time    = new DateTime(1970, 1, 1).AddSeconds(obj.Value <long>("timestamp"));
                int      tradeId = obj.Value <int>("tid");
                if (lastGotTradeId == tradeId)
                {
                    break;
                }

                TradeHistoryItem item = new TradeHistoryItem();

                bool isBuy = obj.Value <string>("type") == "bid";
                item.AmountString = obj.Value <string>("amount");
                item.Time         = time;
                item.Type         = isBuy ? TradeType.Buy : TradeType.Sell;
                item.RateString   = obj.Value <string>("price");
                item.Id           = tradeId;
                double price  = item.Rate;
                double amount = item.Amount;
                item.Total = price * amount;
                if (isBuy)
                {
                    st.BuyAmount  += amount;
                    st.MinBuyPrice = Math.Min(st.MinBuyPrice, price);
                    st.MaxBuyPrice = Math.Max(st.MaxBuyPrice, price);
                    st.BuyVolume  += amount * price;
                }
                else
                {
                    st.SellAmount  += amount;
                    st.MinSellPrice = Math.Min(st.MinSellPrice, price);
                    st.MaxSellPrice = Math.Max(st.MaxSellPrice, price);
                    st.SellVolume  += amount * price;
                }
                ticker.TradeHistory.Insert(index, item);
                index++;
            }
            if (st.MinSellPrice == double.MaxValue)
            {
                st.MinSellPrice = 0;
            }
            if (st.MinBuyPrice == double.MaxValue)
            {
                st.MinBuyPrice = 0;
            }
            ticker.LastTradeStatisticTime = DateTime.UtcNow;
            ticker.TradeStatistic.Add(st);
            if (ticker.TradeStatistic.Count > 5000)
            {
                for (int i = 0; i < 100; i++)
                {
                    ticker.TradeStatistic.RemoveAt(0);
                }
            }
            return(true);
        }