スクリーナーのボラティリティの計算方法は?

ボラティリティは、特定の期間における金融商品の価格変動を測定するものです。価格の変動幅が大きいほど、ボラティリティは高くなり、値幅の範囲が狭いほど、ボラティリティは低くなります。

以下は、当社が計算に使用しているボラティリティの計算式です(週、月、日):

//@version=4
study("volatility")

fastSearchN(xs, x) => // xs - sorted, ascending
    max_bars_back(xs, 366)
    left  = 0
    right = min(bar_index,366)
    mid = 0
    if xs < x
        0
    else
        for i = 0 to 9
            mid := ceil((left+right) / 2)
            if left == right
                break
            else if xs[mid] < x
                right := mid
                continue
            else if xs[mid] > x
                left := mid
                continue
            else
                break
        mid

month1 = 30
month_ago = timenow - 1000*60*60*24*month1
month_ago_this_bar = time - 1000*60*60*24*month1
countOfBars1MonthAgo = fastSearchN(time, month_ago)
countOfBars1MonthAgoThisBar = fastSearchN(time, month_ago_this_bar)

week1 = 7
week_ago = timenow - 1000*60*60*24*week1
week_ago_this_bar = time - 1000*60*60*24*week1
countOfBarsWeekAgo = fastSearchN(time, week_ago)
countOfBarsWeekAgoThisBar = fastSearchN(time, week_ago_this_bar)

// volatility
volatility(bb) =>
    bb2 = bb
    if bar_index == 0
        bb2 := 365
    if bb2 == 0
        na
    else
        s = sum((high-low)/abs(low) * 100 / bb2, bb2)
        if bb == 0
            na
        else
            s

plot(volatility(countOfBarsWeekAgoThisBar), title="Volatility.W")
plot(volatility(countOfBars1MonthAgoThisBar),title="Volatility.M")
plot(tr(true)*100/abs(low), title="Volatility.D")

注: このスクリプトではtimenowを使用しているため、ヒストリカルデータとリアルタイムデータでは計算値が異なります。こちらをご参照ください。https://www.tradingview.com/pine-script-docs/en/v4/essential/Indicator_repainting.html

このスクリプトをPineエディタで日足のチャートに追加すると、パフォーマンスのデータを視覚的に確認することができます。インジケーターがチャート上に表示され、そのプロットには各期間のボラティリティの値が表示されます。