Esempio n. 1
0
    def parse_data_by_file(self, fname):
        if not os.path.isfile(fname):
            raise TdxFileNotFoundException(
                'no tdx kline data, pleaes check path %s', fname)
        with open(fname, 'rb') as f:
            content = f.read()
            raw_li = self.unpack_records("<HHfffffII", content)
            data = []
            for row in raw_li:
                year, month, day = self._parse_date(row[0])
                hour, minute = self._parse_time(row[1])

                data.append(
                    OrderedDict([
                        ("date", "%04d-%02d-%02d %02d:%02d" %
                         (year, month, day, hour, minute)),
                        ("year", year),
                        ('month', month),
                        ('day', day),
                        ('hour', hour),
                        ('minute', minute),
                        ('open', row[2]),
                        ('high', row[3]),
                        ('low', row[4]),
                        ('close', row[5]),
                        ('amount', row[6]),
                        ('volume', row[7]),
                        #('unknown', row[8])
                    ]))
            return data
        return []
Esempio n. 2
0
    def parse_data_by_file(self, fname):

        if not os.path.isfile(fname):
            raise TdxFileNotFoundException(
                'no tdx kline data, pleaes check path %s', fname)

        with open(fname, 'rb') as f:
            content = f.read()
            return self.unpack_records('<IIIIIfII', content)
        return []
Esempio n. 3
0
    def get_df_by_file(self, fname):

        if not os.path.isfile(fname):
            raise TdxFileNotFoundException(
                'no tdx kline data, pleaes check path %s', fname)

        security_type = self.get_security_type(fname)
        if security_type not in self.SECURITY_TYPE:
            print("Unknown security type !\n")
            raise NotImplementedError

        coefficient = self.SECURITY_COEFFICIENT[security_type]
        data = [
            self._df_convert(row, coefficient)
            for row in self.parse_data_by_file(fname)
        ]

        df = pd.DataFrame(data=data,
                          columns=('date', 'open', 'high', 'low', 'close',
                                   'amount', 'volume'))
        df.index = pd.to_datetime(df.date)
        return df[['open', 'high', 'low', 'close', 'amount', 'volume']]