def load_from_file(self): path = UtilsConfig.get_stock_list_path(self.list_name) if path is not None and os.path.isfile(path): try: f = open(path, 'r', encoding=UtilsConfig.get_encoding()) except: f = open(path, 'r') for line in f.readlines(): info = StockIndustryInfo(*line.split()) self.infolist[info.key] = info f.close() return True else: print('load file {} failed.'.format(self.list_name)) return False
def save_to_file(self): path = UtilsConfig.get_stock_list_path(self.list_name) if path is not None: try: f = open(path, 'w', encoding=UtilsConfig.get_encoding()) except: f = open(path, 'w') for key, line in self.infolist.items(): assert(isinstance(line, StockSuperiorInfo)) f.write('{} {} {}\n'.format(line.update_date, line.code, line.code_name)) f.close() return True else: print('save file {} failed.'.format(self.list_name)) return False
def save_to_file(self, file_name='default.list'): path = UtilsConfig.get_stock_list_path(file_name) if path is not None: try: f = open(path, 'w', encoding=UtilsConfig.get_encoding()) except: f = open(path, 'w') for key, line in self.infolist.items(): assert(isinstance(line, StockBasicInfo)) f.write('{} {} {} {} {} {}\n'.format(line.code, line.code_name, line.ipoDate, line.outDate, line.type.numerator, line.status.numerator)) f.close() return True else: print('save file {} failed.'.format(file_name)) return False
def update_k5(self): path = UtilsConfig.get_stock_data_path(self.code_name, stock_type='k5') if path is not None: if os.path.isfile(path) and os.path.getsize(path) > Data5.HEX_LEN: try: file = open(path, 'rb') file.seek(-Data5.HEX_LEN, 2) # from the end of file date_hex = file.read(4) date_str = str(unpack('<L', date_hex)[0] // 10000) file.close() except Exception as e: print(' ' + str(e)) date_str = StockData.NEW_FILE_DATE else: date_str = StockData.NEW_FILE_DATE date_now = datetime.datetime.now() date_record = datetime.datetime(int('20' + date_str[0:2]), int(date_str[2:4]), int(date_str[4:6])) if (date_now - date_record).days > 0: file = open(path, 'ab') k5_list = Stock.query_hist_k5( self.code_name, start_date='20{}-{}-{}'.format(date_str[0:2], date_str[2:4], date_str[4:6])) if isinstance(k5_list, list): k5_list.sort(key=lambda i: int(i[0])) k5_list.sort(key=lambda i: int(i[0])) for item in k5_list: k5 = Data5(*item) if int(date_str) < int(k5.time_str[:6]): # time, open, close, high, low, volume, amount file.write(pack(Data5.FMT_HEX, int(k5.time_str), k5.open, k5.close, k5.high, k5.low, k5.volume, k5.amount)) file.close() else: print('Get path failed.')
def update_kd(self): path = UtilsConfig.get_stock_data_path(self.code_name, stock_type='kd') if path is not None: if os.path.isfile(path) and os.path.getsize(path) > DataD.HEX_LEN: try: file = open(path, 'rb') file.seek(-DataD.HEX_LEN, 2) # from the end of file date_hex = file.read(4) date_str = str(unpack('<L', date_hex)[0]) file.close() except Exception as e: print(' ' + str(e)) date_str = StockData.NEW_FILE_DATE else: date_str = StockData.NEW_FILE_DATE date_now = datetime.datetime.now() date_record = datetime.datetime(int('20' + date_str[0:2]), int(date_str[2:4]), int(date_str[4:6])) if (date_now - date_record).days > 0: file = open(path, 'ab') kd_list = Stock.query_hist_kd( self.code_name, start_date='20{}-{}-{}'.format(date_str[0:2], date_str[2:4], date_str[4:6])) for item in kd_list: kd = DataD(*item) if int(date_str) < int(kd.date_str): file.write(pack(DataD.FMT_HEX, # date, open, close, high, low, unused, volume, amount, int(kd.date_str), kd.open, kd.close, kd.high, kd.low, 0, kd.volume, kd.amount, # adjustflag, turn, tradestatus, pctChg, peTTM, int(kd.adjust_flag), kd.turn, int(kd.trade_status), kd.pctChg, kd.peTTM, # psTTM, pcfNcfTTM, pbMRQ, isST kd.psTTM, kd.pcfNcfTTM, kd.pbMRQ, int(kd.isST))) file.close() else: print('Get path failed.')
def load_k5(self, days, start_date=0, end_date=999999): path = UtilsConfig.get_stock_data_path(self.code, stock_type='k5') if path is not None and os.path.isfile(path): self.k5_list = StockData.parse_hex_k5(path, days, start_date, end_date) return True else: # print(self.code, 'Load k5 error') return False
def update_up_list(file_name='stock_update.list', nmc_name='v_nmc.db'): nmc_path = UtilsConfig.get_stock_list_path(nmc_name) nmc = EnvParam(nmc_path).get() nmc = [(k, v) for k, v in nmc.items()] if isinstance(nmc, list): nmc.sort(key=lambda x: x[1], reverse=True) sbi = StocksBasicInfo() sbi_rec = StocksBasicInfo() sbi.load_from_file('stock_all.list') for n in nmc[:1000]: if n[0] in sbi.get_dict(): sbi_rec.add_instance(sbi.get_dict()[n[0]]) if len(sbi_rec.get_list()) > 0: sbi_rec.save_to_file(file_name)
def update_report(file_name='v_report.db'): path = UtilsConfig.get_stock_list_path(file_name) if path is None: return env = EnvParam(path) report = env.get() report = {} if report is None else report last_rec = None for y in range(2010, 2099): for q in range(1, 5): if (y, q) not in report: req = StockRtData.get_report_data(y, q) if req is None or len(req) == 0: if last_rec is not None: req = StockRtData.get_report_data(*last_rec) report[last_rec] = req env.put(report).save() return report[(y, q)] = req last_rec = (y, q)
def update_recent_nmc(file_name='v_nmc.db'): path = UtilsConfig.get_stock_list_path(file_name) if path is None: return nmc = StockRtData.get_recent_all_nmc() EnvParam(path).put(nmc).save()