def monitor(self): self.has_sent_kzz = dict(zip(self.kzz_code, [datetime.datetime.now()] * len(self.kzz_code))) self.has_sent_diff = dict(zip(self.kzz_code, [datetime.datetime.now()] * len(self.kzz_code))) self.has_sent_zg = dict(zip(self.zg_code, [datetime.datetime.now()] * len(self.zg_code))) while 1: current = trading_time() # current=0 if current == MARKET_OPENING: self.get_realtime_info(self.kzz_code, self.has_sent_kzz, '转债', self.kzz_stocks, self.kzz_stocks_yjl, ZZ_ALERT_PERCENT) self.get_realtime_info(self.zg_code, self.has_sent_zg, '正股', self.zg_stocks, self.zg_stocks_yjl, ZG_ALERT_PERCENT) self.get_price_diff(self.kzz_code, self.has_sent_diff, '差价') time.sleep(LOOP_TIME) elif current == -1: time.sleep(LOOP_TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format(datetime.datetime.now())) logger.info(e) exit(0)
def monitor(self): self.has_sent_kzz = dict(zip(self.kzz_code, [datetime.datetime.now()] * len(self.kzz_code))) self.has_sent_diff = dict(zip(self.kzz_code, [datetime.datetime.now()] * len(self.kzz_code))) self.has_sent_zg = dict(zip(self.zg_code, [datetime.datetime.now()] * len(self.zg_code))) while 1: current = trading_time() # current=0 if current == MARKET_OPENING: self.get_realtime_info(self.kzz_code, self.has_sent_kzz, '转债', self.kzz_stocks, self.kzz_stocks_yjl, ZZ_ALERT_PERCENT) self.get_realtime_info(self.zg_code, self.has_sent_zg, '正股', self.zg_stocks, self.zg_stocks_yjl, ZG_ALERT_PERCENT) self.get_price_diff(self.kzz_code, self.has_sent_diff, '差价') time.sleep(LOOP_TIME) elif current == -1: time.sleep(LOOP_TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format(datetime.datetime.now())) logger.info(e) exit(0)
def monitor(self): while 1: current = trading_time() if current == MARKET_OPENING: try: price_df = ts.quotes(self.code_list, conn=self.api) except Exception as e: logger.info(e) self.api = ts.get_apis() time.sleep(EXECEPTION_TIME) else: price_df = price_df[price_df['cur_vol'] != 0] price_df['percent'] = (price_df['price'] - price_df['last_close']) / price_df['last_close'] * 100 price_df['percent'] = price_df['percent'].map(lambda x: round(x, 2)) ret_dt = price_df[(price_df['percent'] > 2) | (price_df['percent'] < -2)][ ['code', 'price', 'percent']] if len(ret_dt) > 0: name_list = [] yjl_list = [] # 只会提醒一次,下次就不会再出来了 for i in ret_dt['code']: name_list.append(self.stocks[i]) yjl_list.append(self.stocks_yjl[i]) self.code_list.remove(i) ret_dt['name'] = name_list ret_dt[u'溢价率'] = yjl_list ret_dt = ret_dt.sort_values(by='percent', ascending=False) ret_dt = ret_dt.reset_index(drop=True) try: wechat.send_content(ret_dt.to_string()) # sendmail(ret_dt.to_string(), '波动的可转债') # logger.info("Send mail successfully at {}".format(datetime.datetime.now())) except Exception as e: logger.info('sending wechat failed') logger.info(e) time.sleep(LOOP__TIME) elif current == -1: time.sleep(LOOP__TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format(datetime.datetime.now())) logger.info(e) exit(0)
def monitor(self, total_market=True): ''' total_market 默认监控全市场 total_market = True ''' if total_market: (kzz_stocks, zg_stocks, kzz_yjl, zg_yjl) = self.all_bond_market() else: (kzz_stocks, zg_stocks, kzz_yjl, zg_yjl) = self.get_current_position() zg_code = list(zg_stocks.keys()) kzz_code = list(kzz_stocks.keys()) self.has_sent_kzz = dict( zip(kzz_code, [datetime.datetime.now()] * len(kzz_code))) self.has_sent_diff = dict( zip(kzz_code, [datetime.datetime.now()] * len(kzz_code))) self.has_sent_zg = dict( zip(zg_code, [datetime.datetime.now()] * len(zg_code))) while 1: current = trading_time() if current == MARKET_OPENING: self.get_realtime_info(kzz_code, self.has_sent_kzz, '转债', kzz_stocks, kzz_yjl, ZZ_ALERT_PERCENT) self.get_realtime_info(zg_code, self.has_sent_zg, '正股', zg_stocks, zg_yjl, ZG_ALERT_PERCENT) self.get_price_diff(codes=kzz_code, has_sent_=self.has_sent_diff, types='差价', kzz_stocks=kzz_stocks, kzz_stocks_yjl=kzz_yjl) time.sleep(LOOP_TIME) elif current == -1: time.sleep(LOOP_TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format( datetime.datetime.now())) logger.info(e) exit(0)
def monitor(self): while 1: current = trading_time() if current == 0: # if True: try: price_df = ts.quotes(self.code_list, conn=self.api) price_df = price_df[price_df['cur_vol'] != 0] price_df['percent'] = ( price_df['price'] - price_df['last_close']) / price_df['last_close'] * 100 price_df['percent'] = map(lambda x: round(x, 2), price_df['percent']) ret_dt = price_df[(price_df['percent'] > 2) | (price_df['percent'] < -2)][[ 'code', 'price', 'percent' ]] if len(ret_dt) > 0: name_list = [] ylj_list = [] # 只会提醒一次,下次就不会再出来了 for i in ret_dt['code']: name_list.append(self.stocks[i]) yjl_list.append(self.stocks_yjl[i]) self.code_list.remove(i) # name_list =[self.stocks[i] for i in ret_dt['code'] ] ret_dt['name'] = name_list ret_dt[u'溢价率'] = yjl_list ret_dt = ret_dt.sort_values(by='percent', ascending=False) ret_dt = ret_dt.reset_index(drop=True) # print ret_dt # print datetime.datetime.now() try: sendmail(ret_dt.to_string(), u'波动的可转债') logger.log("Send mail successfully at {}".format( datetime.datetime.now())) except Exception, e: logger.log('sending mail failed') logger.log(e) time.sleep(LOOP__TIME) except Exception, e: logger.log(e) self.api = ts.get_apis() time.sleep(EXECEPTION_TIME)
def monitor(self): while 1: current = trading_time() if current == MARKET_OPENING: # 持仓 try: price_df = ts.quotes(self.code_lists, conn=self.api) except Exception as e: logger.error('获取持仓数据异常>>> {}'.format(e)) try: self.api = ts.get_apis() except Exception as e: logger.error('重新启动get_api出错 {}'.format(e)) else: # 去除不合法的数据 price_df = price_df.dropna() filter_df = price_df[price_df['last_close'] != 0] filter_df = filter_df.reset_index(drop=True) filter_df['percent'] = (filter_df['price'] - filter_df['last_close'] ) / filter_df['last_close'] * 100 filter_df['percent'] = filter_df['percent'].map( lambda x: round(x, 2)) ret_df = filter_df[ (filter_df['percent'] > ALERT_PERCENTAGE) | (filter_df['percent'] < ALERT_PERCENTAGE * -1)] if len(ret_df) > 0: d = dict( zip(list(self.df['证券代码'].values), list(self.df['证券名称']))) ret_df['name'] = ret_df['code'].map(lambda x: d.get(x)) ret_df['amount'] = ret_df['amount'].map( lambda x: round(x / 10000, 1)) rename_column = { 'code': '证券代码', 'name': '证券名称', 'price': '当前价格', 'percent': '涨幅', 'amount': '成交金额(万)' } ret_df = ret_df.rename(columns=rename_column) ret = ret_df[list(rename_column.values())] ret = ret.reset_index(drop=True) content = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') + '\n' + ret.to_string() try: wechat.send_content(content) except Exception as e: logger.error('微信发送失败 {}'.format(e)) # 可转债 try: price_df = ts.quotes(self.kzz_code_list, conn=self.api) except Exception as e: logger.error('获取可转债异常 >>>> {}'.format(e)) try: self.api = ts.get_apis() except Exception as e: logger.error('异常中存在异常{}'.format(e)) # continue time.sleep(EXECEPTION_TIME) # continue else: if len(price_df) != 0: # continue price_df = price_df[price_df['cur_vol'] != 0] price_df['percent'] = (price_df['price'] - price_df['last_close'] ) / price_df['last_close'] * 100 price_df['percent'] = price_df['percent'].map( lambda x: round(x, 2)) ret_dt = price_df[(price_df['percent'] > ALERT_PERCENT) | (price_df['percent'] < -1 * ALERT_PERCENT)][[ 'code', 'price', 'percent' ]] if len(ret_dt) > 0: name_list = [] yjl_list = [] # 只会提醒一次,下次就不会再出来了 for i in ret_dt['code']: name_list.append(self.stocks[i]) yjl_list.append(self.stocks_yjl[i]) self.kzz_code_list.remove(i) ret_dt['name'] = name_list ret_dt[u'溢价率'] = yjl_list ret_dt = ret_dt.sort_values(by='percent', ascending=False) ret_dt1 = ret_dt.reset_index(drop=True) content0 = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' ) + '\n' + ret_dt1.to_string() try: wechat.send_content(content0) except Exception as e: logger.info('发送微信失败') logger.info(e) # 自选池 try: price_pool = ts.quotes(self.pool_list, conn=self.api) except Exception as e: logger.error('获取自选错误 >>> {}'.format(e)) try: self.api = ts.get_apis() except Exception as e: logger.error('异常中出现异常 {}'.format(e)) time.sleep(EXECEPTION_TIME) # continue # continue else: if len(price_pool) == 0: continue price_pool = price_pool[price_pool['cur_vol'] != 0] price_pool['percent'] = (price_pool['price'] - price_pool['last_close'] ) / price_pool['last_close'] * 100 price_pool['percent'] = price_pool['percent'].map( lambda x: round(x, 2)) ret_dt_pool = price_pool[ (price_pool['percent'] > ALERT_PERCENT_POOL) | (price_pool['percent'] < -1 * ALERT_PERCENT_POOL)][[ 'code', 'price', 'percent' ]] if len(ret_dt_pool) > 0: name_list_pool = [] # 只会提醒一次,下次就不会再出来了 for i in ret_dt_pool['code']: name_list_pool.append(self.pool_dict[i]) self.pool_list.remove(i) ret_dt_pool['name'] = name_list_pool ret_dt_pool = ret_dt_pool.sort_values(by='percent', ascending=False) ret_dt_pool = ret_dt_pool.reset_index(drop=True) content1 = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' ) + '\n' + ret_dt_pool.to_string() try: wechat.send_content(content1) except Exception as e: logger.error('微信发送异常{}'.format(e)) time.sleep(LOOP_TIME) elif current == -1: time.sleep(LOOP_TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format( datetime.datetime.now())) logger.info(e) exit(0)