Пример #1
0
        public static void BuildLines(List <Currency> currencies, List <Indicator> indicators, List <Watcher> watchers, List <DataPoint> lines, int dependencyLevel, int stopAt)
        {
            foreach (var currency in currencies)
            {
                foreach (var indicator in indicators)
                {
                    if (indicator.DependencyLevel == dependencyLevel) // We set the consecutive ones
                    {
                        // Get latest line for this currency indicator pair
                        var line = lines.FirstOrDefault(LineExpression.Line(lines[0].Time, currency.CurrencyId, indicator.IndicatorId).Compile());
                        // Get all watchers for this currency indicator pair
                        var filteredWatchers = watchers.Where(WatcherExpression.WatcherFilter(null, currency.CurrencyId, indicator.IndicatorId).Compile()).ToList();
                        // Build
                        var value       = IndicatorBuilder.BuildValue(currency, indicator, lines);
                        var averageBuy  = IndicatorBuilder.BuildAverageBuy(filteredWatchers);
                        var averageSell = IndicatorBuilder.BuildAverageSell(filteredWatchers);
                        // Set
                        line?.Set(value, averageBuy, averageSell);
                    }
                }
            }

            // Do the same with the next level recursively
            if (dependencyLevel < stopAt)
            {
                BuildLines(currencies, indicators, watchers, lines, dependencyLevel + 1, stopAt);
            }
        }
Пример #2
0
        public static Dictionary <DateTime, Dictionary <string, Dictionary <string, decimal?> > > BuildValues(
            List <DataPoint> lines)
        {
            // Distinct
            var times      = lines.Select(x => x.Time).Distinct().ToList();
            var targets    = lines.Select(x => x.TargetId).Distinct().ToList();
            var indicators = lines.Select(x => x.IndicatorId).Distinct().ToList();

            // Loop
            var level1 = new Dictionary <DateTime, Dictionary <string, Dictionary <string, decimal?> > >();

            foreach (var time in times)
            {
                var level2 = new Dictionary <string, Dictionary <string, decimal?> >();
                foreach (var indicator in indicators)
                {
                    var level3 = new Dictionary <string, decimal?>();
                    foreach (var target in targets)
                    {
                        var line = lines.FirstOrDefault(LineExpression.Line(time, target, indicator).Compile());
                        level3.Add(target, line?.Value);
                    }
                    level2.Add(indicator, level3);
                }
                level1.Add(time, level2);
            }

            // Return
            return(level1);
        }
Пример #3
0
        public async Task RemoveObsoleteLines()
        {
            // Start watch
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Get lines to be removed
            var lines = await _mainDbContext.Lines.Where(LineExpression.ObsoleteLine()).ToListAsync();

            // Remove
            _mainDbContext.Lines.RemoveRange(lines);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Stop watch
            stopwatch.Stop();

            // Log into Splunk
            _logger.LogSplunkInformation("RemoveObsoleteLines", new
            {
                lines.Count,
                ExecutionTime = stopwatch.Elapsed.TotalSeconds
            });
        }
Пример #4
0
        public async Task <List <DataPointResponse> > GetAllLines(string currencyId = null, IndicatorType?indicatorType = null, string indicatorId = null, string userId = null)
        {
            // Get all lines
            var lines = await _lineRepository.GetAll(LineExpression.LineFilter(currencyId, indicatorType, indicatorId, userId));

            // Response
            var response = _mapper.Map <List <DataPointResponse> >(lines);

            // Return
            return(response);
        }
Пример #5
0
        private async Task SetCurrentLinesAsNoLongerCurrent()
        {
            // Get current lines
            var currentLines = await _lineRepository.GetAll(LineExpression.CurrentLine());

            // Set as no longer current
            LineBuilder.SetLinesAsNoLongerCurrent(currentLines);

            // Update
            _lineRepository.UpdateRange(currentLines);
        }
Пример #6
0
        public async Task <List <Responses.Line> > GetAllLines(string currencyId = null, IndicatorType?indicatorType = null, string indicatorId = null, string userId = null)
        {
            // Get all lines
            var lines = await _mainDbContext.Lines.Where(LineExpression.LineFilter(currencyId, indicatorType, indicatorId, userId)).ToListAsync();

            // Response
            var response = _mapper.Map <List <Responses.Line> >(lines);

            // Return
            return(response);
        }
Пример #7
0
        public async Task <ScriptVariables> GetScriptVariables(GetScriptVariables query)
        {
            // Get all lines
            var lines = await _mainDbContext.Lines.Where(LineExpression.Filter(query.Period, query.CurrencyIds, query.UserIds, query.IndicatorIds)).ToListAsync();

            // Response
            var response = ScriptVariablesBuilder.BuildScriptVariables(lines);

            // Return
            return(response);
        }
Пример #8
0
        public async Task <List <Responses.Line> > GetLines(GetLines query)
        {
            // Get all lines
            var lines = await _mainDbContext.Lines.Where(LineExpression.Filter(query.Period, query.CurrencyIds, query.UserIds, query.IndicatorIds)).ToListAsync();

            // Response
            var response = _mapper.Map <List <Responses.Line> >(lines);

            // Return
            return(response);
        }
