本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / PineScriptMastery
// Last Updated: 3rd June, 2021
// @version=4
study("[2021] Session Volatility", overlay=true)
// Get user input
startDate = input(title="Start Date", defval=timestamp("01 Jan 1000 13:30 +0000"), type=input.time, tooltip="Date & time to begin analysis")
endDate = input(title="End Date", defval=timestamp("1 Jan 2099 19:30 +0000"), type=input.time, tooltip="Date & time to stop analysis")
timeSession = input(title="Time Session To Analyze", type=input.session, defval="0600-0915", tooltip="Time session to analyze volatility")
colorBG = input(title="Color Background?", type=input.bool, defval=true, tooltip="Change the background color based on whether the current bar falls within the given session?")
// Custom function to convert forex pips into whole numbers
atr = atr(14)
toWhole(number) =>
if syminfo.type == "forex" // This method only works on forex pairs
return = atr < 1.0 ? (number / syminfo.mintick) / 10 : number
return := atr >= 1.0 and atr < 100.0 and syminfo.currency == "JPY" ? return * 100 : return
else
number
// This function returns true if the current bar falls within the given time session (:1234567 is to include all weekdays)
inSession(sess) => na(time(timeframe.period, sess + ":1234567")) == false and time >= startDate and time <= endDate
bgcolor(inSession(timeSession) and colorBG ? color.rgb(0, 0, 255, 90) : na)
// Count how many bars are loaded onto the current chart
var barCount = 0
barCount := barCount + 1
// Check if a new session has begun
var withinSession = false
if inSession(timeSession) and not inSession(timeSession)[1]
withinSession := true
// Declare required variables for analysis
var recentMovement = 0.0
var totalMovement = 0.0
var sessionsCounted = 0
var MAXINT = 2147483647.0
var highestHigh = -1.0
var lowestLow = MAXINT
// Check if a session has ended
if not inSession(timeSession) and inSession(timeSession)[1]
withinSession := false
// Analyze movement
recentMovement := highestHigh - lowestLow
totalMovement := totalMovement + recentMovement
sessionsCounted := sessionsCounted + 1
// Reset stored high/low
highestHigh := -1.0
lowestLow := MAXINT
// Get highest high and lowest low of current session
if withinSession
if high > highestHigh
highestHigh := high
if low < lowestLow
lowestLow := low
// Calculate average pip movement over all analyzed sessions
averageMovement = totalMovement / sessionsCounted
// Plot volatility info
plot(withinSession ? highestHigh : na, color=color.rgb(0,255,0,100), style=plot.style_linebr, title="Highest High")
plot(withinSession ? lowestLow : na, color=color.rgb(255,0,0,100), style=plot.style_linebr, title="Lowest Low")
plot(not withinSession ? recentMovement : na, color=color.rgb(128,0,128,100), title="Recent Change")
plot(not withinSession ? recentMovement / averageMovement : na, color=color.rgb(255,165,0,100), title="Recent Change ÷ Average Change")
// Create an output table to display average pip change
var table resultTable = table.new(position.top_right, 3, 2, border_width = 3)
f_fillCell(_table, _column, _row, _value, _text) =>
_c_color = color.green
_transp = 80
_cellText = tostring(_value, "#.#####") + "\n" + _text
table.cell(_table, _column, _row, _cellText, bgcolor = color.new(_c_color, _transp), text_color = _c_color, width = 6)
// Only fill the cell data on the last bar on the chart (for optimization)
if barstate.islast
f_fillCell(resultTable, 0, 0, toWhole(averageMovement), syminfo.type == "forex" ? "Pip Change" : "Price Change")