public IQuotesDalGetTimeseriesValuesResult GetTimseriesValues(IQuotesDalGetTimeSeriesValuesParams getQuotesParams)
        {
            IQuotesDalGetTimeseriesValuesResult result = new QuotesDalCSVGetQuotesResult();

            foreach (var t in getQuotesParams.Tickers)
            {
                string fileName = t + "." + getQuotesParams.TimeFrame.ToString() + ".csv";
                string filePath = Path.Combine(_rootFolder, getQuotesParams.Country, fileName);

                if (File.Exists(filePath))
                {
                    using (StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open)))
                    {
                        IQuotesData data = new BaseQuotesData();
                        data.Ticker    = t;
                        data.TimeFrame = getQuotesParams.TimeFrame;
                        data.Country   = getQuotesParams.Country;

                        using (CsvHelper.CsvReader reader = new CsvHelper.CsvReader(sr))
                        {
                            reader.Read();
                            reader.ReadHeader();
                            while (reader.Read())
                            {
                                ITimeSeriesRecord record = new BaseQuotesRecord();
                                record.Time      = reader.GetField <DateTime>("Time");
                                record["Open"]   = reader.GetField <decimal>("Open");
                                record["High"]   = reader.GetField <decimal>("High");
                                record["Low"]    = reader.GetField <decimal>("Low");
                                record["Close"]  = reader.GetField <decimal>("Close");
                                record["Volume"] = reader.GetField <decimal>("Volume");

                                if (record.Time <= getQuotesParams.PeriodEnd && record.Time >= getQuotesParams.PeriodStart)
                                {
                                    data.AddRecord(record);
                                }
                            }
                        }

                        result.Quotes.Add(data);
                    }
                }
                else
                {
                    result.AddError(Interfaces.EErrorCodes.QuotesNotFound, Interfaces.EErrorType.Warning, string.Format("Quotes not found: {0}, {1}", t, getQuotesParams.TimeFrame));
                }
            }

            if (result.Quotes.Count == 0 && getQuotesParams.Tickers.Count != 0)
            {
                result.Success = false;
            }
            else
            {
                result.Success = true;
            }

            return(result);
        }
        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);
        }
Example #3
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);
        }
        public IQuotesDalGetTimeseriesValuesResult GetTimseriesValues(IQuotesDalGetTimeSeriesValuesParams getQuotesParams)
        {
            IQuotesDalGetTimeseriesValuesResult result = new QuotesDalMSSQLGetQuotesResult();

            string        spName = "[SP_Get_Ticker_Timeseries_Values]";
            SqlConnection conn   = OpenConnection("ConnectionStringTimeSeries");


            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = schema + "." + spName;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection  = conn;

            SqlParameter paramTypeId = new SqlParameter("@IN_Period_Type_Id", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Current, (int)getQuotesParams.TimeFrame);

            SqlParameter paramTickerId = new SqlParameter("@IN_Ticker_Id", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Current, 0);

            cmd.Parameters.Add(paramTypeId);
            cmd.Parameters.Add(paramTickerId);

            for (int i = 0; i < getQuotesParams.Tickers.Count; ++i)
            {
                // getting ID of the current ticker
                paramTickerId.Value = GetTickerId(getQuotesParams.Tickers[i], conn);

                DataSet        ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;

                try
                {
                    da.Fill(ds);

                    if (ds.Tables.Count >= 1)
                    {
                        BaseQuotesData qdata = new BaseQuotesData();
                        qdata.Ticker    = getQuotesParams.Tickers[i];
                        qdata.Country   = getQuotesParams.Country;
                        qdata.TimeFrame = getQuotesParams.TimeFrame;

                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            DateTime dtEvent = (DateTime)row[0];
                            if (dtEvent >= getQuotesParams.PeriodStart && dtEvent <= getQuotesParams.PeriodEnd)
                            {
                                int valCount           = row.ItemArray.Count(x => !DBNull.Value.Equals(x));
                                ITimeSeriesRecord qrec = new CustomTimeseriesRecord(new List <string>(new string[valCount - 1]), dtEvent);

                                for (int r = 1; r <= valCount - 1; ++r)
                                {
                                    qrec[r - 1] = (decimal)row[r];
                                }

                                qdata.AddRecord(qrec);
                            }
                        }

                        result.Quotes.Add(qdata);
                    }
                }
                catch (Exception ex)
                {
                    result.Errors.Add(new Interfaces.Error()
                    {
                        Code    = Interfaces.EErrorCodes.QuotesNotFound,
                        Type    = Interfaces.EErrorType.Error,
                        Message = string.Format("'{0}, {1}': failed to read timeseries. SQL message: {2}", getQuotesParams.Tickers[i], getQuotesParams.TimeFrame, ex.Message)
                    });
                }
            }

            if (result.Quotes.Count == 0 && getQuotesParams.Tickers.Count != 0)
            {
                result.Success = false;
            }
            else
            {
                result.Success = true;
            }

            conn.Close();

            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);
        }