Пример #1
0
        private List <Candle> GetCandles(int oldInterval, string securityName, DateTime startTime, DateTime endTime)
        {
            if (oldInterval < _supportedIntervals.First().Key)
            {
                return(null);
            }

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

            var needIntervalForQuery =
                CandlesCreator.DetermineAppropriateIntervalForRequest(oldInterval, _supportedIntervals,
                                                                      out var needInterval);

            var step = new TimeSpan(0, 0, (int)(needInterval * CandlesDownloadLimit));

            var actualTime = startTime;

            var midTime = actualTime + step;

            while (true)
            {
                if (actualTime >= endTime)
                {
                    break;
                }

                if (midTime > endTime)
                {
                    midTime = endTime;
                }

                var histaricalPricesResponse = _ftxRestApi.GetHistoricalPricesAsync(securityName, needInterval, CandlesDownloadLimit, actualTime, midTime).Result;

                List <Candle> newCandles = _candlesCreator.Create(histaricalPricesResponse.SelectToken("result"));

                if (newCandles != null && newCandles.Count != 0)
                {
                    candles.AddRange(newCandles);

                    actualTime = candles[candles.Count - 1].TimeStart.AddSeconds(needInterval);
                    midTime    = actualTime + step;
                }
                Thread.Sleep(1000);
            }

            if (candles.Count == 0)
            {
                return(null);
            }

            if (oldInterval == needInterval)
            {
                return(candles);
            }

            var requiredIntervalCandles =
                CandlesCreator.CreateCandlesRequiredInterval(needInterval, oldInterval, candles);

            return(requiredIntervalCandles);
        }
Пример #2
0
        private List <Candle> GetCandles(int old_interval, string security, DateTime start_time, DateTime end_time)
        {
            lock (locker_candles)
            {
                List <Candle> tmp_candles = new List <Candle>();
                DateTime      end_over    = end_time;

                string need_interval_for_query = CandlesCreator.DetermineAppropriateIntervalForRequest(old_interval, supported_intervals, out var need_interval);

                while (true)
                {
                    var from = TimeManager.GetTimeStampSecondsToDateTime(start_time);

                    if (end_over <= start_time)
                    {
                        break;
                    }

                    List <Candle> new_candles = BybitCandlesCreator.GetCandleCollection(client, security, need_interval_for_query, from);

                    if (new_candles != null && new_candles.Count != 0)
                    {
                        tmp_candles.AddRange(new_candles);
                    }
                    else
                    {
                        break;
                    }

                    start_time = tmp_candles[tmp_candles.Count - 1].TimeStart.AddMinutes(old_interval);

                    Thread.Sleep(20);
                }

                for (int i = tmp_candles.Count - 1; i > 0; i--)
                {
                    if (tmp_candles[i].TimeStart > end_time)
                    {
                        tmp_candles.Remove(tmp_candles[i]);
                    }
                }

                if (old_interval == need_interval)
                {
                    return(tmp_candles);
                }

                List <Candle> result_candles = CandlesCreator.CreateCandlesRequiredInterval(need_interval, old_interval, tmp_candles);

                return(result_candles);
            }
        }
Пример #3
0
        private List <Candle> GetCandles(int oldInterval, string security, DateTime startTime, DateTime endTime)
        {
            lock (_locker)
            {
                var needIntervalForQuery =
                    CandlesCreator.DetermineAppropriateIntervalForRequest(oldInterval, _supportedIntervals,
                                                                          out var needInterval);

                var clientId = "";

                string topic = $"market.{security}.kline.{needIntervalForQuery}";

                var from = TimeManager.GetTimeStampSecondsToDateTime(startTime);
                var to   = TimeManager.GetTimeStampSecondsToDateTime(endTime);

                _marketDataSource.SendMessage(
                    $"{{ \"req\": \"{topic}\",\"id\": \"{clientId}\", \"from\":{from}, \"to\":{to} }}");

                var startLoadingTime = DateTime.Now;

                while (startLoadingTime.AddSeconds(40) > DateTime.Now)
                {
                    var candles = _allCandleSeries.Find(s =>
                                                        s.security == security && s.GetTimeFrame() == needIntervalForQuery);

                    if (candles != null)
                    {
                        _allCandleSeries.Remove(candles);

                        var oldCandles = CreateCandlesFromJson(candles);

                        if (oldInterval == needInterval)
                        {
                            return(oldCandles);
                        }

                        var newCandles =
                            CandlesCreator.CreateCandlesRequiredInterval(needInterval, oldInterval, oldCandles);

                        return(newCandles);
                    }

                    Thread.Sleep(500);
                }

                SendLogMessage(OsLocalization.Market.Message95 + security, LogMessageType.Error);

                return(null);
            }
        }
Пример #4
0
        private List <Candle> GetCandles(int oldInterval, string security, DateTime startTime, DateTime endTime)
        {
            lock (_locker)
            {
                try
                {
                    var needIntervalForQuery = CandlesCreator.DetermineAppropriateIntervalForRequest(oldInterval, _supportedIntervals, out var needInterval);

                    var from = TimeManager.GetTimeStampSecondsToDateTime(startTime);
                    var to   = TimeManager.GetTimeStampSecondsToDateTime(endTime);

                    string timeStamp = TimeManager.GetUnixTimeStampSeconds().ToString();
                    var    headers   = new Dictionary <string, string>();
                    headers.Add("Timestamp", timeStamp);

                    RestRequestBuilder requestBuilder = new RestRequestBuilder();
                    requestBuilder.AddParam("contract", security);
                    requestBuilder.AddParam("from", from.ToString());
                    requestBuilder.AddParam("to", to.ToString());
                    requestBuilder.AddParam("interval", needIntervalForQuery);

                    PublicUrlBuilder urlBuilder = new PublicUrlBuilder(_host, _path, _wallet);

                    var candlesJson = _requestREST.SendGetQuery("GET", "", urlBuilder.Build("/candlesticks", requestBuilder), headers);

                    var candlesOut = JsonConvert.DeserializeObject <GfCandle[]>(candlesJson);

                    var oldCandles = CreateCandlesFromJson(candlesOut);

                    if (oldInterval == needInterval)
                    {
                        return(oldCandles);
                    }

                    var newCandles = CandlesCreator.CreateCandlesRequiredInterval(needInterval, oldInterval, oldCandles);

                    return(newCandles);
                }
                catch
                {
                    SendLogMessage(OsLocalization.Market.Message95 + security, LogMessageType.Error);

                    return(null);
                }
            }
        }
Пример #5
0
        public List <Candle> GetCandleHistory(string security, TimeSpan interval)
        {
            int oldInterval = Convert.ToInt32(interval.TotalMinutes);

            var needIntervalForQuery =
                CandlesCreator.DetermineAppropriateIntervalForRequest(oldInterval, _supportedIntervals, out var needInterval);

            var jsonCandles = RequestCandlesFromExchange(needIntervalForQuery, security);

            var oldCandles = CreateCandlesFromJson(jsonCandles);

            if (oldInterval == needInterval)
            {
                return(oldCandles);
            }

            var newCandles = CandlesCreator.CreateCandlesRequiredInterval(needInterval, oldInterval, oldCandles);

            return(newCandles);
        }