def __init__(self, security_code, is_reset, flag): self.security_code = security_code self.is_reset = is_reset self.flag = flag log_list = [ Utils.get_now(), Utils.get_info(), self.get_classs_name(), self.security_code, self.get_method_name() ] Utils.print_log(log_list)
def processing_real_time_kline(self): """ 处理单只股票的实时行情 :return: """ try: current_date = datetime.datetime.now().replace(microsecond=0) start_date = current_date.replace(hour=9, minute=30, second=0, microsecond=0) end_date = current_date.replace(hour=15, minute=0, second=0, microsecond=0) if current_date <= end_date and current_date >= start_date: log_list = [ Utils.get_now(), Utils.get_info(), self.get_classs_name(), self.security_code, self.get_method_name(), '当日全部5分钟实时行情 开始时间', start_date, '拉取时间', current_date, '结束时间', end_date ] Utils.print_log(log_list) # if True: # 5分钟K的实时行情 day_kline = tt.get_stock_bar(self.security_code, 1) # 处理单只股票的实时行情,并入库 self.analysis_real_time_kline(day_kline, start_date) else: log_list = [ Utils.get_now(), Utils.get_warn(), self.get_classs_name(), self.security_code, self.get_method_name(), '当日全部5分钟实时行情 开始时间', start_date, '拉取时间', current_date, '结束时间', end_date, '【交易结束或不在交易时间段内】' ] Utils.print_log(log_list) except Exception: traceback.print_exc()
def processing_single_security_code(self): log_list = [ Utils.get_now(), Utils.get_info(), self.get_classs_name(), self.security_code, self.get_method_name(), self.security_code + '【【start】】' ] Utils.print_log(log_list) """ 单只股票处理方法 :return: """ # 股票日K数据处理方法 if self.flag: self.processing_day_kline() self.processing_real_time_kline() log_list = [ Utils.get_now(), Utils.get_info(), self.get_classs_name(), self.security_code, self.get_method_name(), self.security_code + '【【end】】' ] Utils.print_log(log_list)
def analysis_real_time_kline(self, day_kline, start_date): """ 解析单只股票的实时行情,并入库 :param day_kline: :param start_date: :return: """ try: if day_kline.empty == False: indexes_values = day_kline.index.values if indexes_values is None or len(indexes_values) == 0: log_list = [ Utils.get_now(), Utils.get_warn(), self.get_classs_name(), self.security_code, self.get_method_name(), '当日全部5分钟实时行情 开始时间', start_date, '【行情为空】' ] Utils.print_log(log_list) return the_date = None first_idx = None last_idx = indexes_values[len(indexes_values) - 1] for idx in indexes_values: idx_datetime = idx.astype('M8[ms]').astype('O') # idx_datetime = datetime.datetime.utcfromtimestamp(idx.astype('O') / 1e9) # 由于第三方接口返回的数据是最近1000个5分钟K,所以需要剔除不是今天的数据 if idx_datetime >= start_date: first_idx = idx day_kline = day_kline[idx:] the_date = idx_datetime the_date = Utils.format_date(the_date) break if the_date is not None: # 统计数据,包括min, max 等 day_kline_describe = day_kline.describe() open = Utils.base_round_zero( day_kline.at[first_idx, 'open'], 2) high = Utils.base_round_zero( day_kline_describe.at['max', 'high'], 2) low = Utils.base_round_zero( day_kline_describe.at['min', 'low'], 2) close = Utils.base_round_zero( day_kline.at[last_idx, 'close'], 2) # sum统计 day_kline_sum = day_kline.sum() amount_count = Utils.base_round_zero( day_kline_sum['amount'] * 100, 2) vol_count = Utils.base_round_zero( day_kline_sum['vol'] * 100, 2) dict_data = { 'security_code': Utils.quotes_surround(self.security_code), 'the_date': Utils.quotes_surround(the_date), 'amount': amount_count, 'vol': vol_count, 'open': open, 'high': high, 'low': low, 'close': close } self.dbService.upsert(dict_data, 'tquant_stock_history_quotation', ['security_code', 'the_date']) print('secuirty_code', self.security_code, '实时行情基础数据入库成功') self.calculate_last_10_day(the_date) except Exception: traceback.print_exc()
def processing_day_kline(self): """ 股票日K数据处理,分全量还是增量 :return: """ try: if self.is_reset: max_the_date = datetime.datetime.now().replace(year=1970, month=1, day=1) else: max_the_date = self.get_max_the_date() diff_days = Utils.diff_days(max_the_date) log_list = [ Utils.get_now(), Utils.get_info(), self.get_classs_name(), self.security_code, self.get_method_name(), '最大交易日', max_the_date, '距今', Utils.format_date(datetime.date.today()), '差', diff_days, '天' ] Utils.print_log(log_list) dict_data_pre = None if diff_days is None: result = tt.get_all_daybar(self.security_code, 'bfq') else: result = tt.get_last_n_daybar(self.security_code, diff_days, 'bfq') if result.empty == False: # 按照正序排序,时间小的排前面 result.sort_index(ascending=True) # 需要处理的单只股票进度计数 add_up = 0 # 需要处理的单只股票进度打印字符 process_line = '=' len_indexes = len(result.index.values) for i in range(len_indexes): add_up += 1 if i < 9: continue end = i + 1 start = i - 9 section = result.iloc[start:end, :] dict_data = self.processing_section(section, dict_data_pre) dict_data_pre = dict_data # 批量打印日志 if add_up % 200 == 0: process_line += '=' progress = Utils.base_round( Utils.division_zero(add_up, len_indexes) * 100, 2) log_list = [ Utils.get_now(), Utils.get_info(), self.get_classs_name(), self.security_code, self.get_method_name(), '处理进度', add_up, len_indexes, process_line, str(progress) + '%' ] Utils.print_log(log_list) process_line += '=' progress = Utils.base_round( Utils.division_zero(add_up, len_indexes) * 100, 2) log_list = [ Utils.get_now(), Utils.get_info(), self.get_classs_name(), self.security_code, self.get_method_name(), '处理进度', add_up, len_indexes, process_line, str(progress) + '%' ] Utils.print_log(log_list) # self.dbService.upsert_many(list_db_data, 'tquant_stock_history_quotation', ['security_code', 'the_date']) else: log_list = [ Utils.get_now(), Utils.get_warn(), self.get_classs_name(), self.security_code, self.get_method_name(), '【日K DataFrame为空】' ] Utils.print_log(log_list) except Exception: traceback.print_exc()