Пример #9
0
        public async Task Run()
        {
            try
            {
                // Start watch
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                // Get current lines
                var currentLines = await _lineRepository.GetAll(LineExpression.CurrentLine());

                // Build default watchers
                var newDefaultWatchers = WatcherBuilder.BuildDefaultWatchers(currentLines);

                // Get all default watchers
                var defaultWatchers = await _watcherRepository.GetAll(WatcherExpression.DefaultWatcher());

                // Update
                _watcherRepository.UpdateCollection(defaultWatchers, newDefaultWatchers);

                // Save
                await _mainDbContext.SaveChangesAsync();

                // Stop watch
                stopwatch.Stop();

                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    newDefaultWatchers.Count,
                    ExecutionTime = stopwatch.Elapsed.TotalSeconds
                });

                // Return
                await Task.CompletedTask;
            }
            catch (Exception ex)
            {
                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    JobFailed = ex.Message
                });

                // Log error into Splunk
                _logger.LogSplunkError(ex);
            }
        }
Пример #10
0
        public async Task <List <Watcher> > UpdateDefaultWatchers(List <Line> lines)
        {
            // Return if there are no lines
            if (lines.Count == 0)
            {
                return(new List <Watcher>());
            }

            // Start watch
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Get newest time
            var newestTime = lines.Max(x => x.Time);

            // Get current lines
            var currentLines = await _lineRepository.GetAll(LineExpression.CurrentLine(newestTime));

            // Build default watchers
            var newDefaultWatchers = WatcherBuilder.BuildDefaultWatchers(currentLines);

            // Get all default watchers
            var defaultWatchers = await _watcherRepository.GetAll(WatcherExpression.DefaultWatcher());

            // Update
            _watcherRepository.UpdateCollection(defaultWatchers, newDefaultWatchers);

            // Save
            await _dbContext.SaveChangesAsync();

            // Stop watch
            stopwatch.Stop();

            // Log into Splunk
            _logger.LogSplunkInformation("UpdateDefaultWatchers", new
            {
                newDefaultWatchers.Count,
                ExecutionTime = stopwatch.Elapsed.TotalSeconds
            });

            // Return
            return(defaultWatchers);
        }
Пример #11
0
        public async Task Run()
        {
            try
            {
                // Start watch
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                // Get lines to be removed
                var lines = await _lineRepository.GetAll(LineExpression.ObsoleteLine());

                // Remove
                _lineRepository.RemoveRange(lines);

                // Save
                await _mainDbContext.SaveChangesAsync();

                // Stop watch
                stopwatch.Stop();

                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    lines.Count,
                    ExecutionTime = stopwatch.Elapsed.TotalSeconds
                });

                // Return
                await Task.CompletedTask;
            }
            catch (Exception ex)
            {
                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    JobFailed = ex.Message
                });

                // Log error into Splunk
                _logger.LogSplunkError(ex);
            }
        }
Пример #12
0
        public async Task <List <Responses.Chart> > GetCharts(Period period = Period.ONE_MINUTE, List <string> currencyIds = null, List <string> userIds = null, List <string> indicatorIds = null)
        {
            // Get all currencies
            var currencies = await _mainDbContext.Currencies.Where(CurrencyExpression.Filter(currencyIds)).ToListAsync();

            // Get all indicators
            var indicators = await _mainDbContext.Indicators.Where(IndicatorExpression.Filter(indicatorIds)).ToListAsync();

            // Get all lines
            var lines = await _mainDbContext.Lines.Where(LineExpression.Filter(period, currencyIds, userIds, indicatorIds)).ToListAsync();

            // Build charts
            var charts = ChartBuilder.BuildCharts(currencies, indicators, lines);

            // Response
            var response = _mapper.Map <List <Responses.Chart> >(charts);

            // Return
            return(response);
        }
Пример #13
0
        public async Task <List <Responses.LineChart> > GetAllLineCharts(string currencyId = null, IndicatorType?indicatorType = null, string indicatorId = null, string userId = null)
        {
            // Get all currencies
            var currencies = await _currencyRepository.GetAll(CurrencyExpression.CurrencyFilter(currencyId));

            // Get all indicators
            var indicators = await _indicatorRepository.GetAll(IndicatorExpression.IndicatorFilter(indicatorType, indicatorId, userId));

            // Get all lines
            var lines = await _lineRepository.GetAll(LineExpression.LineFilter(currencyId, indicatorType, indicatorId, userId));

            // Build
            var lineCharts = LineChartBuilder.BuildLineCharts(currencies, indicators, lines);

            // Response
            var response = _mapper.Map <List <Responses.LineChart> >(lineCharts);

            // Return
            return(response);
        }
Пример #14
0
        public async Task <List <Responses.Chart> > GetAllCharts(string currencyId = null, IndicatorType?indicatorType = null, string indicatorId = null, string userId = null)
        {
            // Get all currencies
            var currencies = await _mainDbContext.Currencies.Where(CurrencyExpression.CurrencyFilter(currencyId)).ToListAsync();

            // Get all indicators
            var indicators = await _mainDbContext.Indicators.Where(IndicatorExpression.IndicatorFilter(indicatorType, indicatorId, userId)).ToListAsync();

            // Get all lines
            var lines = await _mainDbContext.Lines.Where(LineExpression.LineFilter(currencyId, indicatorType, indicatorId, userId)).ToListAsync();

            // Build
            var charts = ChartBuilder.BuildCharts(currencies, indicators, lines);

            // Response
            var response = _mapper.Map <List <Responses.Chart> >(charts);

            // Return
            return(response);
        }
Пример #15
0
        public async Task RemoveObsoleteLines()
        {
            // Start watch
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Get lines to be removed
            var lines = await _mainDbContext.Lines.Where(LineExpression.ObsoleteLine(_appSettings.LineRetention)).ToListAsync();

            // Remove
            _mainDbContext.Lines.RemoveRange(lines);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Stop watch
            stopwatch.Stop();

            // Log
            _logger.LogInformation("{@Event}, {@Count}, {@ExecutionTime}", "ObsoleteLinesRemoved", lines.Count, stopwatch.Elapsed.TotalSeconds);
        }