def read(self, name): """ 功能:读取pickle name:文件名,str 返回:数据 """ data = pickle.load( StringIO(read_file('%s%s.pkl' % (self.__path, name)))) return data
def send_image_qqmail(subject, message, image_list=[], file_root='./', file_ext='png'): """ 发送html含image格式QQ邮件 subject:邮件主题 message:邮件正文 image_list:图片列表 file_root:图片所在目录 file_ext:图片扩展名 """ #发送的邮箱 sender = 'DGCIdea<*****@*****.**>' #要接受的邮箱 receiver = '*****@*****.**' #smtp服务器 smtpserver = 'smtp.qq.com' #邮箱账号 username = '******' #邮箱授权码。一个16位字符串 password = '******' #smtp server = smtplib.SMTP_SSL('smtp.qq.com') #配置 msgRoot = MIMEMultipart('related') #邮件主题 msgRoot['Subject'] = subject #发送和接收都为自己 msgRoot['to'] = receiver msgRoot['from'] = sender #邮件文本信息 #中文需参数‘utf-8',单字节字符不需要str(message),纯文本为参数'plain',html为参数'html' msgText = MIMEText(message, 'html', 'utf-8') msgRoot.attach(msgText) if len(image_list) != 0: #邮件图像信息 for name in image_list: file_name = ('%s%s.%s') % (file_root, name, file_ext) image_id = ('<%s>') % (name) image_data = read_file(file_name) msgImage = MIMEImage(image_data) msgImage.add_header('Content-ID', image_id) msgRoot.attach(msgImage) try: #ssl(弃用) #server.connect() #登陆 server.login(username, password) #发送 server.sendmail(sender, receiver, msgRoot.as_string()) except Exception as e: raise e #结束 server.quit()
def get_wg_info(file_path=FILE_PATH, file_name=FILE_NAME): # 打开excel文件 content = read_file('%s%s' % (file_path, file_name)) xlr_f = xlrd.open_workbook(file_contents=content) # 获取第一个表 table = xlr_f.sheet_by_index(0) # 获取标的列表 etf_list = [ str(item)[:6] for item in table.col_values(2)[4:] if item is not u'' ] # 标的信息有序字典 etf_dict = {} etf_dict = collections.OrderedDict() # 遍历所有工作表 for i, code in zip(range(1, len(etf_list) + 2), etf_list): table = xlr_f.sheet_by_index(i) # 获取标的的网格价格、买入数量、卖出数量 price_list = [ round(item, 3) for item in table.col_values(2)[6:] if item is not u'' ] buy_list = [ int(item) for item in table.col_values(4)[6:] if item is not u'' ] sell_list = [ int(item) for item in table.col_values(5)[6:] if item is not u'' ] # 保存到字典 etf_dict.update({ code: { 'name': table.name, 'value': { 'price': price_list, 'buy': buy_list, 'sell': sell_list } } }) return etf_dict
def get_value_index(): #指数关注列表 index_list=[ 'HSCEI',#恒生国企, 'HSI',#恒生指数 'SPX',#恒生指数 '000902.SH',#全市场 '000300.SH',#沪深300 '000905.SH',#中证500 '399006.sz',#创业板指 '399005.sz',#中小板指 '000991.SH',#全指医药 '000992.SH',#全指金融 '000990.SH',#全指消费 '000993.SH',#全指信息 '399812.sz',#中证养老 '000807.SH',#食品饮料 '000922.SH',#中证红利 '399967.sz',#中证军工 '399989.sz',#中证医疗 '000827.SH',#中证环保 '399971.sz',#中证传媒 '399986.sz',#中证银行 '399975.sz',#全指证券 '000015.SH',#上证红利 ] #读取指数分析表 df=pd.read_csv(StringIO(read_file(FILE_PATH+'idx_value.csv')),index_col=0) df=df[df['aid']==10] #按照关注列表筛选 df=df[df.index.isin(index_list)] #全市场估值 code='000902.SH' #当前估值、百分位、区间 #区间为极低的前5个指数名称 info=pd.DataFrame( data=[ ['#估值','','','','',''], ['全市场','PE:%s'%(df.ix[code,'pe_e']),'%s%%'%(df.ix[code,'pe_e_ratio']),#,df.ix[code,'pe_e_state'] 'PB:%s'%(df.ix[code,'pb_e']),'%s%%'%(df.ix[code,'pb_e_ratio'])],#,df.ix[code,'pb_e_state'] ['关注']+df[df['pe_e_state']=='极低']['name'][0:5].tolist()], columns=range(6)) #返回信息 return info
def read(self, name, cols=None, parse_dates=False, encoding=None): """ 功能:读取数据库表 name:表名,str,如:‘idx_000300’ cols:字段名,list,如:[‘close’,'open'] parse_dates:是否解析日期,bool encoding:编码格式,str 返回:数据表,dataframe """ # 使用cols时,默认不包括index列,所以必须加上index列 cols = None if cols is None else [0] + cols # 策略中必须使用StringIO+read_file方法 df = pd.read_csv(StringIO(read_file('%s%s.csv' % (self.__path, name))), usecols=cols, index_col=0, parse_dates=parse_dates, encoding=encoding) # 返回数据表 return df
def __connection(self): """ 功能:连接数据库 参数:无 返回:数据库链接 """ if self.__in_research: # 研究中,直接连接数据库 connect = create_engine('sqlite:///%s%s.db' % (self.__path, self.__name)) else: # 策略中,生成一个副本数据库到策略的默认目录,生成副本数据库的连接 # 数据库文件 data_file = '%s%s.db' % (self.__path, self.__name) # 读取数据库到内存 data = read_file(data_file) # 副本文件 temp_file = '%s_temp.db' % (self.__name) # 写内存数据到策略默认目录中 with open(temp_file, 'wb') as f: f.write(data) # 生成数据库副本连接 connect = create_engine('sqlite:///%s' % temp_file) return connect