Ejemplo n.º 1
0
    def test_all(self):
        """
        Test all inside a symbol folder (max 2 year, 3 files)
        :return:
        """
        for year_folder in glob(os.path.join(self.test_dir, '*'))[:2]:
            thinkback_files = glob(os.path.join(year_folder, '*.*'))

            for test_file in thinkback_files[:3]:
                date, symbol = os.path.basename(test_file)[:-4].split('-StockAndOptionQuoteFor')

                raw_data = open(test_file).read()

                open_thinkback = OpenThinkBack(date=date, data=raw_data)
                stock, options = open_thinkback.format()

                print 'stock: %s' % stock
                print 'option count: %d' % len(options)
                print '.' * 80

            print '*' * 100
Ejemplo n.º 2
0
    def test_all(self):
        """
        Test all inside a symbol folder (max 2 year, 3 files)
        :return:
        """
        for year_folder in glob(os.path.join(self.test_dir, '*'))[:2]:
            thinkback_files = glob(os.path.join(year_folder, '*.*'))

            for test_file in thinkback_files[:3]:
                date, symbol = os.path.basename(test_file)[:-4].split(
                    '-StockAndOptionQuoteFor')

                raw_data = open(test_file).read()

                open_thinkback = OpenThinkBack(date=date, data=raw_data)
                stock, options = open_thinkback.format()

                print 'stock: %s' % stock
                print 'option count: %d' % len(options)
                print '.' * 80

            print '*' * 100
