Пример #1
0
        /// <summary>
        /// Python implementation of GetFundamental, get fundamental data for input symbols or tickers
        /// </summary>
        /// <param name="input">The symbols or tickers to retrieve fundamental data for</param>
        /// <param name="selector">Selects a value from the Fundamental data to filter the request output</param>
        /// <param name="start">The start date of selected data</param>
        /// <param name="end">The end date of selected data</param>
        /// <returns>pandas DataFrame</returns>
        public PyObject GetFundamental(PyObject input, string selector, DateTime?start = null, DateTime?end = null)
        {
            //Null selector is not allowed for Python DataFrame
            if (string.IsNullOrWhiteSpace(selector))
            {
                throw new ArgumentException("Invalid selector. Cannot be None, empty or consist only of white-space characters");
            }

            //Covert to symbols
            var symbols = PythonUtil.ConvertToSymbols(input);

            //Fetch the data
            var fundamentalData = GetAllFundamental(symbols, selector, start, end);

            using (Py.GIL())
            {
                var data = new PyDict();
                foreach (var day in fundamentalData.OrderBy(x => x.Key))
                {
                    var orderedValues = day.Value.OrderBy(x => x.Key.ID.ToString()).ToList();
                    var columns       = orderedValues.Select(x => x.Key.ID.ToString());
                    var values        = orderedValues.Select(x => x.Value);
                    var row           = _pandas.Series(values, columns);
                    data.SetItem(day.Key.ToPython(), row);
                }

                return(_pandas.DataFrame.from_dict(data, orient: "index"));
            }
        }
Пример #2
0
        /// <summary>
        /// Python implementation of GetFundamental, get fundamental data for input symbols or tickers
        /// </summary>
        /// <param name="input">The symbols or tickers to retrieve fundamental data for</param>
        /// <param name="selector">Selects a value from the Fundamental data to filter the request output</param>
        /// <param name="start">The start date of selected data</param>
        /// <param name="end">The end date of selected data</param>
        /// <returns>pandas DataFrame</returns>
        public PyObject GetFundamental(PyObject input, string selector = null, DateTime?start = null, DateTime?end = null)
        {
            //Covert to symbols
            var symbols = PythonUtil.ConvertToSymbols(input);

            //Fetch the data
            var fundamentalData = GetAllFundamental(symbols, selector, start, end);

            using (Py.GIL())
            {
                var data = new PyDict();
                foreach (var day in fundamentalData.OrderBy(x => x.Key))
                {
                    var orderedValues = day.Value.OrderBy(x => x.Key.ID.ToString()).ToList();
                    var columns       = orderedValues.Select(x => x.Key.ID.ToString());
                    var values        = orderedValues.Select(x => x.Value);
                    var row           = _pandas.Series(values, columns);
                    data.SetItem(day.Key.ToPython(), row);
                }

                return(_pandas.DataFrame.from_dict(data, orient: "index"));
            }
        }
Пример #3
0
        public void ConvertToSymbolsTest()
        {
            var expected = new List <Symbol>
            {
                Symbol.Create("AIG", SecurityType.Equity, Market.USA),
                Symbol.Create("BAC", SecurityType.Equity, Market.USA),
                Symbol.Create("IBM", SecurityType.Equity, Market.USA),
                Symbol.Create("GOOG", SecurityType.Equity, Market.USA)
            };

            using (Py.GIL())
            {
                // Test Python String
                var test1 = PythonUtil.ConvertToSymbols(new PyString("AIG"));
                Assert.IsTrue(typeof(List <Symbol>) == test1.GetType());
                Assert.AreEqual(expected.FirstOrDefault(), test1.FirstOrDefault());

                // Test Python List of Strings
                var list  = (new List <string> {
                    "AIG", "BAC", "IBM", "GOOG"
                }).ToPyList();
                var test2 = PythonUtil.ConvertToSymbols(list);
                Assert.IsTrue(typeof(List <Symbol>) == test2.GetType());
                Assert.IsTrue(test2.SequenceEqual(expected));

                // Test Python Symbol
                var test3 = PythonUtil.ConvertToSymbols(expected.FirstOrDefault().ToPython());
                Assert.IsTrue(typeof(List <Symbol>) == test3.GetType());
                Assert.AreEqual(expected.FirstOrDefault(), test3.FirstOrDefault());

                // Test Python List of Symbols
                var test4 = PythonUtil.ConvertToSymbols(expected.ToPyList());
                Assert.IsTrue(typeof(List <Symbol>) == test4.GetType());
                Assert.IsTrue(test4.SequenceEqual(expected));
            }
        }