public IEnumerable <CandleTrade> GetLastCandlesTradeHistoryDesc(string brokerId, string symbol, int count, CandleType type)
        {
            var now = DateTime.UtcNow;
            var day = new DateTime(now.Year, now.Month, now.Day);

            var to = now.AddYears(-10);

            var index = 0;

            var reader = GetReaderTrade(brokerId, type);

            while (index < count && day > to)
            {
                var data = reader.Get(CandleTradeNoSql.GeneratePartitionKey(symbol, day));

                if (data != null)
                {
                    foreach (var item in data.Select(e => e.Candle).OrderByDescending(e => e.DateTime))
                    {
                        index++;
                        yield return(item);

                        if (index >= count)
                        {
                            break;
                        }
                    }
                }

                day = day.AddDays(-1);
            }
        }
        public IEnumerable <CandleTrade> GetCandlesTradeHistoryDesc(string brokerId, string symbol, DateTime @from, DateTime to, CandleType type)
        {
            var reader = GetReaderTrade(brokerId, type);

            var day = new DateTime(to.Year, to.Month, to.Day);

            var end = from.AddDays(-1);

            while (day >= end)
            {
                var data = reader.Get(CandleTradeNoSql.GeneratePartitionKey(symbol, day),
                                      entity => entity.Candle.DateTime >= from && entity.Candle.DateTime <= to);

                if (data != null)
                {
                    foreach (var item in data.Select(e => e.Candle).OrderByDescending(e => e.DateTime))
                    {
                        yield return(item);
                    }
                }

                day = day.AddDays(-1);
            }
        }
Exemple #3
0
        private async Task DoProcess()
        {
            try
            {
                while (!_token.IsCancellationRequested)
                {
                    try
                    {
                        foreach (var pair in _brokers.ToList())
                        {
                            var brokerId = pair.Key;
                            var symbol   = pair.Value;

                            var writerBidAsk = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleBidAskNoSql>(
                                Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                                CandleBidAskNoSql.TableNameMinute(brokerId), true);

                            await writerBidAsk.CleanAndKeepMaxRecords(CandleBidAskNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 1).Date), 0);

                            await writerBidAsk.CleanAndKeepMaxRecords(CandleBidAskNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 2).Date), 0);

                            await writerBidAsk.CleanAndKeepMaxRecords(CandleBidAskNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 3).Date), 0);

                            await writerBidAsk.CleanAndKeepMaxRecords(CandleBidAskNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 4).Date), 0);

                            await writerBidAsk.CleanAndKeepMaxRecords(CandleBidAskNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 5).Date), 0);

                            await writerBidAsk.CleanAndKeepMaxRecords(CandleBidAskNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 6).Date), 0);

                            await writerBidAsk.CleanAndKeepMaxRecords(CandleBidAskNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 7).Date), 0);

                            var writerTrade = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleTradeNoSql>(
                                Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                                CandleTradeNoSql.TableNameMinute(brokerId), true);

                            await writerTrade.CleanAndKeepMaxRecords(CandleTradeNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 1).Date), 0);

                            await writerTrade.CleanAndKeepMaxRecords(CandleTradeNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 2).Date), 0);

                            await writerTrade.CleanAndKeepMaxRecords(CandleTradeNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 3).Date), 0);

                            await writerTrade.CleanAndKeepMaxRecords(CandleTradeNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 4).Date), 0);

                            await writerTrade.CleanAndKeepMaxRecords(CandleTradeNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 5).Date), 0);

                            await writerTrade.CleanAndKeepMaxRecords(CandleTradeNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 6).Date), 0);

                            await writerTrade.CleanAndKeepMaxRecords(CandleTradeNoSql.GeneratePartitionKey(symbol, DateTime.UtcNow.AddDays(-Program.Settings.DaysToKeepMinutes - 7).Date), 0);

                            _logger.LogInformation("Cleanup minutes for broker: {brokerId} and symbol: {symbol}", brokerId, symbol);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Cannot cleanup minutes");
                    }

                    await Task.Delay(TimeSpan.FromHours(1), _token.Token);
                }
            }
            catch (Exception)
            { }
        }
        private void RegisterNoSqlClient(string brokerId)
        {
            MyNoSqlTcpClient client;

            lock (_gate)
            {
                if (_myNoSqlSubscriberByBroker.TryGetValue(brokerId, out client))
                {
                    return;
                }

                client = new MyNoSqlTcpClient(_getMyNoSqlHostPort, $"{_appName}-{brokerId}");


                _minuteBidAskReaderByBroker[brokerId] = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameMinute(brokerId));
                _hourBidAskReaderByBroker[brokerId]   = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameHour(brokerId));
                _dayBidAskReaderByBroker[brokerId]    = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameDay(brokerId));
                _monthBidAskReaderByBroker[brokerId]  = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameMonth(brokerId));

                _minuteTradeReaderByBroker[brokerId] = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameMinute(brokerId));
                _hourTradeReaderByBroker[brokerId]   = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameHour(brokerId));
                _dayTradeReaderByBroker[brokerId]    = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameDay(brokerId));
                _monthTradeReaderByBroker[brokerId]  = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameMonth(brokerId));

                _myNoSqlSubscriberByBroker[brokerId] = client;
            }

            client.Start();

            //todo: use wait initialization

            var index = 0;

            while (index < 50)
            {
                if (_minuteBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _hourBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _dayBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _monthBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _minuteTradeReaderByBroker[brokerId].Count() > 0 &&
                    _hourTradeReaderByBroker[brokerId].Count() > 0 &&
                    _dayTradeReaderByBroker[brokerId].Count() > 0 &&
                    _monthTradeReaderByBroker[brokerId].Count() > 0)
                {
                    break;
                }

                Thread.Sleep(100);
                index++;
            }
        }
Exemple #5
0
        public IMyNoSqlServerDataWriter <CandleTradeNoSql> GetWriter(string brokerId, CandleType type)
        {
            if (type == CandleType.Minute)
            {
                if (_minuteWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleTradeNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleTradeNoSql.TableNameMinute(brokerId), true);

                _minuteWriterByBroker[brokerId] = writer;

                return(writer);
            }

            if (type == CandleType.Hour)
            {
                if (_hourWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleTradeNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleTradeNoSql.TableNameHour(brokerId), true);

                _hourWriterByBroker[brokerId] = writer;

                return(writer);
            }

            if (type == CandleType.Day)
            {
                if (_dayWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleTradeNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleTradeNoSql.TableNameDay(brokerId), true);

                _dayWriterByBroker[brokerId] = writer;

                return(writer);
            }

            if (type == CandleType.Month)
            {
                if (_monthWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleTradeNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleTradeNoSql.TableNameMonth(brokerId), true);

                _monthWriterByBroker[brokerId] = writer;

                return(writer);
            }

            throw new Exception($"Unknown candle type {type}");
        }