Пример #1
0
 /// <summary>
 /// Get the K-Line for the specified ticker symbol and the specified time range.
 /// </summary>
 /// <param name="symbol">The symbol</param>
 /// <param name="type">The K-Line type (the length of time represented by a single candlestick)</param>
 /// <param name="startTime">Start time</param>
 /// <param name="endTime">End time</param>
 /// <returns>A list of candlesticks</returns>
 public async Task <List <FuturesCandle> > GetKline(string symbol, FuturesKlineType type, DateTime?startTime = null, DateTime?endTime = null)
 {
     return(await GetKline <FuturesCandle, FuturesCandle, List <FuturesCandle> >(symbol, type, startTime, endTime));
 }
Пример #2
0
        /// <summary>
        /// Get a customized K-Line for the specified ticker symbol and the specified time range.
        /// </summary>
        /// <typeparam name="TCandle">The type of the K-Line objects to create that implements both <see cref="IFullCandle"/> and <typeparamref name="TCustom"/>.</typeparam>
        /// <typeparam name="TCustom">
        /// The (usually user-provided) type of the objects to return.
        /// Objects of this type will be returned in the newly created collection.
        /// </typeparam>
        /// <typeparam name="TCol">The type of the collection that contains <typeparamref name="TCustom"/> objects.</typeparam>
        /// <param name="symbol">The symbol</param>
        /// <param name="type">The K-Line type (the length of time represented by a single candlestick)</param>
        /// <param name="startTime">Start time</param>
        /// <param name="endTime">End time</param>
        /// <returns>A list of candlesticks</returns>
        public async Task <TCol> GetKline <TCandle, TCustom, TCol>(
            string symbol,
            FuturesKlineType type,
            DateTime?startTime = null,
            DateTime?endTime   = null)
            where TCandle : ICandle, TCustom, new()
            where TCol : IList <TCustom>, new()
        {
            var curl = "/api/v1/kline/query";

            long st, et;
            var  param = new Dictionary <string, object>();

            DateTime d;

            param.Add("symbol", symbol);
            param.Add("granularity", (int)type);


            if (startTime == null)
            {
                st = 0;
            }
            else
            {
                d  = (DateTime)startTime;
                st = EpochTime.DateToMilliseconds(d);
                param.Add("from", st.ToString());
            }

            if (endTime == null)
            {
                et = 0;
            }
            else
            {
                d  = (DateTime)startTime;
                et = EpochTime.DateToMilliseconds(d);
                param.Add("to", et.ToString());
            }

            if (startTime != null && endTime != null && et < st)
            {
                throw new ArgumentException("End time must be greater than start time");
            }

            var jobj = await MakeRequest(HttpMethod.Get, curl, 5, false, param);

            var klineRaw = jobj.ToObject <List <List <string> > >();

            var results = new TCol();

            //klineRaw.Reverse();

            foreach (var values in klineRaw)
            {
                var candle = new TCandle
                {
                    Timestamp  = EpochTime.MillisecondsToDate(long.Parse(values[0])),
                    OpenPrice  = decimal.Parse(values[1]),
                    HighPrice  = decimal.Parse(values[2]),
                    LowPrice   = decimal.Parse(values[3]),
                    ClosePrice = decimal.Parse(values[4]),
                    Volume     = decimal.Parse(values[5])
                };

                if (candle is IKlineCandle <FuturesKlineType> tc)
                {
                    tc.Type = type;
                }
                else if (candle is IFullKlineCandle <FuturesKlineType> tce)
                {
                    tce.Type = type;
                }

                results.Add(candle);
            }

            return(results);
        }