Gaussian Channel Strategy v2 - Copy (3)
Gaussian Channel Strategy v2 - Copy (3)
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Gaussian Channel Indicator - courtesy of @DonovanWall
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Note: Both the sampling period and number of poles directly affect how much lag
the indicator has, and how smooth the output is.
// Larger inputs will result in smoother outputs with increased lag, and
smaller inputs will have noisier outputs with reduced lag.
// For the best results, I recommend not setting the sampling period any lower
than the number of poles + 1. Going lower truncates the equation.
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Updates:
// Huge shoutout to @e2e4mfck for taking the time to improve the calculation
method!
// -> migrated to v4
// -> pi is now calculated using trig identities rather than being explicitly
defined.
// -> The filter calculations are now organized into functions rather than being
individually defined.
// -> Revamped color scheme.
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Functions - courtesy of @e2e4mfck
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Filter function
f_filt9x (_a, _s, _i) =>
int _m2 = 0, int _m3 = 0, int _m4 = 0, int _m5 = 0, int _m6 = 0,
int _m7 = 0, int _m8 = 0, int _m9 = 0, float _f = .0, _x = (1 - _a)
// Weights.
// Initial weight _m1 is a pole number and equal to _i
_m2 := _i == 9 ? 36 : _i == 8 ? 28 : _i == 7 ? 21 : _i == 6 ? 15 : _i == 5 ?
10 : _i == 4 ? 6 : _i == 3 ? 3 : _i == 2 ? 1 : 0
_m3 := _i == 9 ? 84 : _i == 8 ? 56 : _i == 7 ? 35 : _i == 6 ? 20 : _i == 5 ?
10 : _i == 4 ? 4 : _i == 3 ? 1 : 0
_m4 := _i == 9 ? 126 : _i == 8 ? 70 : _i == 7 ? 35 : _i == 6 ? 15 : _i == 5 ? 5
: _i == 4 ? 1 : 0
_m5 := _i == 9 ? 126 : _i == 8 ? 56 : _i == 7 ? 21 : _i == 6 ? 6 : _i == 5 ? 1
: 0
_m6 := _i == 9 ? 84 : _i == 8 ? 28 : _i == 7 ? 7 : _i == 6 ? 1 : 0
_m7 := _i == 9 ? 36 : _i == 8 ? 8 : _i == 7 ? 1 : 0
_m8 := _i == 9 ? 9 : _i == 8 ? 1 : 0
_m9 := _i == 9 ? 1 : 0
// filter
_f := math.pow(_a, _i) * nz(_s) +
_i * _x * nz(_f[1]) - (_i >= 2 ?
_m2 * math.pow(_x, 2) * nz(_f[2]) : 0) + (_i >= 3 ?
_m3 * math.pow(_x, 3) * nz(_f[3]) : 0) - (_i >= 4 ?
_m4 * math.pow(_x, 4) * nz(_f[4]) : 0) + (_i >= 5 ?
_m5 * math.pow(_x, 5) * nz(_f[5]) : 0) - (_i >= 6 ?
_m6 * math.pow(_x, 6) * nz(_f[6]) : 0) + (_i >= 7 ?
_m7 * math.pow(_x, 7) * nz(_f[7]) : 0) - (_i >= 8 ?
_m8 * math.pow(_x, 8) * nz(_f[8]) : 0) + (_i == 9 ?
_m9 * math.pow(_x, 9) * nz(_f[9]) : 0)
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Inputs
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Source
src = input(defval=hlc3, title="Source")
// Poles
int N = input.int(defval=4, title="Poles", minval=1, maxval=9)
// Period
int per = input.int(defval=144, title="Sampling Period", minval=2)
// Lag Reduction
bool modeLag = input.bool(defval=false, title="Reduced Lag Mode")
bool modeFast = input.bool(defval=false, title="Fast Response Mode")
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Definitions
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Lag
lag = (per - 1)/(2*N)
// Data
srcdata = modeLag ? src + (src - src[lag]) : src
trdata = modeLag ? ta.tr(true) + (ta.tr(true) - ta.tr(true)[lag]) : ta.tr(true)
// Filtered Values
[filtn, filt1] = f_pole(alpha, srcdata, N)
[filtntr, filt1tr] = f_pole(alpha, trdata, N)
// Lag Reduction
filt = modeFast ? (filtn + filt1)/2 : filtn
filttr = modeFast ? (filtntr + filt1tr)/2 : filtntr
// Bands
hband = filt + filttr*mult
lband = filt - filttr*mult
// Colors
color1 = #0aff68
color2 = #00752d
color3 = #ff0a5a
color4 = #990032
fcolor = filt > filt[1] ? #0aff68 : filt < filt[1] ? #ff0a5a : #cccccc
barcolor = (src > src[1]) and (src > filt) and (src < hband) ? #0aff68 : (src >
src[1]) and (src >= hband) ? #0aff1b : (src <= src[1]) and (src > filt) ? #00752d :
(src < src[1]) and (src < filt) and (src > lband) ? #ff0a5a : (src <
src[1]) and (src <= lband) ? #ff0a11 : (src >= src[1]) and (src < filt) ? #990032 :
#cccccc
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Outputs
//---------------------------------------------------------------------------------
--------------------------------------------------------------------------------
// Filter Plot
filtplot = plot(filt, title="Filter", color=fcolor, linewidth=3)
// Band Plots
hbandplot = plot(hband, title="Filtered True Range High Band", color=fcolor)
lbandplot = plot(lband, title="Filtered True Range Low Band", color=fcolor)
// Channel Fill
fill(hbandplot, lbandplot, title="Channel Fill", color=color.new(fcolor, 80))
// Bar Color
barcolor(barcolor)
// Trading Logic
longCondition = ta.crossover(close, hband) and timeCondition
closeAllCondition = ta.crossunder(close, hband) and timeCondition
if longCondition
strategy.entry("long", strategy.long)
if closeAllCondition
strategy.close("long")