Ejemplo n.º 1
0
    def test_download_ibm(self):
        symbols = ['IBM']
        target.download_stocks(symbols)
        self.assertTrue(os.path.exists(self.default_result_file))

        # make sure all lines start with 'IBM,'
        bIBM_column = True
        with open(self.default_result_file, 'r') as f_result:
            last_line = None
            for line in f_result:
                last_line = line
                first_column = last_line.split(',', 1)[0].strip()
                bIBM_column = (first_column == symbols[0])

                if not bIBM_column:
                    break
            self.assertTrue(bIBM_column, msg=last_line)

            # make sure there are 8 columns
            columns = last_line.split(',')
            self.assertTrue(len(columns) == 8,
                            msg='columns must be 8 but {}'.format(
                                len(columns)))

            # make sure the first date is correct
            expected = '1962-01-02'
            self.assertEqual(expected,
                             columns[1].strip(),
                             msg='the first date must be {} but {}'.format(
                                 expected, columns[1].strip()))

        os.remove(self.default_result_file)
Ejemplo n.º 2
0
    def test_download_ibm(self):
        symbols = ["IBM"]
        target.download_stocks(symbols)
        self.assertTrue(os.path.exists(self.default_result_file))

        # make sure all lines start with 'IBM,'
        bIBM_column = True
        with open(self.default_result_file, "r") as f_result:
            last_line = None
            for line in f_result:
                last_line = line
                first_column = last_line.split(",", 1)[0].strip()
                bIBM_column = first_column == symbols[0]

                if not bIBM_column:
                    break
            self.assertTrue(bIBM_column, msg=last_line)

            # make sure there are 8 columns
            columns = last_line.split(",")
            self.assertTrue(len(columns) == 8, msg="columns must be 8 but {}".format(len(columns)))

            # make sure the first date is correct
            expected = "1962-01-02"
            self.assertEqual(
                expected,
                columns[1].strip(),
                msg="the first date must be {} but {}".format(expected, columns[1].strip()),
            )

        os.remove(self.default_result_file)
Ejemplo n.º 3
0
    def test_download_append(self):
        '''
        Make sure downloaded data is appended to the existing one
        '''
        # make a target file
        target_fn = 'deleteme.txt'
        base_file = os.path.join('data', 'filewrap.txt')
        target_file = os.path.join('data', target_fn)
        shutil.copyfile(base_file, target_file)

        symbol = ['YHOO']
        from_date = '2014-01-06'
        to_date = '2014-02-04'
        try:
            target.download_stocks(symbol, from_date, to_date, target_file,
                                   True)

            with open(target_file, 'r') as fh_target:
                # make sure the original part is intace
                with open(base_file, 'r') as fh_org:
                    for line_org in fh_org:
                        line = fh_target.readline()
                        self.assertEqual(
                            line_org, line,
                            'original={}, target={}'.format(line_org, line))

                # make sure the data is appended
                # the first line is a header
                line = fh_target.readline()

                # this should be the actual stock data
                line = fh_target.readline()
                columns = line.split(',')
                self.assertEqual(
                    symbol[0], columns[0].strip(),
                    'Symbol should be {} but is {}'.format(
                        symbol[0], columns[0]))

                # make sure to_date is correct
                self.assertEqual(
                    to_date, columns[1].strip(),
                    'to_date should be {} but is {}'.format(
                        to_date, columns[1]))

                # make sure from_date is correct
                for line in fh_target:
                    pass  # read it to last
                columns = line.split(',')
                self.assertEqual(
                    from_date, columns[1].strip(),
                    'from_date should be {} but is {}'.format(
                        from_date, columns[1]))
        finally:
            os.remove(target_file)
Ejemplo n.º 4
0
    def test_download_two(self):
        symbols = ['IBM', 'YHOO']
        from_date = '2014-01-06'
        to_date = '2014-02-04'
        file_name = 'IY.csv'
        target.download_stocks(symbols, from_date, to_date, file_name)
        self.assertTrue(os.path.exists(file_name))

        bIBM = False
        bYHOO = False
        with open(file_name, 'r') as f_result:
            first_line = None
            last_line = None
            lcnt = 0
            for line in f_result:
                # the first line is a label line. The second line is
                # the first data
                if lcnt == 1:
                    first_line = line
                lcnt += 1
                last_line = line

                # make sure first column is either IBM or YHOO
                first_column = last_line.split(',', 1)[0].strip()
                self.assertTrue(first_column in symbols,
                                msg='First column is' + first_column)
                if first_column == 'IBM':
                    bIBM = True
                else:
                    bYHOO = True

            # make sure there were both IBM and YHOO
            self.assertTrue(bIBM and bYHOO,
                            msg='IBM={}, YHOO={}'.format(bIBM, bYHOO))

            #make sure from_date is correct
            columns = last_line.split(',')
            self.assertEqual(from_date,
                             columns[1].strip(),
                             msg='the from_date must be {} but {}'.format(
                                 from_date, columns[1].strip()))

            #make sure to_date is correct
            columns = first_line.split(',')
            self.assertEqual(to_date,
                             columns[1].strip(),
                             msg='the to_date must be {} but {}'.format(
                                 to_date, columns[1].strip()))

        os.remove(file_name)
