Best Smart Trail Algo V.1.2
Best Smart Trail Algo V.1.2
///////////////////////////////////
TrendUp = Up68
TrendDown = Dn68
Trend = 1
ex = 0.0
ex := ta.crossover(Trend, 0) ? norm_h : ta.crossunder(Trend, 0) ? norm_l : Trend ==
1 ? math.max(ex[1], norm_h) : Trend == -1 ? math.min(ex[1], norm_l) : ex[1]
//}
//}
fib1Level = 61.8
fib2Level = 78.6
fib3Level = 88.6
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////
//@version=5
//indicator('SimKle Zigzag UDT', 'SZU', true, max_bars_back = 500)
// 0. Inputs
// 1. Types
// 2. Switches
// 3. Variables and arrays
// 4. Custom Functions
// 5. Execution
// 6. Constructs
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
at https://mozilla.org/MPL/2.0/
// © FluxChart
//@version=5
const bool DEBUG = false
//indicator("Support & Resistance (MTF) | Flux Charts", overlay = true,
max_labels_count = 100, max_lines_count = 100, max_boxes_count = 100, max_bars_back
= 305)
createRSLabel () =>
label.new(na, na, "", style = label.style_none, textcolor = textColor)
type RSInfo
bool isBroken = na
int brokenTime = na
string RSType = na
float price = na
line line = na
box box = na
label priceLabel = na
chart.point[] points = na
label[] debugPoints = na
label breakLabel = na
label[] retestLabels = na
line breakLine = na
box breakBox = na
newRSInfo
newRS.debugPoints := array.new<label>(0)
newRS.retestLabels := array.new<label>(0)
newRS.points := array.new<chart.point>(0)
if enableZones
newRS.box := createRSBox(RSType == "Resistance" ? resistanceColor :
supportColor, xloc.bar_time)
moveRSInfoBox(newRS.box, startTime, newRS.price, endTime)
box.set_extend(newRS.box, expandLines ? extend.both : extend.none)
box.set_text(newRS.box, histText)
else
newRS.line := line.copy(RSInfoF.line)
moveLine(newRS.line, startTime, newRS.price, endTime)
line.set_extend(newRS.line, expandLines ? extend.both : extend.none)
newRS.priceLabel := label.copy(RSInfoF.priceLabel)
label.set_text(newRS.priceLabel, histText)
label.set_xloc(newRS.priceLabel, (startTime + endTime) / 2, xloc.bar_time)
if not na(newRS.breakLabel)
newRS.breakLabel := label.copy(RSInfoF.breakLabel)
newRS
RSInfoF.points.clear()
if RSInfoF.debugPoints.size() > 0
for i = 0 to RSInfoF.debugPoints.size() - 1
label.delete(RSInfoF.debugPoints.get(i))
RSInfoF.debugPoints.clear()
if RSInfoF.retestLabels.size() > 0
for i = 0 to RSInfoF.retestLabels.size() - 1
label.delete(RSInfoF.retestLabels.get(i))
RSInfoF.retestLabels.clear()
label.delete(RSInfoF.breakLabel)
line.delete(RSInfoF.breakLine)
box.delete(RSInfoF.breakBox)
type timeframeInfo
int index = na
string timeframeStr = na
bool isEnabled = false
RSInfo[] resistances = na
RSInfo[] supports = na
float[] highPivots = na
int[] highTimes = na
float[] lowPivots = na
int[] lowTimes = na
newTFInfo.resistances := array.new<RSInfo>(debug_lastXResistances)
newTFInfo.supports := array.new<RSInfo>(debug_lastXSupports)
newTFInfo.highPivots := array.new<float>()
newTFInfo.highTimes := array.new<int>()
newTFInfo.lowPivots := array.new<float>()
newTFInfo.lowTimes := array.new<int>()
newTFInfo
index = pivotsCount - i - 1
occurances = 0
invalidValue = false
pivotValue1 = pivots.get(index)
if bannedValues.size() > 0
for a = 0 to bannedValues.size() - 1
if doValuesTouch(pivotValue1, bannedValues.get(a))
invalidValue := true
break
if invalidValue
continue
for j = 0 to pivotsCount - 1
if j >= maxTraverse
break
index2 = pivotsCount - j - 1
pivotValue2 = pivots.get(index2)
if doValuesTouch(pivotValue1, pivotValue2)
occurances += 1
if not na(latestRSF)
break
if not na(latestRSF)
cnt = 0
if pivotsCount > 0
for i = 0 to pivotsCount - 1
if i >= maxTraverse
break
index = pivotsCount - i - 1
pivotValue = pivots.get(index)
if doValuesTouch(pivotValue, latestRSF.price)
labelTime = times.get(index)
latestRSF.points.push(chart.point.from_time(labelTime,
pivotValue))
cnt += 1
if cnt == strength
break
curIndex = 0
float foundBreakLevel = na
int foundBreakTime = na
while true
if curIndex >= maxTraverse
break
isBarBreak = rsInfo.RSType == "Resistance" ? (close[curIndex + 1] <=
rsInfo.price and close[curIndex] > rsInfo.price) : (close[curIndex + 1] >=
rsInfo.price and close[curIndex] < rsInfo.price)
if isBarBreak
isTrueBreakout = true
if avoidFalseBreaks
shortTerm = 5
longTerm = 15
shortSum = 0.0
longSum = 0.0
for i = 0 to shortTerm
shortSum += volume[curIndex + i]
for i = 0 to longTerm
longSum += volume[curIndex + i]
if isTrueBreakout
foundBreakLevel := rsInfo.RSType == "Resistance" ? low[curIndex] :
high[curIndex]
foundBreakTime := time[curIndex]
curIndex += 1
if time[curIndex] <= rsInfo.points.get(strength - 1).time
break
[foundBreakLevel, foundBreakTime]
curIndex = 0
lastRetestIndex = -999
int[] retestTimes = array.new<int>()
float[] retestLevels = array.new<float>()
while true
if curIndex >= maxTraverse
break
if retestLevels.size() == maxRetestLabels
break
if rsInfo.isBroken and time[curIndex] >= rsInfo.brokenTime
curIndex += 1
continue
if not na(foundBreakLevel)
if showBreaks
if na(RSInfoF.breakLabel)
RSInfoF.breakLabel := createBreakLabel(RSInfoF.RSType)
label.set_xy(RSInfoF.breakLabel, foundBreakTime, foundBreakLevel *
(RSInfoF.RSType == "Resistance" ? (1.0 / labelOffsetsY) : labelOffsetsY))
if expandLines
if na(RSInfoF.breakLine) and enableZones == false
RSInfoF.breakLine := createRSLine(color.black)
if not enableZones
line.set_extend(RSInfoF.breakLine, extend.right)
else
box.set_extend(RSInfoF.breakBox, extend.right)
if showRetests
[retestLevels, retestTimes] = getRetests(RSInfoF)
//timeSkipOffset = maxTimeskipMS / 4
timeSkipOffset = 0
if enableZones
zoneEndX = time + timeSkipOffset +
timeframe.in_seconds(timeframe.period) * 1000 * labelOffsetsXIndex
startTime = math.min(time, RSInfoF.points.get(strength - 1).time)
moveRSInfoBox(RSInfoF.box, startTime, RSInfoF.price, na(foundBreakTime)
? zoneEndX : foundBreakTime)
moveRSInfoBox(RSInfoF.breakBox, foundBreakTime, RSInfoF.price,
zoneEndX)
else
endTime = time + timeSkipOffset +
timeframe.in_seconds(timeframe.period) * 1000
startTime = math.min(time, RSInfoF.points.get(strength - 1).time)
moveLine(RSInfoF.line, startTime, RSInfoF.price, na(foundBreakTime) ?
endTime : foundBreakTime)
moveLine(RSInfoF.breakLine, foundBreakTime, RSInfoF.price, endTime)
//log.info(str.tostring(RSInfoF.price) + " | " +
str.tostring(RSInfoF.points.get(strength - 1).time) + " = " +
str.tostring(line.get_x1(RSInfoF.line)) + " | " + str.tostring(endTime) + " = " +
str.tostring(line.get_x2(RSInfoF.line)))
if expandLines
if not enableZones
line.set_extend(RSInfoF.line, (na(foundBreakTime)) ? extend.both :
extend.left)
else
box.set_extend(RSInfoF.box, (na(foundBreakTime)) ? extend.both :
extend.left)
else
if not enableZones
line.set_extend(RSInfoF.line, na(foundBreakTime) ? extend.right :
extend.none)
else
box.set_extend(RSInfoF.box, na(foundBreakTime) ? extend.right :
extend.none)
if not enableZones
label.set_text(RSInfoF.priceLabel, enableZones ? "" : labelTitle)
label.set_y(RSInfoF.priceLabel, RSInfoF.price)
else
box.set_text(RSInfoF.box, (RSInfoF.isBroken and expandLines) ? "" :
labelTitle)
box.set_text(RSInfoF.breakBox, labelTitle)
timeframeInfoF.lowPivots.clear()
timeframeInfoF.highPivots.clear()
timeframeInfoF.lowTimes.clear()
timeframeInfoF.highTimes.clear()
timeframeInfoF.lowPivots := lowPivots
timeframeInfoF.highPivots := highPivots
timeframeInfoF.lowTimes := lowTimes
timeframeInfoF.highTimes := highTimes
if timeframeInfoF.resistances.size() > 0
for j = 0 to timeframeInfoF.resistances.size() - 1
RSInfo RSInfoF = timeframeInfoF.resistances.get(j)
if not na(RSInfoF)
if debug_enabledHistory
if checkIfArrHasRS(oldRSList, RSInfoF) == false
oldRSList.push(RSInfoF)
oldRetestsCount += RSInfoF.retestLabels.size()
oldBreaksCount += RSInfoF.isBroken ? 1 : 0
if timeframeInfoF.supports.size() > 0
for j = 0 to timeframeInfoF.supports.size() - 1
RSInfo RSInfoF = timeframeInfoF.supports.get(j)
if not na(RSInfoF)
if debug_enabledHistory
if checkIfArrHasRS(history, RSInfoF) == false
oldRSList.push(RSInfoF)
oldRetestsCount += RSInfoF.retestLabels.size()
oldBreaksCount += RSInfoF.isBroken ? 1 : 0
timeframeInfoF.resistances.clear()
timeframeInfoF.supports.clear()
[oldRetestsCount, oldBreaksCount]
if count > 0
for j = 0 to count - 1
foundRS = findLatestNthRS(timeframeInfoF, RSType, pivots, times, j + 1)
if not na(foundRS)
notDuplicate = true
for a = 0 to timeframeInfos.size() - 1
aInfo = timeframeInfos.get(a)
if na(aInfo) or aInfo.isEnabled == false
continue
otherTimeframeArray = (RSType == "Resistance" ?
aInfo.resistances : aInfo.supports)
if otherTimeframeArray.size() > 0
for b = 0 to otherTimeframeArray.size() - 1
if checkIfRSAreSame(foundRS,
otherTimeframeArray.get(b))
notDuplicate := false
break
if notDuplicate == false
break
if arr.size() > 0
for j = 0 to arr.size() - 1
curRS = arr.get(j)
if not na(curRS)
handleRSInfo(timeframeInfoF, curRS, j, RSType)
curRetestsCount += curRS.retestLabels.size()
curBreaksCount += curRS.isBroken ? 1 : 0
[curRetestsCount, curBreaksCount]
lowPivot = getPivot("low")
highPivot = getPivot("high")
if not na(lowPivot)
allLowPivots.push(lowPivot)
allLowTimes.push(time[pivotRange])
if allLowPivots.size() > maxPivotsAllowed
allLowPivots.remove(0)
allLowTimes.remove(0)
if not na(highPivot)
allHighPivots.push(highPivot)
allHighTimes.push(time[pivotRange])
if allHighPivots.size() > maxPivotsAllowed
allHighPivots.remove(0)
allHighTimes.remove(0)
if calculateTimeSkipMS
if last_bar_index - bar_index < 350 and time - time[1] >
timeframe.in_seconds(timeframe.period) * 1000
maxTimeskipMS := math.max(maxTimeskipMS, time - time[1])
//plot(nz(na,timeframeInfos.get(0).highPivots.size() > 0 ?
timeframeInfos.get(0).highPivots.get(timeframeInfos.get(0).highPivots.size() - 1) :
0), color = color.blue, title = "High Pivots")
//plot(nz(na,timeframeInfos.get(0).lowPivots.size() > 0 ?
timeframeInfos.get(0).lowPivots.get(timeframeInfos.get(0).lowPivots.size() - 1) :
0), color = color.fuchsia, title = "Low Pivots")
if barstate.islast
int oldRetestsCount = 0
int curRetestsCount = 0
int oldBreaksCount = 0
int curBreaksCount = 0
for i = 0 to timeframeCount - 1
timeframeInfo curInfo = timeframeInfos.get(i)
if not curInfo.isEnabled
continue
if debug_enabledHistory
historyIndexesToDelete = array.new<int>(0)
if history.size() > 0
for i = 0 to history.size() - 1
if checkIfArrHasRS(curRSList, history.get(i))
historyIndexesToDelete.push(i)
if historyIndexesToDelete.size() > 0
for i = 0 to historyIndexesToDelete.size() - 1
deleteIndex =
historyIndexesToDelete.get(historyIndexesToDelete.size() - i - 1)
safeDeleteRSInfo(history.get(deleteIndex))
history.remove(deleteIndex)
if oldRSList.size() > 0
for i = 0 to oldRSList.size() - 1
curRS = oldRSList.get(i)
if checkIfArrHasRS(curRSList, curRS) == false
history.push(histRSInfo(curRS))
if history.size() > debug_maxHistoryRecords
safeDeleteRSInfo(history.get(0))
history.remove(0)
if oldRSList.size() > 0
for i = 0 to oldRSList.size() - 1
safeDeleteRSInfo(oldRSList.get(i))
curRSList.clear()
oldRSList.clear()
if DEBUG
log.info("History Size : " + str.tostring(history.size()))
log.info("Label Count : " + str.tostring(label.all.size()))
log.info("Line Count : " + str.tostring(line.all.size()))
log.info("Box Count : " + str.tostring(box.all.size()))
initRun := false
/////////////////////////////////////////////////////////////
//@version=5
//indicator(title="Parabolic SAR", shorttitle="SAR", overlay=true, timeframe="",
timeframe_gaps=true)
start = input(0.02)
increment = input(0.02)
maximum = input(0.2, "Max Value")
out = ta.sar(start, increment, maximum)
plot(out, "ParabolicSAR", style=plot.style_cross, color=#ffff00,display =
display.none)
/////////////////////////////////////////////////
// Functions
percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100
// Get components
rb = 10
prd = 284
ChannelW = 10
label_loc = 55
style = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ?
line.style_dotted : line.style_dashed
ph = ta.pivothigh(rb, rb)
pl = ta.pivotlow (rb, rb)
sr_levels = array.new_float(21, na)
prdhighest = ta.highest(prd)
prdlowest = ta.lowest(prd)
cwidth = percWidth(prd, ChannelW)
zonePerc = percWidth(300, zoneWidth)
aas = array.new_bool(41, true)
u1 = 0.0, u1 := nz(u1[1])
d1 = 0.0, d1 := nz(d1[1])
highestph = 0.0, highestph := highestph[1]
lowestpl = 0.0, lowestpl := lowestpl[1]
var sr_levs = array.new_float(21, na)
label hlabel = na, label.delete(hlabel[1])
label llabel = na, label.delete(llabel[1])
var sr_lines = array.new_line(21, na)
var sr_linesH = array.new_line(21, na)
var sr_linesL = array.new_line(21, na)
var sr_linesF = array.new_linefill(21, na)
var sr_labels = array.new_label(21, na)
if ph or pl
for x = 0 to array.size(sr_levels) - 1
array.set(sr_levels, x, na)
highestph := prdlowest
lowestpl := prdhighest
countpp = 0
for x = 0 to prd
if na(close[x])
break
if not na(ph[x]) or not na(pl[x])
highestph := math.max(highestph, nz(ph[x], prdlowest), nz(pl[x],
prdlowest))
lowestpl := math.min(lowestpl, nz(ph[x], prdhighest), nz(pl[x],
prdhighest))
countpp += 1
if countpp > 40
break
if array.get(aas, countpp)
upl = (ph[x] ? high[x + rb] : low[x + rb]) + cwidth
dnl = (ph[x] ? high[x + rb] : low[x + rb]) - cwidth
u1 := countpp == 1 ? upl : u1
d1 := countpp == 1 ? dnl : d1
tmp = array.new_bool(41, true)
cnt = 0
tpoint = 0
for xx = 0 to prd
if na(close[xx])
break
if not na(ph[xx]) or not na(pl[xx])
chg = false
cnt += 1
if cnt > 40
break
if array.get(aas, cnt)
if not na(ph[xx])
if high[xx + rb] <= upl and high[xx + rb] >= dnl
tpoint += 1
chg := true
if not na(pl[xx])
if low[xx + rb] <= upl and low[xx + rb] >= dnl
tpoint += 1
chg := true
if chg and cnt < 41
array.set(tmp, cnt, false)
if tpoint >= strengthSR
for g = 0 to 40 by 1
if not array.get(tmp, g)
array.set(aas, g, false)
if ph[x] and countpp < 21
array.set(sr_levels, countpp, high[x + rb])
if pl[x] and countpp < 21
array.set(sr_levels, countpp, low[x + rb])
// Plot
var line highest_ = na, line.delete(highest_)
var line lowest_ = na, line.delete(lowest_)
var line highest_fill1 = na, line.delete(highest_fill1)
var line highest_fill2 = na, line.delete(highest_fill2)
var line lowest_fill1 = na, line.delete(lowest_fill1)
var line lowest_fill2 = na, line.delete(lowest_fill2)
hi_col = close >= highestph ? colorSup : colorRes
lo_col = close >= lowestpl ? colorSup : colorRes
if enableSR
highest_ := line.new(bar_index - 311, highestph, bar_index, highestph,
xloc.bar_index, expandSR ? extend.both : extend.right, hi_col, style, lineWidth)
lowest_ := line.new(bar_index - 311, lowestpl , bar_index, lowestpl ,
xloc.bar_index, expandSR ? extend.both : extend.right, lo_col, style, lineWidth)
if useHLZones
highest_fill1 := line.new(bar_index - 311, highestph + zonePerc, bar_index,
highestph + zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)
highest_fill2 := line.new(bar_index - 311, highestph - zonePerc, bar_index,
highestph - zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)
lowest_fill1 := line.new(bar_index - 311, lowestpl + zonePerc , bar_index,
lowestpl + zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na)
lowest_fill2 := line.new(bar_index - 311, lowestpl - zonePerc , bar_index,
lowestpl - zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na)
linefill.new(highest_fill1, highest_fill2, color.new(hi_col, 80))
linefill.new(lowest_fill1 , lowest_fill2 , color.new(lo_col, 80))
if ph or pl
for x = 0 to array.size(sr_lines) - 1
array.set(sr_levs, x, array.get(sr_levels, x))
for x = 0 to array.size(sr_lines) - 1
line.delete(array.get(sr_lines, x))
line.delete(array.get(sr_linesH, x))
line.delete(array.get(sr_linesL, x))
linefill.delete(array.get(sr_linesF, x))
if array.get(sr_levs, x) and enableSR
line_col = close >= array.get(sr_levs, x) ? colorSup : colorRes
array.set(sr_lines, x, line.new(bar_index - 355, array.get(sr_levs, x),
bar_index, array.get(sr_levs, x), xloc.bar_index, expandSR ? extend.both :
extend.right, line_col, style, lineWidth))
if useZones
array.set(sr_linesH, x, line.new(bar_index - 355, array.get(sr_levs, x)
+ zonePerc, bar_index, array.get(sr_levs, x) + zonePerc, xloc.bar_index, expandSR ?
extend.both : extend.right, na))
array.set(sr_linesL, x, line.new(bar_index - 355, array.get(sr_levs, x)
- zonePerc, bar_index, array.get(sr_levs, x) - zonePerc, xloc.bar_index, expandSR ?
extend.both : extend.right, na))
array.set(sr_linesF, x, linefill.new(array.get(sr_linesH, x),
array.get(sr_linesL, x), color.new(line_col, 80)))
for x = 0 to array.size(sr_labels) - 1
label.delete(array.get(sr_labels, x))
if array.get(sr_levs, x) and enableSR
lab_loc = close >= array.get(sr_levs, x) ? label.style_label_up :
label.style_label_down
lab_col = close >= array.get(sr_levs, x) ? colorSup : colorRes
array.set(sr_labels, x, label.new(bar_index + label_loc, array.get(sr_levs,
x), str.tostring(math.round_to_mintick(array.get(sr_levs, x))), color=lab_col ,
textcolor=#000000, style=lab_loc))
hlabel := enableSR ? label.new(bar_index + label_loc +
math.round(math.sign(label_loc)) * 20, highestph, "High Level : " +
str.tostring(highestph), color=hi_col, textcolor=#000000,
style=label.style_label_down) : na
llabel := enableSR ? label.new(bar_index + label_loc +
math.round(math.sign(label_loc)) * 20, lowestpl , "Low Level : " +
str.tostring(lowestpl) , color=lo_col, textcolor=#000000,
style=label.style_label_up ) : na
//////////////////////////////////////////////////////////////////////
// © Dreadblitz
//@version=5
//indicator(shorttitle='1 - Follow Line 2in1', title='1 - Follow Line 2in1',
overlay=true)
//////////////////////////////////
//
UCCR = input(true, title='Use Current Chart Resolution?')
TYPE = input.string(title='Type Of MA', defval='EMA', options=['RMA', 'SMA', 'EMA',
'WMA', 'VWMA', 'SMMA', 'KMA', 'TMA', 'HullMA', 'DEMA', 'TEMA', 'CTI'])
RES = input.timeframe('60', title='Resolution')
LEN = input(21, title='Period')
BBdeviations = input.float(defval=1.00, title='Deviations', minval=0.1, step=0.05)
ATRperiod = input.int(defval=5, title='ATR Period', minval=1)
SOUR = input(title='Source', defval=close)
hl = input(defval=false, title='Hide Labels')
//
cd = 0.0
cti(sm, src, cd) =>
di = (sm - 1.0) / 2.0 + 1.0
c1 = 2 / (di + 1.0)
c2 = 1 - c1
c3 = 3.0 * (cd * cd + cd * cd * cd)
c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd)
c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd
i1 = 0.0
i2 = 0.0
i3 = 0.0
i4 = 0.0
i5 = 0.0
i6 = 0.0
i1 := c1 * src + c2 * nz(i1[1])
i2 := c1 * i1 + c2 * nz(i2[1])
i3 := c1 * i2 + c2 * nz(i3[1])
i4 := c1 * i3 + c2 * nz(i4[1])
i5 := c1 * i4 + c2 * nz(i5[1])
i6 := c1 * i5 + c2 * nz(i6[1])
bfr = -cd * cd * cd * i6 + c3 * i5 + c4 * i4 + c5 * i3
bfr
/////////////////////////////
//src1 = input(close)
src1 = close
plot(ksrc, "Line2", color= ksrc > ksrc[1] ? color.rgb(9, 255, 95) : color.red,
linewidth=2)
// Color Scheme
bullcolor = colors == 'DARK' ? color.white : color.green
bearcolor = colors == 'DARK' ? color.blue : color.red
int upcandles = 0
for i = 1 to periods by 1
upcandles += (close[i] > open[i] ? 1 : 0) // Determine color of subsequent
candles (must all be green to identify a valid Bearish OB)
upcandles
int downcandles = 0
for i = 1 to periods by 1
downcandles += (close[i] < open[i] ? 1 : 0) // Determine color of subsequent
candles (must all be red to identify a valid Bearish OB)
downcandles
// Plotting