Пример #1
0
        def getSymbolDataInstanceForDateDictionaryDataPoints(symbol_key_list_tuple):
            """
            :param list symbol_data_list: This is a list whose elements are tuples of the form (Key, Value) where each
                                            Key is a stock symbol and each Value is a list of comma-delimited strings,
                                            each representing a date's worth of stock data

            :return: tuple (key, value)|None: The returned key is a stock's symbol while the returned value is an object of
                                                type SymbolData. The symbol data object will contain the data which was
                                                contained in the list of comma-delimited strings. However if the list
                                                is empty, a value of None will be returned
            """

            # Index 1 of the symbol_key_list_tuple contains the list of comma-delimited strings
            symbol_data_list = symbol_key_list_tuple[1]
            if len(symbol_data_list) < 1:
                # If the stock had no data points within the time-frame specified by dateIntervalDictionary, return a
                #    None value
                return SymbolData(None)

            # Index 0 of the symbol_key_list_tuple contains the stock symbol
            symbol_code = symbol_key_list_tuple[0]
            # Instantiate the SymbolData object
            symbolDataInstance = SymbolData(symbol_code)

            # Iterate over the strings of stock-date data in the list
            for symbol_date_data in symbol_data_list:

                # We have an eventual data point which will operate on the stock's close price and percentage delta,
                #       so record those values if this symbol_date_data string is specific to today's date
                date = symbol_date_data[StockRdd.DATE_INDEX]
                if date == today_date:
                    symbolDataInstance.setTodayPrice(symbol_date_data[StockRdd.CLOSE_PRICE_INDEX])
                    symbolDataInstance.setTodayDeltaPercentage(symbol_date_data[StockRdd.DELTA_PERCENTAGE_INDEX])

                # Get all of the time spans which the date for this row belongs to
                date_interval_codes_for_date = dateIntervalDictionary.getIntervalCodesByDate(date)
                if not(date_interval_codes_for_date is None):
                    for date_interval_code in date_interval_codes_for_date:
                        # Add a value row for each span that this date belongs to
                        symbolDataInstance.addSpanValueByCode(date_interval_code, date_interval_code, symbol_date_data[StockRdd.CLOSE_PRICE_INDEX],
                                                                  symbol_date_data[StockRdd.OPEN_PRICE_INDEX], symbol_date_data[StockRdd.HIGH_PRICE_INDEX],
                                                                  symbol_date_data[StockRdd.LOW_PRICE_INDEX], symbol_date_data[StockRdd.DELTA_INDEX],
                                                                  symbol_date_data[StockRdd.DELTA_PERCENTAGE_INDEX])

            # Return a tuple comprised of the stock symbol and the SymbolData object
            return (symbol_code, symbolDataInstance)
class TestSymbolData(unittest.TestCase):

    def tearDown(self):
        self.unitDeltaTestsSymbol = None
        self.emptySymbol = None

    def setUp(self):
        self.unit_delta_test_symbol = 'tud'
        self.unitDeltaTestsSymbol = SymbolData(self.unit_delta_test_symbol)
        self.tud_span_code = 'tud_span'
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.tud_span_code)
        self.unit_label_one = 'unit_one'
        self.unit_delta_test_list_one = [0.3, 13.4, 0.8]
        self.unit_label_two = 'unit_two'
        self.unit_delta_test_list_two = [2.8, 13.7, 0.9]
        self.unit_label_three = 'unit_three'
        self.unit_delta_test_list_three = [10.9, 14.4, 15.3]
        self.unit_label_four = 'unit_four'
        self.unit_delta_test_list_four = [15.3, 16.3, 17.3]
        self.unit_label_five = 'unit_five'
        self.unit_delta_test_list_five = [37.3, 16.3, 34.1]
        self.unit_dict = {self.unit_label_one : self.unit_delta_test_list_one,
                          self.unit_label_two : self.unit_delta_test_list_two,
                          self.unit_label_three : self.unit_delta_test_list_three,
                          self.unit_label_four : self.unit_delta_test_list_four,
                          self.unit_label_five : self.unit_delta_test_list_five}
        for label, list_of_prices in self.unit_dict.items():
            for price in list_of_prices:
                self.unitDeltaTestsSymbol.addSpanValueByCode(self.tud_span_code, [label], close_price = price, open_price = price)
        self.max_unit_delta = 4.4
        self.max_unit_delta_label = self.unit_label_three
        self.min_unit_delta = -3.2
        self.min_unit_delta_label = self.unit_label_five
        self.max_unit_delta_percentage = 1.66667
        self.max_unit_delta_percentage_label = self.unit_label_one
        self.min_unit_delta_percentage = -0.678571429
        self.min_unit_delta_percentage_label = self.unit_label_two

        self.tud_span_code_two = 'tud_span_two'
        self.tud_span_two_unit_label_one = 'span_two_unit_one'
        self.tud_span_two_unit_delta_test_list_one = [14.3, 13.4, 115.1]
        self.tud_span_two_unit_label_two = 'span_two_unit_two'
        self.tud_span_two_unit_delta_test_list_two = [32.8, 13.7, 0.9]
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.tud_span_code_two)
        self.unit_dict_two = {self.tud_span_two_unit_label_one : self.tud_span_two_unit_delta_test_list_one,
                              self.tud_span_two_unit_label_two : self.tud_span_two_unit_delta_test_list_two}
        for label, list_of_prices in self.unit_dict_two.items():
            for price in list_of_prices:
                self.unitDeltaTestsSymbol.addSpanValueByCode(self.tud_span_code_two, [label], close_price = price, open_price = price)
        self.max_unit_delta_two = 100.8
        self.max_unit_delta_percentage_two = 7.048951049
        self.max_unit_delta_label_two = self.tud_span_two_unit_label_one
        self.min_unit_delta_two = -31.9
        self.min_unit_delta_percentage_two = -0.972560976
        self.min_unit_delta_label_two = self.tud_span_two_unit_label_two
        self.expected_span_delta_value_to_span_max_delta_ratio = -3.045454545
        self.expected_span_delta_value_to_span_max_delta_percentage_ratio = -0.56224
        self.expected_span_delta_value_to_span_min_delta_ratio = 4.1875
        self.expected_span_delta_value_to_span_min_delta_percentage_ratio = 1.38093

        self.empty_symbol_code = 'empty'
        self.emptySymbol = SymbolData(self.empty_symbol_code)
        self.emptySymbol.initializeSpanByCode(self.tud_span_code)

        self.zero_delta_span_code = 'zero_delta'
        self.zero_delta_span = [1.34, 5.6, -3.5, -0.3, 1.34]
        self.zero_delta_span_off_average_test_value = 1.56
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.zero_delta_span_code)
        for value in self.zero_delta_span:
            self.unitDeltaTestsSymbol.addSpanValueByCode(self.zero_delta_span_code, self.zero_delta_span_code, close_price = value, open_price = value)
        self.expected_percentage_delta_off_zero_delta_span_average = 0.7410714285714288

        self.zero_average_span_code = 'zero_average'
        self.zero_average_span = [1.34, 0.45, -1.65, -1.54, 1.4]
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.zero_average_span_code)
        for value in self.zero_average_span:
            self.unitDeltaTestsSymbol.addSpanValueByCode(self.zero_average_span_code, self.zero_average_span_code, close_price = value, open_price = value)
        self.expected_percentage_delta_off_zero_delta_span_average = 0.7410714285714288

        self.span_unit_deltas = [-1.3, -1.5, .3, 4.4, 5.0, 1.1]
        self.expected_span_unit_min_delta = -1.5
        self.span_unit_delta_percentages = [-.13, -.15, .3, .4, -.04, 2.3]
        self.expected_unit_delta_percentage = -.15
        self.span_code_prefix = 'span_unit_'
        self.spanUnitDeltasSymbol = SymbolData('span_unit_deltas')
        for i in range(0, len(self.span_unit_deltas)):
            unit_delta = self.span_unit_deltas[i]
            unit_delta_percentage = self.span_unit_delta_percentages[i]
            span_code = self.span_code_prefix + str(i)
            self.spanUnitDeltasSymbol.addSpanValueByCode(span_code, [span_code], delta = unit_delta, delta_percentage = unit_delta_percentage)
            oscillating_span_code = self.span_code_prefix + str(i % 2)
            self.spanUnitDeltasSymbol.addSpanValueByCode(oscillating_span_code, [oscillating_span_code], delta = unit_delta, delta_percentage = unit_delta_percentage)

    def test_getMinSpanUnitDelta(self):
        test_min_unit_delta = self.spanUnitDeltasSymbol.getMinSpanUnitDelta()
        self.assertEqual(self.expected_span_unit_min_delta, test_min_unit_delta)
        test_min_unit_delta_percentage = self.spanUnitDeltasSymbol.getMinSpanUnitDelta(get_percentage_delta = True)
        self.assertEqual(test_min_unit_delta_percentage, self.expected_unit_delta_percentage)

    def test_getSpanByCode(self):
        zero_delta_span = self.unitDeltaTestsSymbol.getSpanByCode(self.zero_delta_span_code)
        self.assertEqual(self.zero_delta_span_code, zero_delta_span.code)
        self.assertEqual(zero_delta_span.getUnitsCount(), 5)
        tudSpan = self.unitDeltaTestsSymbol.getSpanByCode(self.tud_span_code)
        self.assertEqual(self.tud_span_code, tudSpan.code)
        self.assertEqual(15, tudSpan.getUnitsCount())
        tudSpanTwo = self.unitDeltaTestsSymbol.getSpanByCode(self.tud_span_code_two)
        self.assertEqual(self.tud_span_code_two, tudSpanTwo.code)
        self.assertEqual(6, tudSpanTwo.getUnitsCount())

    def test_getSpanUnitCount(self):
        self.assertEqual(self.unitDeltaTestsSymbol.getSpanUnitCount(self.zero_delta_span_code), 5)
        self.assertEqual(self.unitDeltaTestsSymbol.getSpanUnitCount(self.zero_average_span_code), 5)
        self.assertEqual(self.unitDeltaTestsSymbol.getSpanUnitCount(self.tud_span_code), 15)
        self.assertEqual(self.unitDeltaTestsSymbol.getSpanUnitCount(self.tud_span_code_two), 6)
        self.assertEqual(self.emptySymbol.getSpanUnitCount(self.tud_span_code), 0)

    def test_getPercentageDeltaOffSpanAverage(self):
        test_percentage_delta_off_zero_delta_span_average = self.unitDeltaTestsSymbol.getPercentageDeltaOffSpanAverage(self.zero_delta_span_code, self.zero_delta_span_off_average_test_value)
        self.assertEqual(test_percentage_delta_off_zero_delta_span_average, self.expected_percentage_delta_off_zero_delta_span_average)
        test_percentage_delta_off_zero_average_span_average = self.unitDeltaTestsSymbol.getPercentageDeltaOffSpanAverage(self.zero_average_span_code, self.zero_delta_span_off_average_test_value)
        self.assertEqual(test_percentage_delta_off_zero_average_span_average, float("inf"))

    def test_getSpanDeltaValueToSpanMaxDeltaRatio(self):
        test_max_ratio = round(self.unitDeltaTestsSymbol.getSpanDeltaValueToSpanMaxDeltaRatio(self.tud_span_code_two, self.tud_span_code), 9)
        self.assertEqual(test_max_ratio, self.expected_span_delta_value_to_span_max_delta_ratio)
        test_max_ratio_percentage = round(self.unitDeltaTestsSymbol.getSpanDeltaValueToSpanMaxDeltaRatio(self.tud_span_code_two, self.tud_span_code, True), 5)
        self.assertEqual(test_max_ratio_percentage, self.expected_span_delta_value_to_span_max_delta_percentage_ratio)
        test_percentage_delta_off_zero_delta_span_average = self.unitDeltaTestsSymbol.getSpanDeltaValueToSpanMaxDeltaRatio(self.tud_span_code_two, self.zero_delta_span_code)
        self.assertEqual(test_percentage_delta_off_zero_delta_span_average, float("inf"))

    def test_getSpanDeltaValueToSpanMinDeltaRatio(self):
        test_min_ratio = round(self.unitDeltaTestsSymbol.getSpanDeltaValueToSpanMinDeltaRatio(self.tud_span_code_two, self.tud_span_code), 4)
        self.assertEqual(test_min_ratio, self.expected_span_delta_value_to_span_min_delta_ratio)
        test_min_ratio_percentage = round(self.unitDeltaTestsSymbol.getSpanDeltaValueToSpanMinDeltaRatio(self.tud_span_code_two, self.tud_span_code, True), 5)
        self.assertEqual(test_min_ratio_percentage, self.expected_span_delta_value_to_span_min_delta_percentage_ratio)
        test_percentage_delta_off_zero_delta_span_average = self.unitDeltaTestsSymbol.getSpanDeltaValueToSpanMinDeltaRatio(self.tud_span_code_two, self.zero_delta_span_code)
        self.assertEqual(test_percentage_delta_off_zero_delta_span_average, float("inf"))

    def test_getMaxDeltaForSpan(self):
        test_max_delta_for_span_tud_one = self.unitDeltaTestsSymbol.getMaxDeltaForSpan(self.tud_span_code)
        test_max_value = test_max_delta_for_span_tud_one['delta']
        test_max_value_label = test_max_delta_for_span_tud_one['label']
        self.assertEqual(test_max_value, self.max_unit_delta)
        self.assertEqual(test_max_value_label, self.max_unit_delta_label)
        test_max_delta_percentage = self.unitDeltaTestsSymbol.getMaxDeltaForSpan(self.tud_span_code, True)
        test_max_delta_percentage_value = round(test_max_delta_percentage['delta'], 5)
        test_max_delta_percentage_label = test_max_delta_percentage['label']
        self.assertEqual(test_max_delta_percentage_value, self.max_unit_delta_percentage)
        self.assertEqual(test_max_delta_percentage_label, self.max_unit_delta_percentage_label)
        test_max_delta_two = self.unitDeltaTestsSymbol.getMaxDeltaForSpan(self.tud_span_code_two)
        test_max_value_two = test_max_delta_two['delta']
        test_max_value_label_two = test_max_delta_two['label']
        self.assertEqual(test_max_value_two, self.max_unit_delta_two)
        self.assertEqual(test_max_value_label_two, self.max_unit_delta_label_two)
        test_max_delta_percentage_two = self.unitDeltaTestsSymbol.getMaxDeltaForSpan(self.tud_span_code_two, True)
        test_max_delta_percentage_value_two = round(test_max_delta_percentage_two['delta'], 9)
        test_max_delta_percentage_label_two = test_max_delta_percentage_two['label']
        self.assertEqual(test_max_delta_percentage_value_two, self.max_unit_delta_percentage_two)
        self.assertEqual(test_max_delta_percentage_label_two, self.max_unit_delta_label_two)
        # Test empty Symbol
        test_empty_value = self.emptySymbol.getMaxDeltaForSpan(self.tud_span_code)
        test_value = test_empty_value['delta']
        test_label = test_empty_value['label']
        self.assertIsNone(test_value)
        self.assertIsNone(test_label)

    def test_getMaxDeltaForSpanValue(self):
        test_max_value_explicit = self.unitDeltaTestsSymbol.getMaxDeltaForSpanValue(self.tud_span_code)
        self.assertEqual(test_max_value_explicit, self.max_unit_delta)
        test_max_delta_percentage_value_explicit = round(self.unitDeltaTestsSymbol.getMaxDeltaForSpanValue(self.tud_span_code, True), 5)
        self.assertEqual(test_max_delta_percentage_value_explicit, self.max_unit_delta_percentage)

    def test_getMinDeltaForSpan(self):
        test_min_delta_for_span_tud_one = self.unitDeltaTestsSymbol.getMinDeltaForSpan(self.tud_span_code)
        test_min_value = round(test_min_delta_for_span_tud_one['delta'], 2)
        test_min_value_label = test_min_delta_for_span_tud_one['label']
        self.assertEqual(test_min_value, self.min_unit_delta)
        self.assertEqual(test_min_value_label, self.min_unit_delta_label)
        test_min_delta_percentage = self.unitDeltaTestsSymbol.getMinDeltaForSpan(self.tud_span_code, True)
        test_min_delta_percentage_value = round(test_min_delta_percentage['delta'], 9)
        test_min_delta_percentage_label = test_min_delta_percentage['label']
        self.assertEqual(test_min_delta_percentage_value, self.min_unit_delta_percentage)
        self.assertEqual(test_min_delta_percentage_label, self.min_unit_delta_percentage_label)
        test_min_delta_two = self.unitDeltaTestsSymbol.getMinDeltaForSpan(self.tud_span_code_two)
        test_min_value_two = round(test_min_delta_two['delta'], 2)
        test_min_value_label_two = test_min_delta_two['label']
        self.assertEqual(test_min_value_two, self.min_unit_delta_two)
        self.assertEqual(test_min_value_label_two, self.min_unit_delta_label_two)
        test_min_delta_percentage_two = self.unitDeltaTestsSymbol.getMinDeltaForSpan(self.tud_span_code_two, True)
        test_min_delta_percentage_value_two = round(test_min_delta_percentage_two['delta'], 9)
        test_min_delta_percentage_label_two = test_min_delta_percentage_two['label']
        self.assertEqual(test_min_delta_percentage_value_two, self.min_unit_delta_percentage_two)
        self.assertEqual(test_min_delta_percentage_label_two, self.min_unit_delta_label_two)
        # Test empty Symbol
        test_empty_value = self.emptySymbol.getMinDeltaForSpan(self.tud_span_code)
        test_value = test_empty_value['delta']
        test_label = test_empty_value['label']
        self.assertIsNone(test_value)
        self.assertIsNone(test_label)

    def test_getMinDeltaForSpanValue(self):
        test_min_value_explicit = round(self.unitDeltaTestsSymbol.getMinDeltaForSpanValue(self.tud_span_code), 2)
        self.assertEqual(test_min_value_explicit, self.min_unit_delta)
        test_min_delta_percentage_explicit = round(self.unitDeltaTestsSymbol.getMinDeltaForSpanValue(self.tud_span_code, True), 9)
        self.assertEqual(test_min_delta_percentage_explicit, self.min_unit_delta_percentage)

    def test_init(self):
        expected_symbol = 'A'
        symbolDataTest = SymbolData(expected_symbol)
        test_symbol = symbolDataTest.symbol
        self.assertEqual(expected_symbol, test_symbol, 'A SymbolData object was initialized but its symbol was returned as {0} while expected to be {1}'.format(test_symbol, expected_symbol))

    def test_getTodayPrice(self):
        expected_today_price = 12.4
        symbolDataTest = SymbolData('A')
        symbolDataTest.setTodayPrice(expected_today_price)
        test_today_price = symbolDataTest.getTodayPrice()
        self.assertEqual(expected_today_price, test_today_price, 'A SymbolData object did not return the expected value when calling getTodayPrice(), expected {0} but returned {1}'.format(expected_today_price, test_today_price))

    def test_getSpanAverageByCode_and_getPercentageDeltaOffSpanAverage(self):
        testSymbolData = SymbolData('A')
        span_to_test = [1.34, 5.6, -3.5, -0.3, 2.2]
        span_expected_average = 1.068
        span_even_expected_average = 0.01333
        span_odd_expected_average = 2.65
        off_average_value_to_test = 1.12
        span_code = 'test_span'
        testSymbolData.initializeSpanByCode(span_code)
        first_expected_none_value = testSymbolData.getSpanAverageByCode(span_code)
        self.assertIsNone(first_expected_none_value, 'A None value was not returned when getting the average of a span with no values')
        second_expected_none_value = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test)
        self.assertIsNone(second_expected_none_value, 'A None value was not returned when getting the percentage delta off average of a span with no values')
        unit_label_prefix = 'units_'
        i = 0
        for value in span_to_test:
            unit_label_suffix = 'even' if ((i % 2) == 0) else 'odd'
            unit_label = unit_label_prefix + unit_label_suffix
            unit_labels_list = [unit_label, i]
            testSymbolData.addSpanValueByCode(span_code, unit_labels_list, value)
            i += 1
        # Test average for all units in span
        span_test_average = testSymbolData.getSpanAverageByCode(span_code)
        self.assertEqual(span_expected_average, span_test_average, 'The computed average for a test span for a test SymbolData object was expected to be {0} but was {1}'.format(span_expected_average, span_test_average))
        expected_delta_off_average = 0.0486891385768
        test_delta_off_average = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test)
        test_delta_off_average = round(test_delta_off_average, 13)
        self.assertEqual(expected_delta_off_average, test_delta_off_average, 'The computed delta percentage off average for a test SymbolData object was expected to be {0} but was {1}'.format(expected_delta_off_average, test_delta_off_average))
        # Test average for odd/even units in span
        even_units_label = unit_label_prefix + 'even'
        test_even_units_average = testSymbolData.getSpanAverageByCode(span_code, even_units_label)
        test_even_units_average = round(test_even_units_average, 5)
        self.assertEqual(test_even_units_average, span_even_expected_average)
        odd_units_label = unit_label_prefix + 'odd'
        test_odd_units_average = testSymbolData.getSpanAverageByCode(span_code, odd_units_label)
        self.assertEqual(test_odd_units_average, span_odd_expected_average)
        # Test off-average for odd/even units in span
        expected_off_even_average_value = 83.0
        expected_off_odd_average_value = -0.57736
        test_delta_off_even_average = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test, even_units_label)
        test_delta_off_even_average = round(test_delta_off_even_average, 1)
        test_delta_off_odd_average = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test, odd_units_label)
        test_delta_off_odd_average = round(test_delta_off_odd_average, 5)
        self.assertEqual(expected_off_even_average_value, test_delta_off_even_average)
        self.assertEqual(expected_off_odd_average_value, test_delta_off_odd_average)



    def test_getSpanDeltaByCode(self):
        testSymbolData = SymbolData('A')
        span_code = 'test_span'
        testSymbolData.initializeSpanByCode(span_code)
        span_to_test = [[1.34, 1.10], [5.6, 1.0], [-3.5, 5.6], [-0.3, -3.5], [2.01, -0.3], [2.34, 2.01]]
        unit_label_prefix = 'units_'
        i = 0
        for unit_data in span_to_test:
            unit_label_suffix = 'even' if ((i % 2) == 0) else 'odd'
            unit_label = unit_label_prefix + unit_label_suffix
            unit_labels_list = [unit_label, i]
            close_price = unit_data[0]
            open_price = unit_data[1]
            testSymbolData.addSpanValueByCode(span_code, unit_labels_list, close_price, open_price)
            i += 1
        expected_span_delta = 1.24
        test_span_delta = testSymbolData.getSpanDeltaByCode(span_code)
        test_span_delta = round(test_span_delta, 2)
        self.assertEqual(expected_span_delta, test_span_delta)
        expected_span_delta_percentage = round(expected_span_delta / 1.10, 6)
        test_span_delta_percentage = round(testSymbolData.getSpanDeltaByCode(span_code, get_percentage = True), 6)
        self.assertEqual(expected_span_delta_percentage, test_span_delta_percentage)
        # Test the even/odd span deltas
        even_units_label = unit_label_prefix + 'even'
        odd_units_label = unit_label_prefix + 'odd'
        expected_even_units_delta = 0.91
        expected_odd_units_delta = 1.34
        test_even_units_delta = testSymbolData.getSpanDeltaByCode(span_code, even_units_label)
        test_even_units_delta = round(test_even_units_delta, 2)
        test_odd_units_delta = testSymbolData.getSpanDeltaByCode(span_code, odd_units_label)
        test_odd_units_delta = round(test_odd_units_delta, 2)
        self.assertEqual(expected_even_units_delta, test_even_units_delta)
        self.assertEqual(expected_odd_units_delta, test_odd_units_delta)
        test_even_units_delta_percentage = round(testSymbolData.getSpanDeltaByCode(span_code, even_units_label, get_percentage = True), 6)
        expected_even_units_delta_percentage = round(expected_even_units_delta / 1.10, 6)
        self.assertEqual(expected_even_units_delta_percentage, test_even_units_delta_percentage)
        test_odd_units_delta_percentage = round(testSymbolData.getSpanDeltaByCode(span_code, odd_units_label, get_percentage = True), 6)
        expected_even_units_delta_percentage = round(expected_odd_units_delta / 1.0, 6)
        self.assertEqual(expected_even_units_delta_percentage, test_odd_units_delta_percentage)
