tradingview-
QQQ5
/@version=5
indicator(" 历史波动率 (252天窗口)", overlay=false)
// 计算每日对数收益率,避免错误数据
log_return = na(close[1]) ? na : math.log(close / close[1])
// 计算历史波动率(252天窗口)
lookback = 252
hv = ta.stdev(log_return, lookback) * math.sqrt(252) * 100 // 年化波动率,转换为百分比
// 设定时间范围(2024年6月1日 - 2025年3月14日)
start_date = timestamp(2022, 7, 13, 00, 00)
end_date = timestamp(2025, 3, 20, 23, 59)
// 仅在指定时间范围内绘制波动率
hv_filtered = (time >= start_date and time <= end_date) ? hv : na
plot(hv_filtered, title="年化历史波动率 (%)", color=color.blue, linewidth=2)
// 在选定时间范围内添加背景颜色
bgcolor(time >= start_date and time <= end_date ? color.rgb(252, 228, 6) : na)
沒有留言:
張貼留言