def down_minute_data(self, count=400, freq=None): if pd.isnull(freq): self.data = get_k_data_jq(self.stk_code, count=count, end_date=get_current_datetime_str(), freq=self.freq) else: self.data = get_k_data_jq(self.stk_code, count=count, end_date=get_current_datetime_str(), freq=freq)
def init_today_minute_data(self, count=200, debug_datetime=None): """ 为了避免重复下载数据,有时需要维护最近一段时间的分钟数据 :return: """ try: datetime_now = { True: get_current_datetime_str(), False: fgv.debug_datetime }.get(isinstance(fgv.debug_datetime, type(None))) self.rt_minute_data = get_k_data_jq(stk=self.stk_code, freq=self.freq, count=count, end_date=datetime_now) self.rt_minute_data.loc[:, 'datetime'] = self.rt_minute_data.index self.rt_minute_data.loc[:, 'num'] = range(len(self.rt_minute_data)) self.rt_minute_data = self.rt_minute_data.set_index('num') self.rt_minute_update_minute = self.get_minute_from_datetime( self.rt_minute_data.tail(1)['datetime'].values[0]) return True except Exception as e_: logger_eml.exception('初始下载“rt minute数据”时出错!具体为:%s' % str(e_)) return False
def config_log_file(self, save_dir_relative='/logs/', time_span=10, backupCount=100): """ 配置打印文件 :param save_dir_relative: :param file_name: :param level: 打印日志文件的级别 logging.WARNING :param time_span: 日志文件按时间分割,分割间隔 :return: """ file_name = self.logger_name + '_pid' + str( os.getpid()) + '_' + get_current_datetime_str().replace(':', '-') + '_' # 定义一个打印到文件中的 log_dir = self.root_path + save_dir_relative if not os.path.exists(log_dir): os.makedirs(log_dir) fh = logging.handlers.TimedRotatingFileHandler( filename=log_dir + file_name + str(self.file_level) + '.log', when='M', interval=time_span, backupCount=backupCount, encoding='utf-8', ) # fh = logging.FileHandler(log_dir + file_name + get_current_date_str() + '.log', mode='w', encoding='UTF-8') fh.setLevel(self.file_level) fh.setFormatter(self.format) self.logger.addHandler(fh)
def update_rt_minute_data(self, rt_p, rt=False): """ :param rt_p: :param rt: 实时标志位,如果为ture,则表示对close实时更新,若为false,则表示跨了周期(比如5分钟)再更新 :return: """ debug_datetime = fgv.debug_datetime # 获取当前分钟数 try: # 检查是否跨天 self.update_rt_minute_global_info() datetime_now = { True: get_current_datetime_str(), False: debug_datetime }.get(isinstance(debug_datetime, type(None))) minute_now = self.get_minute_from_datetime(datetime_now) if minute_now - self.rt_minute_update_minute >= self.freq_d: df_l = len(self.rt_minute_data) self.rt_minute_data.loc[df_l, 'close'] = rt_p self.rt_minute_data.loc[df_l, 'datetime'] = datetime_now logger.debug('更新对比数据:跨时段!\nrt_p:%s\n时间:%s' % (str(rt_p), datetime_now)) logger.debug('\n【update_rt_minute_data】对比数据:%s' % self.rt_minute_data.to_string()) # 记录上次更新分钟数 self.rt_minute_update_minute = minute_now else: if rt: self.rt_minute_data.loc[len(self.rt_minute_data) - 1, 'close'] = rt_p logger.debug('更新对比数据:同时段!\nrt_p:%s\n时间:%s' % (str(rt_p), datetime_now)) logger.debug('\n【update_rt_minute_data】对比数据:%s' % self.rt_minute_data.to_string()) except Exception as e_: logger_eml.exception('更新实时minute数据计算的对比数据时出错!原因:\n %s\n数据为:\n%s' % (str(e_), self.rt_minute_data.to_string()))
def get_today_df(self, debug_date=None): df_ = get_k_data_jq(stk=self.stk_code, count=50, end_date={ True: get_current_datetime_str(), False: debug_date }.get(isinstance(debug_date, type(None)))) # 调整df,增加date列,增加正数index列 df_['datetime'] = df_.index df_['datetime_str'] = df_.apply(lambda x: str(x['datetime']), axis=1) # 重置索引 df_ = df_.reset_index(drop=True) return df_
def get_current_price_JQ(stk_code): # 使用聚宽数据接口替代 if stk_code in ['sh', 'sz', 'cyb']: stk_code_normal = { 'sh': '000001.XSHG', 'sz': '399001.XSHE', 'cyb': '399006.XSHE' }[stk_code] else: stk_code_normal = jq.normalize_code(stk_code) current_price = float(jq.get_price(stk_code_normal, count=1, end_date=get_current_datetime_str(),frequency='1m')['close'].values[0]) return current_price
def update_today_df(self): """ 更新当天网格 :return: """ debug_date = fgv.debug_date date_now = { True: get_current_datetime_str(), False: debug_date }.get(isinstance(debug_date, type(None))) if (self.today_df_update_date != date_now) | (isinstance( self.today_df, type(None))): self.today_df = self.get_today_df(debug_date=debug_date) self.today_df_update_date = date_now logger.debug('\n网格计算,检测到跨天,today_df变量更新成功!') else: logger.debug('\n网格计算,未检测到跨天,today_df变量无需更新!')
def debug_print_txt(file_name, stk, value, enable=False): """ :param enable: :param file_name: :param stk: :param value: :return: """ if not enable: return file_dir = data_dir + 'Debug_log/' + get_current_date_str() file_url = file_dir + '/' + file_name + '_' + stk + '.txt' # 如果文件夹不存在,创建 if not os.path.exists(file_dir): os.makedirs(file_dir) with open(file_url, 'a+') as f: f.write(get_current_datetime_str() + ':\n-------------------\n' + value + '\n')
def get_last_m_stray(self): """ 找出上一次穿越m线的情况 :return: """ # 取出一副本,再进行drop操作 df = self.data.dropna(axis=0) if df.empty: return '近期无穿越M%s均线(freq=%s)的行为' % (str(self.m), self.freq) # 取出最后一个转折点 df_pot = df[df.apply(lambda x: x['pn_pot_' + str(self.m)], axis=1)] if df_pot.empty: return '近期无穿越M%s均线(freq=%s)的行为' % (str(self.m), self.freq) last_pot = df_pot.tail(1) self.last_result = last_pot['m_pn_' + str(self.m)].values[0] return self.last_m_pot_note(get_current_datetime_str(), last_pot['datetime'].values[0], last_pot['m_pn_' + str(self.m)].values[0])
# encoding=utf-8
@staticmethod def get_index_jq_code(index_str): """ 将 字符格式的指数转为聚宽代码 :param index_str: :return: """ index_str_2_jq_dict = { 'sh': '000001.XSHG', 'sz': '399001.XSHE', 'cyb': '399006.XSHE', 'zz500': '000905.XSHG', 'hs300': '000300.XSHG', 'sz50': '000016.XSHG' } return index_str_2_jq_dict.get(index_str, index_str) if __name__ == '__main__': jq_login() while True: r = get_current_price_JQ('M2012.XDCE') print(get_current_datetime_str() + ':' + str(r)) time.sleep(1)
logger.debug('邮箱已清空!清空后邮箱状态:%s' % str(self.mail_server.stat())) except Exception as e_: logger.exception('清空邮箱时出错,原因:\n%s' % str(e_)) def get_mail_obj_list(self): """ 返回邮箱对象及编号,编号用于定点删除 :return: """ return [(self.get_mail_object(x + 1), x + 1) for x in range(self.msg_count)] if __name__ == '__main__': er = EmailRead(root_path + '/server/mail_subscribe/global_value/senderInfo.json') while True: er.update_mailbox_info() if er.msg_count > 0: obj_list = er.get_mail_obj_list() for obj in obj_list: print(er.get_mail_content(obj)) er.clear_mailbox() else: logger.debug('【%s】:邮箱为空现在!' % get_current_datetime_str()) time.sleep(10)