Пример #3
0
#Grab the symbol from the user and return it
def getSymbol():
    return raw_input("Enter ticker: ")


#Grab the type of data to query
def getFunction():
    invalid = True
    function = ""

    while (invalid):
        function = input(
            "Enter quote type (1. daily, 2. intraday, 3. daily adjusted): ")
        if (function < 1 or function > 3):
            print "Invalid type.\n"
        else:
            invalid = False

    return function


symbol = getSymbol()

if len(symbol) > 0:
    function = getFunction()
    request = QuoteRequest(symbol, function, API_KEY)
    response = request.execute()
    data = SymbolData(symbol, response)
    for marketDay in data.dailyData:
        print marketDay.day + "\n"
 def test_getSpanDeltaByCode(self):
     testSymbolData = SymbolData('A')
     span_code = 'test_span'
     testSymbolData.initializeSpanByCode(span_code)
     span_to_test = [[1.34, 1.10], [5.6, 1.0], [-3.5, 5.6], [-0.3, -3.5], [2.01, -0.3], [2.34, 2.01]]
     unit_label_prefix = 'units_'
     i = 0
     for unit_data in span_to_test:
         unit_label_suffix = 'even' if ((i % 2) == 0) else 'odd'
         unit_label = unit_label_prefix + unit_label_suffix
         unit_labels_list = [unit_label, i]
         close_price = unit_data[0]
         open_price = unit_data[1]
         testSymbolData.addSpanValueByCode(span_code, unit_labels_list, close_price, open_price)
         i += 1
     expected_span_delta = 1.24
     test_span_delta = testSymbolData.getSpanDeltaByCode(span_code)
     test_span_delta = round(test_span_delta, 2)
     self.assertEqual(expected_span_delta, test_span_delta)
     expected_span_delta_percentage = round(expected_span_delta / 1.10, 6)
     test_span_delta_percentage = round(testSymbolData.getSpanDeltaByCode(span_code, get_percentage = True), 6)
     self.assertEqual(expected_span_delta_percentage, test_span_delta_percentage)
     # Test the even/odd span deltas
     even_units_label = unit_label_prefix + 'even'
     odd_units_label = unit_label_prefix + 'odd'
     expected_even_units_delta = 0.91
     expected_odd_units_delta = 1.34
     test_even_units_delta = testSymbolData.getSpanDeltaByCode(span_code, even_units_label)
     test_even_units_delta = round(test_even_units_delta, 2)
     test_odd_units_delta = testSymbolData.getSpanDeltaByCode(span_code, odd_units_label)
     test_odd_units_delta = round(test_odd_units_delta, 2)
     self.assertEqual(expected_even_units_delta, test_even_units_delta)
     self.assertEqual(expected_odd_units_delta, test_odd_units_delta)
     test_even_units_delta_percentage = round(testSymbolData.getSpanDeltaByCode(span_code, even_units_label, get_percentage = True), 6)
     expected_even_units_delta_percentage = round(expected_even_units_delta / 1.10, 6)
     self.assertEqual(expected_even_units_delta_percentage, test_even_units_delta_percentage)
     test_odd_units_delta_percentage = round(testSymbolData.getSpanDeltaByCode(span_code, odd_units_label, get_percentage = True), 6)
     expected_even_units_delta_percentage = round(expected_odd_units_delta / 1.0, 6)
     self.assertEqual(expected_even_units_delta_percentage, test_odd_units_delta_percentage)
 def test_getSpanAverageByCode_and_getPercentageDeltaOffSpanAverage(self):
     testSymbolData = SymbolData('A')
     span_to_test = [1.34, 5.6, -3.5, -0.3, 2.2]
     span_expected_average = 1.068
     span_even_expected_average = 0.01333
     span_odd_expected_average = 2.65
     off_average_value_to_test = 1.12
     span_code = 'test_span'
     testSymbolData.initializeSpanByCode(span_code)
     first_expected_none_value = testSymbolData.getSpanAverageByCode(span_code)
     self.assertIsNone(first_expected_none_value, 'A None value was not returned when getting the average of a span with no values')
     second_expected_none_value = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test)
     self.assertIsNone(second_expected_none_value, 'A None value was not returned when getting the percentage delta off average of a span with no values')
     unit_label_prefix = 'units_'
     i = 0
     for value in span_to_test:
         unit_label_suffix = 'even' if ((i % 2) == 0) else 'odd'
         unit_label = unit_label_prefix + unit_label_suffix
         unit_labels_list = [unit_label, i]
         testSymbolData.addSpanValueByCode(span_code, unit_labels_list, value)
         i += 1
     # Test average for all units in span
     span_test_average = testSymbolData.getSpanAverageByCode(span_code)
     self.assertEqual(span_expected_average, span_test_average, 'The computed average for a test span for a test SymbolData object was expected to be {0} but was {1}'.format(span_expected_average, span_test_average))
     expected_delta_off_average = 0.0486891385768
     test_delta_off_average = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test)
     test_delta_off_average = round(test_delta_off_average, 13)
     self.assertEqual(expected_delta_off_average, test_delta_off_average, 'The computed delta percentage off average for a test SymbolData object was expected to be {0} but was {1}'.format(expected_delta_off_average, test_delta_off_average))
     # Test average for odd/even units in span
     even_units_label = unit_label_prefix + 'even'
     test_even_units_average = testSymbolData.getSpanAverageByCode(span_code, even_units_label)
     test_even_units_average = round(test_even_units_average, 5)
     self.assertEqual(test_even_units_average, span_even_expected_average)
     odd_units_label = unit_label_prefix + 'odd'
     test_odd_units_average = testSymbolData.getSpanAverageByCode(span_code, odd_units_label)
     self.assertEqual(test_odd_units_average, span_odd_expected_average)
     # Test off-average for odd/even units in span
     expected_off_even_average_value = 83.0
     expected_off_odd_average_value = -0.57736
     test_delta_off_even_average = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test, even_units_label)
     test_delta_off_even_average = round(test_delta_off_even_average, 1)
     test_delta_off_odd_average = testSymbolData.getPercentageDeltaOffSpanAverage(span_code, off_average_value_to_test, odd_units_label)
     test_delta_off_odd_average = round(test_delta_off_odd_average, 5)
     self.assertEqual(expected_off_even_average_value, test_delta_off_even_average)
     self.assertEqual(expected_off_odd_average_value, test_delta_off_odd_average)
 def test_getTodayPrice(self):
     expected_today_price = 12.4
     symbolDataTest = SymbolData('A')
     symbolDataTest.setTodayPrice(expected_today_price)
     test_today_price = symbolDataTest.getTodayPrice()
     self.assertEqual(expected_today_price, test_today_price, 'A SymbolData object did not return the expected value when calling getTodayPrice(), expected {0} but returned {1}'.format(expected_today_price, test_today_price))
    def setUp(self):
        self.unit_delta_test_symbol = 'tud'
        self.unitDeltaTestsSymbol = SymbolData(self.unit_delta_test_symbol)
        self.tud_span_code = 'tud_span'
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.tud_span_code)
        self.unit_label_one = 'unit_one'
        self.unit_delta_test_list_one = [0.3, 13.4, 0.8]
        self.unit_label_two = 'unit_two'
        self.unit_delta_test_list_two = [2.8, 13.7, 0.9]
        self.unit_label_three = 'unit_three'
        self.unit_delta_test_list_three = [10.9, 14.4, 15.3]
        self.unit_label_four = 'unit_four'
        self.unit_delta_test_list_four = [15.3, 16.3, 17.3]
        self.unit_label_five = 'unit_five'
        self.unit_delta_test_list_five = [37.3, 16.3, 34.1]
        self.unit_dict = {self.unit_label_one : self.unit_delta_test_list_one,
                          self.unit_label_two : self.unit_delta_test_list_two,
                          self.unit_label_three : self.unit_delta_test_list_three,
                          self.unit_label_four : self.unit_delta_test_list_four,
                          self.unit_label_five : self.unit_delta_test_list_five}
        for label, list_of_prices in self.unit_dict.items():
            for price in list_of_prices:
                self.unitDeltaTestsSymbol.addSpanValueByCode(self.tud_span_code, [label], close_price = price, open_price = price)
        self.max_unit_delta = 4.4
        self.max_unit_delta_label = self.unit_label_three
        self.min_unit_delta = -3.2
        self.min_unit_delta_label = self.unit_label_five
        self.max_unit_delta_percentage = 1.66667
        self.max_unit_delta_percentage_label = self.unit_label_one
        self.min_unit_delta_percentage = -0.678571429
        self.min_unit_delta_percentage_label = self.unit_label_two

        self.tud_span_code_two = 'tud_span_two'
        self.tud_span_two_unit_label_one = 'span_two_unit_one'
        self.tud_span_two_unit_delta_test_list_one = [14.3, 13.4, 115.1]
        self.tud_span_two_unit_label_two = 'span_two_unit_two'
        self.tud_span_two_unit_delta_test_list_two = [32.8, 13.7, 0.9]
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.tud_span_code_two)
        self.unit_dict_two = {self.tud_span_two_unit_label_one : self.tud_span_two_unit_delta_test_list_one,
                              self.tud_span_two_unit_label_two : self.tud_span_two_unit_delta_test_list_two}
        for label, list_of_prices in self.unit_dict_two.items():
            for price in list_of_prices:
                self.unitDeltaTestsSymbol.addSpanValueByCode(self.tud_span_code_two, [label], close_price = price, open_price = price)
        self.max_unit_delta_two = 100.8
        self.max_unit_delta_percentage_two = 7.048951049
        self.max_unit_delta_label_two = self.tud_span_two_unit_label_one
        self.min_unit_delta_two = -31.9
        self.min_unit_delta_percentage_two = -0.972560976
        self.min_unit_delta_label_two = self.tud_span_two_unit_label_two
        self.expected_span_delta_value_to_span_max_delta_ratio = -3.045454545
        self.expected_span_delta_value_to_span_max_delta_percentage_ratio = -0.56224
        self.expected_span_delta_value_to_span_min_delta_ratio = 4.1875
        self.expected_span_delta_value_to_span_min_delta_percentage_ratio = 1.38093

        self.empty_symbol_code = 'empty'
        self.emptySymbol = SymbolData(self.empty_symbol_code)
        self.emptySymbol.initializeSpanByCode(self.tud_span_code)

        self.zero_delta_span_code = 'zero_delta'
        self.zero_delta_span = [1.34, 5.6, -3.5, -0.3, 1.34]
        self.zero_delta_span_off_average_test_value = 1.56
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.zero_delta_span_code)
        for value in self.zero_delta_span:
            self.unitDeltaTestsSymbol.addSpanValueByCode(self.zero_delta_span_code, self.zero_delta_span_code, close_price = value, open_price = value)
        self.expected_percentage_delta_off_zero_delta_span_average = 0.7410714285714288

        self.zero_average_span_code = 'zero_average'
        self.zero_average_span = [1.34, 0.45, -1.65, -1.54, 1.4]
        self.unitDeltaTestsSymbol.initializeSpanByCode(self.zero_average_span_code)
        for value in self.zero_average_span:
            self.unitDeltaTestsSymbol.addSpanValueByCode(self.zero_average_span_code, self.zero_average_span_code, close_price = value, open_price = value)
        self.expected_percentage_delta_off_zero_delta_span_average = 0.7410714285714288

        self.span_unit_deltas = [-1.3, -1.5, .3, 4.4, 5.0, 1.1]
        self.expected_span_unit_min_delta = -1.5
        self.span_unit_delta_percentages = [-.13, -.15, .3, .4, -.04, 2.3]
        self.expected_unit_delta_percentage = -.15
        self.span_code_prefix = 'span_unit_'
        self.spanUnitDeltasSymbol = SymbolData('span_unit_deltas')
        for i in range(0, len(self.span_unit_deltas)):
            unit_delta = self.span_unit_deltas[i]
            unit_delta_percentage = self.span_unit_delta_percentages[i]
            span_code = self.span_code_prefix + str(i)
            self.spanUnitDeltasSymbol.addSpanValueByCode(span_code, [span_code], delta = unit_delta, delta_percentage = unit_delta_percentage)
            oscillating_span_code = self.span_code_prefix + str(i % 2)
            self.spanUnitDeltasSymbol.addSpanValueByCode(oscillating_span_code, [oscillating_span_code], delta = unit_delta, delta_percentage = unit_delta_percentage)
