3 wawes RS2021

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

//@version=5

indicator('Linear Regression Channel Screener', shorttitle = 'LRC', overlay = true,


max_bars_back = 1000, max_lines_count = 300, dynamic_requests = true)

//------------------------------------------------------------------------------
// Symbol Inputs
//-----------------------------------------------------------------------------{
sym1 = input.symbol('BINANCE:BTCUSDT', 'Symbol 1', group = 'Symbols')
sym2 = input.symbol('BINANCE:ETHUSDT', 'Symbol 2', group = 'Symbols')
sym3 = input.symbol('BINANCE:BNBUSDT', 'Symbol 3', group = 'Symbols')
sym4 = input.symbol('BINANCE:XRPUSDT', 'Symbol 4', group = 'Symbols')
sym5 = input.symbol('BINANCE:ADAUSDT', 'Symbol 5', group = 'Symbols')
sym6 = input.symbol('BINANCE:DOGEUSDT', 'Symbol 6', group = 'Symbols')
sym7 = input.symbol('BINANCE:LTCUSDT', 'Symbol 7', group = 'Symbols')
sym8 = input.symbol('BINANCE:SOLUSDT', 'Symbol 8', group = 'Symbols')
sym9 = input.symbol('BINANCE:DOTUSDT', 'Symbol 9', group = 'Symbols')
sym10 = input.symbol('BINANCE:LINKUSDT', 'Symbol 10', group = 'Symbols')

src = input(defval = close, title = 'Source')


len = input.int(defval = 100, title = 'Length', minval = 10)
devlen = input.float(defval = 2., title = 'Deviation', minval = 0.1, step = 0.1)
extendit = input(defval = true, title = 'Extend Lines')
showbroken = false //input.bool(defval=true, title='Show Broken Channel',
inline='brk')
//brokencol = input.color(defval = color.blue, title = '', inline = 'brk')
upcol = input.color(defval = color.lime, title = 'Up/Down Trend Colors', inline =
'trcols')
dncol = input.color(defval = color.red, title = '', inline = 'trcols')
width = 2//input(defval = 2, title = 'Line Width')

var colors = array.new_color(2)


if barstate.isfirst
array.unshift(colors, upcol)
array.unshift(colors, dncol)

get_channel(src, len) =>


mid = math.sum(src, len) / len
slope = ta.linreg(src, len, 0) - ta.linreg(src, len, 1)
intercept = mid - slope * math.floor(len / 2) + (1 - len % 2) / 2 * slope
endy = intercept + slope * (len - 1)
dev = 0.0
for x = 0 to len - 1 by 1
dev := dev + math.pow(src[x] - (slope * (len - x) + intercept), 2)
dev
dev := math.sqrt(dev / len)
[intercept, endy, dev, slope]

[y1_, y2_, dev, slope] = get_channel(src, len)

outofchannel = slope > 0 and close < y2_ - dev * devlen ? 0 : slope < 0 and close >
y2_ + dev * devlen ? 2 : -1

var reglines = array.new_line(3)


for x = 0 to 2 by 1
if not showbroken or outofchannel != x or nz(outofchannel[1], -1) != -1
line.delete(array.get(reglines, x))

array.set(reglines, x, line.new(x1 = bar_index - (len - 1), y1 = y1_ + dev *


devlen * (x - 1), x2 = bar_index, y2 = y2_ + dev * devlen * (x - 1), color =
array.get(colors, math.round(math.max(math.sign(slope), 0))), style = x % 2 == 1 ?
line.style_solid : line.style_dashed, width = 2, extend = extendit ? extend.right :
extend.none))

var label sidelab = label.new(x = bar_index - (len - 1), y = y1_, text = 'S', size
= size.large)
txt = slope > 0 ? slope > slope[1] ? '⇑' : '⇗' : slope < 0 ? slope < slope[1] ? '⇓'
: '⇘' : '⇒'
stl = slope > 0 ? slope > slope[1] ? label.style_label_up :
label.style_label_upper_right : slope < 0 ? slope < slope[1] ?
label.style_label_down : label.style_label_lower_right : label.style_label_right
label.set_style(sidelab, stl)
label.set_text(sidelab, txt)
label.set_x(sidelab, bar_index - (len - 1))
label.set_y(sidelab, slope > 0 ? y1_ - dev * devlen : slope < 0 ? y1_ + dev *
devlen : y1_)
label.set_color(sidelab, slope > 0 ? upcol : slope < 0 ? dncol : color.blue)

alertcondition(outofchannel, title = 'Channel Broken', message = 'Channel Broken')

// direction
trendisup = math.sign(slope) != math.sign(slope[1]) and slope > 0
trendisdown = math.sign(slope) != math.sign(slope[1]) and slope < 0
alertcondition(trendisup, title = 'Up trend', message = 'Up trend')
alertcondition(trendisdown, title = 'Down trend', message = 'Down trend')

