mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-07-27 17:27:43 +00:00
48 lines
2.2 KiB
Plaintext
48 lines
2.2 KiB
Plaintext
// Licensed under the Apache License, Version 2.0
|
|
// © mihakralj
|
|
//@version=6
|
|
indicator("Aberration (ABERR)", "ABERR", overlay=true)
|
|
|
|
//@function Calculates Aberration bands measuring deviation from a central moving average
|
|
//@param source Series to calculate aberration from
|
|
//@param ma_line Pre-calculated moving average line
|
|
//@param period Lookback period for deviation calculation
|
|
//@param multiplier Multiplier for deviation bands
|
|
//@returns [upper_band, lower_band, deviation] Aberration band values and deviation
|
|
//@optimized Uses simple deviation averaging with O(n) complexity
|
|
aberr(series float source, series float ma_line, simple int period, simple float multiplier) =>
|
|
if period <= 0 or multiplier <= 0.0
|
|
runtime.error("Period and multiplier must be greater than 0")
|
|
float deviation = math.abs(nz(source) - nz(ma_line))
|
|
float avg_deviation = ta.sma(deviation, period)
|
|
float upper_band = ma_line + multiplier * avg_deviation
|
|
float lower_band = ma_line - multiplier * avg_deviation
|
|
[upper_band, lower_band, avg_deviation]
|
|
|
|
// ---------- Main loop ----------
|
|
|
|
// Inputs
|
|
i_source = input.source(close, "Source")
|
|
i_period = input.int(20, "Period", minval=1)
|
|
i_ma_type = input.string("SMA", "Moving Average Type", options=["SMA", "EMA", "WMA", "RMA", "HMA"])
|
|
i_multiplier = input.float(2.0, "Deviation Multiplier", minval=0.1, step=0.1)
|
|
i_show_ma = input.bool(true, "Show Moving Average Line")
|
|
|
|
// Calculate the moving average based on selected type
|
|
ma_line = switch i_ma_type
|
|
"SMA" => ta.sma(i_source, i_period)
|
|
"EMA" => ta.ema(i_source, i_period)
|
|
"WMA" => ta.wma(i_source, i_period)
|
|
"RMA" => ta.rma(i_source, i_period)
|
|
"HMA" => ta.wma(2 * ta.wma(i_source, i_period / 2) - ta.wma(i_source, i_period), math.round(math.sqrt(i_period)))
|
|
=> ta.sma(i_source, i_period)
|
|
|
|
// Calculation
|
|
[upper_band, lower_band, deviation] = aberr(i_source, ma_line, i_period, i_multiplier)
|
|
|
|
// Plots
|
|
p_upper = plot(upper_band, "Upper Band", color=color.yellow, linewidth=2)
|
|
p_lower = plot(lower_band, "Lower Band", color=color.yellow, linewidth=2)
|
|
plot(i_show_ma ? ma_line : na, "MA", color=color.yellow, linewidth=2)
|
|
fill(p_upper, p_lower, color=color.new(color.blue, 90), title="Band Fill")
|