Пример #1
0
 def processing_section(self, section, dict_data_pre):
     """
     处理切片,分3、5、10等切片数据
     :param section:
                     DataFrame切片数据
                     example         amount  close  code  high   low  open        vol
                     date
                     1991-11-29     8557000   0.11     1  0.13  0.11  0.13     306200
                     1991-11-30     3962000   0.11     1  0.11  0.11  0.11     142900
                     1991-12-02    11099000   0.10     1  0.11  0.10  0.11     410500
                     1991-12-03     6521000   0.12     1  0.12  0.10  0.10     234800
                     1991-12-04     6853000   0.11     1  0.12  0.11  0.12     242700
                     1991-12-05     3458000   0.10     1  0.11  0.10  0.11     126000
                     1991-12-06     4341000   0.11     1  0.11  0.10  0.10     159400
                     1991-12-07     2218000   0.11     1  0.11  0.11  0.11      80600
     :param dict_data_pre:
                     前一交易日数据 格式类似本次返回的数据
     :return:
     """
     dict_data = {
         'security_code': Utils.quotes_surround(self.security_code)
     }
     # 日涨跌幅计算
     section_tail1 = section.tail(1)
     idx = section_tail1.index.values[0]
     if isinstance(idx, datetime.datetime) or isinstance(
             idx, datetime.date):
         the_date = idx
     else:
         the_date = idx.astype('M8[ms]').astype('O')
     week_day = Utils.format_week_day(the_date)
     dict_data['week_day'] = week_day
     the_date = Utils.format_date(the_date)
     dict_data['the_date'] = Utils.quotes_surround(the_date)
     if dict_data_pre is None:
         print('secuirty_code', self.security_code, '第一次dict_data_pre为None')
         # 转换时间格式,结果为<class 'datetime.datetime'> 1991-11-29 00:00:00
         dict_data_pre = self.get_dict_data_pre(the_date)
         if dict_data_pre is None:
             print('secuirty_code', self.security_code,
                   '查询后dict_data_pre还是为None,只能创建为0的数据')
             dict_data_pre = self.create_blank_dict_data()
     self.processing_1_day_chg(section_tail1, idx, dict_data, dict_data_pre)
     for days in [1, 3, 5, 10]:
         self.processing_avg(section.tail(days), days, dict_data,
                             dict_data_pre)
     self.dbService.upsert(dict_data, 'tquant_stock_history_quotation',
                           ['security_code', 'the_date'])
     return dict_data
Пример #2
0
 def get_day_kline_chg_upsertsql(security_code, the_date, dict_data):
     """
     根据计算得出的股票日K涨跌幅指标数据,生成需要更新的sql语句
     :param security_code: 股票代码,字符串类型
     :param the_date: 交易日,日期类型
     :param dict_data: 股票涨跌幅数据,字典类型
     :return: 
     """
     sql = "update tquant_stock_history_quotation set vol_chg = {vol_chg}, " \
           "close_chg = {close_chg}, price_avg = {price_avg}, price_avg_chg = {price_avg_chg}, " \
           "close_price_avg_chg = {close_price_avg_chg}, close_open_chg = {close_open_chg} " \
           "where security_code = {security_code} and the_date = {the_date} "
     sql = sql.format(vol_chg=dict_data['vol_chg'],
                      close_chg=dict_data['close_chg'],
                      price_avg=dict_data['price_avg'],
                      price_avg_chg=dict_data['price_avg_chg'],
                      close_price_avg_chg=dict_data['close_price_avg_chg'],
                      close_open_chg=dict_data['close_open_chg'],
                      security_code=Utils.quotes_surround(security_code),
                      the_date=Utils.quotes_surround(
                          Utils.format_date(the_date)))
     return sql
Пример #3
0
 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()
Пример #4
0
    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()