def predict_price(self): session = requests.Session() session.get(url=home_page, headers=home_headers) r = session.get(url=xueqiu_url, headers=headers) js_data = r.json() quote = js_data.get('data', {}).get('quote') quote['crawltime'] = datetime.datetime.now() doc.insert_one(quote) percent = quote.get('percent') jsl_qdii, est_val_dt = self.qdii_info() if jsl_qdii: predict_v = round((1 + percent * 0.95 * 0.01) * jsl_qdii, 3) self.logger.info(f'最新估值{predict_v}') d = {'日期': today, '估值': predict_v} client['db_stock']['huabaoyouqi_predict'].insert_one(d) title = f'华宝估值{predict_v} 净值日期{est_val_dt[5:]}' send_from_aliyun(title, '') else: notify(title='华宝油气获取估值失败')
def weekly_drop_rank(self, current=True, type_='week'): ''' :type_ week或者month :param current: :param type_: :return: ''' self.__type = type_ month_data, week_data, code_dict, price_dict, yjl_dict = self.get_rank(current) if type_ == 'week': rank_data = week_data else: rank_data = month_data result = [] for i in rank_data: self.logger.info(f'{i[0]} {code_dict.get(i[0])} : {i[1]}%') d = {} d['代码'] = i[0] d['名称'] = code_dict.get(i[0]) d['当前价格'] = price_dict.get(i[0]) d['溢价率'] = yjl_dict.get(i[0]) d['跌幅'] = i[1] d['更新日期'] = self.today result.append(d) # self.save_mongodb(result) title, body = self.mail_content(result) send_from_aliyun(title=title, content=body, types='html')
def yesterday(self, df, post_fix, engine): df = df.set_index('序号') formula = lambda x: round(x * 100, 3) df['最大涨幅'] = df['最大涨幅'].map(formula) df['最大跌幅'] = df['最大跌幅'].map(formula) df['今日开盘涨幅'] = df['今日开盘涨幅'].map(formula) df['昨日涨停强度'] = df['昨日涨停强度'].map(lambda x: round(x, 0)) df['今日涨停强度'] = df['今日涨停强度'].map(lambda x: round(x, 0)) try: df.to_sql(self.today + post_fix, engine, if_exists='fail') except Exception as e: notify(f'{self.__class__} 出错') self.logger.info(e) avg = round(df['今日涨幅'].mean(), 2) median = round(df['今日涨幅'].median(), 2) min_v = round(df['今日涨幅'].min(), 2) min_index = df['今日涨幅'].argmin() min_percent_name = df.iloc[min_index]['名称'] current = datetime.datetime.now().strftime('%Y-%m-%d') title = '昨涨停今天{}平均涨{}\n'.format(current, avg) content = '<p>昨天涨停今天<font color="red">{}</font></p>' \ '<p>平均涨幅 <font color="red">{}</font></p>' \ '<p>涨幅中位数 <font color="red">{}</font></p>' \ '<p>涨幅最小 <font color="red">{}</font></p>' \ '<p>涨幅最小股 <font color="red">{}</font></p>'.format(current, avg, median, min_v, min_percent_name) try: send_from_aliyun(title, content, types='html') except Exception as e: self.logger.error(e)
def start(self): query_result_str, has_data = self.query_big_volatility_share() if has_data: title = f'{self.today} LOF/ETF 申购波动数据' print(title) print(query_result_str) send_from_aliyun(title, content=query_result_str) else: self.logger.info(f'今天{self.today}没有数据')
def send_mail(self, df): send_content = df.to_html(index=False, border=1, justify='center') send_content = send_content.replace('class', 'cellspacing=\"0\" class') title = '{} 可转债综合价格前20名'.format( datetime.datetime.now().strftime('%Y-%m-%d')) try: send_from_aliyun(title, send_content, types='html') except Exception as e: self.logger.error('发送邮件出错') self.logger.error(e) else: self.logger.info('发送成功!')
def store_mysql(self, df): try: df.to_sql('tb_jsl_{}'.format(self.date), self.engine, if_exists='replace', dtype={'可转债代码': VARCHAR(10)}) db_stock = self.DB.get_engine('db_stock', self.remote) df.to_sql('tb_bond_jisilu'.format(self.date), db_stock, if_exists='replace', dtype={'可转债代码': VARCHAR(10)}) except Exception as e: self.logger.info(e) send_from_aliyun(title='jisilu可转债', content='写入数据库出错')
def send_mail(self, content_dict): current = '' title = '{}转债跌大于正股数:{}'.format(current, content_dict["zz_than_zg_count"]) content = f'<p>转债上涨比例:<font color="red">{content_dict["raise_ratio"]}</font></p>' \ f'<p>转债跌>正股数: <font color="red">{content_dict["zz_than_zg_count"]}</font></p>' \ f'可转债涨幅>=0: <font color="red">{content_dict["plug_count"]}</font></p>' \ f'可转债涨幅<0: <font color="red">{content_dict["minus_count"]}</font></p>' \ f'涨幅最大值:<font color="red">{content_dict["max_v"]}</font> 属于<font color="red">{content_dict["raise_name"]}</font></p>' \ f'涨幅最小值:<font color="red">{content_dict["min_v"]}</font> 属于<font color="red">{content_dict["fall_name"]}</font></p>' \ f'涨幅均值:<font color="red">{content_dict["mean"]}</font></p>' \ f'涨幅中位数:<font color="red">{content_dict["median"]}</font></p>' \ f'涨幅波动的方差:<font color="red">{content_dict["ripple_ratio"]}</font></p>' try: send_from_aliyun(title, content, types='html') except Exception as e: self.notify('邮件发送失败', f'{self.__class__}:{e}') self.logger.error(e)
def analysis(self, date=None, head=300): date = datetime.date.today().strftime( '%Y-%m-%d') if date is None else None kzz_big_deal_count = [] for code in self.jisilu_df['可转债代码'].values: kzz_big_deal_count.append(self.get_volume_distribition(code, date)) kzz_big_deal_order = list( sorted(kzz_big_deal_count, key=lambda x: x[1], reverse=True)) send_content = [] for item in kzz_big_deal_order[:head]: self.logger.info('{} |大单{}'.format( self.code_name_dict.get(item[0]), item[1])) send_content.append('{}|大单 {}'.format( self.code_name_dict.get(item[0]), item[1])) # 入库的 big_deal_doc = self.mongodb['db_stock']['big_deal_logger'] batch_size = [] for item in kzz_big_deal_order: d = { 'Date': date, 'name': self.code_name_dict.get(item[0]), 'times': int(item[1]) } batch_size.append(d) try: big_deal_doc.insert_many(batch_size) # 写入mongo except Exception as e: self.logger.error(e) content = '\n'.join(send_content) title = '{}-大单监控'.format(date) try: send_from_aliyun(title, content) except Exception as e: self.notify('bigdeal msg', 'bigdeal send failed') self.logger.error(e) else: self.logger.info('发送成功')
def notice_me(self, today): now = datetime.datetime.now() if now.hour > NOTIFY_HOUR: # 下午才会发通知 title = f'{today} 基金折溢价' html = '' html = self.combine_html(html, TODAY) try: send_from_aliyun(title, html, types='html') except Exception as e: self.logger.error(e) self.logger.info('发送失败') else: self.logger.info('发送成功')
def yesterday_zt(self, df, post_fix, engine): df = df.set_index('序号') formula = lambda x: round(x * 100, 3) df['最大涨幅'] = df['最大涨幅'].map(formula) df['最大跌幅'] = df['最大跌幅'].map(formula) df['今日开盘涨幅'] = df['今日开盘涨幅'].map(formula) df['昨日涨停强度'] = df['昨日涨停强度'].map(lambda x: round(x, 0)) df['今日涨停强度'] = df['今日涨停强度'].map(lambda x: round(x, 0)) try: df.to_sql(self.today + post_fix, engine, if_exists='fail') except Exception as e: notify(f'{self.__class__} 出错') self.logger.info(e) title, content = self.generate_html(df) try: send_from_aliyun(title, content, types='html') except Exception as e: self.logger.error(e)
def data_parse(self, bond_list, adjust_no_use): cell_list = [] for item in bond_list: cell_list.append(pd.Series(item.get('cell'))) df = pd.DataFrame(cell_list) if adjust_no_use: # 类型转换 部分含有% df['premium_rt'] = df['premium_rt'].map(lambda x: float(x.replace('%', ''))) df['price'] = df['price'].astype('float64') df['convert_price'] = df['convert_price'].astype('float64') df['premium_rt'] = df['premium_rt'].astype('float64') df['redeem_price'] = df['redeem_price'].astype('float64') def convert_float(x): try: ret_float = float(x) except: ret_float = None return ret_float def convert_percent(x): try: ret = float(x) * 100 except: ret = None return ret def remove_percent(x): try: ret = x.replace(r'%', '') ret = float(ret) except Exception as e: ret = None return ret df['put_convert_price'] = df['put_convert_price'].map(convert_float) df['sprice'] = df['sprice'].map(convert_float) df['ration'] = df['ration'].map(convert_percent) df['volume'] = df['volume'].map(convert_float) df['convert_amt_ratio'] = df['convert_amt_ratio'].map(remove_percent) df['ration_rt'] = df['ration_rt'].map(convert_float) df['increase_rt'] = df['increase_rt'].map(remove_percent) df['sincrease_rt'] = df['sincrease_rt'].map(remove_percent) rename_columns = {'bond_id': '可转债代码', 'bond_nm': '可转债名称', 'price': '可转债价格', 'stock_nm': '正股名称', 'stock_cd': '正股代码', 'sprice': '正股现价', 'sincrease_rt': '正股涨跌幅', 'convert_price': '最新转股价', 'premium_rt': '溢价率', 'increase_rt': '可转债涨幅', 'put_convert_price': '回售触发价', 'convert_dt': '转股起始日', 'short_maturity_dt': '到期时间', 'volume': '成交额(万元)', 'redeem_price': '强赎价格', 'year_left': '剩余时间', 'next_put_dt': '回售起始日', 'rating_cd': '评级', # 'issue_dt': '发行时间', # 'redeem_tc': '强制赎回条款', # 'adjust_tc': '下修条件', 'adjust_tip': '下修提示', # 'put_tc': '回售', 'adj_cnt': '下调次数', # 'ration':'已转股比例' 'convert_amt_ratio': '转债剩余占总市值比', 'curr_iss_amt': '剩余规模', 'orig_iss_amt': '发行规模', 'ration_rt': '股东配售率', 'redeem_flag': '发出强赎公告', 'redeem_dt': '强赎日期', } df = df.rename(columns=rename_columns) df = df[list(rename_columns.values())] df['更新日期'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') df = df.set_index('可转债代码', drop=True) try: df.to_sql('tb_jsl_{}'.format(self.date), self.engine, if_exists='replace', dtype={'可转债代码': VARCHAR(10)}) db_stock = self.DB.get_engine('db_stock', self.remote) df.to_sql('tb_bond_jisilu'.format(self.date), db_stock, if_exists='replace', dtype={'可转债代码': VARCHAR(10)}) except Exception as e: self.logger.info(e) send_from_aliyun(title='jisilu可转债', content='写入数据库出错')
def send_mail(self): html = self.html() title = f'{self.today_str} 雪球趋势' send_from_aliyun(title, html, types='html')