# ETF定投成本摊薄 - R实现
nav <-c(1.00, 0.90, 0.80, 0.95, 1.10) # 各期净值
amt <-1000# 每期定投金额
shares <-sum(amt / nav)
total <-length(nav) * amt
avg_cost <- total / shares
cat("累计份额:", round(shares,2),
" 单位成本:", round(avg_cost,4), "\n")
cat("期末资产:", round(shares*nav[length(nav)],2),
" 收益:", round(shares*nav[length(nav)]-total,2), "\n")
# ETF定投成本摊薄 - Python实现
nav = [1.00, 0.90, 0.80, 0.95, 1.10]
amt = 1000
shares = sum(amt / n for n in nav)
total = len(nav) * amt
avg_cost = total / shares
print(f"累计份额: {shares:.2f} 单位成本: {avg_cost:.4f}")
print(f"期末资产: {shares*nav[-1]:.2f} 收益: {shares*nav[-1]-total:.2f}")
动量信号: M = R_(t-1M) − R_(t-12M)
若 M > 0 持有; 否则切换至货币基金/债券ETF
# 双ETF动量轮动 - R实现# priceA/priceB 为两只ETF净值序列
momentum <-function(p, win=20) p[length(p)] / p[length(p)-win] - 1
mA <-momentum(priceA); mB <-momentum(priceB)
if (mA > mB && mA > 0) hold <-"ETF_A"else if (mB > 0) hold <-"ETF_B"else hold <-"Cash"cat("本期持仓:", hold, "\n")
# 双ETF动量轮动 - Python实现defmomentum(p, win=20):
return p[-1] / p[-win] - 1
mA, mB = momentum(priceA), momentum(priceB)
if mA > mB and mA > 0: hold = "ETF_A"elif mB > 0: hold = "ETF_B"else: hold = "Cash"print("本期持仓:", hold)
# 等权大类资产配置 - R实现
w <-c(股ETF=0.5, 债ETF=0.3, 黄金ETF=0.2)
ret <-c(0.10, 0.04, 0.06)
vol <-c(0.18, 0.05, 0.15)
cat("组合预期收益:", round(sum(w*ret),4), "\n")
# 等权大类资产配置 - Python实现
w = {"股ETF":0.5, "债ETF":0.3, "黄金ETF":0.2}
ret = {"股ETF":0.10, "债ETF":0.04, "黄金ETF":0.06}
port_ret = sum(w[k]*ret[k] for k in w)
print(f"组合预期收益: {port_ret:.4f}")