def favorite(): code = request.values.get('code') action = request.values.get('action') if action == 'cancel': #dbpool.executeUpdate(['update __relation set FLAG=0 where CODE=%s'% code]) db.session.query(RELATION).filter(RELATION.CODE == code).update( {RELATION.FLAG: 0 - RELATION.FLAG}) db.session.commit() return "<script>alert('取消收藏成功!');window.close();</script>" elif action == 'add': #RELATION.filter_by(CODE=code).update({RELATION.FLAG: '1'}) r = RELATION.query.filter_by(CODE=code).first() if r == None: r = RELATION(CODE=code, FLAG=1, REMARK='', FAVORITE_TIME=datetime.now().strftime("%Y-%m-%d")) db.session.add(r) db.session.commit() else: dbpool.executeUpdate([ 'update __relation set FLAG=abs(FLAG)+1 where CODE=%s' % code ]) return "<script>alert('加入收藏夹成功!');window.close();</script>" return None
def generate_lists(): #stocks = {} sqls = [] url = "http://quote.eastmoney.com/stocklist.html" headers = { 'Accept': '*/*', 'Referer': 'http://www.huawa.com/orders', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36' } req = urllib.request.Request(url, headers=headers) try: with urllib.request.urlopen(req) as resp: content = resp.read().decode('gb18030') #print(content.decode('gb18030')) soup = BeautifulSoup(content, 'html.parser') div_1 = soup.find('div', class_='quotebody') for html in div_1.find_all('li', recursive=True): o = html.find('a').get_text().strip() if o[-7:-5] == '00' or o[-7:-5] == '60': #只取上证60和深证00 #stocks[o[-7:-1]] = o[:-8] sqls.append( "insert into LISTS(CODE,NAME)VALUES('%s','%s')" % (o[-7:-1], o[:-8])) dbpool.executeUpdate(sqls) except urllib.error.URLError as e: logger.exception(">>获取股票清单失败>>") if hasattr(e, 'reason'): logger.exception(e.reason)
def __generate_MA_5_10_20(code, day, *args): try: ma5 = 0 ma10 = 0 ma20 = 0 sql = "select TCLOSE,DAY from DAY_DATAS where CODE=%s and DAY <= '%s' order by DAY desc limit %d" % ( code, day, 20) df = pd.read_sql_query(sql, engine) series = df["TCLOSE"] if len(series) >= 5: ma5 = series[0:5].sum() / 5 if len(series) >= 10: ma10 = series[0:10].sum() / 10 if len(series) == 20: ma20 = series[0:20].sum() / 20 dbpool.executeUpdate([ "update DAY_DATAS set MA5=%.2f,MA10=%.2f,MA20=%.2f where CODE=%s and DAY='%s'" % (ma5, ma10, ma20, code, day) ]) #print("update DAY_DATAS set MA5=%.2f,MA10=%.2f,MA20=%.2f where CODE=%s and DAY='%s'" % (ma5,ma10,ma20,code,day)) except Exception as e: logger.exception("生成股票[%s]MA参数时出错 >> " % (code)) logger.exception(e) finally: if args and isinstance(args[0], ThreadPool): #如果第一个参数是线程池,则执行添加新线程操作 args[0].add_thread()
def __collect_Two(code, name, sday, eday, *args): data_sqls = [] url = "http://q.stock.sohu.com/hisHq?code=cn_%s&start=%s&end=%s&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp" % ( code, sday, eday) headers = { 'Accept': '*/*', 'Referer': 'http://www.huawa.com/orders', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36' } req = urllib.request.Request(url, headers=headers) try: with urllib.request.urlopen(req, timeout=5) as resp: content = resp.read().decode('gb18030') i = content.find('[[') if i < 0: return #当天没有数据 ii = content.find(']]') s = content[i + 2:ii].replace('"', '').replace('%', '') ls = s.split(",") data_sqls.append( "insert into DAY_DATAS(CODE,NAME,TOPEN,HIGH,LOW,TCLOSE,LCLOSE,CHG,PCHG,VOTURNOVER,TURNOVER,VATURNOVER,TCAP,MCAP,DAY,CREATE_TIME) VALUES" "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',now())" % (code, name, ls[1], ls[6], ls[5], ls[2], 0, ls[3], ls[4], int(ls[7]) * 100, ls[9], float(ls[8]) * 10000, 0, 0, ls[0])) dbpool.executeUpdate(data_sqls) except Exception as e: logger.exception("采集股票[%s-%s]交易数据时出错 >> " % (code, name)) logger.exception(e) __failed_one(code, name) finally: if args and isinstance(args[0], ThreadPool): #如果第一个参数是线程池,则执行添加新线程操作 args[0].add_thread()
def cal_CHANGEHAND(day): try: dbpool.executeUpdate([ "update DAY_DATAS set CHANGEHAND = VOTURNOVER/(MCAP/TCLOSE)*100 where DAY='%s'" % (day) ]) except Exception as e: logger.exception("计算换手率出错 >> ")
def set_remark(): remark = request.values.get('remark') code = request.values.get('code') dbpool.executeUpdate([ "update __RELATION set REMARK = '%s' where CODE='%s'" % (remark, code) ]) return redirect(url_for('favorite_list'))
def __collect_One(code, name, sday, eday, *args): data_sqls = [] if code[0:2] == '00': _code = "1" + code if code[0:2] == '60': _code = "0" + code if code[0:2] == '68': _code = "0" + code if code[0:2] == '30': _code = "1" + code url = "http://quotes.money.163.com/service/chddata.html?code=%s&start=%s&end=%s&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP" % ( _code, sday, eday) headers = { 'Accept': '*/*', 'Referer': 'http://www.huawa.com/orders', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36' } req = urllib.request.Request(url, headers=headers) try: with urllib.request.urlopen(req, timeout=5) as resp: content = resp.read().decode('gb18030') data = content.split("\r\n") data.remove(data[0]) for i in data: if i == '': break x = i.split(",") if x[6] == '0.0': #开盘价为0.0,代表该股今日停牌 break data_sqls.append( "insert into DAY_DATAS(CODE,NAME,TOPEN,HIGH,LOW,TCLOSE,LCLOSE,CHG,PCHG,VOTURNOVER,TURNOVER,VATURNOVER,TCAP,MCAP,DAY,CREATE_TIME) VALUES" "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',now())" % (code, name, x[6], x[4], x[5], x[3], x[7], x[8], x[9], x[11], x[10], x[12], x[13], x[14], x[0])) dbpool.executeUpdate(data_sqls) except Exception as e: logger.exception("采集股票[%s-%s]交易数据时出错 >> " % (code, name)) #logger.exception(e) __failed_one(code, name) finally: if args and isinstance(args[0], ThreadPool): #如果第一个参数是线程池,则执行添加新线程操作 args[0].add_thread()
def clear_tmp_failed(): try: dbpool.executeUpdate(["truncate table TMP_FAILED"]) except Exception as e: logger.exception(e)
def clear_day_datas(day): try: dbpool.executeUpdate(["delete from DAY_DATAS where DAY='%s'" % day]) except Exception as e: logger.exception(e)
def clear_lists(): try: dbpool.executeUpdate(["truncate table LISTS"]) except Exception as e: logger.exception(e)
def __failed_one(code, name): dbpool.executeUpdate([ "insert into TMP_FAILED(F_CODE,F_NAME) VALUES ('%s','%s')" % (code, name) ])