// Plot lines for highest high and lowest low var line hhLine = na var line llLine = na var line midline = na var label fibLabelh = na var label fibLabell = na var label fibLabelm = na var label fibLabel1 = na var label fibLabel2 = na var label fibLabel3 = na var label fibLabel4 = na var label fibLabel5 = na var label fibLabel6 = na var line fibLine1 = na var line fibLine2 = na var line fibLine3 = na var line fibLine4 = na var line fibLine5 = na var line fibLine6 = na if barstate.islast and show_fib while high[h1Index] != h1[1] h1Index := h1Index + 1
while low[l2Index] != l2[1] l2Index := l2Index + 1
n = math.max(h1Index,l2Index) if not na(hhLine) line.delete(hhLine) if not na(llLine) line.delete(llLine) if not na(midline) line.delete(midline) if not na(fibLabelh) label.delete(fibLabelh) if not na(fibLabell) label.delete(fibLabell)
if not na(fibLabelm) label.delete(fibLabelm) if not na(fibLabel1) label.delete(fibLabel1) if not na(fibLabel2) label.delete(fibLabel2) if not na(fibLabel3) label.delete(fibLabel3)
if not na(fibLabel4) label.delete(fibLabel4) if not na(fibLabel5) label.delete(fibLabel5)
if not na(fibLabel6) label.delete(fibLabel6) if not na(fibLine1) line.delete(fibLine1) if not na(fibLine2) line.delete(fibLine2) if not na(fibLine3) line.delete(fibLine3) if not na(fibLine4) line.delete(fibLine4) if not na(fibLine5) line.delete(fibLine5) if not na(fibLine6) line.delete(fibLine6) hhLine := line.new(x1=bar_index-n+1, y1=hh, x2=bar_index, y2=hh, color=color.purple, width=1) fibLabelh := label.new(x=bar_index + 10, y=hh , text="1("+str.tostring(hh )+")", style = label.style_none,xloc=xloc.bar_index, yloc=yloc.price, color=color.white, textcolor=color.gray)
calcSlope(source, length) => max_bars_back(source, 5000) if not barstate.islast 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 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]
[s, a, i] = calcSlope(sourceInput, lengthInput) startPrice = i + s * (lengthInput - 1) endPrice = i var line baseLine = na if na(baseLine) and not na(startPrice) baseLine := line.new(bar_index - lengthInput + 1, startPrice, bar_index, endPrice, width=1, extend=extendStyle, color=color.new(colorLower, 0)) else line.set_xy1(baseLine, bar_index - lengthInput + 1, startPrice) line.set_xy2(baseLine, bar_index, endPrice) na
calcDev(source, length, slope, average, intercept) => 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(sourceInput, lengthInput, s, a, i) upperStartPrice = startPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev) upperEndPrice = endPrice + (useUpperDevInput ? upperMultInput * stdDev : upDev) var line upper = na lowerStartPrice = startPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev) lowerEndPrice = endPrice + (useLowerDevInput ? -lowerMultInput * stdDev : -dnDev) var line lower = na if na(upper) and not na(upperStartPrice) upper := line.new(bar_index - lengthInput + 1, upperStartPrice, bar_index, upperEndPrice, width=1, extend=extendStyle, color=color.new(colorUpper, 0)) else line.set_xy1(upper, bar_index - lengthInput + 1, upperStartPrice) line.set_xy2(upper, bar_index, upperEndPrice) na if na(lower) and not na(lowerStartPrice) lower := line.new(bar_index - lengthInput + 1, lowerStartPrice, bar_index, lowerEndPrice, width=1, extend=extendStyle, color=color.new(colorUpper, 0)) else line.set_xy1(lower, bar_index - lengthInput + 1, lowerStartPrice) line.set_xy2(lower, bar_index, lowerEndPrice) na linefill.new(upper, baseLine, color = colorUpper) linefill.new(baseLine, lower, color = colorLower)
float trend = math.sign(startPrice - endPrice)
// Pearson's R var label r = na label.delete(r[1]) if showPearsonInput and not na(pearsonR) r := label.new(bar_index - lengthInput + 1, lowerStartPrice, str.tostring(pearsonR, "#.################"), color = color.new(color.white, 100), textcolor=color.new(colorUpper, 0), size=size.normal, style=label.style_label_up)
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.