Pinescript
Pinescript
// ZigZag function {
zigzag(_momentum_direction) =>
zz_goingup = _momentum_direction == 1
zz_goingdown = _momentum_direction == -1
var float zz_peak = na
var float zz_bottom = na
zz_peak := high > zz_peak[1] and zz_goingup or zz_goingdown[1] and zz_goingup ?
high : nz(zz_peak[1])
zz_bottom := low < zz_bottom[1] and zz_goingdown or zz_goingup[1] and
zz_goingdown ? low : nz(zz_bottom[1])
zigzag = zz_goingup and zz_goingdown[1] ? zz_bottom[1] : zz_goingup[1] and
zz_goingdown ? zz_peak[1] : na
zigzag
// } End of ZigZag function
ema = ta.ema(esrc,elen)
plot(ema, "EMA")
// MACD {
fast_length = input.int(title='Fast Length', defval=12, group='if MACD Selected',
inline='macd')
slow_length = input.int(title='Slow Length', defval=26, group='if MACD Selected',
inline='macd')
src = input.source(title='Source', defval=close, group='if MACD Selected',
inline='macd')
signal_length = input.int(title='Signal Smoothing', minval=1, maxval=50, defval=9,
group='if MACD Selected', inline='macd')
sma_source = input.string(title='Oscillator MA Type', defval='EMA', options=['SMA',
'EMA'], group='if MACD Selected', inline='macd')
sma_signal = input.string(title='Signal Line MA Type', defval='EMA',
options=['SMA', 'EMA'], group='if MACD Selected', inline='macd')
// Moving Averages {
smoothing_type = input.string(title='Average type', defval='SMA', options=['EMA',
'SMA', 'WMA', 'VWMA', 'HMA', 'RMA', 'DEMA'], inline='movingaverage', group='if
Moving Average selected')
ma_length = input.int(20, title='Length', inline='movingaverage', group='if Moving
Average selected')
moving_average(_series, _length, _smoothing) =>
_smoothing == 'EMA' ? ta.ema(_series, _length) : _smoothing == 'SMA' ?
ta.sma(_series, _length) : _smoothing == 'WMA' ? ta.wma(_series, _length) :
_smoothing == 'VWMA' ? ta.vwma(_series, _length) : _smoothing == 'HMA' ?
ta.hma(_series, _length) : _smoothing == 'RMA' ? ta.rma(_series, _length) :
_smoothing == 'DEMA' ? 2 * ta.ema(_series, _length) - ta.ema(ta.ema(_series,
_length), _length) : ta.ema(_series, _length)
movingaverage = moving_average(close, ma_length, smoothing_type)
maUP = movingaverage > movingaverage[1] and movingaverage[2] > movingaverage[1]
maDOWN = movingaverage < movingaverage[1] and movingaverage[2] < movingaverage[1]
// } End of Moving Averages
// QQE {
RSI_Period = input.int(14, title='RSI Length', inline='qqe', group='if QQE
selected')
qqeslow = input.float(4.238, title='QQE Factor', inline='qqe', group='if QQE
selected')
SFslow = input.int(5, title='RSI Smoothing', inline='qqe', group='if QQE selected')
ThreshHold = input.int(10, title='Thresh-hold', inline='qqe', group='if QQE
selected')
rsi_currenttf = ta.rsi(close, RSI_Period)
// { Force detection
rsi5 = ta.rsi(close, 5)
ob = 80
os = 20
barssince_momentumUP = ta.barssince(momentumUP)
barssince_momentumDOWN = ta.barssince(momentumDOWN)
momentum_DOWN_was_force_up = momentumDOWN and (barssince_momentumUP >=
ta.barssince(rsi5 > ob))[1]
momentum_UP_was_force_down = momentumUP and (barssince_momentumDOWN >=
ta.barssince(rsi5 < os))[1]
zzcolor_rsi5 = momentum_DOWN_was_force_up ? color.lime : momentum_UP_was_force_down
? color.red : color.black
// } End of Force detection
ZigZag = zigzag(momentum_direction)
plot(ZigZag, linewidth=5, color=color_zigzag_lines ? zzcolor_rsi5 : color.black,
title='ZIGZAG', style=plot.style_line, transp=0)
if t_type == "Scalping"
hma1 := 10
hma2 := 20
hma3 := 100
if t_type == "Intraday"
hma1 := 20
hma2 := 50
hma3 := 100
ma1 = ta.hma(close,hma1)
ma2 = ta.hma(close,hma2)
ma3 = ta.hma(close,hma3)
l_avg = math.avg(ma1,ma2,ma3)
l_col = color.gray
plot(ma3,color = l_col,linewidth = 2)
var co = 0
if golong1
co:=1
if goshort1
co:=-1
// GoShort = momentumDOWN and (not momentum_DOWN_was_force_up) and scn and co==-1
// GoLong = momentumUP and (not momentum_UP_was_force_down) and bcn and co==1
pl = ta.valuewhen(momentumUP, ZigZag, 0)
ph = ta.valuewhen(momentumDOWN, ZigZag, 0)
if GoLong
stoploss_long := low < pl ? low : pl
stoploss_long
if GoShort
stoploss_short := high > ph ? high : ph
stoploss_short
TakeProfitLevel=input(200)
if GoLong
alertsyntax_golong = 'long slprice=' + str.tostring(stoploss_long) + ' tp=' +
str.tostring(TakeProfitLevel)
alert(message=alertsyntax_golong, freq=alert.freq_once_per_bar_close)
alertcondition(GoLong, "Momentum-based ZigZag + EMA : BUY")
if GoShort
alertsyntax_goshort = 'short slprice=' + str.tostring(stoploss_short) + ' tp='
+ str.tostring(TakeProfitLevel)
alert(message=alertsyntax_goshort, freq=alert.freq_once_per_bar_close)
alertcondition(GoShort, "Momentum-based ZigZag + EMA : SELL")