mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 04:58:08 +00:00
SIMD Refactor: Merge simd-dev into dev (#55)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat> Co-authored-by: Warp <agent@warp.dev>
This commit is contained in:
co-authored by
Claude Opus 4.5
aider
Warp
parent
5bcdf8d614
commit
86fe32a682
@@ -0,0 +1,56 @@
|
||||
// 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)
|
||||
//@doc https://github.com/mihakralj/pinescript/blob/main/indicators/volume/twap.md
|
||||
//@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)
|
||||
Reference in New Issue
Block a user