Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("Linear Regression Channel", shorttitle="LinReg", overlay=true,max_bars_back=1000,max_lines_count = 500)
- lengthInput = input.int(200, title="Length", minval = 1, maxval = 5000)
- sourceInput = input.source(close, title="Source")
- group1 = "Channel Settings"
- useUpperDevInput = input.bool(true, title="Upper Deviation", inline = "Upper Deviation", group = group1)
- upperMultInput = input.float(2.0, title="", inline = "Upper Deviation", group = group1)
- useLowerDevInput = input.bool(true, title="Lower Deviation", inline = "Lower Deviation", group = group1)
- lowerMultInput = input.float(2.0, title="", inline = "Lower Deviation", group = group1)
- group2 = "Display Settings"
- extendLeftInput = input.bool(false, "Extend Lines Left", group = group2)
- extendRightInput = input.bool(true, "Extend Lines Right", group = group2)
- extendStyle = switch
- extendLeftInput and extendRightInput => extend.both
- extendLeftInput => extend.left
- extendRightInput => extend.right
- => extend.none
- // group3 = "Color Settings"
- // colorUpper = input.color(color.new(color.blue, 85), "", inline = group3, group = group3)
- // colorLower = input.color(color.new(color.red, 85), "", inline = group3, group = group3)
- //*************[ Variables ]***************
- // lengthBank = math.round(lengthInput*0.05 + 0.5) // roundup 5% of length to represent Bank move direction
- // lengthBankMid = math.round(lengthBank/4 + 0.5) // mid term
- // lengthBankShort = math.round(lengthBankMid/4 + 0.5) // mid term
- 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[i]
- 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]
- //###################[ Long term Slope ]############################
- // [s, a, i] = calcSlope(sourceInput[lengthBank], lengthInput)
- [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(color.gray, 100))
- else
- line.set_xy1(baseLine, bar_index - lengthInput + 1, startPrice)
- line.set_xy2(baseLine, bar_index, endPrice)
- na
- //###################[ Mid term Slope ]############################
- lengthInputMid = math.round(lengthInput/4 + 0.5)
- [sMid, aMid, iMid] = calcSlope(sourceInput, lengthInputMid)
- // [sMid, aMid, iMid] = calcSlope(sourceInput[lengthBankMid], lengthInputMid)
- startPriceMid = iMid + sMid * (lengthInputMid - 1)
- endPriceMid = iMid
- var line baseLineMid = na
- if na(baseLineMid) and not na(startPriceMid)
- baseLineMid := line.new(bar_index - lengthInputMid + 1, startPriceMid, bar_index, endPriceMid, width=1, extend=extendStyle, color=color.new(color.gray, 100))
- else
- line.set_xy1(baseLineMid, bar_index - lengthInputMid + 1, startPriceMid)
- line.set_xy2(baseLineMid, bar_index, endPriceMid)
- na
- //###################[ Short term Slope ]############################
- lengthInputShort = math.round(lengthInputMid/4 + 0.5)
- [sShort, aShort, iShort] = calcSlope(sourceInput, lengthInputShort)
- // [sShort, aShort, iShort] = calcSlope(sourceInput[lengthBankShort], lengthInputShort)
- startPriceShort = iShort + sShort * (lengthInputShort - 1)
- endPriceShort = iShort
- var line baseLineShort = na
- if na(baseLineShort) and not na(startPriceShort)
- baseLineShort := line.new(bar_index - lengthInputShort + 1, startPriceShort, bar_index, endPriceShort, width=1, extend=extendStyle, color=color.new(color.gray, 100))
- else
- line.set_xy1(baseLineShort, bar_index - lengthInputShort + 1, startPriceShort)
- line.set_xy2(baseLineShort, bar_index, endPriceShort)
- 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]
- //###################[ Long term Standard Deviation ]############################
- // [stdDev, pearsonR, upDev, dnDev] = calcDev(sourceInput[lengthBank], lengthInput, s, a, i)
- [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(color.green, 100))
- 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(color.green, 100))
- else
- line.set_xy1(lower, bar_index - lengthInput + 1, lowerStartPrice)
- line.set_xy2(lower, bar_index, lowerEndPrice)
- na
- linefill.new(upper, baseLine, color = s>0? color.new(color.red,90):color.new(color.green,80) )
- linefill.new(baseLine, lower, color = s>0? color.new(color.red,80):color.new(color.green,90) )
- //###################[ Mid term Standard Deviation ]############################
- // [stdDevMid, pearsonRMid, upDevMid, dnDevMid] = calcDev(sourceInput[lengthBankMid], lengthInputMid, sMid, aMid, iMid)
- [stdDevMid, pearsonRMid, upDevMid, dnDevMid] = calcDev(sourceInput, lengthInputMid, sMid, aMid, iMid)
- upperStartPriceMid = startPriceMid + (useUpperDevInput ? upperMultInput * stdDevMid : upDevMid)
- upperEndPriceMid = endPriceMid + (useUpperDevInput ? upperMultInput * stdDevMid : upDevMid)
- var line upperMid = na
- lowerStartPriceMid = startPriceMid + (useLowerDevInput ? -lowerMultInput * stdDevMid : -dnDevMid)
- lowerEndPriceMid = endPriceMid + (useLowerDevInput ? -lowerMultInput * stdDevMid : -dnDevMid)
- var line lowerMid = na
- if na(upperMid) and not na(upperStartPriceMid)
- upperMid := line.new(bar_index - lengthInputMid + 1, upperStartPriceMid, bar_index, upperEndPriceMid, width=1, extend=extendStyle, color=color.silver)
- else
- line.set_xy1(upperMid, bar_index - lengthInputMid + 1, upperStartPriceMid)
- line.set_xy2(upperMid, bar_index, upperEndPriceMid)
- na
- if na(lowerMid) and not na(lowerStartPriceMid)
- lowerMid := line.new(bar_index - lengthInputMid + 1, lowerStartPriceMid, bar_index, lowerEndPriceMid, width=1, extend=extendStyle, color=color.silver)
- else
- line.set_xy1(lowerMid, bar_index - lengthInputMid + 1, lowerStartPriceMid)
- line.set_xy2(lowerMid, bar_index, lowerEndPriceMid)
- na
- // linefill.new(upperMid, baseLineMid, color = colorUpper)
- // linefill.new(baseLineMid, lowerMid, color = colorLower)
- //###################[ Short term Standard Deviation ]############################
- // [stdDevShort, pearsonRShort, upDevShort, dnDevShort] = calcDev(sourceInput[lengthBankShort], lengthInputShort, sShort, aShort, iShort)
- [stdDevShort, pearsonRShort, upDevShort, dnDevShort] = calcDev(sourceInput, lengthInputShort, sShort, aShort, iShort)
- upperStartPriceShort = startPriceShort + (useUpperDevInput ? upperMultInput * stdDevShort : upDevShort)
- upperEndPriceShort = endPriceShort + (useUpperDevInput ? upperMultInput * stdDevShort : upDevShort)
- var line upperShort = na
- lowerStartPriceShort = startPriceShort + (useLowerDevInput ? -lowerMultInput * stdDevShort : -dnDevShort)
- lowerEndPriceShort = endPriceShort + (useLowerDevInput ? -lowerMultInput * stdDevShort : -dnDevShort)
- var line lowerShort = na
- if na(upperShort) and not na(upperStartPriceShort)
- upperShort := line.new(bar_index - lengthInputShort + 1, upperStartPriceShort, bar_index, upperEndPriceShort, width=1, extend=extendStyle, color=color.gray)
- else
- line.set_xy1(upperShort, bar_index - lengthInputShort + 1, upperStartPriceShort)
- line.set_xy2(upperShort, bar_index, upperEndPriceShort)
- na
- if na(lowerShort) and not na(lowerStartPriceShort)
- lowerShort := line.new(bar_index - lengthInputShort + 1, lowerStartPriceShort, bar_index, lowerEndPriceShort, width=1, extend=extendStyle, color=color.gray)
- else
- line.set_xy1(lowerShort, bar_index - lengthInputShort + 1, lowerStartPriceShort)
- line.set_xy2(lowerShort, bar_index, lowerEndPriceShort)
- na
- // linefill.new(upperShort, baseLineShort, color = colorUpper)
- // linefill.new(baseLineShort, lowerShort, color = colorLower)
- //########################[ Plot DC ]###############################
- // upDC = ta.highest(high,lengthInput)
- // lowDC = ta.lowest(low,lengthInput)
- // midDC = (upDC+lowDC)/2
- // plot(upDC,style=plot.style_linebr,linewidth = 1,color=color.aqua)
- // plot(midDC,style=plot.style_linebr,linewidth = 1,color=color.yellow)
- // plot(lowDC,style=plot.style_linebr,linewidth = 1,color=color.orange)
- //########################[ Plot Median ]###############################
- // aClose = array.new_float(0)
- // for j = 0 to lengthInput
- // array.push(aClose, close[j])
- // _median = array.median(aClose)
- // _stdev = array.stdev(aClose)
- // plot(_median,style=plot.style_linebr,linewidth = 1,color=color.yellow)
- // plot(_median + _stdev*2,style=plot.style_linebr,linewidth = 1,color=color.white)
- // plot(_median - _stdev*2,style=plot.style_linebr,linewidth = 1,color=color.orange)
- // //########################[ Plot Bollinger Band ]###############################
- // [_middle, _upper, _lower] = ta.bb(close, lengthInput, 2)
- // plot(_middle,style=plot.style_linebr,linewidth = 1,color=color.yellow)
- // plot(_upper,style=plot.style_linebr,linewidth = 1,color=color.white)
- // plot(_lower,style=plot.style_linebr,linewidth = 1,color=color.orange)
- //########################[ Identify Trend ]###############################
- // _atr = ta.atr(lengthInputShort)
- var line sLine = na // support longterm
- var line rLine = na // resistance longterm
- // var box midBox = na // mid term box
- var box l0Box = na // long term current box #0
- var box l1Box = na // long term previous box #1
- var box l2Box = na // long term previous box #2
- var box l3Box = na // long term previous box #3
- // retest, turnaround and run !
- // isBreakUpMidBox = (ta.crossover(close,box.get_top(midBox)) or ta.crossover(close,box.get_bottom(midBox)) ) and close>open
- // isBreakDownMidBox = (ta.crossunder(close,box.get_top(midBox)) or ta.crossunder(close,box.get_bottom(midBox)) ) and close<open
- isStrongBuyZone = s<=0 and close>box.get_top(l0Box) and ( close>box.get_top(l1Box) or bar_index > nz(box.get_right(l1Box)) ) and ( close>box.get_top(l2Box) or bar_index > nz(box.get_right(l2Box)) ) and ( close>box.get_top(l3Box) or bar_index > nz(box.get_right(l3Box)) ) // close > top all active boxes
- isStrongSellZone = s>0 and close<box.get_bottom(l0Box) and ( close<box.get_bottom(l1Box) or bar_index > nz(box.get_right(l1Box)) ) and ( close<box.get_bottom(l2Box) or bar_index > nz(box.get_right(l2Box)) ) and ( close<box.get_bottom(l3Box) or bar_index > nz(box.get_right(l3Box)) ) // close < bottom all active boxes
- // plot(isStrongBuy? low-_atr:na, title='BUY',style=plot.style_circles,linewidth=1,color=color.white)
- // plot(isStrongSell? high+_atr:na, title='SELL',style=plot.style_circles,linewidth=1,color=color.orange)
- // plot(isBreakUpMidBox? low-_atr:na, title='Take Profit Short',style=plot.style_circles,linewidth=1,color=color.white)
- // plot(isBreakDownMidBox? high+_atr:na, title='Take Profit Long',style=plot.style_circles,linewidth=1,color=color.orange)
- if (s>0 and s[1]<=0) or (s<0 and s[1]>=0) // identify long term trend change from sideway
- l3Box := l2Box
- l2Box := l1Box
- l1Box := l0Box
- l0Box := box.new(left=bar_index-lengthInput,top=upperEndPrice,right=bar_index+lengthInput*2,bottom=lowerEndPrice,border_width = 0,bgcolor = color.new(color.gray,90))
- line.set_color(rLine,color.new(color.orange,70))
- line.set_color(sLine,color.new(color.orange,70))
- rLine := line.new(x1=bar_index-lengthInput,y1=upperEndPrice,x2=bar_index+lengthInput*2,y2=upperEndPrice,color = color.orange)
- sLine := line.new(x1=bar_index-lengthInput,y1=lowerEndPrice,x2=bar_index+lengthInput*2,y2=lowerEndPrice,color = color.orange)
- // if (sMid>0 and sMid[1]<=0) or (sMid<0 and sMid[1]>=0) // identify mid term trend change from sideway
- // midBox := box.new(left=bar_index-lengthInputMid,top=upperEndPriceMid,right=bar_index+lengthInputMid,bottom=lowerEndPriceMid,border_width = 0,bgcolor = color.new(color.gray,90))
- // if s<0 and (sMid>0 and sMid[1]<=0) // identify long term uptrend but short trend change to down trend -> take profit
- // line.new(x1=bar_index-lengthInputMid,y1=upperEndPriceMid,x2=bar_index+lengthInputMid*2,y2=upperEndPriceMid,color = color.orange)
- // line.new(x1=bar_index-lengthInputMid,y1=lowerEndPriceMid,x2=bar_index+lengthInputMid*2,y2=lowerEndPriceMid,color = color.orange)
- // if s<0 and sMid<0 and (sShort>0 and sShort[1]<=0) // identify long term uptrend but short trend change to down trend -> take profit
- // line.new(x1=bar_index-lengthInputShort,y1=upperEndPriceShort,x2=bar_index+lengthInputShort*2,y2=upperEndPriceShort,color = color.gray)
- // line.new(x1=bar_index-lengthInputShort,y1=lowerEndPriceShort,x2=bar_index+lengthInputShort*2,y2=lowerEndPriceShort,color = color.gray)
- // highBody = close>open ? close : open //get high from body, no wick
- // lowBody = close<open ? close : open //get low from body, no wick
- // dcBodyHigh = ta.highest(highBody,lengthInputMid)
- // dcBodyLow = ta.lowest(lowBody,lengthInputMid)
- // dcBodyBase = (dcBodyHigh+dcBodyLow)/2
- // plot(dcBodyHigh,color=color.white,style=plot.style_linebr,linewidth = 1)
- // plot(dcBodyLow,color=color.white,style=plot.style_linebr,linewidth = 1)
- // plot(dcBodyBase,color=color.white,style=plot.style_linebr,linewidth = 1)
- // _roc = ta.roc(3)
- // [middleC, upperC, lowerC] = ta.bb(close, lengthInput, 1)
- // plot(endPrice,color=color.yellow)
- // plot(upperEndPrice,color=color.red,linewidth = 2)
- // plot(lowerEndPrice,color=color.red,linewidth = 2)
- // plot(upperEndPriceMid,color=color.orange)
- // plot(lowerEndPriceMid,color=color.orange)
- // plot(upperEndPriceShort,color=color.yellow)
- // plot(lowerEndPriceShort,color=color.yellow)
- // plot(middleC,color=color.orange)
- // plot(upperC,color=color.orange)
- // plot(lowerC,color=color.orange)
- // isBreakUp(_lengthBank) =>
- // int counter = 0
- // isBankMove = close>upperEndPrice and _atr>upperAtr
- // for j=0 to _lengthBank
- // if close[j] > upperEndPrice[j]
- // counter += 1
- // else
- // break
- // isBankMove or counter >= _lengthBank ? true : false
- // isBreakDown(_lengthBank) =>
- // int counter = 0
- // isBankMove = close<lowerEndPrice and _atr>upperAtr
- // for j=0 to _lengthBank
- // if close[j] < lowerEndPrice[j]
- // counter += 1
- // else
- // break
- // isBankMove or counter >= _lengthBank ? true : false
- // plot(isBreakUp(lengthBank)? upperEndPrice : na ,style=plot.style_linebr,linewidth = 3,color=color.green)
- // plot(isBreakDown(lengthBank)? lowerEndPrice : na ,style=plot.style_linebr,linewidth = 3,color=color.red)
- float trend = math.sign(startPrice - endPrice)
- alertcondition(sourceInput > line.get_price(upper, bar_index) or sourceInput < line.get_price(lower, bar_index), title='Regression Channel Exited', message="The price movement has exited Regression Channel's bounds")
- alertcondition(trend[1] >= 0 and trend < 0, title='Switched to Uptrend', message='The Regression Channel trend switched from Downtrend to Uptrend')
- alertcondition(trend[1] <= 0 and trend > 0, title='Switched to Downtrend', message='The Regression Channel trend switched from Uptrend to Downtrend')
- //########################[ Identify Trend ]###############################
- var table ratings = table.new(position.top_right, 8, 2, frame_color = #000000)
- // if barstate.islastconfirmedhistory
- // table.cell(ratings, 0, 0, "bar_index", bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 1, 0, "L1 right", bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 2, 0, "L2 right", bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 3, 0, "Support price", bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 4, 0, "Resistance price", bgcolor = color.black, text_color = color.white)
- table.cell(ratings, 5, 0, "Buy bias?", bgcolor = color.black, text_color = color.green)
- table.cell(ratings, 6, 0, "Sell bias?", bgcolor = color.black, text_color = color.red)
- // table.cell(ratings, 0, 1, str.tostring(bar_index), bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 1, 1, str.tostring(box.get_right(l1Box)), bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 2, 1, str.tostring(box.get_right(l2Box)), bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 3, 1, str.tostring(line.get_price(sLine,bar_index),format.mintick ), bgcolor = color.black, text_color = color.white)
- // table.cell(ratings, 4, 1, str.tostring(line.get_price(rLine,bar_index),format.mintick ), bgcolor = color.black, text_color = color.white)
- table.cell(ratings, 5, 1, isStrongBuyZone?'Yes':'No', bgcolor = color.black, text_color = color.white)
- table.cell(ratings, 6, 1, isStrongSellZone?'Yes':'No', bgcolor = color.black, text_color = color.white)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement