def break_monitor(code, warning_vol): start = True start_monitor = True waiting_time = datetime.datetime.now() conn = ts.get_apis() while 1: current = datetime.datetime.now() try: if start_monitor: try: df = ts.quotes(code, conn=conn) except Exception as e: print(e) time.sleep(EXCEPTION_TIME_OUT) conn = ts.get_apis() continue print('under monitor {}'.format(current)) if df['bid_vol1'].values[0] < warning_vol and start: title=code+' Buy +1 : '+str(df['bid_vol1'].values[0]) sendmail(title,title) # msg.send_ceiling(code, df['bid_vol1'].values[0]) start = False start_monitor = False waiting_time = current + datetime.timedelta(seconds=TIME_RESET) time.sleep(NORMAL_TIME_OUT) except Exception as e: print(e) time.sleep(EXCEPTION_TIME_OUT) conn = ts.get_apis() continue if current > waiting_time: start = True start_monitor = True
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 = [] 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) # 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 as e: logger.log('sending mail failed') logger.log(e) time.sleep(LOOP__TIME) except Exception as e: logger.log(e) self.api = ts.get_apis() time.sleep(EXECEPTION_TIME) elif current == -1: time.sleep(LOOP__TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.log('fail to stop monitor {}'.format( datetime.datetime.now())) exit(0)
def main(): obj = Filter_Stock() now =datetime.datetime.now() today = now.strftime("%Y%m%d") tb_name = today+'zdt' end = str(now.year)+'-'+str(now.month - 1) df = obj.get_new_stock('2015',end) code_list =df['code'].values engine = get_engine('db_zdt') zt_df = pd.read_sql(tb_name,engine,index_col='index') zt_df[u'涨停强度']=map(lambda x:round(x,0),zt_df[u'涨停强度']) ret_df = zt_df[zt_df[u'代码'].isin(code_list)] if not ret_df.empty: tb_name_save = today+'_cx' excel_name = today+'_cx.xls' ret_df.to_excel(excel_name,encoding='gbk') ret_df.to_sql(tb_name_save,engine) s= ret_df[[u'代码',u'名称',u'涨停强度',u'打开次数',u'第一次涨停时间',u'最后一次涨停时间']].to_string() sendmail(s,today+u'次新涨停')
def main(): obj = Filter_Stock() now =datetime.datetime.now() today = now.strftime("%Y%m%d") tb_name = today+'zdt' end = str(now.year)+'-'+str(now.month - 1) df = obj.get_new_stock('2015',end) code_list =df['code'].values engine = get_engine('db_zdt') zt_df = pd.read_sql(tb_name,engine,index_col='index') zt_df['涨停强度']=map(lambda x:round(x,0),zt_df['涨停强度']) ret_df = zt_df[zt_df['代码'].isin(code_list)] if not ret_df.empty: tb_name_save = today+'_cx' excel_name = today+'_cx.xls' ret_df.to_excel(excel_name,encoding='gbk') ret_df.to_sql(tb_name_save,engine) s= ret_df[['代码','名称','涨停强度','打开次数','第一次涨停时间','最后一次涨停时间']].to_string() sendmail(s,today+'次新涨停')
def sendmail(self): buy, sell = self.run() sub = '{}: 美元汇率{}'.format( datetime.datetime.now().strftime('%Y-%m-%d %H:%M'), buy) sendmail('', sub) conn = get_mysql_conn('db_stock') cursor = conn.cursor() cmd = 'insert into `usd_ratio` (`price`,`date`) VALUES ({},{!r})'.format( buy, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) try: cursor.execute(cmd) conn.commit() except Exception as e: print(e) conn.rollback() conn.close()
# conn.commit() # except Exception, e: # print e # conn.rollback() all_contents.append(str_temp) f_open.write(str_temp) num = num + 1 # itchat.send(str_temp,toUserName=username) # time.sleep(1) # print "index %d" %index index = index + 1 f_open.close() sendmail(''.join(all_contents), temp_time) db_name = 'db_stock' conn = get_mysql_conn(db_name, local=True) create_tb(conn) cur = conn.cursor() for i in cmd_list: try: cur.execute(i) except Exception, e: conn.rollback() conn.commit() conn.close() db_name = 'qdm225205669_db'
def getinfo(max_index_user=3, days=-2): last_day = datetime.datetime.now() + datetime.timedelta(days=days) stock_news_site = "http://ggjd.cnstock.com/gglist/search/ggkx/" my_useragent = [ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1', 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1', 'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11', 'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)'] index = 0 max_index = max_index_user num = 1 temp_time = time.strftime("[%Y-%m-%d]-[%H-%M]", time.localtime()) store_filename = "StockNews-%s.log" % temp_time f_open = codecs.open(store_filename, 'w', 'utf-8') all_contents = [] cmd_list=[] while index <= max_index: user_agent = random.choice(my_useragent) # print(user_agent) company_news_site = stock_news_site + str(index) # content = urllib2.urlopen(company_news_site) headers = {'User-Agent': user_agent, 'Host': "ggjd.cnstock.com", 'DNT': '1', 'Accept': 'text/html, application/xhtml+xml, */*', } req = urllib2.Request(url=company_news_site, headers=headers) resp = None raw_content = "" retry=6 for _ in range(retry): try: resp = urllib2.urlopen(req, timeout=30) except urllib2.HTTPError as e: e.fp.read() except urllib2.URLError as e: if hasattr(e, 'code'): logger.log("error code %d" % e.code) elif hasattr(e, 'reason'): logger.log("error reason %s " % e.reason) finally: if resp: raw_content = resp.read() break try: soup = BeautifulSoup(raw_content, "html.parser") all_content = soup.find_all("span", "time") except Exception as e: logger.log(e) return None # cmd_list = [] for i in all_content: news_time = i.string # print(news_time) node = i.next_sibling url=node['href'] try: year = re.findall('tjd_ggkx/(\d+)/',url)[0][:4] except Exception as e: continue news_time_f = datetime.datetime.strptime(year +'-' +news_time, '%Y-%m-%d %H:%M') if news_time_f >= last_day: # news_time_f=news_time_f.replace(2018) # print(news_time_f) str_temp = "No.%s \n%s\t%s\n---> %s \n\n" % (str(num), news_time, node['title'], node['href']) # print("inside %d" %num) # print(str_temp) cmd = '''INSERT INTO tb_cnstock (Date,Title,URL ) VALUES(\'%s\',\'%s\',\'%s\');''' % ( news_time_f, node['title'].strip(), node['href'].strip()) # print(cmd) cmd_list.append(cmd) # try: # cur.execute(cmd) # conn.commit() # except Exception as e: # print(e) # conn.rollback() all_contents.append(str_temp) f_open.write(str_temp) num = num + 1 # itchat.send(str_temp,toUserName=username) # time.sleep(1) # print("index %d" %index) index = index + 1 f_open.close() if len(all_contents)>0: sendmail(''.join(all_contents), temp_time) db_name='db_stock' conn =get_mysql_conn(db_name,local=True) # create_tb(conn) cur = conn.cursor() for i in cmd_list: try: cur.execute(i) conn.commit() except Exception as e: logger.log(e) conn.rollback() conn.commit() # conn.close() db_name='qdm225205669_db' conn2=get_mysql_conn(db_name,local=False) # create_tb(conn2) cur2=conn2.cursor() for i in cmd_list: try: cur2.execute(i) conn2.commit() except Exception as e: print(e) conn2.rollback() conn2.commit() conn2.close() conn.close()