Ejemplo n.º 3
0
class TestOpenThinkBack(TestSetUp):
    # noinspection PyUnresolvedReferences
    def setUp(self):
        TestSetUp.setUp(self)

        self.symbol = 'WFC'
        self.year = '2015'
        self.date = '2015-01-30'
        self.test_path = list()
        for path in glob(os.path.join(THINKBACK_DIR, '*')):
            if os.path.isdir(path):
                self.test_path.append(path)

        self.test_dir = [path for path in self.test_path if self.symbol.lower() in path].pop()

        # single test file
        self.test_file = r'D:\rivers\data' \
                         r'/csv\%s\%s\%s-StockAndOptionQuoteFor%s.csv' % \
                         (self.symbol.lower(), self.year, self.date, self.symbol.upper())

        self.open_thinkback = OpenThinkBack(
            date=self.date,
            data=open(self.test_file).read()
        )

    def test_get_underlying(self):
        """
        Test get underlying stock price in line 5
        """
        expected_keys = ['date', 'volume', 'open', 'high', 'low', 'last', 'net_change']

        print 'run get_stock...'
        stock = self.open_thinkback.get_stock()

        self.assertEqual(type(stock), dict)

        print 'stock dict:'
        pprint(stock)
        for key, value in stock.items():
            self.assertIn(key, expected_keys)

            if key is not 'date':
                self.assertEqual(type(value), float)
            else:
                self.assertEqual(type(value), str)

    def test_get_cycles(self):
        """
        Test get cycles from each option chain head title
        """
        print 'run get_cycles...'
        cycles = self.open_thinkback.get_cycles()

        self.assertEqual(type(cycles), list)

        print 'cycle dict:'
        pprint(cycles, width=400)

        for cycle in cycles:
            self.assertEqual(type(cycle), dict)

            self.assertEqual(len(cycle['data']), 5)
            self.assertEqual(type(cycle['dte']), int)
            self.assertGreaterEqual(cycle['dte'], 0)
            self.assertEqual(type(cycle['line']), str)
            self.assertGreater(cycle['start'], 10)
            self.assertLess(cycle['start'], cycle['stop'])

    def test_get_cycle_options(self):
        """
        Test get cycle options from option chain
        """
        months = [month_name[i + 1][:3].upper() for i in range(12)]

        contract_keys = [
            'ex_month', 'ex_year', 'right', 'special', 'others',
            'strike', 'contract', 'option_code'
        ]

        option_keys = [
            'date', 'dte',
            'last', 'mark', 'bid', 'ask', 'delta', 'gamma', 'theta', 'vega',
            'theo_price', 'impl_vol', 'prob_itm', 'prob_otm', 'prob_touch', 'volume',
            'open_int', 'intrinsic', 'extrinsic'
        ]

        print 'run get_cycles...'
        cycles = self.open_thinkback.get_cycles()

        for cycle in cycles[:1]:
            print 'cycle: %s' % cycle['data']
            print 'run get_cycle_option...'
            options = self.open_thinkback.get_cycle_options(cycle)

            for contract, option in options:
                print 'current contract, option code: %s' % contract['option_code']
                pprint(contract, width=400)

                self.assertEqual(type(contract), dict)
                self.assertEqual(sorted(contract.keys()), sorted(contract_keys))
                self.assertEqual(type(contract['others']), str)

                self.assertEqual(type(contract['ex_month']), str)
                self.assertIn(contract['ex_month'][:3], months)
                if len(contract['ex_month']) == 4:
                    self.assertGreater(int(contract['ex_month'][3]), 0)
                    self.assertLessEqual(int(contract['ex_month'][3]), 12)

                self.assertEqual(type(contract['ex_year']), int)
                self.assertGreater(contract['ex_year'], 0)
                self.assertLessEqual(contract['ex_year'], 99)

                self.assertEqual(type(contract['right']), str)  # not int, str

                self.assertEqual(type(contract['contract']), str)
                self.assertIn(contract['contract'], ['CALL', 'PUT'])

                self.assertEqual(type(contract['special']), str)
                self.assertIn(contract['special'], ['Weeklys', 'Standard', 'Mini'])

                self.assertEqual(type(contract['strike']), float)
                self.assertGreater(contract['strike'], 0)

                self.assertEqual(type(contract['option_code']), str)

                print 'current option:'
                pprint(option, width=400)

                for key in option.keys():
                    self.assertIn(key, option_keys)
                    if key == 'date':
                        self.assertEqual(type(option['date']), str)
                        self.assertTrue(datetime.strptime(option['date'], '%Y-%m-%d'))
                    elif key == 'dte':
                        self.assertEqual(type(option['dte']), int)
                    else:
                        self.assertEqual(type(option[key]), float)

                print '.' * 80

            print '\n' + '*' * 100 + '\n'

    def test_format(self):
        """
        Test format a raw lines data into dict
        """
        stock, option = self.open_thinkback.format()

        print 'stock: %s' % stock
        print 'option: '
        pprint(option, width=400)

    def test_all(self):
        """
        Test all inside a symbol folder (max 2 year, 3 files)
        :return:
        """
        for year_folder in glob(os.path.join(self.test_dir, '*'))[:2]:
            thinkback_files = glob(os.path.join(year_folder, '*.*'))

            for test_file in thinkback_files[:3]:
                date, symbol = os.path.basename(test_file)[:-4].split('-StockAndOptionQuoteFor')

                raw_data = open(test_file).read()

                open_thinkback = OpenThinkBack(date=date, data=raw_data)
                stock, options = open_thinkback.format()

                print 'stock: %s' % stock
                print 'option count: %d' % len(options)
                print '.' * 80

            print '*' * 100