Пример #8
0
    def __init__(self, max_length=100):
        """
        Parameters
        ----------
        max_length : int
            An integer representing how much training data to get for each number from the MNIST database
        """

        self.mnist_data = MnistData()

        self.symbol_data = SymbolData()

        zeros : list = []
        ones : list = []
        twos : list = []
        threes : list = []
        fours : list = []
        fives : list = []
        sixes : list = []
        sevens : list = []
        eights : list = []
        nines : list = []

        for x in range(len(self.mnist_data.training_labels)):
            label = self.mnist_data.training_labels[x]

            if label == 0 and len(zeros) < max_length:
                zeros.append(Picture(self.mnist_data.training_images[x]))

            elif label == 1 and len(ones) < max_length:
                ones.append(Picture(self.mnist_data.training_images[x]))

            elif label == 2 and len(twos) < max_length:
                twos.append(Picture(self.mnist_data.training_images[x]))

            elif label == 3 and len(threes) < max_length:
                threes.append(Picture(self.mnist_data.training_images[x]))
                
            elif label == 4 and len(fours) < max_length:
                fours.append(Picture(self.mnist_data.training_images[x]))
                
            elif label == 5 and len(fives) < max_length:
                fives.append(Picture(self.mnist_data.training_images[x]))
                
            elif label == 6 and len(sixes) < max_length:
                sixes.append(Picture(self.mnist_data.training_images[x]))
                
            elif label == 7 and len(sevens) < max_length:
                sevens.append(Picture(self.mnist_data.training_images[x]))
                
            elif label == 8 and len(eights) < max_length:
                eights.append(Picture(self.mnist_data.training_images[x]))
                
            elif label == 9 and len(nines) < max_length:
                nines.append(Picture(self.mnist_data.training_images[x]))
        
        self.training_data['0'] = zeros
        self.training_data['1'] = ones
        self.training_data['2'] = twos
        self.training_data['3'] = threes
        self.training_data['4'] = fours
        self.training_data['5'] = fives
        self.training_data['6'] = sixes
        self.training_data['7'] = sevens
        self.training_data['8'] = eights
        self.training_data['9'] = nines
        self.training_data['+'] = self.symbol_data.plus_data
        self.training_data['-'] = self.symbol_data.minus_data
        self.training_data['*'] = self.symbol_data.multi_data
        self.training_data['/'] = self.symbol_data.divi_data