Monday, April 14, 2025 12:40 AM
// @ Julien_Eche
//@version=6
indicator("Adaptive Trend Finder", overlay=true, max_bars_back=1200)
sourceInput = close
// CHANNEL SETTINGS
bool shortChannel = input.bool(true, "Show Strongest Short-Term Channel", group="Channels",
inline="Chan1")
color shortColor = input.color(color.gray, "", group="Channels", inline="Chan1")
bool longChannel = input.bool(false, "Show Strongest Long-Term Channel", group="Channels",
inline="Chan2")
color longColor = input.color(color.gray, "", group="Channels", inline="Chan2", tooltip="If the channel is not
visible, scroll back on the chart for additional historical data.")
// MIDLINE SETTINGS
bool showMidline = input.bool(false, "Display Midline", group="Midline Settings", inline="Mid")
color midlineColor = input.color(color.gray, "", group="Midline Settings", inline="Mid")
string midLineStyle = input.string("Dashed", "", options=["Dotted", "Solid", "Dashed"], group="Midline
Settings", inline="Mid")
int midTransp = input.int(50, "Transp", minval=0, maxval=100, step=10, group="Midline Settings",
inline="Mid")
// CHANNEL CONFIGURATION
bool isLog = input.bool(false, "Log Adjustment (for linear charts only)", tooltip="Enable if the chart is in linear
scale.", group="Channel Configuration")
devMultiplier = input.float(2.0, "Deviation Multiplier", step=0.1, group="Channel Configuration",
inline="Chan1")
string lineStyle1 = input.string("Solid", "", options=["Solid", "Dotted", "Dashed"], group="Channel
Configuration", inline="Chan1")
string extendStyle = input.string("Extend Right", "", options=["Extend Right", "Extend Both", "Extend None",
"Extend Left"], group="Channel Configuration", inline="Chan1")
int channelTransparency = input.int(40, "Line Transp", minval=0, maxval=100, step=1, group="Channel
Configuration", inline="Chan2")
int fillTransparency = input.int(93, "Fill Transp", minval=0, maxval=100, step=1, group="Channel
Configuration", inline="Chan2")
// TABLE DATA
string tablePositionInput = input.string("Bottom Right", "Table Position", options=["Bottom Right","Bottom
Left","Middle Right","Middle Left","Top Right","Top Left","Top Center","Bottom Center"], group="Table Data",
inline="Disp")
string textSizeInput = input.string("Normal", "Text Size", options=["Normal", "Large", "Small"], group="Table
Data", inline="Disp")
bool showAutoPeriod = input.bool(true, "Show Trend Period", tooltip="Automatically shows the period with
the strongest trend correlation.", group="Table Data")
bool showTrendStrength = input.bool(true, "Show Trend Strength", group="Table Data", inline="Strength")
bool showPearson = input.bool(false, "Use Pearson's R", tooltip="Pearson's R measures linear price/trend
correlation (1 strong positive, -1 strong negative).", group="Table Data", inline="Strength")
bool showAnnualReturn = input.bool(true, "Show Annualized Return", group="Table Data")
f_getTablePos(_pos) =>
switch _pos
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
"Bottom Center" => position.bottom_center
"Top Right" => position.top_right
"Top Left" => position.top_left
"Top Center" => position.top_center
"Middle Right" => position.middle_right
=> position.middle_left
confidence(r) =>
switch
New Section 1 Page 1
switch
r < 0.2 => "Extremely Weak"
r < 0.3 => "Very Weak"
r < 0.4 => "Weak"
r < 0.5 => "Mostly Weak"
r < 0.6 => "Somewhat Weak"
r < 0.7 => "Moderately Weak"
r < 0.8 => "Moderate"
r < 0.9 => "Moderately Strong"
r < 0.92 => "Mostly Strong"
r < 0.94 => "Strong"
r < 0.96 => "Very Strong"
r < 0.98 => "Exceptionally Strong"
=> "Ultra Strong"
calcDev(src, len) =>
float base = isLog ? src : math.log(src)
var int n1 = len - 1
if barstate.islast
float sumX = 0.0, sumXX = 0.0, sumYX = 0.0, sumY = 0.0
for i = 1 to len by 1
float val = base[i - 1]
sumX += i
sumXX += i * i
sumYX += i * val
sumY += val
float slope = nz((len * sumYX - sumX * sumY) / (len * sumXX - sumX * sumX))
float avg = sumY / len
float intercept = avg - slope * sumX / len + slope
float sumDev = 0.0, sumDxx = 0.0, sumDyy = 0.0, sumDyx = 0.0
float reg = intercept + slope * n1 * 0.5
float sumSlope = intercept
for i = 0 to n1 by 1
float v = base[i]
float dxt = v - avg
float dyt = sumSlope - reg
v := v - sumSlope
sumSlope += slope
sumDxx += dxt * dxt
sumDyy += dyt * dyt
sumDyx += dxt * dyt
sumDev += v * v
float unStdDev = math.sqrt(sumDev / n1)
float divisor = sumDxx * sumDyy
float r = nz(sumDyx / math.sqrt(divisor))
[unStdDev, r, slope, intercept]
else
[na, na, na, na]
get_tf_multiplier() =>
var float mult = 1.0
if syminfo.type == "crypto"
if timeframe.isdaily
mult := 365
else if timeframe.isweekly
mult := 52
else
if timeframe.isdaily
mult := 252
else if timeframe.isweekly
mult := 52
mult
is_valid_tf() => timeframe.isdaily or timeframe.isweekly
var string EXTEND_STYLE = switch extendStyle
"Extend Right" => extend.right
"Extend Both" => extend.both
"Extend None" => extend.none
=> extend.left
[stdDev01, pr01, slope01, int01] = calcDev(sourceInput, 300)
New Section 1 Page 2
[stdDev01, pr01, slope01, int01] = calcDev(sourceInput, 300)
[stdDev02, pr02, slope02, int02] = calcDev(sourceInput, 350)
[stdDev03, pr03, slope03, int03] = calcDev(sourceInput, 400)
[stdDev04, pr04, slope04, int04] = calcDev(sourceInput, 450)
[stdDev05, pr05, slope05, int05] = calcDev(sourceInput, 500)
[stdDev06, pr06, slope06, int06] = calcDev(sourceInput, 550)
[stdDev07, pr07, slope07, int07] = calcDev(sourceInput, 600)
[stdDev08, pr08, slope08, int08] = calcDev(sourceInput, 650)
[stdDev09, pr09, slope09, int09] = calcDev(sourceInput, 700)
[stdDev10, pr10, slope10, int10] = calcDev(sourceInput, 750)
[stdDev11, pr11, slope11, int11] = calcDev(sourceInput, 800)
[stdDev12, pr12, slope12, int12] = calcDev(sourceInput, 850)
[stdDev13, pr13, slope13, int13] = calcDev(sourceInput, 900)
[stdDev14, pr14, slope14, int14] = calcDev(sourceInput, 950)
[stdDev15, pr15, slope15, int15] = calcDev(sourceInput, 1000)
[stdDev16, pr16, slope16, int16] = calcDev(sourceInput, 1050)
[stdDev17, pr17, slope17, int17] = calcDev(sourceInput, 1100)
[stdDev18, pr18, slope18, int18] = calcDev(sourceInput, 1150)
[stdDev19, pr19, slope19, int19] = calcDev(sourceInput, 1200)
var int detectedLongPeriod = na
var float detectedLongSlope = na
var float detectedLongIntercept = na
var float detectedLongStdDev = na
var float detectedLongPearson = na
if barstate.islast
float highestLong = math.max(pr01, pr02, pr03, pr04, pr05, pr06, pr07, pr08, pr09, pr10, pr11, pr12, pr13,
pr14, pr15, pr16, pr17, pr18, pr19)
if highestLong == pr01
detectedLongPeriod := 300
detectedLongSlope := slope01
detectedLongIntercept := int01
detectedLongStdDev := stdDev01
detectedLongPearson := pr01
else if highestLong == pr02
detectedLongPeriod := 350
detectedLongSlope := slope02
detectedLongIntercept := int02
detectedLongStdDev := stdDev02
detectedLongPearson := pr02
else if highestLong == pr03
detectedLongPeriod := 400
detectedLongSlope := slope03
detectedLongIntercept := int03
detectedLongStdDev := stdDev03
detectedLongPearson := pr03
else if highestLong == pr04
detectedLongPeriod := 450
detectedLongSlope := slope04
detectedLongIntercept := int04
detectedLongStdDev := stdDev04
detectedLongPearson := pr04
else if highestLong == pr05
detectedLongPeriod := 500
detectedLongSlope := slope05
detectedLongIntercept := int05
detectedLongStdDev := stdDev05
detectedLongPearson := pr05
else if highestLong == pr06
detectedLongPeriod := 550
detectedLongSlope := slope06
detectedLongIntercept := int06
detectedLongStdDev := stdDev06
detectedLongPearson := pr06
else if highestLong == pr07
detectedLongPeriod := 600
detectedLongSlope := slope07
detectedLongIntercept := int07
New Section 1 Page 3
detectedLongIntercept := int07
detectedLongStdDev := stdDev07
detectedLongPearson := pr07
else if highestLong == pr08
detectedLongPeriod := 650
detectedLongSlope := slope08
detectedLongIntercept := int08
detectedLongStdDev := stdDev08
detectedLongPearson := pr08
else if highestLong == pr09
detectedLongPeriod := 700
detectedLongSlope := slope09
detectedLongIntercept := int09
detectedLongStdDev := stdDev09
detectedLongPearson := pr09
else if highestLong == pr10
detectedLongPeriod := 750
detectedLongSlope := slope10
detectedLongIntercept := int10
detectedLongStdDev := stdDev10
detectedLongPearson := pr10
else if highestLong == pr11
detectedLongPeriod := 800
detectedLongSlope := slope11
detectedLongIntercept := int11
detectedLongStdDev := stdDev11
detectedLongPearson := pr11
else if highestLong == pr12
detectedLongPeriod := 850
detectedLongSlope := slope12
detectedLongIntercept := int12
detectedLongStdDev := stdDev12
detectedLongPearson := pr12
else if highestLong == pr13
detectedLongPeriod := 900
detectedLongSlope := slope13
detectedLongIntercept := int13
detectedLongStdDev := stdDev13
detectedLongPearson := pr13
else if highestLong == pr14
detectedLongPeriod := 950
detectedLongSlope := slope14
detectedLongIntercept := int14
detectedLongStdDev := stdDev14
detectedLongPearson := pr14
else if highestLong == pr15
detectedLongPeriod := 1000
detectedLongSlope := slope15
detectedLongIntercept := int15
detectedLongStdDev := stdDev15
detectedLongPearson := pr15
else if highestLong == pr16
detectedLongPeriod := 1050
detectedLongSlope := slope16
detectedLongIntercept := int16
detectedLongStdDev := stdDev16
detectedLongPearson := pr16
else if highestLong == pr17
detectedLongPeriod := 1100
detectedLongSlope := slope17
detectedLongIntercept := int17
detectedLongStdDev := stdDev17
detectedLongPearson := pr17
else if highestLong == pr18
detectedLongPeriod := 1150
detectedLongSlope := slope18
detectedLongIntercept := int18
New Section 1 Page 4
detectedLongIntercept := int18
detectedLongStdDev := stdDev18
detectedLongPearson := pr18
else
detectedLongPeriod := 1200
detectedLongSlope := slope19
detectedLongIntercept := int19
detectedLongStdDev := stdDev19
detectedLongPearson := pr19
[stdDevS01, prS01, slopeS01, intS01] = calcDev(sourceInput, 20)
[stdDevS02, prS02, slopeS02, intS02] = calcDev(sourceInput, 30)
[stdDevS03, prS03, slopeS03, intS03] = calcDev(sourceInput, 40)
[stdDevS04, prS04, slopeS04, intS04] = calcDev(sourceInput, 50)
[stdDevS05, prS05, slopeS05, intS05] = calcDev(sourceInput, 60)
[stdDevS06, prS06, slopeS06, intS06] = calcDev(sourceInput, 70)
[stdDevS07, prS07, slopeS07, intS07] = calcDev(sourceInput, 80)
[stdDevS08, prS08, slopeS08, intS08] = calcDev(sourceInput, 90)
[stdDevS09, prS09, slopeS09, intS09] = calcDev(sourceInput, 100)
[stdDevS10, prS10, slopeS10, intS10] = calcDev(sourceInput, 110)
[stdDevS11, prS11, slopeS11, intS11] = calcDev(sourceInput, 120)
[stdDevS12, prS12, slopeS12, intS12] = calcDev(sourceInput, 130)
[stdDevS13, prS13, slopeS13, intS13] = calcDev(sourceInput, 140)
[stdDevS14, prS14, slopeS14, intS14] = calcDev(sourceInput, 150)
[stdDevS15, prS15, slopeS15, intS15] = calcDev(sourceInput, 160)
[stdDevS16, prS16, slopeS16, intS16] = calcDev(sourceInput, 170)
[stdDevS17, prS17, slopeS17, intS17] = calcDev(sourceInput, 180)
[stdDevS18, prS18, slopeS18, intS18] = calcDev(sourceInput, 190)
[stdDevS19, prS19, slopeS19, intS19] = calcDev(sourceInput, 200)
var int detectedShortPeriod = na
var float detectedShortSlope = na
var float detectedShortIntercept = na
var float detectedShortStdDev = na
var float detectedShortPearson = na
if barstate.islast
float highestShort = math.max(prS01, prS02, prS03, prS04, prS05, prS06, prS07, prS08, prS09, prS10,
prS11, prS12, prS13, prS14, prS15, prS16, prS17, prS18, prS19)
if highestShort == prS01
detectedShortPeriod := 20
detectedShortSlope := slopeS01
detectedShortIntercept := intS01
detectedShortStdDev := stdDevS01
detectedShortPearson := prS01
else if highestShort == prS02
detectedShortPeriod := 30
detectedShortSlope := slopeS02
detectedShortIntercept := intS02
detectedShortStdDev := stdDevS02
detectedShortPearson := prS02
else if highestShort == prS03
detectedShortPeriod := 40
detectedShortSlope := slopeS03
detectedShortIntercept := intS03
detectedShortStdDev := stdDevS03
detectedShortPearson := prS03
else if highestShort == prS04
detectedShortPeriod := 50
detectedShortSlope := slopeS04
detectedShortIntercept := intS04
detectedShortStdDev := stdDevS04
detectedShortPearson := prS04
else if highestShort == prS05
detectedShortPeriod := 60
detectedShortSlope := slopeS05
detectedShortIntercept := intS05
detectedShortStdDev := stdDevS05
detectedShortPearson := prS05
else if highestShort == prS06
New Section 1 Page 5
else if highestShort == prS06
detectedShortPeriod := 70
detectedShortSlope := slopeS06
detectedShortIntercept := intS06
detectedShortStdDev := stdDevS06
detectedShortPearson := prS06
else if highestShort == prS07
detectedShortPeriod := 80
detectedShortSlope := slopeS07
detectedShortIntercept := intS07
detectedShortStdDev := stdDevS07
detectedShortPearson := prS07
else if highestShort == prS08
detectedShortPeriod := 90
detectedShortSlope := slopeS08
detectedShortIntercept := intS08
detectedShortStdDev := stdDevS08
detectedShortPearson := prS08
else if highestShort == prS09
detectedShortPeriod := 100
detectedShortSlope := slopeS09
detectedShortIntercept := intS09
detectedShortStdDev := stdDevS09
detectedShortPearson := prS09
else if highestShort == prS10
detectedShortPeriod := 110
detectedShortSlope := slopeS10
detectedShortIntercept := intS10
detectedShortStdDev := stdDevS10
detectedShortPearson := prS10
else if highestShort == prS11
detectedShortPeriod := 120
detectedShortSlope := slopeS11
detectedShortIntercept := intS11
detectedShortStdDev := stdDevS11
detectedShortPearson := prS11
else if highestShort == prS12
detectedShortPeriod := 130
detectedShortSlope := slopeS12
detectedShortIntercept := intS12
detectedShortStdDev := stdDevS12
detectedShortPearson := prS12
else if highestShort == prS13
detectedShortPeriod := 140
detectedShortSlope := slopeS13
detectedShortIntercept := intS13
detectedShortStdDev := stdDevS13
detectedShortPearson := prS13
else if highestShort == prS14
detectedShortPeriod := 150
detectedShortSlope := slopeS14
detectedShortIntercept := intS14
detectedShortStdDev := stdDevS14
detectedShortPearson := prS14
else if highestShort == prS15
detectedShortPeriod := 160
detectedShortSlope := slopeS15
detectedShortIntercept := intS15
detectedShortStdDev := stdDevS15
detectedShortPearson := prS15
else if highestShort == prS16
detectedShortPeriod := 170
detectedShortSlope := slopeS16
detectedShortIntercept := intS16
detectedShortStdDev := stdDevS16
detectedShortPearson := prS16
else if highestShort == prS17
New Section 1 Page 6
else if highestShort == prS17
detectedShortPeriod := 180
detectedShortSlope := slopeS17
detectedShortIntercept := intS17
detectedShortStdDev := stdDevS17
detectedShortPearson := prS17
else if highestShort == prS18
detectedShortPeriod := 190
detectedShortSlope := slopeS18
detectedShortIntercept := intS18
detectedShortStdDev := stdDevS18
detectedShortPearson := prS18
else
detectedShortPeriod := 200
detectedShortSlope := slopeS19
detectedShortIntercept := intS19
detectedShortStdDev := stdDevS19
detectedShortPearson := prS19
float st_startPrice = isLog ? (detectedShortIntercept + detectedShortSlope * (detectedShortPeriod - 1)) :
math.exp(detectedShortIntercept + detectedShortSlope * (detectedShortPeriod - 1))
float st_endPrice = isLog ? detectedShortIntercept : math.exp(detectedShortIntercept)
int st_startBar = bar_index - detectedShortPeriod + 1
float st_upperStartPrice = isLog ? st_startPrice + devMultiplier * detectedShortStdDev : st_startPrice *
math.exp(devMultiplier * detectedShortStdDev)
float st_upperEndPrice = isLog ? st_endPrice + devMultiplier * detectedShortStdDev : st_endPrice *
math.exp(devMultiplier * detectedShortStdDev)
float st_lowerStartPrice = isLog ? st_startPrice - devMultiplier * detectedShortStdDev : st_startPrice /
math.exp(devMultiplier * detectedShortStdDev)
float st_lowerEndPrice = isLog ? st_endPrice - devMultiplier * detectedShortStdDev : st_endPrice /
math.exp(devMultiplier * detectedShortStdDev)
float lt_startPrice = isLog ? (detectedLongIntercept + detectedLongSlope * (detectedLongPeriod - 1)) :
math.exp(detectedLongIntercept + detectedLongSlope * (detectedLongPeriod - 1))
float lt_endPrice = isLog ? detectedLongIntercept : math.exp(detectedLongIntercept)
int lt_startBar = bar_index - detectedLongPeriod + 1
float lt_upperStartPrice = isLog ? lt_startPrice + devMultiplier * detectedLongStdDev : lt_startPrice *
math.exp(devMultiplier * detectedLongStdDev)
float lt_upperEndPrice = isLog ? lt_endPrice + devMultiplier * detectedLongStdDev : lt_endPrice *
math.exp(devMultiplier * detectedLongStdDev)
float lt_lowerStartPrice = isLog ? lt_startPrice - devMultiplier * detectedLongStdDev : lt_startPrice /
math.exp(devMultiplier * detectedLongStdDev)
float lt_lowerEndPrice = isLog ? lt_endPrice - devMultiplier * detectedLongStdDev : lt_endPrice /
math.exp(devMultiplier * detectedLongStdDev)
var line fillBaseLineShort = na
if shortChannel
if na(fillBaseLineShort)
fillBaseLineShort := line.new(st_startBar, st_startPrice, bar_index, st_endPrice, width=0,
color=color.new(midlineColor, 100), extend=EXTEND_STYLE)
else
line.set_xy1(fillBaseLineShort, st_startBar, st_startPrice)
line.set_xy2(fillBaseLineShort, bar_index, st_endPrice)
var line fillBaseLineLong = na
if longChannel
if na(fillBaseLineLong)
fillBaseLineLong := line.new(lt_startBar, lt_startPrice, bar_index, lt_endPrice, width=0,
color=color.new(midlineColor, 100), extend=EXTEND_STYLE)
else
line.set_xy1(fillBaseLineLong, lt_startBar, lt_startPrice)
line.set_xy2(fillBaseLineLong, bar_index, lt_endPrice)
var line st_upperLine = na
if shortChannel
if na(st_upperLine)
st_upperLine := line.new(st_startBar, st_upperStartPrice, bar_index, st_upperEndPrice, width=1,
extend=EXTEND_STYLE, color=color.new(shortColor, channelTransparency), style = lineStyle1
=="Dotted" ? line.style_dotted : lineStyle1=="Dashed" ? line.style_dashed : line.style_solid)
else
line.set_xy1(st_upperLine, st_startBar, st_upperStartPrice)
line.set_xy2(st_upperLine, bar_index, st_upperEndPrice)
New Section 1 Page 7
line.set_xy2(st_upperLine, bar_index, st_upperEndPrice)
line.set_color(st_upperLine, color.new(shortColor, channelTransparency))
var line st_lowerLine = na
if shortChannel
if na(st_lowerLine)
st_lowerLine := line.new(st_startBar, st_lowerStartPrice, bar_index, st_lowerEndPrice, width=1,
extend=EXTEND_STYLE, color=color.new(shortColor, channelTransparency), style = lineStyle1
=="Dotted" ? line.style_dotted : lineStyle1=="Dashed" ? line.style_dashed : line.style_solid)
else
line.set_xy1(st_lowerLine, st_startBar, st_lowerStartPrice)
line.set_xy2(st_lowerLine, bar_index, st_lowerEndPrice)
line.set_color(st_lowerLine, color.new(shortColor, channelTransparency))
var linefill st_upperFill = na
if shortChannel
if na(st_upperFill)
st_upperFill := linefill.new(st_upperLine, fillBaseLineShort, color=color.new(shortColor,
fillTransparency))
var linefill st_lowerFill = na
if shortChannel
if na(st_lowerFill)
st_lowerFill := linefill.new(fillBaseLineShort, st_lowerLine, color=color.new(shortColor,
fillTransparency))
var line lt_upperLine = na
if longChannel
if na(lt_upperLine)
lt_upperLine := line.new(lt_startBar, lt_upperStartPrice, bar_index, lt_upperEndPrice, width=1,
extend=EXTEND_STYLE, color=color.new(longColor, channelTransparency), style = lineStyle1=="Dotted" ?
line.style_dotted : lineStyle1=="Dashed" ? line.style_dashed : line.style_solid)
else
line.set_xy1(lt_upperLine, lt_startBar, lt_upperStartPrice)
line.set_xy2(lt_upperLine, bar_index, lt_upperEndPrice)
line.set_color(lt_upperLine, color.new(longColor, channelTransparency))
var line lt_lowerLine = na
if longChannel
if na(lt_lowerLine)
lt_lowerLine := line.new(lt_startBar, lt_lowerStartPrice, bar_index, lt_lowerEndPrice, width=1,
extend=EXTEND_STYLE, color=color.new(longColor, channelTransparency), style = lineStyle1=="Dotted" ?
line.style_dotted : lineStyle1=="Dashed" ? line.style_dashed : line.style_solid)
else
line.set_xy1(lt_lowerLine, lt_startBar, lt_lowerStartPrice)
line.set_xy2(lt_lowerLine, bar_index, lt_lowerEndPrice)
line.set_color(lt_lowerLine, color.new(longColor, channelTransparency))
var linefill lt_upperFill = na
if longChannel
if na(lt_upperFill)
lt_upperFill := linefill.new(lt_upperLine, fillBaseLineLong, color=color.new(longColor, fillTransparency))
var linefill lt_lowerFill = na
if longChannel
if na(lt_lowerFill)
lt_lowerFill := linefill.new(fillBaseLineLong, lt_lowerLine, color=color.new(longColor, fillTransparency))
var line dispMidLineShort = na
if shortChannel
if showMidline
if na(dispMidLineShort)
dispMidLineShort := line.new(st_startBar, st_startPrice, bar_index, st_endPrice, width=1,
extend=EXTEND_STYLE, color=color.new(midlineColor, midTransp), style = midLineStyle=="Dotted" ?
line.style_dotted : midLineStyle=="Dashed" ? line.style_dashed : line.style_solid)
else
line.set_xy1(dispMidLineShort, st_startBar, st_startPrice)
line.set_xy2(dispMidLineShort, bar_index, st_endPrice)
line.set_color(dispMidLineShort, color.new(midlineColor, midTransp))
else
if not na(dispMidLineShort)
line.delete(dispMidLineShort)
dispMidLineShort := na
var line dispMidLineLong = na
if longChannel
New Section 1 Page 8
if longChannel
if showMidline
if na(dispMidLineLong)
dispMidLineLong := line.new(lt_startBar, lt_startPrice, bar_index, lt_endPrice, width=1,
extend=EXTEND_STYLE, color=color.new(midlineColor, midTransp), style = midLineStyle=="Dotted" ?
line.style_dotted : midLineStyle=="Dashed" ? line.style_dashed : line.style_solid)
else
line.set_xy1(dispMidLineLong, lt_startBar, lt_startPrice)
line.set_xy2(dispMidLineLong, bar_index, lt_endPrice)
line.set_color(dispMidLineLong, color.new(midlineColor, midTransp))
else
if not na(dispMidLineLong)
line.delete(dispMidLineLong)
dispMidLineLong := na
// Table Setup
var table t = na
int lt_row = 0
int st_row = 0
int max_rows = (showAutoPeriod ? 1 : 0) + (showTrendStrength ? 1 : 0) + (showAnnualReturn ? 1 : 0)
int tableColumns = (shortChannel ? 1 : 0) + (longChannel ? 1 : 0)
t := table.new(f_getTablePos(tablePositionInput), 2, max_rows)
var tblSize = textSizeInput == "Large" ? size.large : textSizeInput == "Small" ? size.small : size.normal
// Compute Trend Annualized Returns
float mult = get_tf_multiplier()
var float st_cagr = na
var float lt_cagr = na
if not na(detectedShortPeriod) and bar_index >= detectedShortPeriod - 1 and is_valid_tf()
float st_close0 = close[detectedShortPeriod - 1]
st_cagr := math.pow(close / st_close0, mult / detectedShortPeriod) - 1
if not na(detectedLongPeriod) and bar_index >= detectedLongPeriod - 1 and is_valid_tf()
float lt_close0 = close[detectedLongPeriod - 1]
lt_cagr := math.pow(close / lt_close0, mult / detectedLongPeriod) - 1
// Long-Term Channel Data (Left Side)
if longChannel
if showAutoPeriod
table.cell(t, 0, lt_row, "Strongest Trend Period (Long Term): " + str.tostring(detectedLongPeriod),
text_color=longColor, text_size=tblSize)
lt_row += 1
if showTrendStrength
table.cell(t, 0, lt_row, showPearson ? "Pearson's R: " + str.tostring(detectedLongSlope > 0.0 ? -
detectedLongPearson : detectedLongPearson, "#.###") : "Trend Strength: " +
confidence(detectedLongPearson), text_color=longColor, text_size=tblSize)
lt_row += 1
if showAnnualReturn
table.cell(t, 0, lt_row, "Trend Annualized Return: " + (is_valid_tf() ? (not na(lt_cagr) ?
str.tostring(lt_cagr * 100, "#.#") + "%" : "N/A") : "Only for Daily or Weekly TF"), text_color=longColor,
text_size=tblSize)
// Short-Term Channel Data (Right Side)
if shortChannel
if showAutoPeriod
table.cell(t, 1, st_row, "Strongest Trend Period (Short Term): " + str.tostring(detectedShortPeriod),
text_color=shortColor, text_size=tblSize)
st_row += 1
if showTrendStrength
table.cell(t, 1, st_row, showPearson ? "Pearson's R: " + str.tostring(detectedShortSlope >
0.0 ? -detectedShortPearson : detectedShortPearson, "#.###") : "Trend Strength: " +
confidence(detectedShortPearson), text_color=shortColor, text_size=tblSize)
st_row += 1
if showAnnualReturn
table.cell(t, 1, st_row, "Trend Annualized Return: " + (is_valid_tf() ? (not na(st_cagr) ?
str.tostring(st_cagr * 100, "#.#") + "%" : "N/A") : "Only for Daily or Weekly TF"), text_color=shortColor,
text_size=tblSize)
New Section 1 Page 9