symbols = array.from(sym1, sym2, sym3, sym4, sym5, sym6, sym7, sym8, sym9, sym10)
var trend_durations = array.new_int(array.size(symbols))
var previous_slopes = array.new_float(array.size(symbols))

// Create table
table_bg_color = chart.bg_color == color.black ? color.white : color.black
table_border_color = color.white
table_text_color = color.white

var table statusTable = table.new(position.top_right, 3, array.size(symbols) + 1,


border_width = 2, border_color = table_border_color, frame_width = 2, frame_color =
table_border_color)

table.cell(statusTable, 0, 0, 'Symbol', bgcolor = table_bg_color, text_color =


table_text_color, text_font_family = font.family_monospace)
table.cell(statusTable, 1, 0, 'Trend', bgcolor = table_bg_color, text_color =
table_text_color, text_font_family = font.family_monospace)
table.cell(statusTable, 2, 0, 'Price\nVS\nMiddle', bgcolor = table_bg_color,
text_color = table_text_color, text_font_family = font.family_monospace)

// Function to get trend status


get_trend_status(sym, index) =>

// Code the indicator logic in a function


// Pass the function to the request.security() function
// Tadaaaa
[y1_, y2_, dev, slope] = request.security(sym, timeframe.period,
get_channel(src, len))
current_price = request.security(sym, timeframe.period, close)

// Determine trend based on slope


trend = slope < 0 ? 'Bearish' : 'Bullish'
// Determine position relative to middle line
middle_line = y2_ // This is the current value of the middle regression line
position = current_price > middle_line ? 'Above' : 'Below'

// Detect trend changes


prev_slope = array.get(previous_slopes, index)
trendisup = math.sign(slope) != math.sign(prev_slope) and slope > 0
trendisdown = math.sign(slope) != math.sign(prev_slope) and slope < 0
signal = trendisup ? '⇑' : trendisdown ? '⇓' : ''

[trend, signal, trendisup, trendisdown, position]

//symbols = array.from(sym1, sym2, sym3, sym4, sym5, sym6, sym7, sym8, sym9, sym10)
// Update table every bar
if barstate.islast
for i = 0 to array.size(symbols) - 1 by 1
sym = array.get(symbols, i)
if str.length(sym) > 0
[trend, signal, trendisup, trendisdown, position] =
get_trend_status(sym, i)
symbol_name = array.get(str.split(sym, ':'), 1)
table.cell(statusTable, 0, i + 1, symbol_name, bgcolor =
table_bg_color, text_color = table_text_color, text_font_family =
font.family_monospace)
table.cell(statusTable, 1, i + 1, trend, bgcolor = table_bg_color,
text_color = trend == 'Bullish' ? upcol : dncol, text_font_family =
font.family_monospace)
table.cell(statusTable, 2, i + 1, position, bgcolor = table_bg_color,
text_color = position == 'Above' ? upcol : dncol, text_font_family =
font.family_monospace)

// alerts
if trendisup
alert(symbol_name + ' Up trend', alert.freq_once_per_bar_close)
else if trendisdown
alert(symbol_name + ' Down trend', alert.freq_once_per_bar_close)

// import necessary functions to calculate and show the zigzag


import LonesomeTheBlue/CreateAndShowZigzag/1 as ZigZag

prd = input.int(defval=8, title='ZigZag Period', minval=2, maxval=50,


group='setup')
ret_rate_min = input.float(defval=0.382, title='Min/Max Retracements',
minval=0.100, maxval=0.900, inline='retrate', group='setup')
ret_rate_max = input.float(defval=0.786, title='', minval=0.100, maxval=0.900,
inline='retrate', group='setup')
checkvol_support = input.bool(defval=true, title='Check Volume Support',
group='setup')
target1_enb = input.bool(defval=true, title='Target 1', inline='t1',
group='targets')
target1_ret = input.float(defval=1., title='', inline='t1', group='targets',
tooltip = "%X of wave 1 from the begining of wave 2")
target2_enb = input.bool(defval=true, title='Target 2', inline='t2',
group='targets')
target2_ret = input.float(defval=1.618, title='', inline='t2', group='targets',
tooltip = "%X of wave 1 from the begining of wave 2")
target3_enb = input.bool(defval=false, title='Target 3', inline='t3',
group='targets')
target3_ret = input.float(defval=2.618, title='', inline='t3', group='targets',
tooltip = "%X of wave 1 from the begining of wave 2")
target4_enb = input.bool(defval=false, title='Target 4', inline='t4',
group='targets')
target4_ret = input.float(defval=3.618, title='', inline='t4', group='targets',
tooltip = "%X of wave 1 from the begining of wave 2")
showwave12 = input.bool(defval=true, title='Show Wave 1 and 2', group='colors')
showbo = input.bool(defval=true, title='Zone', inline='bocol', group='colors')
bupcol = input.color(defval=color.rgb(0, 255, 0, 85), title='', inline='bocol',
group='colors')
bdncol = input.color(defval=color.rgb(255, 0, 0, 85), title='', inline='bocol',
group='colors')
showzigzag = input.bool(defval=false, title='Zig Zag', inline='zzcol',
group='colors')
upcol01 = input.color(defval=color.lime, title='', inline='zzcol', group='colors')
dncol01 = input.color(defval=color.red, title='', inline='zzcol', group='colors')

// definitions for zigzag arrays


var max_array_size = 10 // max length for zigzag array
var zigzag = array.new_float(0)
oldzigzag = array.copy(zigzag) // keep old zigzag

// get the zigzag


dir = ZigZag.getZigzag(zigzag, prd, max_array_size)
// show the zigzag
if showzigzag
ZigZag.showZigzag(zigzag, oldzigzag, dir, upcol01, dncol01)

int len01 = array.size(zigzag) >= 8 ? bar_index - math.round(array.get(zigzag,


7)) : 1
bool vol_support = (not checkvol_support or (checkvol_support and ta.linreg(volume,
len01, 0) - ta.linreg(volume, len01, 1) > 0))
var bool can_check_it = true
bool thereisbo = false
if (dir != dir[1])
can_check_it := true
can_check_it

// check if there is possible 3rd wave and show breakout if there is any
if array.size(zigzag) >= 8 and can_check_it
w12 = math.abs(array.get(zigzag, 2) - array.get(zigzag, 4)) /
math.abs(array.get(zigzag, 4) - array.get(zigzag, 6))
if w12 >= ret_rate_min and w12 <= ret_rate_max and (dir == 1 and high >
array.get(zigzag, 4) or dir == -1 and low < array.get(zigzag, 4))
can_check_it := false
if vol_support
thereisbo := true
// draw bo
if showbo
box.new(left=math.round(array.get(zigzag, 7)),
top=array.get(zigzag, 4), right=bar_index, bottom=array.get(zigzag, 6),
border_color=color.blue, border_width=1, border_style=line.style_dotted,
bgcolor=dir == 1 ? bupcol : bdncol)
if showwave12
line.new(x1=math.round(array.get(zigzag, 7)), y1=array.get(zigzag,
6), x2=math.round(array.get(zigzag, 5)), y2=array.get(zigzag, 4))
line.new(x1=math.round(array.get(zigzag, 5)), y1=array.get(zigzag,
4), x2=math.round(array.get(zigzag, 3)), y2=array.get(zigzag, 2))
label.new(x=math.round(array.get(zigzag, 7)), y=array.get(zigzag,
6), text='0', color=color.new(color.white, 100), textcolor=color.blue, style=dir ==
1 ? label.style_label_up : label.style_label_down)
label.new(x=math.round(array.get(zigzag, 5)), y=array.get(zigzag,
4), text='1', color=color.new(color.white, 100), textcolor=color.blue, style=dir ==
1 ? label.style_label_down : label.style_label_up)
label.new(x=math.round(array.get(zigzag, 3)), y=array.get(zigzag,
2), text='2', color=color.new(color.white, 100), textcolor=color.blue, style=dir ==
1 ? label.style_label_up : label.style_label_down)

// draw label
label.new(x=bar_index, y=array.get(zigzag, 6), color=dir == 1 ? upcol01
: dncol01, style=dir == 1 ? label.style_triangleup : label.style_triangledown,
size=size.small)

base = array.get(zigzag, 2)
wave1 = math.abs(array.get(zigzag, 4) - array.get(zigzag, 6))
if target1_enb
line.new(x1=bar_index, y1=math.max(base + dir * wave1 *
target1_ret, 0), x2=math.round(array.get(zigzag, 7)), y2=math.max(base + dir *
wave1 * target1_ret, 0), style=line.style_dashed)
if target2_enb
line.new(x1=bar_index, y1=math.max(base + dir * wave1 *
target2_ret, 0), x2=math.round(array.get(zigzag, 7)), y2=math.max(base + dir *
wave1 * target2_ret, 0), style=line.style_dashed)
if target3_enb
line.new(x1=bar_index, y1=math.max(base + dir * wave1 *
target3_ret, 0), x2=math.round(array.get(zigzag, 7)), y2=math.max(base + dir *
wave1 * target3_ret, 0), style=line.style_dashed)
if target4_enb
line.new(x1=bar_index, y1=math.max(base + dir * wave1 *
target4_ret, 0), x2=math.round(array.get(zigzag, 7)), y2=math.max(base + dir *
wave1 * target4_ret, 0), style=line.style_dashed)

alertcondition(thereisbo and dir == 1, title = "Breakout Long", message =


"Breakout Long")
alertcondition(thereisbo and dir == -1, title = "Breakout Short", message =
"Breakout Short")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy