algotpandsl
algotpandsl
// # ========================================================================= #
// # | Indicator |
// # ========================================================================= #
indicator(title = "[Pieki Algo] Signals And Overlays | [V.2.0]", shorttitle =
"Signals And Overlays", overlay = true, max_lines_count = 500, max_labels_count =
500, max_boxes_count = 500)
// # ==== [DASHBOARD]
gr_dashboard = "DASHBOARD SETTINGS"
showDash = input(true, "Dashboard",group = gr_dashboard)
showDashtrend = input(true, "Trend Dashboard",group = gr_dashboard)
dashLoc = input.string("Bottom Right", "Location", options = ["Top
Right", "Bottom Right", "Bottom Left"], group = gr_dashboard)
textSize = input.string("Normal", "Size", options = ["Tiny",
"Small", "Normal"], group = gr_dashboard)
// # ==== [RISK MANAGEMENT]
gr_risk = "RISK Management"
levels = input.bool(false, "Show TP/SL Levels" , group = gr_risk ,
inline = "1")
dynamic_tp = input.bool(false, "Dynamic TP" , group = gr_risk , inline
= "1")
slrisk = input.int(1, "SL %", inline = "2", group = gr_risk)
tp1risk = input.int(1, "TP 1 %", inline = "3", group = gr_risk)
tp2risk = input.int(2, "TP 2 %", inline = "4", group = gr_risk)
tp3risk = input.int(3, "TP 3 %", inline = "5", group = gr_risk)
// # ==== [ALERTS]
gr_alert = "ALERT SETTINGS"
sigaler = input.bool(false, "Signals Alerts", inline = "1", group =
gr_alert, tooltip = "1")
trendtraceraalert = input.bool(false, "Trend Tracer", inline = "2", group =
gr_alert, tooltip = "1")
revzonesalert = input.bool(false, "Reversal Zones", inline = "3", group =
gr_alert, tooltip = "1")
cloudalert = input.bool(false, "Cloud", inline = "4", group =
gr_alert, tooltip = "1")
// # ============================[FUNCTIONS]============================ #
// # ==== [Trend Tracer]
// ATR
get_atr(i_length) =>
ta.atr(i_length)
os = 0,max = 0.,min = 0.
os := src > upper ? 1 : src < lower ? 0 : os[1]
spt = os == 1 ? lower : upper
max := ta.cross(src,spt) ? math.max(src,max[1]) : os == 1 ?
math.max(src,max[1]) : spt
min := ta.cross(src,spt) ? math.min(src,min[1]) : os == 0 ?
math.min(src,min[1]) : spt
math.avg(max,min)
// # ==== [SUPPORT RESISTANCE]
src_c = request.security(syminfo.tickerid,timef,close, gaps = barmerge.gaps_off,
lookahead = barmerge.lookahead_off)
src_o = request.security(syminfo.tickerid,timef,open, gaps = barmerge.gaps_off,
lookahead = barmerge.lookahead_off)
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (timeframe.isseconds ? 1. / 60 :
timeframe.isminutes ? 1. : timeframe.isdaily ? 60. * 24 : timeframe.isweekly ? 60.
* 24 * 7 : timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
_resInMinutes
f_timefResInMinutes(_res) =>
request.security(syminfo.tickerid, _res, f_resInMinutes())
f_timefIsIntraday(_res) =>
[intraday, daily, weekly, monthly] = request.security(syminfo.tickerid, _res,
[timeframe.isintraday, timeframe.isdaily, timeframe.isweekly, timeframe.ismonthly])
check = intraday ? "Intraday" : daily ? "Daily" : weekly ? "Weekly" : monthly ?
"Monthly" : "Error"
check
mtimef_multiplier = int (f_timefResInMinutes(timef) / f_resInMinutes())
// # ============================[GET COMPONENTS]============================ #
src = close
rsi = 14
// # ==== [SIGNALS]
rsi_smooth = 3 // RSI Smoothing for signals
wilders_Period = rsi * 2 - 1
sig_rsi = ta.rsi(src, rsi)
sig_ma_rsi = ta.ema(sig_rsi, rsi_smooth)
sig_atr_rsi = math.abs(sig_ma_rsi[1] - sig_ma_rsi)
sig_ma_atr_rsi = ta.ema(sig_atr_rsi, wilders_Period)
dar = ta.ema(sig_ma_atr_rsi, wilders_Period) * sigsensiviti
longband = 0.0
shortband = 0.0
trend1 = 0
DeltaFastAtrRsi = dar
RSIndex = sig_ma_rsi
newshortband = RSIndex + DeltaFastAtrRsi
newlongband = RSIndex - DeltaFastAtrRsi
longband := RSIndex[1] > longband[1] and RSIndex > longband[1] ?
math.max(longband[1], newlongband) : newlongband
shortband := RSIndex[1] < shortband[1] and RSIndex < shortband[1] ?
math.min(shortband[1], newshortband) : newshortband
cross_1 = ta.cross(longband[1], RSIndex)
trend1 := ta.cross(RSIndex, shortband[1]) ? 1 : cross_1 ? -1 :
nz(trend1[1], 1)
FastAtrRsiTL = trend1 == 1 ? longband : shortband
// Find all the QQE Crosses
QQExlong = 0
QQExlong := nz(QQExlong[1])
QQExshort = 0
QQExshort := nz(QQExshort[1])
QQExlong := FastAtrRsiTL < RSIndex ? QQExlong + 1 : 0
QQExshort := FastAtrRsiTL > RSIndex ? QQExshort + 1 : 0
//Conditions
qqeLong = QQExlong == 1 ? FastAtrRsiTL[1] - 50 : na
qqeShort = QQExshort == 1 ? FastAtrRsiTL[1] - 50 : na
// # ==== [TREND TRACER]
trend_length = 10 // ATR Period
lr_length = 20 // Linear Regression (LSMA)
lr_offset = 0 // Linear Regression (LSMA)
// Highest/Lowest of source
lower = ta.lowest(src, trend_length)
upper = ta.highest(src, trend_length)
if trendtracer
lower := ta.lowest(low, trend_length)
upper := ta.highest(high, trend_length)
// Trend direction
string direction = upper > upper[1] ? "up" : lower < lower[1] ? "down" : "neutral"
// Trend and signal variables
float trend = 0.0
string up_down_trend = na
bool trendtracer_buy = false
bool trendtracer_sell = false
// ATR trend logic
if direction == "up"
trend := low - atr
if trend < trend[1]
trend := trend[1]
if direction == "down"
trend := high + atr
if trend > trend[1]
trend := trend[1]
if direction == "neutral"
trend := trend[1]
// Buy/Sell signals
up_down_trend := up_down_trend[1]
if trend > trend[1]
up_down_trend := "up"
if trend < trend[1]
up_down_trend := "down"
// Buy/Sell Signals
trendtracer_buy := up_down_trend[1] == "down" and up_down_trend == "up" ? true :
false
trendtracer_sell := up_down_trend[1] == "up" and up_down_trend == "down" ? true :
false
// # ==== [REVERSAL ZONES]
rg = kama(ta.tr, revzonessens)
basis = kama(close, revzonessens)
upper1 = basis + rg * 9
upper2 = basis + rg * 11
upper3 = basis + rg * 14
lower1 = basis - rg * 9
lower2 = basis - rg * 11
lower3 = basis - rg * 14
smoothedSupert = ta.sma(supert, 2)
smoothedSupert2 = ta.sma(supert2,2)
tool = direction1 > 0 or direction1 < 0
// # ==== [RSI]
ma(x, len) => ta.rma(x, len)
upperr = ta.highest(src, rsi)
lowerr = ta.lowest(src, rsi)
r = upperr - lowerr
d = src - src[1]
dif = upperr > upperr[1] ? r
: lowerr < lowerr[1] ? -r
: d
num = ma(dif, rsi)
den = ma(math.abs(dif), rsi)
arsi = math.min(num / den * 50 + 50,100)
// # ==== [VOLUME]
volback = 13
up_bar = close > hl2
dn_bar = close < hl2
u_vol = up_bar ? volume : 0
d_vol = dn_bar ? volume : 0
up_vol = math.sum(u_vol, volback)
dn_vol = math.sum(d_vol, volback)
vol = math.min(100 * (up_vol - dn_vol) / (up_vol + dn_vol),100)
// # ==== [SQUEEZE]
var max = 0.0
var min = 0.0
max := nz(math.max(src, max - (max - src) / 50), src)
min := nz(math.min(src, min + (src - min) / 50), src)
diff = math.log(max - min)
// # ==== [MONEY FLOW]
mf = math.min(ta.mfi(src, 14),100)
// # ==== [ADX]
up = ta.change(high)
down = -ta.change(low)
trur = ta.rma(ta.tr, 14)
plus = fixnan(100 * ta.rma(up > down and up > 0 ? up : 0, 14) / trur)
minus = fixnan(100 * ta.rma(down > up and down > 0 ? down : 0, 14) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), 14)
// # ==== [TREND DETECTOR MTF]
ema = ta.ema(close, 144)
emaBull = close > ema
trend_5m = request.security(syminfo.tickerid, "5" , emaBull)
trend_15m = request.security(syminfo.tickerid, "15" , emaBull)
trend_30m = request.security(syminfo.tickerid, "30" , emaBull)
trend_1h = request.security(syminfo.tickerid, "60" , emaBull)
trend_4h = request.security(syminfo.tickerid, "240" , emaBull)
trend_1d = request.security(syminfo.tickerid, "1440", emaBull)
// # ==== [RISK MANAGEMENT]
trigger2 = qqeLong ? 1 : 0
countBull = ta.barssince(qqeLong)
countBear = ta.barssince(qqeShort)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
// # ============================[PLOTTING]============================ #
// # ==== [SIGNALS]
// Normal
y1 = low - (ta.atr(30) * 2), y1B = low - ta.atr(30)
y2 = high + (ta.atr(30) * 2), y2B = high + ta.atr(30)
buy = qqeLong and showsignals and signfilter == "None" ? label.new(bar_index, y1,
"^", xloc.bar_index, yloc.price, bullish, label.style_label_up, color.white,
size.normal) : na
sell = qqeShort and showsignals and signfilter == "None"? label.new(bar_index, y2,
"?, xloc.bar_index, yloc.price, bearish, label.style_label_down, color.white,
size.normal) : na
// Trend Tracer
trendtracer_b = trendtracer_buy and showsignals and signfilter == "Trend Tracer" ?
label.new(bar_index, y1, "^", xloc.bar_index, yloc.price, bullish,
label.style_label_up, color.white, size.normal) : na
trendtracer_s = trendtracer_sell and showsignals and signfilter == "Trend Tracer"?
label.new(bar_index, y2, "?, xloc.bar_index, yloc.price, bearish,
label.style_label_down, color.white, size.normal) : na
// Reversal zones
revzones_s = revzonessell and showsignals and signfilter == "Reversal Zones" ?
label.new(bar_index, y2, "?, xloc.bar_index, yloc.price, bearish,
label.style_label_down, color.white, size.normal) : na
revzones_b = revzonesbuy and showsignals and signfilter == "Reversal Zones" ?
label.new(bar_index, y1, "^", xloc.bar_index, yloc.price, bullish,
label.style_label_up, color.white, size.normal) : na
// Cloud
cloud_buy1 = cloud_buy and showsignals and signfilter == "Cloud" ?
label.new(bar_index, y1, "^", xloc.bar_index, yloc.price, bullish,
label.style_label_up, color.white, size.normal) : na
cloud_sell1 = cloud_sell and showsignals and signfilter == "Cloud" ?
label.new(bar_index, y2, "?, xloc.bar_index, yloc.price, bearish,
label.style_label_down, color.white, size.normal) : na
// # ==== [TREND TRACER]
p1 = plot(trendtracer ? trend : na, "ATR Trend", color = up_down_trend == "up" ?
bullish : bearish, linewidth = 2, editable = false)
// # ==== [TREND TRACER]
pp1 = plot(revzones ? upper1 : na, transp=100, editable = false)
pp2 = plot(revzones ? upper2 : na, transp=100, editable = false)
pp3 = plot(revzones ? upper3 : na, transp=100, editable = false)
pp4 = plot(revzones ? lower1 : na, transp=100, editable = false)
pp5 = plot(revzones ? lower2 : na, transp=100, editable = false)
pp6 = plot(revzones ? lower3 : na, transp=100, editable = false)
fill(pp1, pp2, color=color.new(bearish, 70), editable = false)
fill(pp2, pp3, color=color.new(bearish, 50), editable = false)
fill(pp4, pp5, color=color.new(bullish, 70), editable = false)
fill(pp5, pp6, color=color.new(bullish, 50), editable = false)
// # ==== [CLOUD]
A = plot(cloudshow ? senkouA : na,'Senkou Span A',na,offset=offset-1, editable =
false)
B = plot(cloudshow ? senkouB : na,'Senkou Span B',na,offset=offset-1, editable =
false)
fill(A,B,senkouA > senkouB ? color.new(bullish, 80) : color.new(bearish,80))
// # ==== [TREND LINES]
var uptl = trendlineshow ? line.new(na,na,na,na, color = bullish, style =
line.style_dashed, extend = extend.right) : na
var dntl = trendlineshow ? line.new(na,na,na,na, color = bearish, style =
line.style_dashed, extend = extend.right) : na
if ph and trendlineshow
uptl.set_xy1(n-offset_tl, trendlineshow ? ph : upper_tl - slope_ph *
trendlinesens)
uptl.set_xy2(n-offset_tl+1, trendlineshow ? ph - slope : upper_tl - slope_ph *
(trendlinesens+1))
if pl and trendlineshow
dntl.set_xy1(n-offset_tl, trendlineshow ? pl : lower_tl + slope_pl *
trendlinesens)
dntl.set_xy2(n-offset_tl+1, trendlineshow ? pl + slope : lower_tl + slope_pl *
(trendlinesens+1))
// # ==== [SMART TRAIL]
tool12 = plot(smarttrailshow ? ((direction1 < 0 and direction1[1] < 0) or
(direction1 > 0 and direction1[1] > 0) ? smoothedSupert : na) : na, "Extreme line",
color=smarttrailshow ? (close > ((direction1 < 0 and direction1[1] < 0) or
(direction1 > 0 and direction1[1] > 0) ? smoothedSupert : na) ? bullish :
bearish) : na, style=plot.style_linebr, editable = false)
tool2 = plot(smarttrailshow ? (tool ? smoothedSupert2 : na) : na, "Zone line",
color=color.new(color.gray, 100), display=smarttrailshow ? display.none :
display.none, editable=false, style=plot.style_linebr, editable = false)
fill(tool12, tool2, color=close > supert ? color.new(bullish, 80) :
color.new(bearish, 80), fillgaps=false)
lineColor = close > supertrend ? bullish : close < supertrend ? bearish : na
plot(smarttrailshow ? ((direction1 < 0 and direction1[1] < 0) or (direction1 > 0
and direction1[1] > 0) ? smoothedSupert : na) : na, color=smarttrailshow ?
lineColor : na, linewidth=2, style=smarttrailshow ? plot.style_line :
plot.style_line, title="Trend Area Line", editable = false)
// # ==== [COLORING CANDLE]
barcolor(QQExlong ? bullish : bearish)
// # ==== [RISK MANAGEMENT]
entry = levels ? label.new(time, close, "ENTRY | " +
str.tostring(lastTrade(close), "#.####"), xloc.bar_time, yloc.price, #00e2ff,
label.style_none, color.white, size.normal) : na
label.set_x(entry, label.get_x(entry) + math.round(ta.change(time) * 10))
label.set_y(entry, lastTrade(close))
label.delete(entry[1])
stop_y = lastTrade(slris)
stop = levels ? label.new(time, close, "SL | " + str.tostring(stop_y, "#.####"),
xloc.bar_time, yloc.price, bearish, label.style_none, color.white, size.normal) :
na
label.set_x(stop, label.get_x(stop) + math.round(ta.change(time) * 10))
label.set_y(stop, stop_y)
label.delete(stop[1])
// Dynamic TP
rsi_tp = ta.rsi(close ,14)
tpLabels(tp) =>
tp1Bull = ta.crossover (rsi_tp, 70), tp2Bull = ta.crossover (rsi_tp, 75),
tp3Bull = ta.crossover (rsi_tp, 80)
tp1Bear = ta.crossunder(rsi_tp, 30), tp2Bear = ta.crossunder(rsi_tp, 25),
tp3Bear = ta.crossunder(rsi_tp, 20)
tp1Bull := tp1Bull and (nz(ta.barssince(tp1Bull)[1], 9999) > countBull),
tp2Bull := tp2Bull and (ta.barssince(tp1Bull)[1] <= countBull), tp2Bull := tp2Bull
and (nz(ta.barssince(tp2Bull)[1], 9999) > countBull), tp3Bull := tp3Bull and
(ta.barssince(tp2Bull)[1] <= countBull), tp3Bull := tp3Bull and
(nz(ta.barssince(tp3Bull)[1], 9999) > countBull)
tp1Bear := tp1Bear and (nz(ta.barssince(tp1Bear)[1], 9999) > countBear),
tp2Bear := tp2Bear and (ta.barssince(tp1Bear)[1] <= countBear), tp2Bear := tp2Bear
and (nz(ta.barssince(tp2Bear)[1], 9999) > countBear), tp3Bear := tp3Bear and
(ta.barssince(tp2Bear)[1] <= countBear), tp3Bear := tp3Bear and
(nz(ta.barssince(tp3Bear)[1], 9999) > countBear)
if dynamic_tp
trigger ? (tp == 1 ? tp1Bull : tp == 2 ? tp2Bull : tp3Bull) : (tp == 1 ?
tp1Bear : tp == 2 ? tp2Bear : tp3Bear)
find_loc(strength) =>
ret = array.size(sr_strength)
for i = ret > 0 ? array.size(sr_strength) - 1 : na to 0 by 1
if strength <= array.get(sr_strength, i)
break
ret := i
ret
ret
if ph6 or pl6
//because of new calculation, remove old S/R levels
array.clear(sr_up_level)
array.clear(sr_dn_level)
array.clear(sr_strength)
//find S/R zones
for x = 0 to array.size(pivotvals) - 1 by 1
[hi, lo, strength] = get_sr_vals(x)
if check_sr(hi, lo, strength)
loc = find_loc(strength)
// if strength is in first levels sr then insert it to the arrays
if loc < levelssr and strength >= min_strength
array.insert(sr_strength, loc, strength)
array.insert(sr_up_level, loc, hi)
array.insert(sr_dn_level, loc, lo)
// keep size of the arrays = 5
if array.size(sr_strength) > levelssr
array.pop(sr_strength)
array.pop(sr_up_level)
array.pop(sr_dn_level)
for x = 1 to 10 by 1
line.delete(array.get(sr_lines, x))
label.delete(array.get(sr_labels, x))
label.delete(array.get(timef_labels, x))
// # ============================[Alerts]============================ #
// Signals Normal
alertcondition(sigaler ? qqeLong : na, "New Signal", message = "New Buy Signal |
Normal")
alertcondition(sigaler ? qqeShort : na, "New Signal", message = "New Sell Signal |
Normal")
// Signals Trend Tracer
alertcondition(trendtraceraalert ? trendtracer_buy : na, title="New Signal",
message = "New Buy Signal | Trend Tracer")
alertcondition(trendtraceraalert ? trendtracer_sell : na, title="New Signal",
message = "New Sell Signal | Trend Tracer")
// Signals Reversal Zones
alertcondition(revzonesalert ? revzonesbuy : na, title="New Signal", message = "New
Buy Signal | Reversal Zones")
alertcondition(revzonesalert ? revzonessell : na, title="New Signal", message =
"New Sell Signal | Reversal Zones")
// Signals Cloud
alertcondition(cloudalert ? cloud_buy : na, title="New Signal", message = "New Buy
Signal | Cloud")
alertcondition(cloudalert ? cloud_sell : na, title="New Signal", message = "New
Sell Signal | Cloud")