재테크
[Trading View] Pine editor(Custom Indicators) - SQZMOM & MACD Impulse & SUPERTREND
cyy1211
2025. 1. 1. 19:17
728x90
반응형
[커스텀 보조지표 - 스퀴즈모먼트 + 맥디임펄스 + 슈퍼트랜드 올인원]
- 전체 레이아웃
- 보조지표 레이아웃
[//@version=5
strategy(shorttitle='SQZMOM & MACD Impulse & SUPERTREND', title='SQZMOM & MACD Impulse & SUPERTREND', overlay=false, process_orders_on_close = true)
//SuperTrend
Periods = input(title='ATR Period', defval=10)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)
showsignals = input(title='Show Buy/Sell Signals ?', defval=true)
highlighting = input(title='Highlighter On/Off ?', defval=true)
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? trend == 1 ? color.green : color.white : color.white
shortFillColor = highlighting ? trend == -1 ? color.red : color.white : color.white
fill(mPlot, upPlot, title='UpTrend Highligter', color=longFillColor, transp=90)
fill(mPlot, dnPlot, title='DownTrend Highligter', color=shortFillColor, transp=90)
alertcondition(buySignal, title='SuperTrend Buy', message='SuperTrend Buy!')
alertcondition(sellSignal, title='SuperTrend Sell', message='SuperTrend Sell!')
changeCond = trend != trend[1]
alertcondition(changeCond, title='SuperTrend Direction Change', message='SuperTrend has changed direction!')
lengthMA = input(34, title="MA Length")
lengthSignal = input(9, title="Signal Length")
calc_ema(src, length) =>
ta.ema(src, length)
calc_zlema(src, length) =>
ema1 = calc_ema(src, length)
ema2 = calc_ema(ema1, length)
d = ema1 - ema2
ema1 + d
hi = ta.sma(high, lengthMA)
lo = ta.sma(low, lengthMA)
mi = calc_zlema(src, lengthMA)
md = (mi > hi) ? (mi - hi) : (mi < lo) ? (mi - lo) : 0
sb = ta.sma(md, lengthSignal)
sh = md - sb
mdc = src > mi ? src > hi ? color.lime : color.green : src < lo ? color.red : color.orange
//SQZMOM
length = input(20, title='BB Length')
mult = input(2.0, title='BB MultFactor')
lengthKC = input(20, title='KC Length')
multKC = input(1.5, title='KC MultFactor')
useTrueRange = input(true, title='Use TrueRange (KC)')
// Calculate BB
source = close
basis = ta.sma(source, length)
dev = multKC * ta.stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev
// Calculate KC
ma = ta.sma(source, lengthKC)
range_1 = useTrueRange ? ta.tr : high - low
rangema = ta.sma(range_1, 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
val = ta.linreg(source - math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)), ta.sma(close, lengthKC)), lengthKC, 0)
iff_1 = val > nz(val[1]) ? color.lime : color.green // Long 진입 모멘텀
iff_2 = val < nz(val[1]) ? color.red : color.maroon // Short 진입 모멘텀
bcolor = val > 0 ? iff_1 : iff_2
scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray
cond1 = (not noSqz) and (not sqzOn) // true 변동성이 크다, false 변동성이 작다
cond2 = val > 0 // true면 Long, false면 Short
check1 = cond1 ? 1 : 0
check2 = cond2 ? 1 : 0
isVolaStart = ta.change(check1) == 1
isMomentumChange = ta.change(check2) != 0
isLong = isVolaStart and cond2
isShort = isVolaStart and not cond2
if isLong
strategy.entry("롱", strategy.long)
if isMomentumChange
strategy.close("롱")
plot(val, color=bcolor, style=plot.style_histogram, linewidth=4)
plot(0, color=scolor, style=plot.style_cross, linewidth=2)
// Macd Impulse
// Calculate MACD
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
// Set the colors for the background boxes
upTrendColor = color.new(color.green, 90) // Light green for up trend
downTrendColor = color.new(color.red, 90) // Light red for down trend
neutralColor = color.new(color.yellow, 90) // Light yellow for neutral trend
// Draw the background boxes based on the trend
bgcolor(md > 0 ? upTrendColor : md < 0 ? downTrendColor : neutralColor, offset = -1)
bgcolor(sb > 0 ? upTrendColor : sb < 0 ? downTrendColor : neutralColor)
bgcolor(color.rgb(230, 252, 255), offset = 1) // Neutral trend box around the Zero Line
// Plot Impulse Histo in the upper row (row = 1)
plot(md, title="ImpulseHisto", color=color.blue, linewidth=2, style=plot.style_histogram, offset = -1)
// Plot Impulse MACD CD Signal in the middle row (row = 0)
plot(sb, title="ImpulseMACDCDSignal", color=color.maroon, linewidth=2, style=plot.style_histogram)
// Plot Zero Line in the lower row (row = -1)
plot(0, title="Zero Line", color=color.rgb(255, 68, 208), linewidth=1, offset = 1)
// Plot MACD lines
plot(macdLine, title="MACD Line", color=color.blue)
plot(signalLine, title="Signal Line", color=color.orange)
// Enable bar colors option
ebc = input(false, title="Enable bar colors")
barcolor(ebc ? mdc : na)
// Find crossover points
crossoverUp = ta.crossover(macdLine, signalLine)
crossunderDown = ta.crossunder(macdLine, signalLine)
// Plot green and red circles at crossover points
plotshape(series=crossoverUp ? macdLine : na, color=color.green, style=shape.arrowup, location=location.absolute, offset=-1)
plotshape(series=crossunderDown ? macdLine : na, color=color.red, style=shape.arrowdown, location=location.absolute, offset=-1)
// Alerts
alertcondition(crossoverUp, title="MACD Buy Signal", message="MACD crossover: Buy signal!")
alertcondition(crossunderDown, title="MACD Sell Signal", message="MACD crossover: Sell signal!")
- Pine 에디터 코드
728x90
반응형