Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("Gaann medians MA targets", overlay=true, max_bars_back=500, max_lines_count=500, max_labels_count=500)
- // Inputs
- method1 = input.string('Median', options=['EMA', 'Median', 'SMA'])
- length1 = input(56)
- mult1 = input.int(1, minval=0, maxval=1)
- method2 = input.string('Median', options=['EMA', 'Median', 'SMA'])
- length2 = input(100)
- mult2 = input.int(1, minval=0, maxval=1)
- method3 = input.string('Median', options=['EMA', 'Median', 'SMA'])
- length3 = input(200)
- mult3 = input.int(1, minval=0, maxval=1)
- // Calculation of medians
- Avg(x, length, method) =>
- sma_1 = ta.sma(x, length)
- ema_1 = ta.ema(x, length)
- percentile_linear_interpolation_1 = ta.percentile_linear_interpolation(x, length, 50)
- method == 'SMA' ? sma_1 : method == 'EMA' ? ema_1 : percentile_linear_interpolation_1
- a1 = ta.highest(length1) - math.max(close, open)
- b1 = math.min(close, open) - ta.lowest(length1)
- c1 = math.max(close, open) + a1 * mult1
- d1 = math.min(close, open) - b1 * mult1
- a2 = ta.highest(length2) - math.max(close, open)
- b2 = math.min(close, open) - ta.lowest(length2)
- c2 = math.max(close, open) + a2 * mult2
- d2 = math.min(close, open) - b2 * mult2
- a3 = ta.highest(length3) - math.max(close, open)
- b3 = math.min(close, open) - ta.lowest(length3)
- c3 = math.max(close, open) + a3 * mult3
- d3 = math.min(close, open) - b3 * mult3
- // Calculation of volume
- volLength = input.int(20, minval=1, title='Volume Length')
- volMultiplier = input.float(1.0, title='Volume Multiplier')
- volAvg = ta.sma(volume, volLength) * volMultiplier
- // Calculation of signals and liquidity levels
- e1 = Avg(c1, length1, method1)
- f1 = Avg(d1, length1, method1)
- gx1 = 0
- cross_1 = ta.cross(close, f1)
- gx1 := ta.cross(close, e1) ? 1 : cross_1 ? 0 : nz(gx1[1])
- e2 = Avg(c2, length2, method2)
- f2 = Avg(d2, length2, method2)
- gx2 = 0
- cross_2 = ta.cross(close, f2)
- gx2 := ta.cross(close, e2) ? 1 : cross_2 ? 0 : nz(gx2[1])
- e3 = Avg(c3, length3, method3)
- f3 = Avg(d3, length3, method3)
- gx3 = 0
- cross_3 = ta.cross(close, f3)
- gx3 := ta.cross(close, e3) ? 1 : cross_3 ? 0 : nz(gx3[1])
- // Calculation of liquidity levels with volume
- hilo1 = gx1 * f1 + (1 - gx1) * e1
- css1 = gx1 == 1 ? color.green : color.red
- plot(hilo1, color=css1, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
- hilo2 = gx2 * f2 + (1 - gx2) * e2
- css2 = gx2 == 1 ? color.green : color.red
- plot(hilo2, color=css2, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
- hilo3 = gx3 * f3 + (1 - gx3) * e3
- css3 = gx3 == 1 ? color.green : color.red
- plot(hilo3, color=css3, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
- //end of the code
- //@version=5
- //indicator(title='HiLo Activator', overlay=true)
- length = input.int(3, minval=1)
- displace = input.int(0, minval=0)
- highsma = ta.sma(high, length)
- lowsma = ta.sma(low, length)
- iff_1 = close < lowsma[displace] ? -1 : 0
- swing = close > highsma[displace] ? 1 : iff_1
- mah = ta.highest(high, length)
- mal = ta.lowest(low, length)
- var stop = 0.0
- iff_2 = swing == -1 ? mah : stop[1]
- stop := swing == 1 ? mal : iff_2
- linecolor = stop < low ? color.green : color.red
- plot(stop, show_last=1, linewidth=1, trackprice=true, transp=0)
- //end of this part
- //study(title="TrapTrading (Study)", overlay=true)
- // input
- counterTrend = input(defval=false, title='Trade Mode (ON: Counter Trend OFF: Trend Following)')
- len1 = input.int(defval=20, title='Period (1-200)', minval=1, maxval=200)
- multiple = input(defval=0.7, title='Multiple')
- m1 = close - close[len1]
- lowest_1 = ta.lowest(math.abs(m1), len1)
- highest_1 = ta.highest(math.abs(m1), len1)
- controlPoint = counterTrend ? lowest_1 == math.abs(m1) : highest_1 == math.abs(m1)
- baseLine = ta.valuewhen(controlPoint, math.avg(close, close[len1]), 0)
- // trap line
- atr = ta.valuewhen(controlPoint, ta.highest(math.max(math.max(ta.atr(len1), ta.atr(len1 * 2)), ta.atr(len1 * 3)), len1), 0)
- line1Up = baseLine + atr * multiple
- line2Up = baseLine + atr * 2 * multiple
- line3Up = baseLine + atr * 3 * multiple
- line4Up = baseLine + atr * 4 * multiple
- line5Up = baseLine + atr * 5 * multiple
- line6Up = baseLine + atr * 6 * multiple
- line7Up = baseLine + atr * 7 * multiple
- line8Up = baseLine + atr * 8 * multiple
- line9Up = baseLine + atr * 9 * multiple
- line10Up = baseLine + atr * 10 * multiple
- line1Down = baseLine - atr * multiple
- line2Down = baseLine - atr * 2 * multiple
- line3Down = baseLine - atr * 3 * multiple
- line4Down = baseLine - atr * 4 * multiple
- line5Down = baseLine - atr * 5 * multiple
- line6Down = baseLine - atr * 6 * multiple
- line7Down = baseLine - atr * 7 * multiple
- line8Down = baseLine - atr * 8 * multiple
- line9Down = baseLine - atr * 9 * multiple
- line10Down = baseLine - atr * 10 * multiple
- // draw
- //barcolor(controlPoint ? color.yellow : close >= baseLine ? color.teal : color.red, title="Candle Color")
- // find which bar is 5 days away from the current time
- milliseconds_in_5days = 1000 * 60 * 60 * 24 * 1 // millisecs * secs * min * hours * days
- leftborder = timenow - time < milliseconds_in_5days // true or na when false
- rightborder = barstate.islast
- plot(baseLine, title='Base Line', color=color.new(color.purple, 0), linewidth=1, style=plot.style_stepline, show_last=1, linewidth=1, trackprice=true, transp=0)
- //plot(leftborder?line1Up:na, title="1Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line2Up:na, title="2Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line3Up:na, title="3Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line4Up:na, title="4Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line5Up:na, title="5Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line6Up:na, title="6Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line7Up:na, title="7Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line8Up:na, title="8Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line9Up:na, title="9Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line10Up:na, title="10Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line1Down:na, title="1Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line2Down:na, title="2Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line3Down:na, title="3Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line4Down:na, title="4Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line5Down:na, title="5Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line6Down:na, title="6Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line7Down:na, title="7Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line8Down:na, title="8Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line9Down:na, title="9Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //plot(leftborder?line10Down:na, title="10Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
- //end of this part
- upperMult = input(title='Upper Deviation', defval=2)
- lowerMult = input(title='Lower Deviation', defval=-2)
- useUpperDev = input(title='Use Upper Deviation', defval=true)
- useLowerDev = input(title='Use Lower Deviation', defval=true)
- showPearson = input(title='Show Pearson\'s R', defval=true)
- extendLines = input(title='Extend Lines', defval=false)
- len = input(title='Count', defval=100)
- src = input(title='Source', defval=close)
- extend = extendLines ? extend.right : extend.none
- calcSlope(src, len) =>
- if not barstate.islast or len <= 1
- [float(na), float(na), float(na)]
- else
- sumX = 0.0
- sumY = 0.0
- sumXSqr = 0.0
- sumXY = 0.0
- for i = 0 to len - 1 by 1
- val = src[i]
- per = i + 1.0
- sumX += per
- sumY += val
- sumXSqr += per * per
- sumXY += val * per
- sumXY
- slope = (len * sumXY - sumX * sumY) / (len * sumXSqr - sumX * sumX)
- average = sumY / len
- intercept = average - slope * sumX / len + slope
- [slope, average, intercept]
- [s, aLR, i] = calcSlope(src, len)
- startPrice = i + s * (len - 1)
- endPrice = i
- var line baseLineLR = na
- if na(baseLineLR) and not na(startPrice)
- baseLineLR := line.new(bar_index - len + 1, startPrice, bar_index, endPrice, width=4, extend=extend, color=color.red)
- baseLineLR
- else
- line.set_xy1(baseLineLR, bar_index - len + 1, startPrice)
- line.set_xy2(baseLineLR, bar_index, endPrice)
- na
- calcDev(src, len, slope, average, intercept) =>
- upDev = 0.0
- dnDev = 0.0
- stdDevAcc = 0.0
- dsxx = 0.0
- dsyy = 0.0
- dsxy = 0.0
- periods = len - 1
- daY = intercept + slope * periods / 2
- val = intercept
- for i = 0 to periods by 1
- price = high[i] - val
- if price > upDev
- upDev := price
- upDev
- price := val - low[i]
- if price > dnDev
- dnDev := price
- dnDev
- price := src[i]
- dxt = price - average
- dyt = val - daY
- price -= val
- stdDevAcc += price * price
- dsxx += dxt * dxt
- dsyy += dyt * dyt
- dsxy += dxt * dyt
- val += slope
- val
- 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(src, len, s, aLR, i)
- upperStartPrice = startPrice + (useUpperDev ? upperMult * stdDev : upDev)
- upperEndPrice = endPrice + (useUpperDev ? upperMult * stdDev : upDev)
- var line upper = na
- lowerStartPrice = startPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
- lowerEndPrice = endPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
- var line lower = na
- if na(upper) and not na(upperStartPrice)
- upper := line.new(bar_index - len + 1, upperStartPrice, bar_index, upperEndPrice, width=4, extend=extend, color=#0000ff)
- upper
- else
- line.set_xy1(upper, bar_index - len + 1, upperStartPrice)
- line.set_xy2(upper, bar_index, upperEndPrice)
- na
- if na(lower) and not na(lowerStartPrice)
- lower := line.new(bar_index - len + 1, lowerStartPrice, bar_index, lowerEndPrice, width=4, extend=extend, color=#0000ff)
- lower
- else
- line.set_xy1(lower, bar_index - len + 1, lowerStartPrice)
- line.set_xy2(lower, bar_index, lowerEndPrice)
- na
- // Pearson's R
- var label r = na
- transparent = color.new(color.white, 100)
- label.delete(r[1])
- if showPearson and not na(pearsonR)
- r := label.new(bar_index - len + 1, lowerStartPrice, str.tostring(pearsonR, '#.################'), color=transparent, textcolor=#0000ff, size=size.normal, style=label.style_label_up)
- r
- //end of this part
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement