# 比较时间一致, 初始资产一致, 最终收益一致 # 夏普比率的公式 # 夏普比率 = 每天收益率的均值 / 每天收益率的标准差 # # 这里省略了无风险利率 # 计算夏普比率 import pandas as pd def calculate_sharp(data): df = pd.DataFrame(data, columns=['date', 'liquidate']) df['rate_of_return'] = df['liquidate'].rolling(2).apply(lambda x: x[1]/x[0] - 1, raw=True) sharp = df['rate_of_return'].mean() / df['rate_of_return'].std() return sharp data1 = [['2016-07-04', 5000000.00], ['2016-07-05', 4835769.38], ['2016-07-06', 5036838.71], ['2016-07-07', 4881597.25], ['2016-07-08', 5099866.54], ['2016-07-11', 5158305.86], ['2016-07-12', 5379073.81], ['2016-07-13', 5174389.13], ['2016-07-14', 5235795.42], ['2016-07-15', 5050606.65], ['2016-07-18', 5153493.81], ['2016-07-19', 5410731.34], ['2016-07-20', 5233841.72], ['2016-07-21', 5245693.42], ['2016-07-22', 5089468.61], ['2016-07-25', 5368514.36], ['2016-07-26', 5640139.80], ['2016-07-27', 5285918.31], ['2016-07-28', 5669054.75], ['2016-07-29', 5954109.95], ['2016-08-01', 5363140.57], ['2016-08-02', 5463225.81], ['2016-08-03', 5535254.28], ['2016-08-04', 5580878.38], ['2016-08-05', 5583599.43], ['2016-08-08', 5532192.08], ['2016-08-09', 5651183.49], ['2016-08-10', 5668028.27], ['2016-08-11', 5595957.57], ['2016-08-12', 5602812.38], ['2016-08-15', 5668724.48], ['2016-08-16', 5617281.02], ['2016-08-17', 5715316.60], ['2016-08-18', 5770128.40], ['2016-08-19', 5732294.20], ['2016-08-22', 5751937.92], ['2016-08-23', 5874266.66], ['2016-08-24', 5853859.26], ['2016-08-25', 5880573.21], ['2016-08-26', 5890153.76], ['2016-08-29', 5894311.85], ['2016-08-30', 5852469.53], ['2016-08-31', 5834809.34], ['2016-09-01', 5937106.72], ['2016-09-02', 5848258.50], ['2016-09-05', 5835814.67], ['2016-09-06', 5919762.95], ['2016-09-07', 5945231.91], ['2016-09-08', 5996092.42]] data2 = [['2016-07-04', 5000000.00], ['2016-07-05', 5035769.38], ['2016-07-06', 5036838.71], ['2016-07-07', 5081597.25], ['2016-07-08', 5099866.54], ['2016-07-11', 5158305.86], ['2016-07-12', 5179073.81], ['2016-07-13', 5174389.13], ['2016-07-14', 5135795.42], ['2016-07-15', 5150606.65], ['2016-07-18', 5153493.81], ['2016-07-19', 5210731.34], ['2016-07-20', 5233841.72], ['2016-07-21', 5245693.42], ['2016-07-22', 5289468.61], ['2016-07-25', 5368514.36], ['2016-07-26', 5440139.80], ['2016-07-27', 5485918.31], ['2016-07-28', 5469054.75], ['2016-07-29', 5454109.95], ['2016-08-01', 5463140.57], ['2016-08-02', 5463225.81], ['2016-08-03', 5535254.28], ['2016-08-04', 5580878.38], ['2016-08-05', 5583599.43], ['2016-08-08', 5532192.08], ['2016-08-09', 5651183.49], ['2016-08-10', 5668028.27], ['2016-08-11', 5595957.57], ['2016-08-12', 5602812.38], ['2016-08-15', 5668724.48], ['2016-08-16', 5617281.02], ['2016-08-17', 5715316.60], ['2016-08-18', 5770128.40], ['2016-08-19', 5732294.20], ['2016-08-22', 5751937.92], ['2016-08-23', 5874266.66], ['2016-08-24', 5853859.26], ['2016-08-25', 5880573.21], ['2016-08-26', 5890153.76], ['2016-08-29', 5894311.85], ['2016-08-30', 5852469.53], ['2016-08-31', 5834809.34], ['2016-09-01', 5937106.72], ['2016-09-02', 5848258.50], ['2016-09-05', 5835814.67], ['2016-09-06', 5919762.95], ['2016-09-07', 5945231.91], ['2016-09-08', 5996092.42]] sharp1 = calculate_sharp(data1) sharp2 = calculate_sharp(data2) print(sharp1, sharp2)