import math import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons from mysql.MysqlHelper import * from pylab import mpl import datetime from datetime import timedelta, date mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体 mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 #### now = datetime.datetime.now() print(now) db = MysqlHelper() sql = "select * from tan_stock_record where sid='002415' and date='2018-07-18'" stock_info = db.get_all(sql) price = [] money = [] x = [] xi = 0 price0 = 0 money0 = 0 for one_stock in stock_info: if xi == 0: price0 = one_stock[3] money0 = one_stock[8] - one_stock[9] + one_stock[10] - one_stock[11] #第一个点没有资金进入这个点就不要了 if money0 == 0: continue else: price.append(0)
def stock_info(wn, sn, dayn): #now = datetime.datetime.now() - timedelta(days=4) now = datetime.datetime.now() this_week_start = now - timedelta(days=now.weekday()) day1 = this_week_start.strftime('%Y-%m-%d') day2 = (this_week_start + timedelta(days=1)).strftime('%Y-%m-%d') day3 = (this_week_start + timedelta(days=2)).strftime('%Y-%m-%d') day4 = (this_week_start + timedelta(days=3)).strftime('%Y-%m-%d') day5 = (this_week_start + timedelta(days=4)).strftime('%Y-%m-%d') weekday_arr = [0, day1, day2, day3, day4, day5] db = MysqlHelper() all_stock_sql = "select * from tan_stock where is_big=2" all_stock = db.get_all(all_stock_sql) stockn = int((wn - 1) * 10 + sn) sid = all_stock[stockn - 1][1] sname = all_stock[stockn - 1][2] sql = "select * from tan_stock_record where sid='%s' and date='%s'" % ( sid, weekday_arr[dayn]) stock_info = db.get_all(sql) if len(stock_info) == 0: #return price, money, x, sname, sid, pzdf_max, pzdf_min, money_max, money_min, next_pzdf_min, next_pzdf_max, next_price return [], [], [], sname, sid, 0, 0, 0, 0, 0, 0, [], (0, 0), (0, 0) price = [] money = [] x = [] xi = 0 price_min = 9999999 price_max = -9999999 money_min = 9999999 money_max = -99999999 price_all_half_slope = (0, 0) money_all_half_slope = (0, 0) # 只为了得出最大值和最小值 pre = 0 for one_stock in stock_info: # 占多少格 pre = one_stock[7] temp_price = one_stock[3] temp_money = (one_stock[9] - one_stock[10] + one_stock[11] - one_stock[12]) / 10000 if temp_price > price_max: price_max = temp_price if temp_price < price_min: price_min = temp_price if temp_money > money_max: money_max = temp_money if temp_money < money_min: money_min = temp_money pzdf_min = round((price_min - pre) / pre * 100, 1) pzdf_max = round((price_max - pre) / pre * 100, 1) #计算斜率 for one_stock in stock_info: xi += 1 x.append(xi) # 占多少格 temp_price = one_stock[3] temp_money = (one_stock[9] - one_stock[10] + one_stock[11] - one_stock[12]) / 10000 price.append(realPoint(price_min, price_max, temp_price)) money.append(realPoint(money_min, money_max, temp_money)) #获取第二天股价的最高涨幅和最低涨幅 if dayn == 5: next_pzdf_min, next_pzdf_max = 0, 0 next_price = [] else: sql = "select * from tan_stock_record where sid='%s' and date='%s'" % ( sid, weekday_arr[dayn + 1]) stock_info = db.get_all(sql) price_max = -9999999 price_min = 9999999 # 只为了得出最大值和最小值 pre = 0 for one_stock in stock_info: # 占多少格 pre = one_stock[7] temp_price = one_stock[3] if temp_price > price_max: price_max = temp_price if temp_price < price_min: price_min = temp_price #100等分 next_price = [] for one_stock in stock_info: # 占多少格 next_price.append(realPoint(price_min, price_max, one_stock[3])) next_pzdf_min = round((price_min - pre) / pre * 100, 1) next_pzdf_max = round((price_max - pre) / pre * 100, 1) #price,money是计算好的斜率 money_all_half_slope = all_half_slope(x, money) price_all_half_slope = all_half_slope(x, price) return price, money, x, sname, sid, pzdf_max, pzdf_min, money_max, money_min, next_pzdf_min, next_pzdf_max, next_price, money_all_half_slope, price_all_half_slope
class StockHelper(): #获取股票的时间点-价格 def __init__(self): self.mysql_helper = MysqlHelper() self.headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', # 'Cookie':'spversion=20130314; historystock=000760%7C*%7C300383; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1528699762,1528875973,1528878574,1528942207; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1528948403; v=Ag0soJAFas3Nd87hRUPKBs8vHCKD6kG8yx6lkE-SSaQTRiNUFzpRjFtutWLc', # 'Referer':'http://stockpage.10jqka.com.cn/HQ_v4.html' } self.user_agent_list = [ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)", "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)", "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)", "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)", "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)", "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0", "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20", "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52", ] def getStockPrciePoints(self,link,handler_obj={}): time_now = int(time.time()) cookie = "spversion=20130314; historystock=000760%7C*%7C300383; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1528699762,1528875973,1528878574,1528942207; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1="+str(time_now)+"; v=Ag0soJAFas3Nd87hRUPKBs8vHCKD6kG8yx6lkE-SSaQTRiNUFzpRjFtutWLc" self.headers['Cookie'] = cookie # ua = random.choice(self.user_agent_list) # if ua: # self.headers["User-Agent"] = ua try: request = urllib.request.Request(link, headers=self.headers) httpproxy_handler = urllib.request.ProxyHandler(handler_obj) #socket.setdefaulttimeout(10) opener = urllib.request.build_opener(httpproxy_handler) response = opener.open(request) content = response.read().decode() return content except Exception as e: print(e) return None def getStockPrciePoints1(self,link,handler_obj={}): time_now = int(time.time()) #cookie = "spversion=20130314; historystock=000760%7C*%7C300383; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1528699762,1528875973,1528878574,1528942207; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1="+str(time_now)+"; v=Ag0soJAFas3Nd87hRUPKBs8vHCKD6kG8yx6lkE-SSaQTRiNUFzpRjFtutWLc" self.headers['Cookie'] = 'searchGuide=sg; spversion=20130314; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1533263275,1533432205,1533469822,1533472700; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1533472700; historystock=002815%7C*%7C002415%7C*%7C002563%7C*%7C600130%7C*%7C000801; v=AiUE6KiNkkJ8fvYeZV9tagXINOpcYtmcIxa9SicK4dxrPksc77LpxLNmzQW0' self.headers['Referer'] = 'http://stockpage.10jqka.com.cn/realHead_v2.html' self.headers['Host'] = 'd.10jqka.com.cn' # ua = random.choice(self.user_agent_list) # if ua: # self.headers["User-Agent"] = ua try: request = urllib.request.Request(link, headers=self.headers) httpproxy_handler = urllib.request.ProxyHandler(handler_obj) #socket.setdefaulttimeout(10) opener = urllib.request.build_opener(httpproxy_handler) response = opener.open(request) content = response.read().decode() return content except Exception as e: print(e) return None def getStockPrciePoints2(self,link,handler_obj={}): time_now = int(time.time()) #cookie = "spversion=20130314; historystock=000760%7C*%7C300383; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1528699762,1528875973,1528878574,1528942207; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1="+str(time_now)+"; v=Ag0soJAFas3Nd87hRUPKBs8vHCKD6kG8yx6lkE-SSaQTRiNUFzpRjFtutWLc" self.headers['Cookie'] = 'searchGuide=sg; spversion=20130314; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1533263275,1533432205,1533469822,1533472700; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1533472700; historystock=002815%7C*%7C002415%7C*%7C002563%7C*%7C600130%7C*%7C000801; v=Au3MQLBlSir0li5GbUqlUr1w_IJkSiM1q3-Fty_yLUzyYwP095ox7DvOlaW8' self.headers['Referer'] = 'http://stockpage.10jqka.com.cn/002815/' # ua = random.choice(self.user_agent_list) # if ua: # self.headers["User-Agent"] = ua try: request = urllib.request.Request(link, headers=self.headers) httpproxy_handler = urllib.request.ProxyHandler(handler_obj) #socket.setdefaulttimeout(10) opener = urllib.request.build_opener(httpproxy_handler) response = opener.open(request) content = response.read().decode() return content except Exception as e: print(e) return None def getStockPrciePoints3(self,link,handler_obj={}): time_now = int(time.time()) #cookie = "spversion=20130314; historystock=000760%7C*%7C300383; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1528699762,1528875973,1528878574,1528942207; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1="+str(time_now)+"; v=Ag0soJAFas3Nd87hRUPKBs8vHCKD6kG8yx6lkE-SSaQTRiNUFzpRjFtutWLc" self.headers['Cookie'] = 'searchGuide=sg; spversion=20130314; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1533263275,1533432205,1533469822,1533472700; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1533472700; historystock=002815%7C*%7C002415%7C*%7C002563%7C*%7C600130%7C*%7C000801; v=AkdmUmbj0LiK6FQ0-mBvYDOe1vASTB_MdSGftRk0Y8P9Q2nmIRyrfoXwL-Mq' self.headers['Referer'] = 'http://stockpage.10jqka.com.cn/HQ_v4.html' self.headers['Host'] = 'd.10jqka.com.cn' # ua = random.choice(self.user_agent_list) # if ua: # self.headers["User-Agent"] = ua try: request = urllib.request.Request(link, headers=self.headers) httpproxy_handler = urllib.request.ProxyHandler(handler_obj) #socket.setdefaulttimeout(10) opener = urllib.request.build_opener(httpproxy_handler) response = opener.open(request) content = response.read().decode() return content except Exception as e: print(e) return None #获取股票的时间-大中小单 def getStockMoneyPoints(self,link,handler_obj={}): ua = random.choice(self.user_agent_list) if ua: self.headers["User-Agent"] = ua today = datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: request = urllib.request.Request(link, headers=self.headers) httpproxy_handler = urllib.request.ProxyHandler(handler_obj) opener = urllib.request.build_opener(httpproxy_handler) response = opener.open(request) content = response.read().decode() return content except Exception as e: #print(e) return None #获取可用ip def getUsefulIp(self): now_time = int(time.time()) dif_now_time = int(time.time()) - 80 ip_sql = "select * from ip where last_time<%s order by success_num desc limit 30" % (dif_now_time) ip_list = self.mysql_helper.get_all(ip_sql) return ip_list #更新ip使用过 def updateIpUse(self,ip): now_time = int(time.time()) update_sql = 'update ip set last_time=%s where ip="%s"' % (now_time, ip) return self.mysql_helper.update(update_sql) # def getWatchStock(self): sql = "select * from tan_stock where is_watch=1" list = self.mysql_helper.get_all(sql) return list; def insertStockRecord(self,sql): return self.mysql_helper.insert(sql) #远程获取ip def randomIp(self,server_ip='127.0.0.1'): res = urllib.request.urlopen('http://%s:12345/api/proxy/?count=1&scheme=HTTP&anonymity=anonymous'%server_ip) html = res.read().decode() json_content = json.loads(html) ip = json_content['data']['detail'][0]['ip'] scheme = json_content['data']['detail'][0]['scheme'] port = json_content['data']['detail'][0]['port'] return ip,port,scheme def randomIpFromDb(self): sql = "select * from ip where rate>0.5" all_ip = self.mysql_helper.get_all(sql) handler_obj_list = [] for one_ip in all_ip: ip = one_ip[1] port = one_ip[2] scheme = one_ip[3] handler_obj = {} handler_obj[scheme] = ip + ':' + port handler_obj_list.append(handler_obj) return handler_obj_list #更新失败或者成功的ip def updateIp(self,ip,port,scheme,is_success=0): sql_exists = "select * from ip where ip='%s'" % (ip); success_num,fail_num = 0,0 if is_success: success_num += 1 else: fail_num += 1 ip_exists = self.mysql_helper.get_one(sql_exists) time_now = int(datetime.now().timestamp()) if ip_exists is None: sql = 'insert ignore into ip values(null,%(ip)s,%(port)s,%(scheme)s,%(success_num)s,%(fail_num)s,%(rate)s,%(last_time)s)' params = {'ip': ip, 'port': port, 'scheme': scheme, 'success_num': success_num, 'fail_num': fail_num,'rate':0,'last_time':time_now} # ip是否存在,存在更新,失败录入 print(params) self.mysql_helper.insert(sql, params) else: #4,5,6,7 old_success_num = ip_exists[4] + success_num old_fail_num = ip_exists[5] + fail_num rate = old_success_num/(old_fail_num + old_success_num) sql = 'update ip set success_num=%s,fail_num=%s,rate=%s,last_time=%s where ip="%s"' % (old_success_num,old_fail_num,rate,time_now,ip) self.mysql_helper.update(sql) print(sql)
import time