mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-05 12:37:43 +00:00
24e86d762a
- Updated BBWN, BBWP, CCV, CV, CVI, EWMA, GKV, HLV, HV, Jvolty, JVOLTYN, MASSI, NATR, RSV, RV, RVI, TR, UI, VOV, VR, YZV indicators with documentation links. - Added documentation links for Aberration, Acceleration Bands, Andrews' Pitchfork, Adaptive Price Zone, ATR Bands, Bollinger Bands, Center of Gravity, Donchian Channels, Decay Min-Max Channel, Detrended Synthetic Price, EACP, EBSW, HOMOD, Jurik Volatility Bands, Keltner Channel, MA Envelope, Min-Max Channel, Price Channel, Regression Channels, Standard Deviation Channel, Stoller Average Range Channel, Super Trend Bands, Ultimate Bands, Ultimate Channel, VWAP Bands, and VWAP with Standard Deviation Bands.
56 lines
1.9 KiB
Plaintext
56 lines
1.9 KiB
Plaintext
// The MIT License (MIT)
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Time Weighted Average Price (TWAP)", "TWAP", overlay=true)
|
|
|
|
//@function Calculates session-based TWAP (Time Weighted Average Price)
|
|
//@param src Source price series (typically ohlc4 or hlc3)
|
|
//@param reset_condition Condition to reset TWAP calculation
|
|
//@returns TWAP value representing simple average price from session start
|
|
//@optimized for performance and dirty data
|
|
twap(series float src, series bool reset_condition) =>
|
|
var float sum_prices = 0.0
|
|
var int count = 0
|
|
float current_price = nz(src)
|
|
if reset_condition
|
|
sum_prices := 0.0
|
|
count := 0
|
|
sum_prices += current_price
|
|
count += 1
|
|
count > 0 ? sum_prices / count : src
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_source = input.source(ohlc4, "Source")
|
|
i_session_type = input.string("1D", "Session Reset", options=["1m", "2m", "3m", "5m", "10m", "15m", "30m", "45m", "1H", "2H", "3H", "4H", "1D", "1W", "1M", "3M", "6M", "12M", "Never"])
|
|
|
|
// Calculate reset condition
|
|
reset_condition = switch i_session_type
|
|
"1m" => ta.change(time("1")) != 0
|
|
"2m" => ta.change(time("2")) != 0
|
|
"3m" => ta.change(time("3")) != 0
|
|
"5m" => ta.change(time("5")) != 0
|
|
"10m" => ta.change(time("10")) != 0
|
|
"15m" => ta.change(time("15")) != 0
|
|
"30m" => ta.change(time("30")) != 0
|
|
"45m" => ta.change(time("45")) != 0
|
|
"1H" => ta.change(time("60")) != 0
|
|
"2H" => ta.change(time("120")) != 0
|
|
"3H" => ta.change(time("180")) != 0
|
|
"4H" => ta.change(time("240")) != 0
|
|
"1D" => ta.change(time("1D")) != 0
|
|
"1W" => ta.change(time("1W")) != 0
|
|
"1M" => ta.change(time("1M")) != 0
|
|
"3M" => ta.change(time("3M")) != 0
|
|
"6M" => ta.change(time("6M")) != 0
|
|
"12M" => ta.change(time("12M")) != 0
|
|
"Never" => bar_index == 0
|
|
=> false
|
|
|
|
// Calculation
|
|
twap_value = twap(i_source, reset_condition)
|
|
|
|
// Plot
|
|
plot(twap_value, "TWAP", color=color.yellow, linewidth=2)
|