# encoding: utf-8 import decimal import requests import logging import logging.config import random import os import yaml import time import threading import re import datetime import json from collections import deque class TianShu(object): def __init__(self): self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36', 'Connection': 'close'} self.gx12358 = [] def get_stock_code(self,a_type): """ 获取两市股票代码 :param a_type: sh or sz :return: stock_code """ #url = 'http://stock.gtimg.cn/data/index.php?appn=rank&t=rankash/chr&p=1&o=0&l=40&v=list_data' url = 'http://stock.gtimg.cn/data/index.php' params = { 'appn': 'rank', 't': 'ranka{}/chr'.format(a_type), 'p': 1, 'o': 0, 'l': 3000, 'v': 'list_data' } logging.info('url:%s \t params:%s', url, params) res = requests.get(url, params=params, headers=self.headers) res_str = res.content.decode('unicode_escape').rstrip() #logging.info('response:%s:',res_str) res_str_list = res_str[res_str.index("data:'") + 6:res_str.index("'}")].split(',') logging.info(res_str_list) return res_str_list def get_stock_daily_data(self,stock_code): """ 获取日线数据 :param stock_code: 代码 :return: k线数据 """ #url = 'http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_dayqfq¶m=sh601857,day,,,320,qfq&r=0.44412021827221704' url = 'http://web.ifzq.gtimg.cn/appstock/app/fqkline/get' params = { '_var':'kline_dayqfq', 'param': '{},day,,,320,qfq'.format(stock_code), 'r': '0.1700474{}'.format("".join(random.choice("0123456789") for i in range(10))) } #logging.info('url:%s \t params:%s',url,params) res = requests.get(url,params=params,headers=self.headers) res_str = res.content.decode('utf-8') #logging.info('response:%s:',res_str) res_dict = eval(res_str.split('=')[1]) #logging.info('res_dict:%s:', res_dict) daily_data = [] if 'qfqday' in res_dict['data'][stock_code]: daily_data = list(res_dict['data'][stock_code]['qfqday']) elif 'day' in res_dict['data'][stock_code]: daily_data = list(res_dict['data'][stock_code]['day']) else : pass #logging.info(daily_data) # if self.is_tianshu(daily_data): # self.gx12358.append(stock_code) return daily_data def is_tianshu(self,stock_code,daily_data,days=39,zhenghu=26,mdays=180): if len(daily_data) <= 180: logging.info('%s数据太少,跳过。。。'%stock_code) return #320天收盘价列表 ls = [decimal.Decimal(x[2]) for x in daily_data] #days天内收盘价列表,默认39日 lns = ls[-days:] #mdays天内收盘价列表,默认180日 lms = ls[-mdays:] #days天内最低收盘价,默认39日最低收盘价 min_n_s = min(lns) #days天内最高收盘价,默认39日最高收盘价 max_n_s = max(lns) #days天内 最高收-最低收 a1 = max_n_s - min_n_s #days天内 (最高收-最低收)/ 最低收 * 100 a2= a1/min_n_s * decimal.Decimal(100) #mdays收盘价均线,默认180日均线 mam = sum(lms)/len(lms) #默认 (最高收-最低收)/ 最低收 * 100 < 26 , 39日内最低收盘价 > 180日收盘价平均值 ,收盘价>4 ,当前价格在39日内最低收盘价+-1% if a2 < decimal.Decimal(zhenghu) and min_n_s > mam > 4 and min_n_s * decimal.Decimal(0.99) < ls[-1] <min_n_s * decimal.Decimal(1.01): logging.info('%s符合天枢结构'%stock_code) self.gx12358.append(stock_code) if __name__ == '__main__': path = 'logging.yaml' value = os.getenv('LOG_CFG', None) if value: path = value if os.path.exists(path): with open(path, "r") as f: config = yaml.load(f) logging.config.dictConfig(config) else: print('log config file not found!') ts = TianShu() for i in ts.get_stock_code('sh') + ts.get_stock_code('sz'): ts.is_tianshu(i,ts.get_stock_daily_data(i)) logging.info(ts.gx12358)