/// <summary>
        /// 获取Kline数据
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="periodSeconds"></param>
        /// <param name="limit"></param>
        /// <param name="startDate"></param>
        /// <returns></returns>
        public KlineDataModel GetKlineData(string symbol, int periodSeconds, int?limit = null, DateTime?startDate = null)
        {
            try
            {
                var result = new KlineDataModel();

                string url          = $"{BaseUrl}/kline.do?symbol={symbol}";
                string periodString = CryptoUtility.SecondsToPeriodStringLong(periodSeconds);
                url += "&type=" + periodString;

                if (startDate != null)
                {
                    ////url += "&since=" + (long)startDate.Value.UnixTimestampFromDateTimeMilliseconds();
                    url += "&since=" + GetTimeStamp(Convert.ToDateTime(startDate));
                }
                if (limit != null)
                {
                    url += "&size=" + (limit.Value.ToStringInvariant());
                }

                var info = HttpGet(url, null, 5);
                var data = JsonConvert.DeserializeObject <List <object[]> >(info);
                result.Symbol = symbol;
                result.Data   = data;
                var firstItem = data.First();
                result.EventTime = ConvertStringToDateTime(firstItem[0].ToString());

                return(result);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        protected override async Task <IEnumerable <MarketCandle> > OnGetCandlesAsync(string symbol, int periodSeconds, DateTime?startDate = null, DateTime?endDate = null, int?limit = null)
        {
            /*
             * {
             * "status": "ok",
             * "ch": "market.btcusdt.kline.1day",
             * "ts": 1499223904680,
             * “data”: [
             * {
             *  "id": 1499184000,
             *  "amount": 37593.0266,
             *  "count": 0,
             *  "open": 1935.2000,
             *  "close": 1879.0000,
             *  "low": 1856.0000,
             *  "high": 1940.0000,
             *  "vol": 71031537.97866500
             * },
             */

            List <MarketCandle> candles = new List <MarketCandle>();

            symbol = NormalizeSymbol(symbol);
            string url = "/market/history/kline?symbol=" + symbol;

            if (limit != null)
            {
                // default is 150, max: 2000
                url += "&size=" + (limit.Value.ToStringInvariant());
            }
            string periodString = CryptoUtility.SecondsToPeriodStringLong(periodSeconds);

            url += "&period=" + periodString;
            JToken allCandles = await MakeJsonRequestAsync <JToken>(url, BaseUrl, null);

            var ts = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(allCandles.Parent.Parent["ts"].ConvertInvariant <long>());

            foreach (var array in allCandles)
            {
                candles.Add(new MarketCandle
                {
                    ClosePrice      = array["close"].ConvertInvariant <decimal>(),
                    ExchangeName    = Name,
                    HighPrice       = array["high"].ConvertInvariant <decimal>(),
                    LowPrice        = array["low"].ConvertInvariant <decimal>(),
                    Name            = symbol,
                    OpenPrice       = array["open"].ConvertInvariant <decimal>(),
                    PeriodSeconds   = periodSeconds,
                    Timestamp       = CryptoUtility.UnixTimeStampToDateTimeSeconds(array["id"].ConvertInvariant <long>()),
                    BaseVolume      = array["vol"].ConvertInvariant <decimal>() / array["close"].ConvertInvariant <decimal>(),
                    ConvertedVolume = array["vol"].ConvertInvariant <decimal>(),
                    WeightedAverage = 0m
                });
            }

            candles.Reverse();

            return(candles);
        }
        protected override async Task <IEnumerable <MarketCandle> > OnGetCandlesAsync(string symbol, int periodSeconds, DateTime?startDate = null, DateTime?endDate = null, int?limit = null)
        {
            /*
             * [
             *      1417564800000,	timestamp
             *      384.47,		open
             *      387.13,		high
             *      383.5,		low
             *      387.13,		close
             *      1062.04,	volume
             * ]
             */

            List <MarketCandle> candles = new List <MarketCandle>();

            symbol = NormalizeSymbol(symbol);
            string url          = "/future_kline.do?symbol=" + symbol;
            string periodString = CryptoUtility.SecondsToPeriodStringLong(periodSeconds);

            url += "&type=" + periodString;
            url += "&contract_type=this_week";

            if (startDate != null)
            {
                url += "&since=" + (long)startDate.Value.UnixTimestampFromDateTimeMilliseconds();
            }
            if (limit != null)
            {
                url += "&size=" + (limit.Value.ToStringInvariant());
            }

            JToken obj = await MakeJsonRequestAsync <JToken>(url);

            foreach (JArray array in obj)
            {
                decimal closePrice = array[4].ConvertInvariant <decimal>();
                decimal baseVolume = array[5].ConvertInvariant <decimal>();
                candles.Add(new MarketCandle
                {
                    Timestamp       = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(array[0].ConvertInvariant <long>()),
                    OpenPrice       = array[1].ConvertInvariant <decimal>(),
                    HighPrice       = array[2].ConvertInvariant <decimal>(),
                    LowPrice        = array[3].ConvertInvariant <decimal>(),
                    ClosePrice      = closePrice,
                    BaseVolume      = baseVolume,
                    ConvertedVolume = (closePrice * (decimal)baseVolume),
                    ExchangeName    = Name,
                    Name            = symbol,
                    PeriodSeconds   = periodSeconds,
                });
            }

            return(candles);
        }