Professional Documents
Culture Documents
Alma Trend Lines
Alma Trend Lines
//The regression channel in this study is modeled using the least squares
approach with four base average types to choose from:
// -> Arnaud Legoux Moving Average (ALMA)
// -> Exponential Moving Average (EMA)
// -> Simple Moving Average (SMA)
// -> Volume Weighted Moving Average (VWMA)
//When using VWMA, if no volume is present, the calculation will
automatically switch to tick volume, making it compatible with any
cryptocurrency, stock, currency pair, or index you want to analyze.
//There are two window types for calculation in this script as well:
// -> Continuous, which generates a regression model over a fixed number of
bars continuously.
// -> Interval, which generates a regression model that only moves its
starting point when a new interval starts. The number of bars for calculation
cumulatively increases until the end of the interval.
//To observe the path of the regression, I've included a tracer line, which
follows the current point of the regression line. This is also referred to as
a Least Squares Moving Average (LSMA).
//A custom bar color scheme based on channel direction and price proximity to
the current regression value is included.
//I don't necessarily recommend using this tool as a standalone, but rather
as a supplement to your analysis systems.
//Regression analysis is far from an exact science. However, with the right
combination of tools and strategies in place, it can greatly enhance your
analysis and trading.
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Updates:
// -> Script structure has been reorganized.
// -> Corrected NaN filter values in initial states.
// -> Added Rolling Moving Average and Linear Weighted Moving Average to the
list of available base averages.
// -> Added style customization for the channel lines. Because TV doesn't
have built-in style inputs for line objects, you'll find these settings in
the "Inputs" section. For each channel line, you can now adjust:
// - If the line is drawn - selected via the specified toggle switch
// - Width of the line - can be any integer greater than zero
// - Style of the line - available styles are solid, dotted, and dashed
// - Color of the line - uses my RGB color editor, which has 216 colors to
choose from via the specified R, G, and B inputs
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Functions
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//ALMA Function
ALMA(y, t)=>
m = (0.85*(t - 1))
s = t/6
wt = 0.0
wtsum = 0.0
cumwt = 0.0
for i = 0 to (t - 1)
wt := exp(-pow((i - m), 2)/(2*pow(s, 2)))
wtsum := wtsum + wt*nz(y[t - 1 - i], y)
cumwt := cumwt + wt
ALMA = wtsum/cumwt
ALMA
//EMA Function
EMA(y, t)=>
EMA = y
EMA := na(EMA[1]) ? y : (y - nz(EMA[1]))*(2/(t + 1)) + nz(EMA[1])
EMA
//SMA Function
SMA(y, t)=>
num_sum = 0.0
for i = 0 to (t - 1)
num_sum := num_sum + nz(y[i], y)
SMA = num_sum/t
SMA
//SMMA Function
RMA(y, t)=>
RMA = 0.0
RMA := na(RMA[1]) ? SMA(y, t) : (nz(RMA[1])*(t - 1) + y)/t
RMA
//LWMA Function
LWMA(y, t)=>
num_sum = 0.0
den_sum = 0.0
for i = 0 to (t - 1)
num_sum := num_sum + (t - i)*nz(y[i], y)
den_sum := den_sum + (t - i)
LWMA = num_sum/den_sum
LWMA
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Inputs
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Source
src = input(defval=close, title="Source")
//Filter Type
filt_type = input(defval="SMA", options=["ALMA", "EMA", "SMA", "RMA", "LWMA",
"VWMA"], title="Base Average Type")
//Window Type
w_type = input(defval="Continuous", options=["Continuous", "Interval"],
title="Window Type")
//Sampling Period
len = input(defval=200, minval=2, title="Sampling Length (for Continuous
Window Type)")
//Interval Size
res = input(defval="D", title="Interval Size (For Interval Window Type)")
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Definitions
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Interval Bars
var int inter_bars = 1
inter_bars := newint ? 1 : inter_bars + 1
//Standard Deviation
s_dev = st_dev(src, per, filt_type, ndev)
//Colors
pos_color = rgb_color(pos_line_R, pos_line_G, pos_line_B)
neg_color = rgb_color(neg_line_R, neg_line_G, neg_line_B)
hi_color = rgb_color(hi_line_R, hi_line_G, hi_line_B)
q3_color = rgb_color(q3_line_R, q3_line_G, q3_line_B)
q1_color = rgb_color(q1_line_R, q1_line_G, q1_line_B)
lo_color = rgb_color(lo_line_R, lo_line_G, lo_line_B)
reg_color = y_2 > y_1 ? pos_color : y_2 < y_1 ? neg_color :
color.new(#cccccc, 0)
bar_color = (src > y_2) and (y_2 <= y_1) ? #008b00 :
(src > y_2) and (y_2 > y_1) ? #00ff00 :
(src < y_2) and (y_2 >= y_1) ? #8b0000 :
(src < y_2) and (y_2 < y_1) ? #ff0000 : #cccccc
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Outputs
//---------------------------------------------------------------------------
-----------------------------------------------------------------------------
---------
//Regression Line
if show_reg_line
draw_line(y_1, y_2, per, reg_color, reg_ls, reg_lw, extend_lines)
//Channel Q3 Line
if show_q3_line
draw_line(y_1 + s_dev/2, y_2 + s_dev/2, per, q3_color, q3_ls, q3_lw,
extend_lines)
//Channel Q1 Line
if show_q1_line
draw_line(y_1 - s_dev/2, y_2 - s_dev/2, per, q1_color, q1_ls, q1_lw,
extend_lines)
//Bar Color
barcolor(bar_color)
if ph
add_to_array(tval, tpos, ph)
if pl
add_to_array(bval, bpos, pl)
// line definitions
maxline = 3
var bln = array.new_line(maxline, na)
var tln = array.new_line(maxline, na)
if valid
uv1 := hline - diff
uv2 := val2
up1 := lloc
up2 := pos2
break
dv1 = 0.0
dv2 = 0.0
dp1 = 0
dp2 = 0
if countlinehi <= maxline
for p2 = PPnum - 1 to p1 + 1
val1 = array.get(tval, p1)
val2 = array.get(tval, p2)
pos1 = array.get(tpos, p1)
pos2 = array.get(tpos, p2)
if val1 < val2
diff = (val2 - val1) / float(pos1 - pos2)
hline = val2 - diff
lloc = bar_index
lval = high
valid = true
for x = pos2 + 1 - prd to bar_index
if close[bar_index - x] > hline
valid := false
break
lloc := x
lval := hline
hline := hline - diff
if valid
dv1 := hline + diff
dv2 := val2
dp1 := lloc
dp2 := pos2
break
//---------------------------------------------------------------------------
---
// Regular Bullish
// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
plot(
plFound ? overlay_main ? low[lbR] : osc[lbR] : na,
offset=-lbR,
title="Regular Bullish",
linewidth=2,
color=(bullCond ? bullColor : noneColor),
transp=0
)
plotshape(
bullCond ? overlay_main ? low[lbR] : osc[lbR] : na,
offset=-lbR,
title="Regular Bullish Label",
text=" Bull ",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor,
transp=0
)
//---------------------------------------------------------------------------
---
// Hidden Bullish
// Osc: Lower Low
plot(
plFound ? overlay_main ? low[lbR] : osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish",
linewidth=2,
color=(hiddenBullCond ? hiddenBullColor : noneColor),
transp=0
)
plotshape(
hiddenBullCond ? overlay_main ? low[lbR] : osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish Label",
text=" H Bull ",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor,
transp=0
)
//---------------------------------------------------------------------------
---
// Regular Bearish
// Osc: Lower High
bearCond = plotBear and priceHH and oscLH and phFound and repaint
plot(
phFound ? overlay_main ? high[lbR] : osc[lbR] : na,
offset=-lbR,
title="Regular Bearish",
linewidth=2,
color=(bearCond ? bearColor : noneColor),
transp=0
)
plotshape(
bearCond ? overlay_main ? high[lbR] : osc[lbR] : na,
offset=-lbR,
title="Regular Bearish Label",
text=" Bear ",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor,
transp=0
)
//---------------------------------------------------------------------------
---
// Hidden Bearish
// Osc: Higher High
hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound and repaint
plot(
phFound ? overlay_main ? high[lbR] : osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish",
linewidth=2,
color=(hiddenBearCond ? hiddenBearColor : noneColor),
transp=0
)
plotshape(
hiddenBearCond ? overlay_main ? high[lbR] : osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish Label",
text=" H Bear ",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor,
transp=0
)