Files
QuanTAlib/lib/errors/rse/rse.pine
T
Miha Kralj 24e86d762a Add documentation links for various volatility indicators and channels
- 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.
2026-02-18 11:55:48 -08:00

74 lines
3.5 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Relative Squared Error (RSE)", "RSE")
//@function Calculates Relative Squared Error between two sources using SMA for averaging
//@param source1 First series to compare (actual)
//@param source2 Second series to compare (predicted)
//@param period Lookback period for error averaging
//@returns RSE value averaged over the specified period using SMA
rse(series float source1, series float source2, simple int period) =>
if period <= 0
runtime.error("Period must be greater than 0")
int p = math.min(math.max(1, period), 4000)
var float sum_source1 = 0.0
var float[] buffer_source1 = array.new_float(p, na)
var int head_source1 = 0
var int valid_count_source1 = 0
float oldest_source1 = array.get(buffer_source1, head_source1)
if not na(oldest_source1)
sum_source1 := sum_source1 - oldest_source1
valid_count_source1 := valid_count_source1 - 1
if not na(source1)
sum_source1 := sum_source1 + source1
valid_count_source1 := valid_count_source1 + 1
array.set(buffer_source1, head_source1, source1)
head_source1 := (head_source1 + 1) % p
float mean_source1 = valid_count_source1 > 0 ? sum_source1 / valid_count_source1 : source1
float squared_error = math.pow(source1 - source2, 2)
float squared_baseline_error = math.pow(source1 - mean_source1, 2)
var float sum_squared_error = 0.0
var float[] buffer_squared_error = array.new_float(p, na)
var int head_squared_error = 0
var int valid_count_squared_error = 0
float oldest_squared_error = array.get(buffer_squared_error, head_squared_error)
if not na(oldest_squared_error)
sum_squared_error := sum_squared_error - oldest_squared_error
valid_count_squared_error := valid_count_squared_error - 1
if not na(squared_error)
sum_squared_error := sum_squared_error + squared_error
valid_count_squared_error := valid_count_squared_error + 1
array.set(buffer_squared_error, head_squared_error, squared_error)
head_squared_error := (head_squared_error + 1) % p
var float sum_baseline_error = 0.0
var float[] buffer_baseline_error = array.new_float(p, na)
var int head_baseline_error = 0
var int valid_count_baseline_error = 0
float oldest_baseline_error = array.get(buffer_baseline_error, head_baseline_error)
if not na(oldest_baseline_error)
sum_baseline_error := sum_baseline_error - oldest_baseline_error
valid_count_baseline_error := valid_count_baseline_error - 1
if not na(squared_baseline_error)
sum_baseline_error := sum_baseline_error + squared_baseline_error
valid_count_baseline_error := valid_count_baseline_error + 1
array.set(buffer_baseline_error, head_baseline_error, squared_baseline_error)
head_baseline_error := (head_baseline_error + 1) % p
float total_squared_error = valid_count_squared_error > 0 ? sum_squared_error : squared_error
float total_baseline_error = valid_count_baseline_error > 0 ? sum_baseline_error : squared_baseline_error
total_baseline_error != 0 ? total_squared_error / total_baseline_error : 1.0
// ---------- Main loop ----------
// Inputs
i_source1 = input.source(close, "Source")
i_period = input.int(100, "Period", minval=1)
i_source2 = ta.ema(i_source1, i_period)
// Calculation
error = rse(i_source1, i_source2, i_period)
// Plot
plot(error, "RSE", color=color.new(color.red, 60), linewidth=2, style = plot.style_area)
plot(i_source2, "EMA", color=color.yellow, linewidth=1, style = plot.style_line, force_overlay = true)