public void TestBLS_GetMultiple_PartialSuccess() { IQuotesSource source = CreateSource(); IQuotesSourceGetQuotesParams getQuotesParams = source.CreateGetQuotesParams(); getQuotesParams.Country = ConfigurationManager.AppSettings["BLS_COUNTRY"]; getQuotesParams.Tickers.Add(ConfigurationManager.AppSettings["BLS_TICKER_CPI_NSA"]); getQuotesParams.Tickers.Add(ConfigurationManager.AppSettings["BLS_TICKER_INVALID"]); getQuotesParams.PeriodStart = DateTime.Parse("2019/1/1"); getQuotesParams.PeriodEnd = DateTime.Parse("2019/12/12"); getQuotesParams.TimeFrame = ETimeFrame.Monthly; IQuotesSourceGetQuotesResult getQuotesResult = source.GetQuotes(getQuotesParams); Assert.IsNotNull(getQuotesResult); Assert.IsTrue(getQuotesResult.Success); Assert.False(getQuotesResult.HasErrors, "Unexpected error occured"); Assert.IsNotNull(getQuotesResult.QuotesData, "QuotesData object was not created"); Assert.AreEqual(getQuotesResult.QuotesData.Count(), 1, "Unexpected quotes read"); Assert.IsNotNull(getQuotesResult.QuotesData[0].Quotes, "Quotes array was not created"); Assert.Greater(getQuotesResult.QuotesData[0].Quotes.Count(), 0, "Quotes were not read"); Assert.IsNotNull(getQuotesResult.QuotesData[0].Metadata, "Metadata was not crated"); Assert.Greater(getQuotesResult.QuotesData[0].Metadata.Values.Count, 0, "Metadata values were not read"); }
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 void TestBLS_GetMultiple_Success() { IQuotesSource source = CreateSource(); IQuotesSourceGetQuotesParams getQuotesParams = source.CreateGetQuotesParams(); getQuotesParams.Country = ConfigurationManager.AppSettings["BLS_COUNTRY"]; getQuotesParams.Tickers.Add(ConfigurationManager.AppSettings["BLS_TICKER_CPI_NSA"]); getQuotesParams.Tickers.Add(ConfigurationManager.AppSettings["BLS_TICKER_CPI_MEDICAL"]); getQuotesParams.PeriodStart = DateTime.Parse("2019/1/1"); getQuotesParams.PeriodEnd = DateTime.Parse("2019/12/12"); getQuotesParams.TimeFrame = ETimeFrame.Monthly; IQuotesSourceGetQuotesResult getQuotesResult = source.GetQuotes(getQuotesParams); Assert.IsNotNull(getQuotesResult); Assert.IsTrue(getQuotesResult.Success); Assert.False(getQuotesResult.HasErrors, "Unexpected error occured"); Assert.IsNotNull(getQuotesResult.QuotesData, "QuotesData object was not created"); for (int i = 0; i < getQuotesParams.Tickers.Count; ++i) { Assert.IsNotNull(getQuotesResult.QuotesData[i].Quotes, string.Format("{0} - Quotes array was not created", getQuotesParams.Tickers[i])); Assert.Greater(getQuotesResult.QuotesData[i].Quotes.Count(), 0, string.Format("{0} - Quotes were not read", getQuotesParams.Tickers[i])); Assert.IsNotNull(getQuotesResult.QuotesData[i].Metadata, string.Format("{0} - Metadata was not crated", getQuotesParams.Tickers[i])); Assert.Greater(getQuotesResult.QuotesData[i].Metadata.Values.Count, 0, string.Format("{0} - Metadata values were not read", getQuotesParams.Tickers[i])); } }
public IQuotesSourceGetQuotesResult GetQuotes(IQuotesSourceGetQuotesParams getQuotesParams) { IQuotesSourceGetQuotesResult result = new BEASourceGetQuotesResult(); foreach (var t in getQuotesParams.Tickers) { IQuotesSourceCanImportParams canImportParams = CreateCanImportParams(); canImportParams.Tickers.Add(t); IQuotesSourceCanImportResult canImportRes = CanImport(canImportParams); if (canImportRes.Success) { try { // TODO: result.QuotesData.Add(qd); } catch (Exception ex) { result.Success = false; result.AddError(EErrorCodes.QuotesSourceFail, EErrorType.Error, ex.Message); } } } result.Success = result.QuotesData.Count > 0; if (result.Success && result.QuotesData.Count != getQuotesParams.Tickers.Count) { result.AddError(EErrorCodes.QuotesNotFound, EErrorType.Warning, "Not all quotes were found"); } else if (!result.Success) { result.AddError(EErrorCodes.QuotesNotFound, EErrorType.Error, "Requested tickers are not supported or quotes for them not found"); } return(result); }
public void TestStooq_GetQuotesMonthly_InvalidTicker() { IQuotesSource source = CreateSource(); IQuotesSourceGetQuotesParams getQuotesParams = source.CreateGetQuotesParams(); getQuotesParams.Country = ConfigurationManager.AppSettings["STOOQ_COUNTRY"]; getQuotesParams.Tickers.Add(ConfigurationManager.AppSettings["STOOQ_TICKER_INVALID"]); getQuotesParams.PeriodStart = DateTime.Parse("2009/1/1"); getQuotesParams.PeriodEnd = DateTime.Parse("2019/1/1"); getQuotesParams.TimeFrame = ETimeFrame.Monthly; IQuotesSourceGetQuotesResult getQuotesResult = source.GetQuotes(getQuotesParams); Assert.IsNotNull(getQuotesResult); Assert.IsFalse(getQuotesResult.Success); Assert.True(getQuotesResult.HasErrors, "No errors reported"); Assert.IsNotNull(getQuotesResult.QuotesData, "QuotesData object was not created"); Assert.AreEqual(getQuotesResult.QuotesData.Count(), 0, "Quotes object is not empty"); }
public void TestStooq_GetQuotesMonthly_Success() { IQuotesSource source = CreateSource(); IQuotesSourceGetQuotesParams getQuotesParams = source.CreateGetQuotesParams(); getQuotesParams.Country = ConfigurationManager.AppSettings["STOOQ_COUNTRY"]; getQuotesParams.Tickers.Add(ConfigurationManager.AppSettings["STOOQ_TICKER_SPY"]); getQuotesParams.PeriodStart = DateTime.Parse("2009/1/1"); getQuotesParams.PeriodEnd = DateTime.Parse("2019/1/1"); getQuotesParams.TimeFrame = ETimeFrame.Monthly; IQuotesSourceGetQuotesResult getQuotesResult = source.GetQuotes(getQuotesParams); Assert.IsNotNull(getQuotesResult); Assert.IsTrue(getQuotesResult.Success); Assert.False(getQuotesResult.HasErrors, "Unexpected error occured"); Assert.IsNotNull(getQuotesResult.QuotesData, "QuotesData object was not created"); Assert.IsNotNull(getQuotesResult.QuotesData[0].Quotes, "Quotes array was not created"); Assert.Greater(getQuotesResult.QuotesData[0].Quotes.Count(), 0, "Quotes were not read"); }
public IQuotesSourceGetQuotesResult GetQuotes(IQuotesSourceGetQuotesParams getQuotesParams) { // For COT reports we don't extract selected items only because it will be too expensive // Rather we're loading the whole report with all items there // For this purposes we only identifying the types of items we need to extract based on indicator prefixes IQuotesSourceGetQuotesResult result = new CFTCSourceGetQuotesResult(); CFTCParser cftcParser = new CFTCParser(); // checking which types we need and preparing parameters List <ICFTCParserParams> parserParams = new List <ICFTCParserParams>(); foreach (var pt in s_paramTypes) { ICFTCParserParams cotTypeParams = null; // at least one ticker has a prefix of given type - adding corresponding params object to parse proper report // if no tickers are specified - importing all if ((getQuotesParams.Tickers == null || getQuotesParams.Tickers.Count() == 0) || getQuotesParams.Tickers.Count(x => x.Contains(pt.TickerPrefix)) > 0) { cotTypeParams = pt.Clone(); cotTypeParams.OnlyLast = getQuotesParams.PeriodStart.Year == DateTime.Now.Year ? true : false; parserParams.Add(cotTypeParams); } } // for the list of parameters - calling parser to load proper report foreach (var parserParam in parserParams) { ICFTCParserResult parserResult = cftcParser.Parse(parserParam); foreach (CFTCInstrumentQuotes i in parserResult.Instruments.Values) { IQuotesData qd = new BaseQuotesData(); qd.Country = getQuotesParams.Country; qd.Ticker = i.Ticker; qd.Name = i.Description; qd.Unit = TickerUnit(i.Ticker); qd.Type = TickerType(i.Ticker); qd.TimeFrame = ETimeFrame.Weekly; foreach (var q in i.Quotes) { ITimeSeriesRecord tsr = new CustomTimeseriesRecord(i.Timeseries, q.ReportDate, q.Values); qd.AddRecord(tsr); } qd.AgencyCode = s_agencyCode; result.QuotesData.Add(qd); } } result.Success = result.QuotesData.Count > 0; if (result.Success && result.QuotesData.Count < getQuotesParams.Tickers.Count) { result.AddError(Interfaces.EErrorCodes.QuotesNotFound, Interfaces.EErrorType.Warning, "Not all quotes were found"); } else if (!result.Success) { result.AddError(Interfaces.EErrorCodes.QuotesNotFound, Interfaces.EErrorType.Error, "Requested tickers are not supported or quotes for them not found"); } return(result); }
public IQuotesSourceGetQuotesResult GetQuotes(IQuotesSourceGetQuotesParams getQuotesParams) { List <string> tickers = new List <string>(getQuotesParams.Tickers); // if no tickers were provided - importing al available if (tickers.Count == 0) { tickers.AddRange(_tickers.Keys); } DMFX.BLS.Api.BLSApi blsApi = new DMFX.BLS.Api.BLSApi(); IQuotesSourceGetQuotesResult result = new BLSSourceGetQuotesResult(); foreach (var t in tickers) { IQuotesSourceCanImportParams canImportParams = CreateCanImportParams(); canImportParams.Tickers.Add(t); IQuotesSourceCanImportResult canImportRes = CanImport(canImportParams); if (canImportRes.Success) { try { var response = blsApi.Download(_tickers[t].Bls_Code, getQuotesParams.PeriodStart, getQuotesParams.PeriodEnd); IQuotesData qd = new BaseQuotesData(); qd.Country = getQuotesParams.Country; qd.Ticker = _tickers[t].Ticker_Symbol; qd.Name = _tickers[t].Ticker_Name; qd.TimeFrame = GetTimeFrame(response.Timeframe); qd.AgencyCode = s_agencyCode; qd.Unit = EUnit.Value; qd.Type = ETimeSeriesType.Indicator; // adding value records foreach (var q in response.Quotes) { ITimeSeriesRecord tsr = new CustomTimeseriesRecord(qd.Ticker, q.PeriodEnd, q.Value); qd.AddRecord(tsr); } // adding metadata if (_tickers[t].Metadata != null && _tickers[t].Metadata.Count > 0) { ITimeSeriesMetadata metadata = qd.CreateQuotesMetadata(); metadata.Values = _tickers[t].Metadata; qd.Metadata = metadata; } result.QuotesData.Add(qd); } catch (Exception ex) { result.Success = false; result.AddError(EErrorCodes.QuotesSourceFail, EErrorType.Error, ex.Message); } } } result.Success = result.QuotesData.Count > 0; if (result.Success && result.QuotesData.Count <= getQuotesParams.Tickers.Count) { result.AddError(EErrorCodes.QuotesNotFound, EErrorType.Warning, "Not all quotes were found"); } else if (!result.Success) { result.AddError(EErrorCodes.QuotesNotFound, EErrorType.Error, "Requested tickers are not supported or quotes for them not found"); } return(result); }
public IQuotesSourceGetQuotesResult GetQuotes(IQuotesSourceGetQuotesParams getQuotesParams) { StooqApi api = new StooqApi(); IQuotesSourceGetQuotesResult result = new StooqSourceGetQuotesResult(); foreach (var t in getQuotesParams.Tickers) { IQuotesSourceCanImportParams canImportParams = CreateCanImportParams(); canImportParams.Tickers.Add(t); IQuotesSourceCanImportResult canImportRes = CanImport(canImportParams); if (canImportRes.Success) { try { IQuotesData qd = new BaseQuotesData(); var quotes = api.Download(t, getQuotesParams.PeriodStart, getQuotesParams.PeriodEnd, getQuotesParams.Country, getQuotesParams.TimeFrame == ETimeFrame.Daily ? StooqApi.ETimeFrame.Daily : (getQuotesParams.TimeFrame == ETimeFrame.Weekly ? StooqApi.ETimeFrame.Weekly : StooqApi.ETimeFrame.Monthly)); foreach (var q in quotes.Quotes) { ITimeSeriesRecord newRec = qd.CreateQuotesRecord(); newRec["Close"] = q.Close; newRec["High"] = q.High; newRec["Open"] = q.Open; newRec["Low"] = q.Low; newRec["Volume"] = q.Volume; newRec.Time = ToPeriodStart(q.PeriodEnd, getQuotesParams.TimeFrame); qd.AddRecord(newRec); } qd.Country = getQuotesParams.Country; qd.Ticker = t; qd.Name = this.TickerName(t); qd.TimeFrame = getQuotesParams.TimeFrame; qd.Type = this.TickerType(t); qd.Unit = this.TickerUnit(t); result.QuotesData.Add(qd); } catch (Exception ex) { result.Success = false; result.AddError(Interfaces.EErrorCodes.QuotesSourceFail, Interfaces.EErrorType.Error, ex.Message); } } } result.Success = result.QuotesData.Count > 0; if (result.Success && result.QuotesData.Count != getQuotesParams.Tickers.Count) { result.AddError(Interfaces.EErrorCodes.QuotesNotFound, Interfaces.EErrorType.Warning, "Not all quotes were found"); } else if (!result.Success) { result.AddError(Interfaces.EErrorCodes.QuotesNotFound, Interfaces.EErrorType.Error, "Requested tickers are not supported or quotes for them not found"); } 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)); }