def setUp(self): data_file = get_data("path", "data_file") #获取数据文件 auth_user = get_data("server", "auth_user") #获取登录用户名 auth_password = get_data("server", "auth_password") #获取登录密码 self.url = get_data("server", "url_users") #获取url self.auth = (auth_user, auth_password) self.data_list = excel_to_list(data_file, "Sheet1")
def test_run(): stock_symbol = 'EME' # Read data df = get_data(['SPY', stock_symbol], '2016-01-01', '2016-10-24') # plot_data(df) # Compute global statistics for each stock # print(df.mean()) # print(df.median()) # print(df.std()) # Compute Bollinger Bands # 1. Compute rolling mean using a 20-day window rm_SPY = get_rolling_mean(df[stock_symbol], window=20) # 2. Compute rolling standard deviation rstd_SPY = get_rolling_std(df[stock_symbol], window=20) # 3. Compute upper and lower bands upper_band, lower_band = get_bollinger_bands(rm_SPY, rstd_SPY) # Plot raw SPY data, rolling mean and Bollinger Bands ax = df[stock_symbol].plot(title="Bollinger Bands", label=stock_symbol) rm_SPY.plot(label='Rolling mean', ax=ax) upper_band.plot(label='upper band', ax=ax) lower_band.plot(label='lower band', ax=ax) # Add axis labels and legend ax.set_xlabel("Date") ax.set_ylabel("Price") ax.legend(loc='upper left') plt.show()
def test_run2(): # Read data symbols = ['SPY', 'XOM', 'GOLD'] df = get_data(symbols, '2009-01-01', '2012-12-31') # Compute daily returns daily_returns = compute_daily_returns(df) # Scatterplot SPY vs XOM daily_returns.plot(kind='scatter', x='SPY', y='XOM') beta_XOM, alpha_XOM = np.polyfit(daily_returns['SPY'], daily_returns['XOM'], 1) plt.plot(daily_returns['SPY'], beta_XOM * daily_returns['SPY'] + alpha_XOM, '-', color='r') plt.show() # Scatterplot SPY vs XOM daily_returns.plot(kind='scatter', x='SPY', y='GOLD') beta_XOM, alpha_XOM = np.polyfit(daily_returns['SPY'], daily_returns['GOLD'], 1) plt.plot(daily_returns['SPY'], beta_XOM * daily_returns['SPY'] + alpha_XOM, '-', color='r') plt.show()
def test_run(): # Read data symbols = ['SPY', 'XOM'] df = get_data(symbols, '2009-01-01', '2012-12-31') plot_data(df) # Compute daily returns daily_returns = compute_daily_returns(df) plot_data(daily_returns, title="Daily Returns") # Plot a histogram for symbol in symbols: daily_returns[symbol].hist(bins=20, label=symbol) # Get mean and standard deviation for symbol in symbols: mean = daily_returns[symbol].mean() print("{}: mean={}".format(symbol, mean)) std = daily_returns[symbol].std() print("{}: std={}".format(symbol, std)) # Compute kurtosis print(daily_returns.kurtosis()) # Add mean and std to graph # plt.axvline(mean, color='w', linestyle='dashed', linewidth=2) # plt.axvline(mean + std, color='r', linestyle='dashed', linewidth=2) # plt.axvline(mean - std, color='r', linestyle='dashed', linewidth=2) plt.legend(loc='upper right') plt.show()
def send_email(): #获取收件人、发件人、密码、标题、服务器、文件路径信息 toAddr = get_data('mail', "to_addr") fromAddr = get_data('mail', "from_addr") password = get_data('mail', "from_password") title = get_data('mail', "title") smtpserver = get_data('mail', "server") html_dir = get_data('mail', 'html_dir') #读取HTML文件内容 f = open(html_dir, "rb") mail_boby = f.read() f.close() #邮件内容格式编码 message = MIMEMultipart() txt = MIMEText(mail_boby, 'html', 'utf-8') message.attach(txt) message['From'] = (u'tester<%s>' % fromAddr) message['To'] = (u'other<%s>' % toAddr) message['Subject'] = Header(u'自动化测试<%s>' % title, 'utf-8') #添加HTML附件 part = MIMEApplication(open(html_dir, 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename="report.html") message.attach(part) try: server = smtplib.SMTP() server.connect(smtpserver, 25) #server.set_debuglevel(1) server.login(fromAddr, password) server.sendmail(fromAddr, toAddr, message.as_string()) print("发送邮件成功") server.quit() except smtplib.SMTPException: print("发送邮件失败")
poi_messages if all_messages == 'NaN': all_messages = 0 else: all_messages try: fraction = poi_messages / all_messages except ZeroDivisionError: fraction = 0 return fraction data_dict = get_data() submit_dict = {} for name in data_dict: data_point = data_dict[name] from_poi_to_this_person = data_point["from_poi_to_this_person"] to_messages = data_point["to_messages"] fraction_from_poi = compute_fraction(from_poi_to_this_person, to_messages) print(fraction_from_poi) data_point["fraction_from_poi"] = fraction_from_poi from_this_person_to_poi = data_point["from_this_person_to_poi"] from_messages = data_point["from_messages"] fraction_to_poi = compute_fraction(from_this_person_to_poi, from_messages) print(fraction_to_poi)
#!/usr/bin/env python #-*- coding:utf-8 -*- import unittest import HTMLTestRunner from lib.get_data import get_data import os from lib.send_email import send_email from tester.user.test_add import * from tester.user.test_add_group import * if __name__ == '__main__': # suite = unittest.TestSuite() # suite.addTests(unittest.TestLoader().loadTestsFromName('tester.user.test_add.TestAdd')) # suite.addTests(unittest.TestLoader().loadTestsFromName('tester.user.test_add_group.TestAddGroup')) base_path = os.path.dirname(__file__) testdir = os.path.join(base_path + "\\tester\\user\\") discover = unittest.defaultTestLoader.discover(testdir, "test*.py") #生成测试报告 report_path = get_data("path", "report_file") fp = open(report_path, "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'Api Test', description=u'用例执行情况', tester=u'fishyu') runner.run(discover) #runn.run(suite) fp.close() send_email() #将结果用邮件发送
return standard_deviations def compute_sharpe_ratios(df, trading_days=252, daily_risk_free_rate=0): average_daily_returns = compute_average_daily_returns(df) standard_deviations = compute_standard_deviations_of_daily_returns(df) sharpe_ratios = {} for symbol in average_daily_returns.keys(): sharpe_ratios[symbol] = math.sqrt(trading_days) * ((average_daily_returns[symbol] - daily_risk_free_rate) / standard_deviations[symbol]) return sharpe_ratios if __name__ == '__main__': symbols = ['SPY', 'GOOG', 'AAPL', 'XOM'] start_date = '2015-01-01' end_date = '2015-12-31' df = get_data(symbols, start_date, end_date) print("DAILY RETURNS") print(compute_daily_returns(df)) print("CUMULATIVE RETURNS") print(compute_cumulative_returns(df)) print("AVERAGE DAILY RETURNS") print(compute_average_daily_returns(df)) print("STANDARD DEVIATIONS OF DAILY RETURNS") print(compute_standard_deviations_of_daily_returns(df)) print("SHARPE RATIOS") print(compute_sharpe_ratios(df))