Beispiel #1
0
        public void SaveQuotes_Multiple_Success()
        {
            IQuotesSource source = CreateSource();
            IQuotesDal    dal    = PrepareQuotesDal();
            IQuotesDalSaveTimeseriesValuesParams saveParams = dal.CreateSaveTimeseriesValuesParams();

            string[] tickers =
            {
                ConfigurationManager.AppSettings["TickerSPY"],
                ConfigurationManager.AppSettings["TickerQQQ"]
            };

            IQuotesSourceGetQuotesParams getQuotesParams = source.CreateGetQuotesParams();

            foreach (var t in tickers)
            {
                getQuotesParams.Tickers.Add(t);
            }

            getQuotesParams.Country = ConfigurationManager.AppSettings["CountryUS"];

            getQuotesParams.PeriodStart = DateTime.Parse("2009/1/1");
            getQuotesParams.PeriodEnd   = DateTime.Parse("2019/1/1");
            getQuotesParams.TimeFrame   = ETimeFrame.Daily;

            IQuotesSourceGetQuotesResult getQuotesResult = source.GetQuotes(getQuotesParams);

            saveParams.Quotes.AddRange(getQuotesResult.QuotesData);

            IQuotesDalSaveTimeseriesValuesResult saveResult = dal.SaveTimeseriesValues(saveParams);

            Assert.IsTrue(saveResult.Success);
            Assert.IsTrue(!saveResult.HasWarnings, "Unexpected warnings while performing save");
            Assert.IsTrue(!saveResult.HasErrors, "Unexpected errors while performing save");
        }
 public IQuotesDalSaveTimeseriesValuesResult SaveTimeseriesValues(IQuotesDalSaveTimeseriesValuesParams saveQuotesParams)
 {
     throw new NotImplementedException();
 }
        public IQuotesDalSaveTimeseriesValuesResult SaveTimeseriesValues(IQuotesDalSaveTimeseriesValuesParams saveQuotesParams)
        {
            IQuotesDalSaveTimeseriesValuesResult result = new QuotesDalMSSQLSaveQuotesResult();

            SqlConnection conn = OpenConnection("ConnectionStringTimeSeries");

            foreach (var q in saveQuotesParams.Quotes)
            {
                if (q.Quotes.Count > 0)
                {
                    // getting ID of the ticker
                    long tickerId = GetTickerId(q.Ticker, conn);
                    if (tickerId == long.MinValue)
                    {
                        tickerId = AddTicker(q.Ticker, q.Name, q.AgencyCode, q.Notes, conn);
                        if (tickerId != Int32.MinValue && q.Metadata != null && q.Metadata.Values.Count > 0)
                        {
                            SetTickerMetadata(tickerId, q.Metadata, conn);
                        }
                    }

                    int unitId      = (int)q.Unit;
                    int timeFrameId = (int)q.TimeFrame;
                    int typeId      = (int)q.Type;

                    // getting TS info for this ticker
                    long tsId = GetTimeSeriesId(tickerId, timeFrameId, conn);
                    if (tsId == long.MinValue)
                    {
                        // creating new record
                        tsId = CreateTimeseries(tickerId, unitId, typeId, timeFrameId, q.Quotes[0].ValueNames.ToList(), conn);
                    }
                    if (tsId == Int32.MinValue)
                    {
                        result.AddError(
                            Interfaces.EErrorCodes.InvalidSourceParams,
                            Interfaces.EErrorType.Warning,
                            string.Format("Failed to insert {0} - verify input parameters", q.Ticker));
                    }
                    else
                    {
                        // preparing staging table
                        string stageTable = PrepareStageTable(q.Quotes[0].ValueNames.Count, conn);
                        if (!string.IsNullOrEmpty(stageTable))
                        {
                            DataTable dtTimeSeriesValues = ConvertToLoadTimeSeries(q);

                            LoadToStagingTable(q.Quotes[0].ValueNames.Count, stageTable, dtTimeSeriesValues, conn);

                            TriggerStageToCore(stageTable, tsId, conn);

                            result.TimeSeriesSaved.Add(tickerId);
                        }
                        else
                        {
                            result.AddError(
                                Interfaces.EErrorCodes.InvalidSourceParams,
                                Interfaces.EErrorType.Warning,
                                string.Format("Failed to insert {0} - stage table was not created", q.Ticker));
                        }
                    }
                }
            }

            conn.Close();

            result.Success = result.TimeSeriesSaved != null && result.TimeSeriesSaved.Count > 0;

            return(result);
        }
        protected void ImportThread()
        {
            _logger.Log(EErrorType.Info, "ImportThread started");
            if (_compContainer != null)
            {
                // Clearing all errors
                Errors.Clear();

                // validating if there are anything need to be imported
                CurrentState = EImportState.Init;

                var sources = string.IsNullOrEmpty(_impParams.AgencyCode) ? _compContainer.GetExports <IQuotesSource>() : _compContainer.GetExports <IQuotesSource>(_impParams.AgencyCode);

                IQuotesSourceCanImportParams canImportParams = null;

                foreach (var s in sources)
                {
                    // break if stopped
                    if (!_isRunning)
                    {
                        break;
                    }

                    var source = s;

                    if (source != null)
                    {
                        List <string> tickersToImport = new List <string>();

                        //if list of tickers is provided - checking which of them can be imported by the current source
                        if (_impParams.Tickers != null)
                        {
                            // checking which of the given tickers can be imported
                            canImportParams = source.Value.CreateCanImportParams();
                            _impParams.Tickers.ToList().ForEach(x => canImportParams.Tickers.Add(x));

                            IQuotesSourceCanImportResult canImportResult = source.Value.CanImport(canImportParams);
                            if (canImportResult.Success)
                            {
                                tickersToImport.AddRange(canImportResult.Tickers);
                            }
                        }

                        // starting import in two cases: 1) some tickers can be imported by this source OR 2) requested to import all possible tickers by given agency
                        if (tickersToImport.Count > 0 || (_impParams.Tickers == null && !string.IsNullOrEmpty(_impParams.AgencyCode)))
                        {
                            CurrentState = EImportState.ImportSources;

                            IQuotesSourceGetQuotesParams getQuotesParams = source.Value.CreateGetQuotesParams();
                            foreach (var t in tickersToImport)
                            {
                                getQuotesParams.Tickers.Add(t);
                            }
                            try
                            {
                                IQuotesDalSaveTimeseriesValuesParams saveParams = _dal.CreateSaveTimeseriesValuesParams();

                                getQuotesParams.Country = ConfigurationManager.AppSettings["DefaultCountry"];

                                getQuotesParams.PeriodStart = _impParams.DateStart;
                                getQuotesParams.PeriodEnd   = _impParams.DateEnd;
                                getQuotesParams.TimeFrame   = (ETimeFrame)_impParams.TimeFrame;

                                CurrentState = EImportState.ImportSources;

                                IQuotesSourceGetQuotesResult getQuotesResult = source.Value.GetQuotes(getQuotesParams);

                                saveParams.Quotes.AddRange(getQuotesResult.QuotesData);

                                CurrentState = EImportState.Saving;

                                IQuotesDalSaveTimeseriesValuesResult saveResult = _dal.SaveTimeseriesValues(saveParams);

                                if (saveResult.Success)
                                {
                                    foreach (var t in tickersToImport)
                                    {
                                        _tickersProcessed.Add(t);
                                    }

                                    saveResult.TimeSeriesSaved.ToList().ForEach(x => { AddTickerForETL(x); });
                                }

                                _logger.Log(EErrorType.Info, string.Format("Import done"));
                            }
                            catch (Exception ex)
                            {
                                _logger.Log(ex);
                                Errors.Add(new Error()
                                {
                                    Code = EErrorCodes.ImporterError, Type = EErrorType.Error, Message = string.Format("Import failed. Error: {0}", ex.Message)
                                });
                            }
                        }
                    }
                } // foreach
            }
            else
            {
                Errors.Add(new Error()
                {
                    Code = EErrorCodes.ImporterError, Type = EErrorType.Error, Message = "Import failed. Composition comtainer is NULL"
                });
            }

            CurrentState = EImportState.Idle;
            _isRunning   = false;
            _importEnd   = DateTime.UtcNow;

            _logger.Log(EErrorType.Info, string.Format("ImportThread finished. Total errors: {0}, Time: {1}", Errors.Count, _importEnd - _importStart));
        }