本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © et20tradeview
//@version=6
indicator("Session Vector [Gimmigela]", overlay = false)
// --- Inputs ---
open_time = input.string("15", "Opening Period", options = ["15", "30", "60"], tooltip="Opening period in minutes")
sma_length = input.int(180, "Volume SMA Length")
session_length = input.int(540, "Session Minutes", tooltip="Rest-of-day session minutes")
show_workone = input.bool(true, title="Show Work Done")
show_cosineProjection = input.bool(true, title="Show Cosine Similarty Projection")
string session_string = str.tostring(session_length)
// --- Ensuring we get the FIRST bar of the session ---
get_first_period_data() =>
is_new_day = ta.change(time("D")) != 0
first_c = ta.valuewhen(is_new_day, close, 0)
first_v = ta.valuewhen(is_new_day, volume, 0)
[first_c, first_v]
get_cum_vol() =>
is_new_day = ta.change(time("D")) != 0
var float cum_v = 0.0
cum_v := is_new_day ? volume : cum_v + volume
cum_v
// --- Data Extraction ---
is_daily = timeframe.isdaily // note: script only plots on daily timeframe
yclose = close[1] // Yesterday's Close
[p15, v15] = request.security(syminfo.tickerid, open_time, get_first_period_data(), lookahead=barmerge.lookahead_on) // get price and volume, live updat
[pd, vc] = request.security(syminfo.tickerid, session_string, get_first_period_data(), lookahead=barmerge.lookahead_on) // get price and volume, live update
float eps = 1e-6
//==============================================
// I. Work Done Vector
//==============================================
// --- 1. Calculate the SMA of volume e.g. Cboe total ---
v_sma = ta.sma(vc, sma_length)
// --- 2. Calculate Gap % ---
gap = (open - yclose) / yclose * 100
gap15 = (p15 - yclose) / yclose * 100
//3. --- Intrabar move % ---
move = (close - open) / open * 100
move15 = (close - p15) / p15 * 100
//4. --- Gap + Intrabar move ---
combined = gap + move
combined15 = gap15 + move15
//5. --- Normalization Logic ---
//__a opening session e.g. 15min
//__b rest-of-day session
//__c full day session
// Price Displacement as % Change
float d_a = not na(p15) ? ((p15 - yclose) / yclose) * 100 : ((close - yclose) / yclose) * 100
float d_b = not na(p15) ? ((close - p15) / p15) * 100 : 0.0
float d_c = (close - close[1]) / close[1] * 100
// Volume normalization
float v15_capped = math.min(not na(v15) ? v15 : vc, v_sma * 3.0) //capped at 3x ma of volume
float vc_capped = math.min(not na(vc) ? vc : eps, v_sma * 3.0) //capped at 3x ma of volume
float v_a_norm = v15_capped / v_sma
float v_b_norm = (not na(v15) ? (vc_capped - v15_capped) : 0.0) / v_sma
float v_c_norm = vc_capped / v_sma
// Volume as Ratio to SMA
float price_rms = math.sqrt(ta.sma(math.pow(d_a, 2), sma_length))
float vol_rms = math.sqrt(ta.sma(math.pow(v_a_norm, 2), sma_length))
float vol_scale = price_rms / (vol_rms + eps)
//Volume Scaling
float v_a_scaled = v_a_norm * vol_scale
float v_b_scaled = v_b_norm * vol_scale
float v_c_scaled = v_c_norm * vol_scale
// Flipper: Checks rest-of-da up/down direction, parameter name inspired by Warne's flipper
float flipper = math.sign(d_b)
// Vector Order [Displacement, Volume]
float mass_1 = math.abs(d_a) * v_a_scaled
float mass_2 = math.abs(d_b) * v_b_scaled
float mass_d = d_c * v_c_scaled
float work_val = 0.0
if is_daily
if math.sign(d_a) == math.sign(d_b)
// Additive for Follow-through
work_val := math.abs(mass_1 + mass_2)* flipper
else
// Subtractive for Reversals
work_val := (math.abs(mass_1 - mass_2) * flipper) + mass_d
// Work done Score
float work_done_score = work_val
//==============================================
// II. Cosine Similarity
//==============================================
// 1. --- Vector Math ---
//Assign direction to volume so price and volume vector have the same sign
float v_a_dir = v_a_scaled *math.sign(d_a)
float v_b_dir = v_b_scaled * math.sign(d_b)
float v_c_dir = v_c_scaled * math.sign(d_c)
// 2. Magnitudes (Hypotenuse of the Move)
float mag_a = math.sqrt(math.pow(d_a, 2) + math.pow( v_a_dir, 2)) //opening session
float mag_b = math.sqrt(math.pow(d_b, 2) + math.pow(v_b_dir , 2)) // rest-of-day session
float mag_c = math.sqrt(math.pow(d_c, 2) + math.pow(v_c_dir , 2)) // full day session
// 3. Dot Product
float dot_product = (d_a * d_b) + (v_a_dir * v_b_dir)
// 4. Cosine Similarity (The Alignment Score)
float cos_theta = dot_product / ((mag_a * mag_b) + eps)
// 5. Assign Projection score
float proj = 0
if is_daily
if math.sign(d_a) == 1.0 and math.sign(d_b) ==1.0
// Additive for positive Follow-through
proj := cos_theta * mag_b
else if math.sign(d_a) == -1.0 and math.sign(d_b) == -1.0
// Additive for negative Follow-through
proj := cos_theta * mag_b * -1
else if math.sign(d_a) == -1.0 and math.sign(d_b) == 1.0
if math.sign(d_c) == 1.0
//open down close higher
proj := mag_c + (cos_theta * mag_b) *-1
else
//open down close higher but below previous close
proj := -mag_c + (cos_theta * mag_b)*-1
else if math.sign(d_a) == 1.0 and math.sign(d_b) == -1.0
if math.sign(d_c) == 1.0
// open up, close down but close above previous close
proj := mag_c + (cos_theta * mag_b)
else
// open up, close down, close below previous close
proj := -mag_c + (cos_theta * mag_b)
//==============================================
// Visualization
//==============================================
plot(show_workone ? work_done_score : na, "Work", color = work_done_score > 0 ? color.aqua : color.orange, style=plot.style_columns)
plot(show_cosineProjection ? proj : na, "Projection", color=color.red, style=plot.style_stepline_diamond)
// Zero line
hline(0, "Zero", color = color.gray)