0% found this document useful (0 votes)
36 views9 pages

Trend Li̇ne

Uploaded by

borsa240411
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

Trend Li̇ne

Uploaded by

borsa240411
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// © pikusov
//
// Diagonal Supports and Resistances
//
// Classic diagonal support and resistance based on pivot points.
// As a result, they form triangles, wedges, channels and other patterns.
// Realtime update up to 1 second chart.
//
// Input parameters:
// History Bars Back: Number of bars back to find high and low points
// Please note that History Bars Back more than 300 may cause errors
// Resolution: The power of high and low points to take into drawing
//
// You can enable alerts for crossing supports and resistances in the Alerts menu
// https://www.tradingview.com/support/solutions/43000595315-how-to-set-up-alerts
//
//@version=5

var int max_bars_back = 5000


indicator('Trend line dialog', overlay=true, max_bars_back=max_bars_back)

var int history_bars = input(title='History bars back', defval=300)


col_sup = color.new(#17ff27, 80)
style_sup = line.style_solid
col_res = color.new(#ff77ad, 80)
style_res = line.style_solid

// Функция вычисляет цену в точке t3 для линии,


// заданной первыми четырьмя координатами (t1, p1, t2, p2)
price_at(t1, p1, t2, p2, t3) =>
p1 + (p2 - p1) * (t3 - t1) / (t2 - t1)

// Alerts
if 1 == 1
alert('test')
// округление
round_to_tick(x) =>
mult = 1 / syminfo.mintick
value = math.ceil(x * mult) / mult
value

// Тут храним линии для удаления при появлении нового бара


var line[] supports = array.new_line()
var line[] resistances = array.new_line()
var label[] labels = array.new_label()

fire_alert_sup = false
fire_alert_res = false
fire_alert_sup := false
fire_alert_res := false
// Удаляем прошлые линии и заодно вызываем алерты
line temp_line = na
if array.size(supports) > 0
for i = array.size(supports) - 1 to 0 by 1
temp_line := array.get(supports, i)
if low[1] > line.get_price(temp_line, bar_index - 1) and close <
line.get_price(temp_line, bar_index)
fire_alert_sup := true
fire_alert_sup
line.delete(temp_line)
array.remove(supports, i)
if array.size(resistances) > 0
for i = array.size(resistances) - 1 to 0 by 1
temp_line := array.get(resistances, i)
if high[1] < line.get_price(temp_line, bar_index - 1) and close >
line.get_price(temp_line, bar_index)
fire_alert_res := true
fire_alert_res
line.delete(temp_line)
array.remove(resistances, i)
label temp_label = na
if array.size(labels) > 0
for i = array.size(labels) - 1 to 0 by 1
temp_label := array.get(labels, i)
label.delete(temp_label)
array.remove(labels, i)

alertcondition(fire_alert_sup, 'Diagonal Support Alert', 'Diagonal support crossed


down')
alertcondition(fire_alert_res, 'Diagonal Resistance Alert', 'Diagonal resistance
crossed up')

// Определяем экстремумы
min_values = low
max_values = high
x1 = input(title='Resolution (bars)', defval=26)
x2 = math.round(x1 / 2)
int minimums = 0
minimums := ta.lowestbars(min_values, x1) == -x2 ? x2 : minimums[1] + 1

int maximums = 0
maximums := ta.highestbars(max_values, x1) == -x2 ? x2 : maximums[1] + 1

int minimum1 = 0
int minimum2 = 0
int maximum1 = 0
int maximum2 = 0
int medium = 0
// Поддержка
if barstate.islast
//label.new(bar_index, close , style=label.style_labeldown,
text=timeframe.period, color=color.new(color.red, 90))
line last_line = na
label last_label = na
for k1 = 0 to 50 by 1
if minimum1 >= history_bars
break
minimum1 += minimums[minimum1]
minimum2 := minimum1 * 2
for k2 = 0 to 50 by 1
if minimum2 >= minimum1 * 8 or minimum2 >= history_bars
break
minimum2 += minimums[minimum2]
if minimum1 >= history_bars or minimum2 >= history_bars
break

bar1 = bar_index - minimum1


bar2 = bar_index - minimum2

price1 = low[minimum1]
price2 = low[minimum2]

current_price = price_at(bar2, price2, bar1, price1, bar_index)


// Если поддержка проходит ниже текущей цены
if current_price < high[1]

// проверяем пересечения
crossed = 0
medium := 0
for k3 = 0 to 50 by 1
if medium >= minimum2
break
medium += minimums[medium]
if medium >= minimum2
break
if price_at(bar2, price2, bar1, price1, bar_index - medium) >
math.min(open[medium], close[medium])
crossed := 1
break

// если нет пересечений


if crossed == 0 // and overtilt == 0
// сравниваем с прошлой созданной линией
if not na(last_line)
last_price = price_at(line.get_x1(last_line),
line.get_y1(last_line), line.get_x2(last_line), line.get_y2(last_line), bar_index)
if bar1 == line.get_x2(last_line)
if current_price > last_price
line.set_xy1(last_line, bar2, price2)
line.set_xy2(last_line, bar1, price1)
line.set_color(last_line, col_sup)
label.set_xy(last_label, bar_index, current_price)
label.set_text(last_label,
str.tostring(round_to_tick(current_price)))
true
else
last_line := line.new(bar2, price2, bar1, price1,
extend=extend.right, color=col_sup, style=style_sup)
last_label := label.new(bar_index, current_price,
color=col_sup, style=label.style_label_upper_left,
text=str.tostring(round_to_tick(current_price)))
array.push(labels, last_label)
array.push(supports, last_line)
true
else
// добавляем линию
last_line := line.new(bar2, price2, bar1, price1,
extend=extend.right, color=col_sup, style=style_sup)
last_label := label.new(bar_index, current_price,
color=col_sup, style=label.style_label_upper_left,
text=str.tostring(round_to_tick(current_price)))
array.push(labels, last_label)
array.push(supports, last_line)
true

last_line := na
last_label := na
for k1 = 0 to 100 by 1
if maximum1 >= history_bars
break
maximum1 += maximums[maximum1]
maximum2 := maximum1 * 2
for k2 = 0 to 50 by 1
if maximum2 >= maximum1 * 8 or maximum2 >= history_bars
break
maximum2 += maximums[maximum2]

if maximum1 >= history_bars or maximum2 >= history_bars


break

bar1 = bar_index - maximum1


bar2 = bar_index - maximum2

price1 = high[maximum1]
price2 = high[maximum2]

current_price = price_at(bar2, price2, bar1, price1, bar_index)


// Если сопротивоение проходит выше текущей цены
if current_price > low[1]

// проверяем пересечения
crossed = 0
medium := 0
for k3 = 0 to 100 by 1
if medium >= maximum2
break
medium += maximums[medium]
if medium >= maximum2
break
if price_at(bar2, price2, bar1, price1, bar_index - medium) <
math.max(open[medium], close[medium])
crossed := 1
break

// если нет пересечений


if crossed == 0 // and overtilt == 0
// сравниваем с прошлой созданной линией
if not na(last_line)
last_price = price_at(line.get_x1(last_line),
line.get_y1(last_line), line.get_x2(last_line), line.get_y2(last_line), bar_index)
if bar1 == line.get_x2(last_line)
if current_price < last_price
line.set_xy1(last_line, bar2, price2)
line.set_xy2(last_line, bar1, price1)
line.set_color(last_line, col_res)
label.set_xy(last_label, bar_index, current_price)
label.set_text(last_label,
str.tostring(round_to_tick(current_price)))

true
else
last_line := line.new(bar2, price2, bar1, price1,
extend=extend.right, color=col_res, style=style_res)
last_label := label.new(bar_index, current_price,
color=col_res, style=label.style_label_lower_left,
text=str.tostring(round_to_tick(current_price)))
array.push(labels, last_label)
array.push(resistances, last_line)
true
else
// добавляем линию
last_line := line.new(bar2, price2, bar1, price1,
extend=extend.right, color=col_res, style=style_res)
last_label := label.new(bar_index, current_price,
color=col_res, style=label.style_label_lower_left,
text=str.tostring(round_to_tick(current_price)))

array.push(labels, last_label)
array.push(resistances, last_line)
true

// This Pine Script™ code is subject to the terms of the Attribution-NonCommercial-


ShareAlike 4.0 International (CC BY-NC-SA 4.0)
https://creativecommons.org/licenses/by-nc-sa/4.0/
// © UAlgo
//@version=5
//indicator("Trend line", overlay=true, max_lines_count=500, max_bars_back=4900,
max_boxes_count=1500)

showTrendLines = input.bool(true,"Show Trend Lines",group = "Misc")


trendLineLength = input.int(24,"Trend Line Detection Sensitivity",group =
"Misc",minval = 10)
upTlColor = input.color(color.new(color.teal,15),title = "Trend Line Colors" ,group
= "Misc",inline = "tl")
downTlColor = input.color(color.new(color.red,15),title = " ",group = "Misc",inline
= "tl")

var bool drawUp = na


var bool drawDown = na
var string lastState = na
var bool to_up = na
var bool to_down = na
var int trend = 1

var string textOfDiscount = na


var line newBearishTrendline = na
var line newBullishTrendline = na

// Trendlines

extendTrendline(lineId, startIndex, startValue, endIndex, endValue) =>


slope = (endValue - startValue) / (endIndex - startIndex)
newEndIndex = bar_index
newEndValue = startValue + slope * (newEndIndex - startIndex)
line.set_x2(lineId, newEndIndex)
line.set_y2(lineId, newEndValue)

getSlope(startIndex, startValue, endIndex, endValue) =>


slope = (endValue - startValue) / (endIndex - startIndex)
slope
if showTrendLines
phTrend = ta.pivothigh(high,trendLineLength,trendLineLength)
plTrend = ta.pivotlow(low,trendLineLength,trendLineLength)

bullishTrendLineStart = ta.valuewhen(not
na(plTrend),bar_index[trendLineLength],1)
bullishTrendLineEnd = ta.valuewhen(not
na(plTrend),bar_index[trendLineLength],0)
bearishTrendLineStart = ta.valuewhen(not
na(phTrend),bar_index[trendLineLength],1)
bearishTrendLineEnd = ta.valuewhen(not
na(phTrend),bar_index[trendLineLength],0)

bullishTrendLineStartVal = ta.valuewhen(not na(plTrend),low[trendLineLength],1)


bullishTrendLineEndVal = ta.valuewhen(not na(plTrend),low[trendLineLength],0)
bearishTrendLineStartVal = ta.valuewhen(not
na(phTrend),high[trendLineLength],1)
bearishTrendLineEndVal = ta.valuewhen(not na(phTrend),high[trendLineLength],0)

line.delete(newBearishTrendline)
line.delete(newBullishTrendline)

slopeBearish = getSlope(bearishTrendLineStart, bearishTrendLineStartVal,


bearishTrendLineEnd, bearishTrendLineEndVal)
slopeBullish = getSlope(bullishTrendLineStart, bullishTrendLineStartVal,
bullishTrendLineEnd, bullishTrendLineEndVal)

if slopeBearish < 0
newBearishTrendline := line.new(x1 = bearishTrendLineStart,y1
=bearishTrendLineStartVal , x2 =bar_index ,y2 =bearishTrendLineEndVal,xloc =
xloc.bar_index,color=downTlColor,width = 2)
if slopeBullish > 0
newBullishTrendline := line.new(x1 = bullishTrendLineStart,y1
=bullishTrendLineStartVal , x2 =bar_index ,y2 =bullishTrendLineEndVal,xloc =
xloc.bar_index,color=upTlColor,width = 2)

// Extend the trendlines


if (not na(newBearishTrendline))
extendTrendline(newBearishTrendline, bearishTrendLineStart,
bearishTrendLineStartVal, bearishTrendLineEnd, bearishTrendLineEndVal)

if (not na(newBullishTrendline))
extendTrendline(newBullishTrendline, bullishTrendLineStart,
bullishTrendLineStartVal, bullishTrendLineEnd, bullishTrendLineEndVal)

//@version=5

//indicator('Trend Line', overlay=true, max_bars_back=4000)


_period = input.int(defval=10, title='Pivot _period', minval=10, maxval=50)
PivotPointNumber = input.int(defval=6, title='Number of Pivots to be controlled',
minval=2, maxval=6)
up_trend_color = input.color(defval=color.lime, title='Up Trend Colors',
inline='tcol')
down_trend_color = input.color(defval=color.red, title='Down Trend Colors',
inline='tcol')

float pivot_high = ta.pivothigh(_period, _period)


float pivot_low = ta.pivotlow(_period, _period)

var trend_top_values = array.new_float(PivotPointNumber) //high pivot point


var trend_top_position = array.new_int(PivotPointNumber) //pivot position
var trend_bottom_values = array.new_float(PivotPointNumber) //low pivot point
var trend_bottom_position = array.new_int(PivotPointNumber) //pivot position

add_to_array(apointer1, apointer2, val) =>


array.unshift(apointer1, val)
array.unshift(apointer2, bar_index)
array.pop(apointer1)
array.pop(apointer2)

if pivot_high
add_to_array(trend_top_values, trend_top_position, pivot_high)

if pivot_low
add_to_array(trend_bottom_values, trend_bottom_position, pivot_low)

// line definitions
maxline = 3
var bottom_lines = array.new_line(maxline, na)
var top_lines = array.new_line(maxline, na)
//var ticksize = syminfo.mintick

// Pivot point loop to control the existence of a trend line


count_line_low = 0
count_line_high = 0
starttime = timestamp(0, 0, 0, 0, 0, 0)

// bottom trend line calc


if time >= starttime
for x = 0 to maxline - 1 by 1
line.delete(array.get(bottom_lines, x))
line.delete(array.get(top_lines, x))
for pivot1 = 0 to PivotPointNumber - 2 by 1
up_val1 = 0.0
up_val2 = 0.0
up1 = 0
up2 = 0

if count_line_low <= maxline


for pivot2 = PivotPointNumber - 1 to pivot1 + 1 by 1
value1 = array.get(trend_bottom_values, pivot1)
value2 = array.get(trend_bottom_values, pivot2)
position1 = array.get(trend_bottom_position, pivot1)
position2 = array.get(trend_bottom_position, pivot2)

if value1 > value2


different = (value1 - value2) / (position1 - position2)
high_line = value2 + different
low_location = bar_index
low_value = low
valid = true

for x = position2 + 1 - _period to bar_index by 1


if close[bar_index - x] < high_line
valid := false
break
low_location := x
low_value := high_line
high_line += different
high_line

if valid
up_val1 := high_line - different
up_val2 := value2
up1 := low_location
up2 := position2
break

d_value1 = 0.0
d_value2 = 0.0
d_position1 = 0
d_position2 = 0

//top trend line calc


if count_line_high <= maxline
for pivot2 = PivotPointNumber - 1 to pivot1 + 1 by 1
value1 = array.get(trend_top_values, pivot1)
value2 = array.get(trend_top_values, pivot2)
position1 = array.get(trend_top_position, pivot1)
position2 = array.get(trend_top_position, pivot2)
if value1 < value2
different = (value2 - value1) / float(position1 - position2)
high_line = value2 - different
low_location = bar_index
low_value = high
valid = true
for x = position2 + 1 - _period to bar_index by 1
if close[bar_index - x] > high_line
valid := false
break
low_location := x
low_value := high_line
high_line -= different
high_line

if valid
d_value1 := high_line + different
d_value2 := value2
d_position1 := low_location
d_position2 := position2
break

// if there is continues uptrend line then draw it


if up1 != 0 and up2 != 0 and count_line_low < maxline
count_line_low += 1
array.set(bottom_lines, count_line_low - 1, line.new(up2 - _period,
up_val2, up1, up_val1, color=up_trend_color))

// if there is continues downtrend line then draw it


if d_position1 != 0 and d_position2 != 1 and count_line_high < maxline
count_line_high += 1
array.set(top_lines, count_line_high - 1, line.new(d_position2 -
_period, d_value2, d_position1, d_value1, color=down_trend_color))

You might also like