Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public ETimeSeriesType TickerType(string ticker)
        {
            IQuotesSourceCanImportParams canImportParams = CreateCanImportParams();

            canImportParams.Tickers.Add(ticker);

            IQuotesSourceCanImportResult canImportRes = CanImport(canImportParams);

            if (canImportRes.Success)
            {
                return(ETimeSeriesType.Indicator);
            }
            else
            {
                throw new ArgumentException("Unsupported ticker provided");
            }
        }
Exemplo n.º 3
0
        public EUnit TickerUnit(string ticker)
        {
            IQuotesSourceCanImportParams canImportParams = CreateCanImportParams();

            canImportParams.Tickers.Add(ticker);

            IQuotesSourceCanImportResult canImportRes = CanImport(canImportParams);

            if (canImportRes.Success)
            {
                return(EUnit.Value);
            }
            else
            {
                throw new ArgumentException("Unsupported ticker provided");
            }
        }
        public void TestStooq_CanImport_Single_Fail()
        {
            IQuotesSource source = CreateSource();

            IQuotesSourceCanImportParams canImportParams = source.CreateCanImportParams();

            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["STOOQ_TICKER_INVALID"]);


            IQuotesSourceCanImportResult canImportResult = source.CanImport(canImportParams);

            Assert.IsNotNull(canImportResult);
            Assert.IsFalse(canImportResult.Success);
            Assert.False(canImportResult.HasErrors, "Unexpected error occured");
            Assert.IsNotNull(canImportResult.Tickers);
            Assert.AreEqual(canImportResult.Tickers.Count, 0, "Invalid number of tickers returned");
        }
        public void TestStooq_CanImport_Single_Success()
        {
            IQuotesSource source = CreateSource();

            IQuotesSourceCanImportParams canImportParams = source.CreateCanImportParams();

            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["SEC_AAPL_CODE"]);


            IQuotesSourceCanImportResult canImportResult = source.CanImport(canImportParams);

            Assert.IsNotNull(canImportResult);
            Assert.IsTrue(canImportResult.Success);
            Assert.False(canImportResult.HasErrors, "Unexpected error occured");
            Assert.IsNotNull(canImportResult.Tickers);
            Assert.AreEqual(canImportResult.Tickers.Count, 1, "Invalid number of tickers returned");
            Assert.AreEqual(canImportResult.Tickers[0], ConfigurationManager.AppSettings["SEC_AAPL_CODE"], "Expected tickers were not returned");
        }
        public void TestStooq_CanImport_Multiple_PartialSuccess()
        {
            IQuotesSource source = CreateSource();

            IQuotesSourceCanImportParams canImportParams = source.CreateCanImportParams();

            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["SEC_AAPL_CODE"]);
            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["STOOQ_TICKER_INVALID"]);


            IQuotesSourceCanImportResult canImportResult = source.CanImport(canImportParams);

            Assert.IsNotNull(canImportResult);
            Assert.IsTrue(canImportResult.Success);
            Assert.False(canImportResult.HasErrors, "Unexpected error occured");
            Assert.IsNotNull(canImportResult.Tickers);
            Assert.AreEqual(canImportResult.Tickers.Count, 1, "Invalid number of tickers returned");
            Assert.Contains(ConfigurationManager.AppSettings["SEC_AAPL_CODE"], canImportResult.Tickers.ToArray(), "Expected ticker AAPL was not returned");
        }
Exemplo n.º 7
0
        public void TestCFTC_CanImport_Multiple_Success()
        {
            IQuotesSource source = CreateSource();

            IQuotesSourceCanImportParams canImportParams = source.CreateCanImportParams();

            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["CFTC_TICKER_COT_FINFUT"]);
            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["CFTC_TICKER_COT_FINFUTOPT"]);
            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["CFTC_TICKER_COT_CMDTSFUT"]);
            canImportParams.Tickers.Add(ConfigurationManager.AppSettings["CFTC_TICKER_COT_CMDTSFUTOPT"]);


            IQuotesSourceCanImportResult canImportResult = source.CanImport(canImportParams);

            Assert.IsNotNull(canImportResult);
            Assert.IsTrue(canImportResult.Success);
            Assert.False(canImportResult.HasErrors, "Unexpected error occured");
            Assert.IsNotNull(canImportResult.Tickers);
            Assert.AreEqual(canImportResult.Tickers.Count, 4, "Invalid number of tickers returned");
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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));
        }