Ejemplo n.º 5
0
    def test_download_two(self):
        symbols = ["IBM", "YHOO"]
        from_date = "2014-01-06"
        to_date = "2014-02-04"
        file_name = "IY.csv"
        target.download_stocks(symbols, from_date, to_date, file_name)
        self.assertTrue(os.path.exists(file_name))

        bIBM = False
        bYHOO = False
        with open(file_name, "r") as f_result:
            first_line = None
            last_line = None
            lcnt = 0
            for line in f_result:
                # the first line is a label line. The second line is
                # the first data
                if lcnt == 1:
                    first_line = line
                lcnt += 1
                last_line = line

                # make sure first column is either IBM or YHOO
                first_column = last_line.split(",", 1)[0].strip()
                self.assertTrue(first_column in symbols, msg="First column is" + first_column)
                if first_column == "IBM":
                    bIBM = True
                else:
                    bYHOO = True

            # make sure there were both IBM and YHOO
            self.assertTrue(bIBM and bYHOO, msg="IBM={}, YHOO={}".format(bIBM, bYHOO))

            # make sure from_date is correct
            columns = last_line.split(",")
            self.assertEqual(
                from_date,
                columns[1].strip(),
                msg="the from_date must be {} but {}".format(from_date, columns[1].strip()),
            )

            # make sure to_date is correct
            columns = first_line.split(",")
            self.assertEqual(
                to_date, columns[1].strip(), msg="the to_date must be {} but {}".format(to_date, columns[1].strip())
            )

        os.remove(file_name)
Ejemplo n.º 6
0
    def test_download_append(self):
        """
        Make sure downloaded data is appended to the existing one
        """
        # make a target file
        target_fn = "deleteme.txt"
        base_file = os.path.join("data", "filewrap.txt")
        target_file = os.path.join("data", target_fn)
        shutil.copyfile(base_file, target_file)

        symbol = ["YHOO"]
        from_date = "2014-01-06"
        to_date = "2014-02-04"
        try:
            target.download_stocks(symbol, from_date, to_date, target_file, True)

            with open(target_file, "r") as fh_target:
                # make sure the original part is intace
                with open(base_file, "r") as fh_org:
                    for line_org in fh_org:
                        line = fh_target.readline()
                        self.assertEqual(line_org, line, "original={}, target={}".format(line_org, line))

                # make sure the data is appended
                # the first line is a header
                line = fh_target.readline()

                # this should be the actual stock data
                line = fh_target.readline()
                columns = line.split(",")
                self.assertEqual(
                    symbol[0], columns[0].strip(), "Symbol should be {} but is {}".format(symbol[0], columns[0])
                )

                # make sure to_date is correct
                self.assertEqual(
                    to_date, columns[1].strip(), "to_date should be {} but is {}".format(to_date, columns[1])
                )

                # make sure from_date is correct
                for line in fh_target:
                    pass  # read it to last
                columns = line.split(",")
                self.assertEqual(
                    from_date, columns[1].strip(), "from_date should be {} but is {}".format(from_date, columns[1])
                )
        finally:
            os.remove(target_file)
Ejemplo n.º 7
0
def get_dates_until_14():
    '''
    Extracts dates from a stock data
    Returns the list of the dates
    '''
    work_dir = '.'
    ref_stock = 'IBM'
    symbols = [ref_stock]
    download_stocks(symbols, '1960-01-01', '2015-01-01', work_dir)

    opendates = []
    stock_csv = os.path.join(work_dir, ref_stock + '.csv')
    with open(stock_csv, 'r') as fh_csv:
        reader = csv.reader(fh_csv)
        next(reader)
        for row in reader:
            opendates.append(datetime.strptime(row[0], Isoformat))
    os.remove(stock_csv)

    opendates.sort()

    return opendates
Ejemplo n.º 8
0
def get_dates_until_14():
    '''
    Extracts dates from a stock data
    Returns the list of the dates
    '''
    work_dir = '.'
    ref_stock = 'IBM'
    symbols = [ref_stock]
    download_stocks(symbols, '1960-01-01', '2015-01-01', work_dir)

    opendates = []
    stock_csv = os.path.join(work_dir, ref_stock + '.csv')
    with open(stock_csv, 'r') as fh_csv:
        reader = csv.reader(fh_csv)
        next(reader)
        for row in reader:
            opendates.append(datetime.strptime(row[0], Isoformat))
    os.remove(stock_csv)

    opendates.sort()

    return opendates