pandas dataframe 计算收益率
所属分类 quant
浏览量 456
prices = pd.DataFrame({
"a": [100, 110, 108, 120],
"b": [800, 800, 750, 900],
"c": [200, 250, 275, 300]
})
prices["a_daily_return"] = prices["a"].pct_change()
prices["a_shift"] = prices["a"].shift(1)
prices["a_daily_return2"] = prices["a"] / prices["a_shift"] - 1
# 对数收益率
prices["a_daily_log_return"] = np.log(prices["a"] / prices["a_shift"] )
returns = prices[["a","b","c"]].pct_change()
returns.head()
returns.plot()
prices.head()
a b c a_shift a_daily_return a_daily_return2 a_daily_log_return
0 100 800 200 NaN NaN NaN NaN
1 110 800 250 100.0 0.100000 0.100000 0.095310
2 108 750 275 110.0 -0.018182 -0.018182 -0.018349
3 120 900 300 108.0 0.111111 0.111111 0.105361
returns.head()
a b c
0 NaN NaN NaN
1 0.100000 0.0000 0.250000
2 -0.018182 -0.0625 0.100000
3 0.111111 0.2000 0.090909
df['rise20'] = (df['close'] - df['close'].shift(20)) * 100 / (df['close'].shift(20))
df['chg_abs'] = abs(df['close'] - df['close'].shift(1))
df['chg_abs_sum_20'] = df['chg_abs'].rolling(window=20).sum()
df['rsx20'] = (df['close'] - df['close'].shift(20)) * 100 / df['chg_abs_sum_20']
上一篇
下一篇
唐奇安通道指标
pandas 计算 布林通道 和 唐奇通道
pandas 股票指数技术分析
java量化交易技术资料
开源授权协议
多个ETF行情数据join,协方差 相关系数计算