Skip to content

Instantly share code, notes, and snippets.

@elja
Created December 1, 2021 16:28
Show Gist options
  • Save elja/a0716eda781a90dc0bc66f504246ab27 to your computer and use it in GitHub Desktop.
Save elja/a0716eda781a90dc0bc66f504246ab27 to your computer and use it in GitHub Desktop.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DonovanWall
//██████╗ ██╗ ██╗
//██╔══██╗██║ ██║
//██║ ██║██║ █╗ ██║
//██║ ██║██║███╗██║
//██████╔╝╚███╔███╔╝
//╚═════╝ ╚══╝╚══╝
//@version=4
study(title="Quadratic Regression Slope [DW]", shorttitle="QRS [DW]", overlay=false)
//by Donovan Wall
//This is a study geared toward identifying price trends using Quadratic regression.
//Quadratic regression is the process of finding the equation of a parabola that best fits the set of data being analyzed.
//In this study, first a quadratic regression curve is calculated, then the slope of the curve is calculated and plotted.
//Custom bar colors are included. The color scheme is based on whether the slope is positive or negative, and whether it is increasing or decreasing.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Updates:
// -> Migrated to v4.
// -> Updated QRS function for improved performance.
// -> Added an offset parameter to the script, which will base current value on offset values of the extrapolated QR curve.
// -> Included some experimental signal processing procedures:
// - Added an option to normalize the oscillator scale using RMS.
// - Added an optional compression algorithm to the script. This can be used to reduce transients (short duration, high amplitude values) by a specified level of intensity.
// -> Updated color scheme.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Functions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Quadratic Regression Slope
QRS(y, t, o)=>
var x = 1
x := x + 1
b0 = sum(x, t)
b1 = sum(pow(x, 2), t)
b2 = sum(pow(x, 3), t)
b3 = sum(pow(x, 4), t)
c0 = sum(y, t)
c1 = sum(x*y, t)
c2 = sum(pow(x, 2)*y, t)
a0 = (((b1*b3 - pow(b2, 2))*(c0*b2 - c1*b1)) - ((b0*b2 - pow(b1, 2))*(c1*b3 - c2*b2)))/(((b1*b3 - pow(b2, 2))*(t*b2 - b0*b1)) - ((b0*b2 - pow(b1, 2))*((b0*b3 - b1*b2))))
a1 = ((c0*b2 - c1*b1) - (t*b2 - b0*b1)*a0)/(b0*b2 - pow(b1, 2))
a2 = (c0 - t*a0 - b0*a1)/b1
qr = a0 + a1*(x + o) + a2*pow((x + o), 2)
QRS = qr - (na(qr[1]) ? qr : nz(qr[1]))
QRS
//Highest High Function
hi(x, reset)=>
var hi = 0.0
hi := reset or (x > hi) ? x : hi
hi
//Cumulative Mean Function
c_mean(y)=>
var x = 0
x := x + 1
var y_sum = 0.0
y_sum := y_sum + y
c_mean = y_sum/x
c_mean
//RMS Function
RMS(x, t)=>
sqrt(sma(pow(x, 2), t))
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Source
src = input(defval=close, title="Source")
//Sampling Period
per = input(defval=89, minval=1, title="Sampling Period")
//Offset
off = input(defval=0, title="Offset")
//Smoothing Period
smper = input(defval=2, minval=1, title="Smoothing Period")
//Normalization Toggle
norm = input(defval=false, title="Normalize Output Scale")
//Signal Compression Inputs
use_comp = input(defval=false, title="Use Compression")
comp_intensity = input(defval=90.0, minval=0, maxval=100, title="Compression Intensity (%)")
comp_thresh_type = input(defval="Auto", options=["Auto", "Custom"], title="Compression Threshold Type")
comp_ht_custom = input(defval=0.0, minval=0, title="Custom High Compression Threshold")
comp_lt_custom = input(defval=0.0, maxval=0, title="Custom Low Compression Threshold")
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//QRS
qrs1 = QRS(src, per, off)
//RMS Normalization
rms = RMS(qrs1, per)
norm_qrs = qrs1 > 0 ? qrs1/rms : qrs1 < 0 ? -abs(qrs1)/rms : 0
//Oscillator Selection
qrs2 = norm ? norm_qrs : qrs1
//Dichotomized QRS Values
qrs_pos = qrs2 > 0 ? qrs2 : 0
qrs_neg = qrs2 < 0 ? qrs2 : 0
//Compressor Thresholds
hi_qrs = hi(qrs_pos, crossover(qrs2, 0))
lo_qrs = -hi(abs(qrs_neg), crossunder(qrs2, 0))
comp_ht = comp_thresh_type=="Auto" ? c_mean(hi_qrs) : comp_ht_custom
comp_lt = comp_thresh_type=="Auto" ? c_mean(lo_qrs) : comp_lt_custom
//Transient Reduction
qrs = use_comp ? (qrs2 > comp_ht ? qrs2 - comp_intensity*(qrs2 - comp_ht)/100 :
qrs2 < comp_lt ? qrs2 + comp_intensity*(comp_lt - qrs2)/100 : qrs2) : qrs2
//Colors
qcolor = (qrs > 0) and (qrs > qrs[1]) ? #05ffa6 : (qrs > 0) and (qrs <= qrs[1]) ? #00945f : (qrs < 0) and (qrs < qrs[1]) ? #ff0a70 : (qrs < 0) and (qrs >= qrs[1]) ? #990040 : #cccccc
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Outputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//QRS
plot(qrs, color=qcolor, style=plot.style_columns, title="QRS")
//Thresholds
plot(use_comp ? comp_ht : na, color=#cccccc, style=plot.style_circles, transp=80, title="High Compression Threshold")
plot(use_comp ? comp_lt : na, color=#cccccc, style=plot.style_circles, transp=80, title="Low Compression Threshold")
//Bar Color
barcolor(qcolor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment