Пример #1
0
 private Candlestick[] ConvertCandles(Candlestick[] data, Timeframe fromTimeframe, Timeframe toTimeframe)
 {
     Candlestick[] ret = new Candlestick[(int)fromTimeframe / (int)toTimeframe * data.Length + 1];
     for (int i = 0, retId = 0; i < data.Length; i += (int)toTimeframe / (int)fromTimeframe)
     {
         if (i + (int)toTimeframe / (int)fromTimeframe > data.Length)
         {
             break;
         }
         ret[retId].high = 0;
         ret[retId].low  = float.MaxValue;
         int j = i;
         ret[retId].open = data[j].open;
         for (; j < (int)toTimeframe / (int)fromTimeframe; j++)
         {
             if (ret[retId].high < data[j].high)
             {
                 ret[retId].high = data[j].high;
             }
             else if (ret[retId].low > data[j].low)
             {
                 ret[retId].low = data[j].low;
             }
         }
         ret[retId].close = data[j].close;
         retId++;
     }
     return(ret);
 }
Пример #2
0
        // TODO: candlestick time is open time
        public async Task <List <Candlestick> > GetCandlesticks(string baseSymbol, string quoteSymbol, Timeframe timeframe, int limit, string exchange = "CCCAGG")
        {
            if (limit > 2000)
            {
                throw new ArgumentException();
            }
            string endpoint  = _MIN_URL + "data/";
            int    aggregate = 1;

            if ((int)Timeframe.d1 <= (int)timeframe)
            {
                endpoint += "histoday";
                aggregate = (int)timeframe / (int)Timeframe.d1;
            }
            else if ((int)Timeframe.h1 <= (int)timeframe)
            {
                endpoint += "histohour";
                aggregate = (int)timeframe / (int)Timeframe.h1;
            }
            else if ((int)Timeframe.min1 <= (int)timeframe)
            {
                endpoint += "histominute";
                aggregate = (int)timeframe / (int)Timeframe.min1;
            }
            endpoint += $"?fsym={baseSymbol}&tsym={quoteSymbol}";
            if (limit == -1)
            {
                endpoint += $"&allData=true";
            }
            else
            {
                endpoint += $"&limit={limit}";
            }
            if (aggregate != 1)
            {
                endpoint += $"&aggregate={aggregate}";
            }
            if (exchange != "CCCAGG")
            {
                endpoint += $"&e={exchange}";
            }

            //await Limit(RateLimitType.History);
            string response = await Handle(endpoint, RateLimitType.History);

            if (response == null)
            {
                return(null);
            }
            var candlesticks = DeserializeToArray <Candlestick>(JObject.Parse(response)["Data"]);

            // removing placeholder candles
            if (candlesticks[0].close == 0)
            {
                int i = 1;
                while (candlesticks[i].close == 0)
                {
                    i++;
                }
                Candlestick[] output = new Candlestick[candlesticks.Length - i];
                Array.Copy(candlesticks, i, output, 0, candlesticks.Length - i);
                return(output.ToList());
            }
            return(candlesticks.ToList());
        }