Buy Sell Indicator
// by rufey study("Buy Sell Indicator", overlay=true) len = input(title="Length", type=integer, defval=6) th = input(title="threshold", type=integer, defval=6) TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1]))) DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0 DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0 SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange SmoothedDirectionalMovementPlus = nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus SmoothedDirectionalMovementMinus = nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100 DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100 DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100 ADX = sma(DX, len) //plot(DIPlus, color=green, title="DI+") //plot(DIMinus, color=red, title="DI-") //plot(ADX, color=black, title="ADX") //hline(th, color=black, linestyle=dashed) adxval = (DIPlus - DIMinus) * ADX //plot(adxval, color=adxval > 0 ? green : red, style=histogram, linewidth=4) length = input(6, title="BB Length") mult = input(2.0,title="BB MultFactor") lengthKC=input(6, title="KC Length") multKC = input(1.5, title="KC MultFactor") useTrueRange = input(true, title="Use TrueRange (KC)", type=bool) // Calculate BB source = open ema1 = ema(source, length) ema2 = ema(ema1, length) ema3 = ema(ema2, length) basis = 3 * (ema1 - ema2) + ema3 dev = multKC * stdev(source, length) upperBB = basis + dev lowerBB = basis - dev // Calculate KC e1 = ema(source, length) e2 = ema(e1, length) ma = 2 * e1 - e2 range = useTrueRange ? tr : (high - low) rangema = sma(range, lengthKC) upperKC = ma + rangema * multKC lowerKC = ma - rangema * multKC sqzOn = (lowerBB > lowerKC) and (upperBB < upperKC) sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC) noSqz = (sqzOn == false) and (sqzOff == false) val1 = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(source,lengthKC)), lengthKC,0) length2 = input(13, title="BB Length") lengthKC2=input(13, title="KC Length") // Calculate BB basis2 = sma(source, length2) dev2 = multKC * stdev(source, length2) upperBB2 = basis2 + dev2 lowerBB2 = basis2 - dev2 // Calculate KC ma2 = sma(source, lengthKC2) range2 = useTrueRange ? tr : (high - low) rangema2 = sma(range2, lengthKC2) upperKC2 = ma2 + rangema2 * multKC lowerKC2 = ma2 - rangema2 * multKC sqzOn2 = (lowerBB2 > lowerKC2) and (upperBB2 < upperKC2) sqzOff2 = (lowerBB2 < lowerKC2) and (upperBB2 > upperKC2) noSqz2 = (sqzOn2 == false) and (sqzOff2 == false) val2 = linreg(source - avg(avg(highest(high, lengthKC2), lowest(low, lengthKC2)),sma(source,lengthKC2)), lengthKC2,0) val = val1 + val2 bcolor = iff( val > 0, iff( val > nz(val[1]), lime, green), iff( val < nz(val[1]), red, maroon)) scolor = noSqz ? blue : sqzOn ? black : gray //plot(val, color=bcolor, style=histogram, linewidth=4) //plot(0, color=scolor, style=cross, linewidth=2) length3=input(21) bt = input( 100, title="Buy Trigger") st = input( -100, title="Sell Trigger") markCrossovers=input(false, type=bool) prev(s,i) => y=abs(round(i)) s[y] calc_ttf( periods ) => bp = highest( high, periods ) - prev( lowest( low, periods ), - periods ) sp = prev( highest( high, periods ), - periods ) - lowest( low, periods ) 100 * (bp - sp) / ( 0.5*( bp + sp) ) ttf = calc_ttf( length3 ) res = adxval*abs(val) //plot(res, color=res > 0 ? green : red, style=histogram, linewidth=4) plotshape(res > 0 and res[1] <= 0 and ttf < 0 ? res : na, color=lime, style=shape.arrowup, text="Buy") plotshape(res < 0 and res[1] >= 0 and ttf > 50 ? res : na, color=red, style=shape.arrowdown, text="Sell") //plotshape(res > 0 and res[1] <= 0 ? res : na, color=lime, style=shape.arrowup, text="Buy") //plotshape(res < 0 and res[1] >= 0 ? res : na, color=red, style=shape.arrowdown, text="Sell")