public void CreateTest()
        {
            var ticker        = "1234";
            var latestQuarter = FiscalQuarterPeriod.Create(2020, 2);

            // latest
            Assert.AreEqual(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()), TickerPeriodParameterCreator.Create(ticker, "latest", null));

            // day
            Assert.AreEqual(TickerDayParameter.Create(ticker,
                                                      DayPeriod.Create(2020, 1, 1)), TickerPeriodParameterCreator.Create(ticker, "2020-01-01", null));


            // LYLQ
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(0, 0)), TickerPeriodParameterCreator.Create(ticker, "LYLQ", null));

            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(1, 0)), TickerPeriodParameterCreator.Create(ticker, "LY-1LQ", null));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(0, 2)), TickerPeriodParameterCreator.Create(ticker, "LYLQ-2", null));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(1, 2)), TickerPeriodParameterCreator.Create(ticker, "LY-1LQ-2", null));

            // Quarter
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, FiscalQuarterPeriod.Create(2020, 1)), TickerPeriodParameterCreator.Create(ticker, "2020Q1", null));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, "2019", "LQ-1", FiscalQuarterPeriod.Create(2019, 1)), TickerPeriodParameterCreator.Create(ticker, "2019LQ-1", latestQuarter));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, "2019", "LQ-2", FiscalQuarterPeriod.Create(2018, 4)), TickerPeriodParameterCreator.Create(ticker, "2019LQ-2", latestQuarter));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, "LY-2", "4", FiscalQuarterPeriod.Create(2018, 4)), TickerPeriodParameterCreator.Create(ticker, "LY-2Q4", latestQuarter));

            // others
            Assert.ThrowsException <ValidationError>(() => TickerPeriodParameterCreator.Create(ticker, "dummy", null));
        }
        public void GetDailyTest()
        {
            var parameter = TickerDayParameter.Create("6501", DayPeriod.Create(2021, 2, 1));
            var client    = new BuffettCodeApiV3Client(mockApiCore);

            Assert.IsNotNull(client.GetDaily(parameter, false, true, false));
        }
        public void ToApiV3ParametersTest()
        {
            var ticker     = "1234";
            var parameter1 = TickerDayParameter.Create(ticker, DayPeriod.Create(2021, 1, 1)).ToApiV3Parameters();

            Assert.AreEqual("2021-01-01", parameter1["date"]);

            var parameter2 = TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()).ToApiV3Parameters();

            Assert.AreEqual("latest", parameter2["date"]);
        }
        public void CreateTest()
        {
            var ticker = "1234";
            var period = DayPeriod.Create(2021, 1, 1);

            Assert.AreEqual(period, TickerDayParameter.Create(ticker, period).GetPeriod());
            Assert.AreEqual(LatestDayPeriod.GetInstance(), TickerDayParameter.Create(ticker, "latest").GetPeriod());
            Assert.AreEqual(period, TickerDayParameter.Create(ticker, "2021-01-01").GetPeriod());

            // validation error
            Assert.ThrowsException <ValidationError>(() => TickerDayParameter.Create("dummy", period));
        }
Example #5
0
 public static ITickerPeriodParameter Create(string ticker, string periodParam, FiscalQuarterPeriod latestFiscalQuarterPeriod)
 {
     if (periodParam.Equals("latest"))
     {
         return(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()));
     }
     else if (PeriodRegularExpressionConfig.DayRegex.IsMatch(periodParam))
     {
         return(TickerDayParameter.Create(ticker, DayPeriod.Parse(periodParam)));
     }
     else if (PeriodRegularExpressionConfig.FiscalQuarterRegex.IsMatch(periodParam))
     {
         var period = FiscalQuarterPeriod.Parse(periodParam);
         return(TickerQuarterParameter.Create(ticker, period));
     }
     else if (PeriodRegularExpressionConfig.RelativeFiscalQuarterRegex.IsMatch(periodParam))
     {
         var period = RelativeFiscalQuarterPeriod.Parse(periodParam);
         return(TickerQuarterParameter.Create(ticker, period));
     }
     else if (PeriodRegularExpressionConfig.BCodeUdfFiscalQuarterInputRegex.IsMatch(periodParam))
     {
         var match = PeriodRegularExpressionConfig.BCodeUdfFiscalQuarterInputRegex
                     .Match(periodParam);
         var fy = match.Groups["fiscalYear"].Value.Trim();
         var fq = match.Groups["fiscalQuarter"].Value.Trim();
         if (fy.Contains("LY"))
         {
             var prevYears = RelativeFiscalQuarterPeriod.ParseRelativeValue("years", match);
             if (prevYears > latestFiscalQuarterPeriod.Year)
             {
                 throw new ValidationError($"{prevYears} is bigger than {latestFiscalQuarterPeriod.Year}");
             }
             var fiscalQuarter = fq.Replace("Q", "");
             var period        = FiscalQuarterPeriod.Create(latestFiscalQuarterPeriod.Year - prevYears, uint.Parse(fiscalQuarter));
             return(TickerQuarterParameter.Create(ticker, fy, fiscalQuarter, period));
         }
         else if (fq.Contains("LQ"))
         {
             var prevQuarters = RelativeFiscalQuarterPeriod.ParseRelativeValue("quarters", match);
             var period       = FiscalQuarterPeriod.Create(uint.Parse(fy), latestFiscalQuarterPeriod.Quarter).Before(0, prevQuarters);
             return(TickerQuarterParameter.Create(ticker, fy, fq, period));
         }
         else
         {
             throw new ValidationError($"{periodParam} is not supported input format");
         }
     }
     else
     {
         throw new ValidationError($"{periodParam} is not supported input format");
     }
 }
Example #6
0
        public void GetTest()
        {
            var ticker = "2345";
            var client = new BuffettCodeApiV2Client(mockApiCore);
            var day    = TickerDayParameter.Create(ticker, DayPeriod.Create(2021, 2, 1));
            var fyFq   = TickerQuarterParameter.Create(ticker, FiscalQuarterPeriod.Create(2019, 4));
            var empty  = TickerEmptyPeriodParameter.Create(ticker, Snapshot.GetInstance());

            Assert.IsNotNull(client.Get(DataTypeConfig.Indicator, empty, false, true, false));
            Assert.IsNotNull(client.Get(DataTypeConfig.Quarter, fyFq, false, true, false));
            Assert.IsNotNull(client.Get(DataTypeConfig.Company, empty, false, true, false));
            Assert.ThrowsException <NotSupportedDataTypeException>(() => client.Get(DataTypeConfig.Daily, day, false, true, false));
        }
        public void GetHashCodeTest()
        {
            var a = TickerDayParameter.Create("1234", "2021-01-01").GetHashCode();
            var b = TickerDayParameter.Create("1234", DayPeriod.Create(2021, 1, 1)).GetHashCode();
            var c = TickerDayParameter.Create("2345", DayPeriod.Create(2021, 1, 1)).GetHashCode();
            var d = TickerDayParameter.Create("1234", DayPeriod.Create(2021, 1, 2)).GetHashCode();
            var f = TickerDayParameter.Create("1234", LatestDayPeriod.GetInstance()).GetHashCode();
            var g = TickerDayParameter.Create("1234", LatestDayPeriod.GetInstance()).GetHashCode();
            var h = TickerDayParameter.Create("2345", LatestDayPeriod.GetInstance()).GetHashCode();

            Assert.AreEqual(a, b);
            Assert.AreNotEqual(a, c);
            Assert.AreNotEqual(a, d);
            Assert.AreNotEqual(a, f);
            Assert.AreEqual(f, g);
            Assert.AreNotEqual(f, h);
        }
        public void CreateGetDailyRequestTest()
        {
            // use ondemand
            var ticker    = "6501";
            var parameter = TickerDayParameter.Create(ticker, DayPeriod.Create(2021, 1, 1));
            var request   = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(parameter, true);

            Assert.AreEqual(BuffettCodeApiV3Config.ENDPOINT_ONDEMAND_DAILY, request.EndPoint);
            Assert.AreEqual(ticker, request.Parameters["ticker"]);
            Assert.AreEqual("2021-01-01", request.Parameters["date"]);

            // not use ondemand
            request = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(parameter, false);
            Assert.AreEqual(BuffettCodeApiV3Config.ENDPOINT_DAILY, request.EndPoint);
            Assert.AreEqual(ticker, request.Parameters["ticker"]);
            Assert.AreEqual("2021-01-01", request.Parameters["date"]);

            // latest case
            request = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()), false);
            Assert.AreEqual(BuffettCodeApiV3Config.ENDPOINT_DAILY, request.EndPoint);
            Assert.AreEqual(ticker, request.Parameters["ticker"]);
            Assert.AreEqual("latest", request.Parameters["date"]);
        }
        public ITickerPeriodParameter Build()
        {
            switch (dataType)
            {
            case DataTypeConfig.Indicator:
                return(TickerEmptyPeriodParameter.Create(ticker, Snapshot.GetInstance()));

            case DataTypeConfig.Quarter:
                if (string.IsNullOrWhiteSpace(parameter1) && string.IsNullOrWhiteSpace(parameter2))
                {
                    return(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.CreateLatest()));
                }
                else
                {
                    return(TickerQuarterParameter.Create(ticker, FiscalQuarterPeriod.Create(parameter1, parameter2)));
                }

            case DataTypeConfig.Daily:
                return(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()));

            default:
                throw new NotSupportedDataTypeException($"dataType ={dataType} is not supported.");
            }
        }