Ejemplo n.º 1
0
        /// <summary>
        /// Add a new supported granularity to an existing instrument
        /// </summary>
        /// <param name="instrumentName">Name of the instrument to add the granularity to</param>
        /// <param name="granularity">Granularity to add</param>
        /// <returns>The whole instrument with the new granularity added</returns>
        public Contracts.Instrument AddGranularity(Contracts.InstrumentName instrumentName, Contracts.InstrumentGranularityCreation granularity)
        {
            // Check if instrument is in the DB
            var instrumentInDb = GetInstumentFromDbByName(instrumentName);

            if (instrumentInDb == null)
            {
                throw new ProblemDetailsException(HttpStatusCode.NotFound, $"Instrument '{instrumentName}' not found.");
            }

            // Check if granularity is in DB
            var granularityInDb = GetGranularityFromDbByName(instrumentName, granularity.Granularity);

            if (granularityInDb != null)
            {
                // If yes just return the whole instrument object
                return(_mapper.Map <Database.Instrument, Contracts.Instrument>(instrumentInDb));
            }

            // If not then create it in the DB
            var granularityToAdd = _mapper.Map <Contracts.InstrumentGranularityCreation, Database.InstrumentGranularity>(granularity);

            granularityToAdd.State = Database.GranularityState.New; // With the state of new
            instrumentInDb.Granularities.Add(granularityToAdd);
            _dbContext.Update(instrumentInDb);
            _dbContext.SaveChanges();

            // Return the entity saved in the DB
            return(_mapper.Map <Database.Instrument, Contracts.Instrument>(instrumentInDb));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Check if there are any new instruments granularities present in the DB
        /// and if yes then load it's historical data
        /// </summary>
        /// <returns>A async task</returns>
        public async Task CheckInstrumentGranularitiesAndLoadData()
        {
            // Collect all new granularities
            var newGranularities = GetNewGranularities().ToList();

            // Mark them as in process
            foreach (var granularity in newGranularities)
            {
                granularity.State = Database.GranularityState.InDataLoading;
            }
            if (newGranularities.Count() > 0)
            {
                // Commit changes in the DB
                _dbContext.SaveChanges();

                // Get the data loading ones
                var dataLoadingGranularities = GetInDatLoadingGranularities().ToList();

                // Actually start processing them
                foreach (var granularity in dataLoadingGranularities)
                {
                    // Monthly processing
                    var utcBeginningOfTheMonth = new DateTime(START_TIME.Year, START_TIME.Month, 1, 0, 0, 0, DateTimeKind.Utc);
                    var utcEndTime             = DateTime.UtcNow;
                    while (utcBeginningOfTheMonth <= utcEndTime)
                    {
                        var endOfTheMonth = new DateTime(utcBeginningOfTheMonth.Year, utcBeginningOfTheMonth.Month, utcBeginningOfTheMonth.AddMonths(1).AddDays(-1).Day, 0, 0, 0, DateTimeKind.Utc);
                        if (endOfTheMonth > utcEndTime)
                        {
                            endOfTheMonth = utcEndTime;
                        }

                        var candlesOfAMonth = await GetCandlesBetween(granularity.Instrument.Name, granularity.Granularity, utcBeginningOfTheMonth, endOfTheMonth);

                        // Push it to the engine
                        await PostCandlesToTheEngine(granularity.Instrument.Name, granularity.Granularity, candlesOfAMonth);

                        // Push it to the storage account
                        await _instrumentStorageService.StoreInstrumentCandles(new Contracts.InstrumentWithCandles()
                        {
                            InstrumentName = granularity.Instrument.Name,
                            Granularity    = granularity.Granularity,
                            Candles        = candlesOfAMonth
                        });

                        utcBeginningOfTheMonth = utcBeginningOfTheMonth.AddMonths(1);
                    }

                    // Update granularity state to data loaded
                    granularity.State = Database.GranularityState.Tradeable;

                    // Save changes to DB
                    _dbContext.Update(granularity);
                    _dbContext.SaveChanges();
                }
            }
        }