You are on page 1of 7

//Base source is cloned from built-in technicals - "Linear Regression Channel", v26

//@version=5
indicator("Dynamic Regression-EMA-PD level-Alpha trend", overlay=true,
max_lines_count=500, max_boxes_count=500)

upperMultInput = input.float(2.0, title="Upper Deviation", inline = "Upper


Deviation")
colorUpper = input.color(color.new(color.blue, 85), "", inline = "Upper Deviation")
lowerMultInput = input.float(2.0, title="Lower Deviation", inline = "Lower
Deviation")
colorLower = input.color(color.new(color.red, 85), "", inline = "Lower Deviation")

calcSlope(source, length) =>


max_bars_back(source, 5000)
if barstate.isfirst or length <= 1
[float(na), float(na), float(na)]
else
sumX = 0.0
sumY = 0.0
sumXSqr = 0.0
sumXY = 0.0
for i = 0 to length - 1 by 1
val = source[i]
per = i + 1.0
sumX += per
sumY += val
sumXSqr += per * per
sumXY += val * per
slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
average = sumY / length
intercept = average - slope * sumX / length + slope
[slope, average, intercept]

var start_index = 1
lengthInput = bar_index - start_index + 1
[s, a, i] = calcSlope(close, lengthInput)
startPrice = i + s * (lengthInput - 1)
endPrice = i

calcDev(source, length, slope, average, intercept) =>


if barstate.isfirst or length <= 1
[float(na), float(na), float(na), float(na)]
else
upDev = 0.0
dnDev = 0.0
stdDevAcc = 0.0
dsxx = 0.0
dsyy = 0.0
dsxy = 0.0
periods = length - 1
daY = intercept + slope * periods / 2
val = intercept
for j = 0 to periods by 1
price = high[j] - val
if price > upDev
upDev := price
price := val - low[j]
if price > dnDev
dnDev := price
price := source[j]
dxt = price - average
dyt = val - daY
price -= val
stdDevAcc += price * price
dsxx += dxt * dxt
dsyy += dyt * dyt
dsxy += dxt * dyt
val += slope
stdDev = math.sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / math.sqrt(dsxx * dsyy)
[stdDev, pearsonR, upDev, dnDev]

[stdDev, pearsonR, upDev, dnDev] = calcDev(close, lengthInput, s, a, i)


upperStartPrice = startPrice + upperMultInput * stdDev
upperEndPrice = endPrice + upperMultInput * stdDev
lowerStartPrice = startPrice - lowerMultInput * stdDev
lowerEndPrice = endPrice - lowerMultInput * stdDev

var baseLine = line.new(na, na, na, na, width=1, color=color.new(colorLower, 0))


var upper = line.new(na, na, na, na, width=1, color=color.new(colorUpper, 0))
var lower = line.new(na, na, na, na, width=1, color=color.new(colorUpper, 0))
linefill.new(upper, baseLine, color = colorUpper)
linefill.new(baseLine, lower, color = colorLower)

if (close > upperEndPrice or close < lowerEndPrice) and (not barstate.islast or


barstate.isconfirmed)
_baseLine = line.new(bar_index - lengthInput + 1, startPrice[1], bar_index - 1,
endPrice[1], width=1, color=color.new(colorLower, 0))
_upper = line.new(bar_index - lengthInput + 1, upperStartPrice[1], bar_index -
1, upperEndPrice[1], width=1, color=color.new(colorUpper, 0))
_lower = line.new(bar_index - lengthInput + 1, lowerStartPrice[1], bar_index -
1, lowerEndPrice[1], width=1, color=color.new(colorUpper, 0))
linefill.new(_upper, _baseLine, color = colorUpper)
linefill.new(_baseLine, _lower, color = colorLower)
start_index := bar_index
else if barstate.islast
j = close > upperEndPrice or close < lowerEndPrice ? 1: 0
line.set_xy1(baseLine, bar_index - lengthInput + 1, startPrice[j])
line.set_xy2(baseLine, bar_index - j, endPrice[j])
line.set_xy1(upper, bar_index - lengthInput + 1, upperStartPrice[j])
line.set_xy2(upper, bar_index - j, upperEndPrice[j])
line.set_xy1(lower, bar_index - lengthInput + 1, lowerStartPrice[j])
line.set_xy2(lower, bar_index - j, lowerEndPrice[j])

//Multiple EMA
//indicator("Multiple EMA", overlay = true)

Lengthema1 = input.int(title = "EMA1", defval = 10, minval = 1, maxval = 500)


Lengthema2 = input.int(title = "EMA2", defval = 20, minval = 1, maxval = 500)
Lengthema3 = input.int(title = "EMA3", defval = 61, minval = 1, maxval = 500)
Lengthema4 = input.int(title = "EMA4", defval = 89, minval = 1, maxval = 500)
Lengthema5 = input.int(title = "EMA5", defval = 200, minval = 1, maxval = 500)
/// EMA INDICATOR
EMA1 = ta.ema(close,Lengthema1)
EMA2 = ta.ema(close,Lengthema2)
EMA3 = ta.ema(close,Lengthema3)
EMA4 = ta.ema(close,Lengthema4)
EMA5 = ta.ema(close,Lengthema5)

/// PLOT

plot(EMA1, color = color.new(color.green,0))


plot(EMA2, color = color.new(color.red,0))
plot(EMA3, color = color.new(color.blue,0))
plot(EMA4, color = color.new(color.yellow,0))
plot(EMA5, color = color.new(color.purple,0))

//plot(close)

// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
//@version=5
//indicator("Litt Institutional Levels", overlay = true)

instructions = input.bool(title='Instructions', defval=true, inline='1', tooltip =


"The Litt Institutional Levels Indicator plots previous timeperiods Highs, Lows,
and Closes. Institutions track these levels and will use them as support and
resistance to enter and exit
their positions. Not every algorithm is a machine-learning wizard.
Institutions still use these relatively simple levels to conduct their trades. The
best way to use The Litt Institutional Levels Indicator is to find overlapping
levels. These areas or lines are
called confluence levels and will act as a stronger level than a single
line. \n\nFor the labeling, Y stands for Yesterday and L stands for Last. For
example, LMC would equal Last Months Close. LQH, would equal Last Quarters High.
YL, would equal Yeserdays Low.")

daily_levels = input.bool(true, title = "Daily Levels", group = "Daily Levels")


daily_line_color = input.color(color.new(color.silver, 20), title = "Line Color",
group = "Daily Levels", inline = '2')
daily_text_color = input.color(color.new(color.white, 20), title = "Label Text
Color", group = "Daily Levels", inline = '2')

weekly_levels = input.bool(true, title = "Weekly Levels", group = "Weekly Levels")


weekly_line_color = input.color(color.new(color.yellow, 20), title = "Line Color",
group = "Weekly Levels", inline = '2')
weekly_text_color = input.color(color.new(color.yellow, 20), title = "Label Text
Color", group = "Weekly Levels", inline = '2')

monthly_levels = input.bool(true, title = "Monthly Levels", group = "Monthly


Levels")
monthly_line_color = input.color(color.new(color.blue, 20), title = "Line Color",
group = "Monthly Levels", inline = '2')
monthly_text_color = input.color(color.new(color.blue, 20), title = "Label Text
Color", group = "Monthly Levels", inline = '2')

qtr_levels = input.bool(true, title = "Quarterly Levels", group = "Quarterly


Levels")
qtr_line_color = input.color(color.new(color.red, 20), title = "Line Color", group
= "Quarterly Levels", inline = '2')
qtr_text_color = input.color(color.new(color.red, 20), title = "Label Text Color",
group = "Quarterly Levels", inline = '2')

yearly_levels = input.bool(true, title = "Yearly Levels", group = "Yearly Levels")


yearly_line_color = input.color(color.new(color.orange, 20), title = "Line Color",
group = "Yearly Levels", inline = '2')
yearly_text_color = input.color(color.new(color.orange, 20), title = "Label Text
Color", group = "Yearly Levels", inline = '2')

timeChange(period) =>
ta.change(time(period))

institution_function(time_period, line_color, text_color, text_text) =>

is_new_period = timeChange(time_period)

var first_bar_in_period = 0
first_bar_in_period := is_new_period ? time : first_bar_in_period[1]

highs = request.security(syminfo.tickerid,time_period, high[1],


lookahead=barmerge.lookahead_on)
lows = request.security(syminfo.tickerid,time_period, low[1],
lookahead=barmerge.lookahead_on)
closes = request.security(syminfo.tickerid,time_period, close[1],
lookahead=barmerge.lookahead_on)

high_line = line.new(first_bar_in_period, highs, time, y2 = highs, xloc =


xloc.bar_time, extend = extend.right, color = line_color, style =
line.style_dotted, width = 2)
line.delete(high_line[1])

low_line = line.new(first_bar_in_period, lows, time, y2 = lows, xloc =


xloc.bar_time, extend = extend.right, color = line_color, style =
line.style_dotted, width = 2)
line.delete(low_line[1])

close_line = line.new(first_bar_in_period, closes, time, y2 = closes, xloc =


xloc.bar_time, extend = extend.right, color = line_color, style =
line.style_dotted, width = 2)
line.delete(close_line[1])

high_label = label.new(x = bar_index + 10, y = highs, text = text_text + "H",


style = label.style_none, textcolor = text_color)
label.delete(high_label[1])

low_label = label.new(x = bar_index + 10, y = lows, text = text_text + "L",


style = label.style_none, textcolor = text_color)
label.delete(low_label[1])

close_label = label.new(x = bar_index + 10, y = closes, text = text_text + "C",


style = label.style_none, textcolor = text_color)
label.delete(close_label[1])

if daily_levels
institution_function("D",daily_line_color, daily_text_color, "Y")

if weekly_levels
institution_function("W",weekly_line_color, weekly_text_color, "LW")

if monthly_levels
institution_function("M",monthly_line_color, monthly_text_color, "LM")

if qtr_levels
institution_function("3M",qtr_line_color, qtr_text_color, "LQ")

if yearly_levels
institution_function("12M",yearly_line_color, yearly_text_color, "LY")

//TABLE
show_dashboard = input.bool(title='Color Legend', defval=true, inline='1',
group='Dashboard Settings')
LabelSize = input.string(defval='Medium', options=['Small', 'Medium', 'Large'],
title='Dashboard Size', inline='2', group='Dashboard Settings')
label_size = LabelSize == 'Small' ? size.small : LabelSize == 'Medium' ?
size.normal : LabelSize == 'Large' ? size.large : size.small
positioning = position.top_right
var table t = table.new(positioning, 5, 1,frame_color=color.new(#000000, 100),
frame_width=0, border_color=color.new(#000000,100), border_width=0)
if barstate.islast and show_dashboard

//Column 1
table.cell(t, 0, 0, text='D', width=0, bgcolor=daily_line_color,
text_color=color.white, text_size=label_size, text_halign=text.align_center)
table.cell(t, 1, 0, text='W', width=0, bgcolor=weekly_line_color,
text_color=color.white, text_size=label_size, text_halign=text.align_center)
table.cell(t, 2, 0, text='M', width=0, bgcolor=monthly_line_color,
text_color=color.white, text_size=label_size, text_halign=text.align_center)
table.cell(t, 3, 0, text='Q', width=0, bgcolor=qtr_line_color,
text_color=color.white, text_size=label_size, text_halign=text.align_center)
table.cell(t, 4, 0, text='Y', width=0, bgcolor=yearly_line_color,
text_color=color.white, text_size=label_size, text_halign=text.align_center)

// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// author © KivancOzbilgic
// developer © KivancOzbilgic
//@version=5
//indicator('AlphaTrend', shorttitle='AT', overlay=true, format=format.price,
precision=2, timeframe='')
coeff = input.float(1, 'Multiplier', step=0.1)
AP = input(14, 'Common Period')
ATR = ta.sma(ta.tr, AP)
src = input(close)
showsignalsk = input(title='Show Signals?', defval=true)
novolumedata = input(title='Change calculation (no volume data)?', defval=false)
upT = low - ATR * coeff
downT = high + ATR * coeff
AlphaTrend = 0.0
AlphaTrend := (novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT
< nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ?
nz(AlphaTrend[1]) : downT

color1 = AlphaTrend > AlphaTrend[2] ? #00E60F : AlphaTrend < AlphaTrend[2] ?


#80000B : AlphaTrend[1] > AlphaTrend[3] ? #00E60F : #80000B
k1 = plot(AlphaTrend, color=color.new(#0022FC, 0), linewidth=3)
k2 = plot(AlphaTrend[2], color=color.new(#FC0400, 0), linewidth=3)

fill(k1, k2, color=color1)

buySignalk = ta.crossover(AlphaTrend, AlphaTrend[2])


sellSignalk = ta.crossunder(AlphaTrend, AlphaTrend[2])

K1 = ta.barssince(buySignalk)
K2 = ta.barssince(sellSignalk)
O1 = ta.barssince(buySignalk[1])
O2 = ta.barssince(sellSignalk[1])

plotshape(buySignalk and showsignalsk and O1 > K2 ? AlphaTrend[2] * 0.9999 : na,


title='BUY', text='BUY', location=location.absolute, style=shape.labelup,
size=size.tiny, color=color.new(#0022FC, 0), textcolor=color.new(color.white, 0))

plotshape(sellSignalk and showsignalsk and O2 > K1 ? AlphaTrend[2] * 1.0001 : na,


title='SELL', text='SELL', location=location.absolute, style=shape.labeldown,
size=size.tiny, color=color.new(color.maroon, 0), textcolor=color.new(color.white,
0))

alertcondition(buySignalk and O1 > K2, title='Potential BUY Alarm', message='BUY


SIGNAL!')
alertcondition(sellSignalk and O2 > K1, title='Potential SELL Alarm', message='SELL
SIGNAL!')

alertcondition(buySignalk[1] and O1[1] > K2, title='Confirmed BUY Alarm',


message='BUY SIGNAL APPROVED!')
alertcondition(sellSignalk[1] and O2[1] > K1, title='Confirmed SELL Alarm',
message='SELL SIGNAL APPROVED!')

alertcondition(ta.cross(close, AlphaTrend), title='Price Cross Alert',


message='Price - AlphaTrend Crossing!')
alertcondition(ta.crossover(low, AlphaTrend), title='Candle CrossOver Alarm',
message='LAST BAR is ABOVE ALPHATREND')
alertcondition(ta.crossunder(high, AlphaTrend), title='Candle CrossUnder Alarm',
message='LAST BAR is BELOW ALPHATREND!')

alertcondition(ta.cross(close[1], AlphaTrend[1]), title='Price Cross Alert After


Bar Close', message='Price - AlphaTrend Crossing!')
alertcondition(ta.crossover(low[1], AlphaTrend[1]), title='Candle CrossOver Alarm
After Bar Close', message='LAST BAR is ABOVE ALPHATREND!')
alertcondition(ta.crossunder(high[1], AlphaTrend[1]), title='Candle CrossUnder
Alarm After Bar Close', message='LAST BAR is BELOW ALPHATREND!')

You might also like