Ejemplo n.º 4
0
class TestOpenThinkBack(TestSetUp):
    # noinspection PyUnresolvedReferences
    def setUp(self):
        TestSetUp.setUp(self)

        self.symbol = 'WFC'
        self.year = '2015'
        self.date = '2015-01-30'
        self.test_path = list()
        for path in glob(os.path.join(THINKBACK_DIR, '*')):
            if os.path.isdir(path):
                self.test_path.append(path)

        self.test_dir = [
            path for path in self.test_path if self.symbol.lower() in path
        ].pop()

        # single test file
        self.test_file = r'D:\rivers\data' \
                         r'/csv\%s\%s\%s-StockAndOptionQuoteFor%s.csv' % \
                         (self.symbol.lower(), self.year, self.date, self.symbol.upper())

        self.open_thinkback = OpenThinkBack(date=self.date,
                                            data=open(self.test_file).read())

    def test_get_underlying(self):
        """
        Test get underlying stock price in line 5
        """
        expected_keys = [
            'date', 'volume', 'open', 'high', 'low', 'last', 'net_change'
        ]

        print 'run get_stock...'
        stock = self.open_thinkback.get_stock()

        self.assertEqual(type(stock), dict)

        print 'stock dict:'
        pprint(stock)
        for key, value in stock.items():
            self.assertIn(key, expected_keys)

            if key is not 'date':
                self.assertEqual(type(value), float)
            else:
                self.assertEqual(type(value), str)

    def test_get_cycles(self):
        """
        Test get cycles from each option chain head title
        """
        print 'run get_cycles...'
        cycles = self.open_thinkback.get_cycles()

        self.assertEqual(type(cycles), list)

        print 'cycle dict:'
        pprint(cycles, width=400)

        for cycle in cycles:
            self.assertEqual(type(cycle), dict)

            self.assertEqual(len(cycle['data']), 5)
            self.assertEqual(type(cycle['dte']), int)
            self.assertGreaterEqual(cycle['dte'], 0)
            self.assertEqual(type(cycle['line']), str)
            self.assertGreater(cycle['start'], 10)
            self.assertLess(cycle['start'], cycle['stop'])

    def test_get_cycle_options(self):
        """
        Test get cycle options from option chain
        """
        months = [month_name[i + 1][:3].upper() for i in range(12)]

        contract_keys = [
            'ex_month', 'ex_year', 'right', 'special', 'others', 'strike',
            'contract', 'option_code'
        ]

        option_keys = [
            'date', 'dte', 'last', 'mark', 'bid', 'ask', 'delta', 'gamma',
            'theta', 'vega', 'theo_price', 'impl_vol', 'prob_itm', 'prob_otm',
            'prob_touch', 'volume', 'open_int', 'intrinsic', 'extrinsic'
        ]

        print 'run get_cycles...'
        cycles = self.open_thinkback.get_cycles()

        for cycle in cycles[:1]:
            print 'cycle: %s' % cycle['data']
            print 'run get_cycle_option...'
            options = self.open_thinkback.get_cycle_options(cycle)

            for contract, option in options:
                print 'current contract, option code: %s' % contract[
                    'option_code']
                pprint(contract, width=400)

                self.assertEqual(type(contract), dict)
                self.assertEqual(sorted(contract.keys()),
                                 sorted(contract_keys))
                self.assertEqual(type(contract['others']), str)

                self.assertEqual(type(contract['ex_month']), str)
                self.assertIn(contract['ex_month'][:3], months)
                if len(contract['ex_month']) == 4:
                    self.assertGreater(int(contract['ex_month'][3]), 0)
                    self.assertLessEqual(int(contract['ex_month'][3]), 12)

                self.assertEqual(type(contract['ex_year']), int)
                self.assertGreater(contract['ex_year'], 0)
                self.assertLessEqual(contract['ex_year'], 99)

                self.assertEqual(type(contract['right']), str)  # not int, str

                self.assertEqual(type(contract['contract']), str)
                self.assertIn(contract['contract'], ['CALL', 'PUT'])

                self.assertEqual(type(contract['special']), str)
                self.assertIn(contract['special'],
                              ['Weeklys', 'Standard', 'Mini'])

                self.assertEqual(type(contract['strike']), float)
                self.assertGreater(contract['strike'], 0)

                self.assertEqual(type(contract['option_code']), str)

                print 'current option:'
                pprint(option, width=400)

                for key in option.keys():
                    self.assertIn(key, option_keys)
                    if key == 'date':
                        self.assertEqual(type(option['date']), str)
                        self.assertTrue(
                            datetime.strptime(option['date'], '%Y-%m-%d'))
                    elif key == 'dte':
                        self.assertEqual(type(option['dte']), int)
                    else:
                        self.assertEqual(type(option[key]), float)

                print '.' * 80

            print '\n' + '*' * 100 + '\n'

    def test_format(self):
        """
        Test format a raw lines data into dict
        """
        stock, option = self.open_thinkback.format()

        print 'stock: %s' % stock
        print 'option: '
        pprint(option, width=400)

    def test_all(self):
        """
        Test all inside a symbol folder (max 2 year, 3 files)
        :return:
        """
        for year_folder in glob(os.path.join(self.test_dir, '*'))[:2]:
            thinkback_files = glob(os.path.join(year_folder, '*.*'))

            for test_file in thinkback_files[:3]:
                date, symbol = os.path.basename(test_file)[:-4].split(
                    '-StockAndOptionQuoteFor')

                raw_data = open(test_file).read()

                open_thinkback = OpenThinkBack(date=date, data=raw_data)
                stock, options = open_thinkback.format()

                print 'stock: %s' % stock
                print 'option count: %d' % len(options)
                print '.' * 80

            print '*' * 100