Nas 2.0
Nas 2.0
// Risk Management
levels = input.bool(false, "Show TP/SL Levels" , group = "Risk Management" ,
inline = "MMDB2")
lvlLines = input.bool(false, "Show Lines ", inline="levels", group = "Risk
Management")
linesStyle = input.string("SOLID", "", ["SOLID", "DASHED", "DOTTED"],
inline="levels", group = "Risk Management")
lvlDistance = input.int(1, "Distance", 1, inline="levels2", group = "Risk
Management")
lvlDecimals = input.int(2, " Decimals", 1, 8, inline="levels2", group = "Risk
Management")
atrRisk = input.int(1, "Risk % ", 1, group = "Risk Management" ,
inline="levels3")
atrLen = input.int(14, " ATR Length", 1, group = "Risk Management" ,
inline="levels3")
decimals = lvlDecimals == 1 ? "#.#" : lvlDecimals == 2 ? "#.##" : lvlDecimals == 3
? "#.###" : lvlDecimals == 4 ? "#.####" : lvlDecimals == 5 ? "#.#####" :
lvlDecimals == 6 ? "#.######" : lvlDecimals == 7 ? "#.#######" : "#.########"
// Heikinashi Request
myTicker = ticker.heikinashi(syminfo.tickerid)
ha_open = optimize ? request.security(myTicker, timeframe.period, open,
lookahead=barmerge.lookahead_off) : open
ha_close = optimize ? request.security(myTicker, timeframe.period, close,
lookahead=barmerge.lookahead_off) : close
ha_Ohlc4 = optimize ? request.security(myTicker, timeframe.period, ohlc4,
lookahead=barmerge.lookahead_off) : ohlc4
// Signal Generation
supertrend(_src, factor, atrLen) =>
atr = ta.atr(atrLen)
upperBand = _src + factor * atr
lowerBand = _src - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or ha_close[1] < prevLowerBand ?
lowerBand : prevLowerBand
upperBand := upperBand < prevUpperBand or ha_close[1] > prevUpperBand ?
upperBand : prevUpperBand
int direction = na
float superTrend = na
prevSuperTrend = superTrend[1]
if na(atr[1])
direction := 1
else if prevSuperTrend == prevUpperBand
direction := ha_close > upperBand ? -1 : 1
else
direction := ha_close < lowerBand ? 1 : -1
superTrend := direction == -1 ? lowerBand : upperBand
[superTrend, direction]
//
// SMA
ocAvg = math.avg(ha_open, ha_open)
sma4 = ta.sma(ha_close, 8)
sma5 = ta.sma(ha_close, 9)
psar = ta.sar(0.02, 0.02, 0.2)
// Colors
green = #2BBC4D, green2 = #00DD00
red = #C51D0B, red2 = #DD0000
barcolor(colorVar)
// High Lows
y1 = low - (ta.atr(30) * 6), y1B = low - ta.atr(30)
y2 = high + (ta.atr(30) * 6), y2B = high + ta.atr(30)
buy = bull ? label.new(bar_index, y1, sma4 >= sma5 ? "Buy" : "Strong Buy",
xloc.bar_index, yloc.price, #00CC00, label.style_label_up,color.white, size.normal)
: na
sell = bear ? label.new(bar_index, y2, sma4 <= sma5 ? "Sell" : "Strong Sell",
xloc.bar_index, yloc.price, #CC0000, label.style_label_down, color.white,
size.normal) : na
trigger2 = bull ? 1 : 0
countBull = ta.barssince(bull)
countBear = ta.barssince(bear)
trigger = nz(countBull, bar_index) < nz(countBear, bar_index) ? 1 : 0
atrBand = ta.atr(atrLen) * atrRisk
atrStop = trigger == 1 ? low - atrBand : high + atrBand
stop_y = lastTrade(atrStop)
stop = levels ? label.new(time, close, "SL " + str.tostring(stop_y, decimals),
xloc.bar_time, yloc.price, red2, label.style_label_left, color.white,
size.normal) : na
label.set_x(stop, label.get_x(stop) + math.round(ta.change(time) * lvlDistance))
label.set_y(stop, stop_y)
label.delete(stop[1])
if bull
shortLabel := na
longHigh := high
longLabel := label.new(bar_index, high, str.tostring(high),style =
label.style_label_down, color=color.new(green, 20))
if bear
longLabel := na
shortLow := low
shortLabel := label.new(bar_index, low, str.tostring(low), style =
label.style_label_up, color = color.new(red, 20))
//
text_color = #d1d4dc
table_color = color.gray
uptrend_indicator = "🟢"
downtrend_indicator = "🔴"
neutraltrend_indicator = ""
ema_length = 50
ema_src =close
max_table_size = 200
min_table_size = 10
check_impulse() =>
impulseLength = 34
impulseStrength = 9
hi = calc_smma(high, impulseLength)
lo = calc_smma(low, impulseLength)
mi = calc_zlema(hlc3, impulseLength)
md = (mi > hi) ? (mi - hi) : (mi < lo) ? (mi - lo) : 0
md_prev = (mi[1] > hi[1]) ? (mi[1] - hi[1]) : (mi[1] < lo[1]) ? (mi[1] - lo[1])
: 0
sb = ta.sma(md, impulseStrength)
sb_prev = ta.sma(md_prev, impulseStrength)
sh = md - sb
sh_prev = md_prev - sb_prev
get_candle_values() =>
current_candle = ta.ema(ema_src, ema_length)
previous_candle = current_candle[1]
[current_candle, previous_candle, check_impulse()]
if (barstate.islast)
if (show_1m)
table.cell(t, 1, 2, "1m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 2, get_trend_indicator(current_1m, previous_1m,
impulse_1m), text_color = text_color, text_size = table_text_size)
if (show_3m)
table.cell(t, 1, 3, "3m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 3, get_trend_indicator(current_3m, previous_3m,
impulse_3m), text_color = text_color, text_size = table_text_size)
if (show_5m)
table.cell(t, 1, 4, "5m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 4, get_trend_indicator(current_5m, previous_5m,
impulse_5m), text_color = text_color, text_size = table_text_size)
if (show_8m)
table.cell(t, 1, 5, "8m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 5, get_trend_indicator(current_8m, previous_8m,
impulse_8m), text_color = text_color, text_size = table_text_size)
if (show_12m)
table.cell(t, 1, 6, "12m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 6, get_trend_indicator(current_12m, previous_12m,
impulse_12m), text_color = text_color, text_size = table_text_size)
if (show_15m)
table.cell(t, 1, 7, "15m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 7, get_trend_indicator(current_15m, previous_15m,
impulse_15m), text_color = text_color, text_size = table_text_size)
if (show_20m)
table.cell(t, 1, 8, "20m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 8, get_trend_indicator(current_20m, previous_20m,
impulse_20m), text_color = text_color, text_size = table_text_size)
if (show_30m)
table.cell(t, 1, 9, "30m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 9, get_trend_indicator(current_30m, previous_30m,
impulse_30m), text_color = text_color, text_size = table_text_size)
if (show_45m)
table.cell(t, 1, 10, "45m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 10, get_trend_indicator(current_45m, previous_45m,
impulse_45m), text_color = text_color, text_size = table_text_size)
if (show_1h)
table.cell(t, 1, 11, "1h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 11, get_trend_indicator(current_1h, previous_1h,
impulse_1h), text_color = text_color, text_size = table_text_size)
if (show_2h)
table.cell(t, 1, 12, "2h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 12, get_trend_indicator(current_2h, previous_2h,
impulse_2h), text_color = text_color, text_size = table_text_size)
if (show_3h)
table.cell(t, 1, 13, "3h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 13, get_trend_indicator(current_3h, previous_3h,
impulse_3h), text_color = text_color, text_size = table_text_size)
if (show_4h)
table.cell(t, 1, 14, "4h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 14, get_trend_indicator(current_4h, previous_4h,
impulse_4h), text_color = text_color, text_size = table_text_size)
if (show_D)
table.cell(t, 1, 15, "D", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 15, get_trend_indicator(current_D, previous_D, impulse_D),
text_color = text_color, text_size = table_text_size)
if (show_W)
table.cell(t, 1, 16, "W", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 16, get_trend_indicator(current_W, previous_W, impulse_W),
text_color = text_color, text_size = table_text_size)
if (show_M)
table.cell(t, 1, 17, "M", text_color = text_color, text_size =
table_text_size)
table.cell(t, 5, 17, get_trend_indicator(current_M, previous_M, impulse_M),
text_color = text_color, text_size = table_text_size)
confidence(pearsonR) =>
switch
pearsonR < 0.2 => "Extremely Weak"
pearsonR < 0.3 => "Very Weak"
pearsonR < 0.4 => "Weak"
pearsonR < 0.5 => "Mostly Weak"
pearsonR < 0.6 => "Somewhat Weak"
pearsonR < 0.7 => "Moderately Weak"
pearsonR < 0.8 => "Moderate"
pearsonR < 0.9 => "Moderately Strong"
pearsonR < 0.92 => "Mostly Strong"
pearsonR < 0.94 => "Strong"
pearsonR < 0.96 => "Very Strong"
pearsonR < 0.98 => "Exceptionally Strong"
=> "Ultra Strong"
string t1 = "In Long-Term Channel mode, if the channel is not visible, scroll back
on the chart for additional historical data."
string t2 = "Pearson's R, a statistical measure in Adaptive Trend Finder, gauges
the linear relationship between price and trend projection. A value closer to 1
indicates a strong positive correlation, reinforcing confidence in the trend
direction based on historical price movements."
// Length Inputs
var array<int> Periods = periodMode ?
array.from(na,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,9
50,1000) :
array.from(na,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200)
if barstate.islast
// Find the highest Pearson's R
float highestPearsonR = math.max(pearsonR01, pearsonR02, pearsonR03,
pearsonR04, pearsonR05, pearsonR06, pearsonR07, pearsonR08, pearsonR09, pearsonR10,
pearsonR11, pearsonR12, pearsonR13, pearsonR14, pearsonR15, pearsonR16, pearsonR17,
pearsonR18, pearsonR19)
switch highestPearsonR
pearsonR01 =>
detectedPeriod := Periods.get(1)
detectedSlope := slope01
detectedIntrcpt := intercept01
detectedStdDev := stdDev01
pearsonR02 =>
detectedPeriod := Periods.get(2)
detectedSlope := slope02
detectedIntrcpt := intercept02
detectedStdDev := stdDev02
pearsonR03 =>
detectedPeriod := Periods.get(3)
detectedSlope := slope03
detectedIntrcpt := intercept03
detectedStdDev := stdDev03
pearsonR04 =>
detectedPeriod := Periods.get(4)
detectedSlope := slope04
detectedIntrcpt := intercept04
detectedStdDev := stdDev04
pearsonR05 =>
detectedPeriod := Periods.get(5)
detectedSlope := slope05
detectedIntrcpt := intercept05
detectedStdDev := stdDev05
pearsonR06 =>
detectedPeriod := Periods.get(6)
detectedSlope := slope06
detectedIntrcpt := intercept06
detectedStdDev := stdDev06
pearsonR07 =>
detectedPeriod := Periods.get(7)
detectedSlope := slope07
detectedIntrcpt := intercept07
detectedStdDev := stdDev07
pearsonR08 =>
detectedPeriod := Periods.get(8)
detectedSlope := slope08
detectedIntrcpt := intercept08
detectedStdDev := stdDev08
pearsonR09 =>
detectedPeriod := Periods.get(9)
detectedSlope := slope09
detectedIntrcpt := intercept09
detectedStdDev := stdDev09
pearsonR10 =>
detectedPeriod := Periods.get(10)
detectedSlope := slope10
detectedIntrcpt := intercept10
detectedStdDev := stdDev10
pearsonR11 =>
detectedPeriod := Periods.get(11)
detectedSlope := slope11
detectedIntrcpt := intercept11
detectedStdDev := stdDev11
pearsonR12 =>
detectedPeriod := Periods.get(12)
detectedSlope := slope12
detectedIntrcpt := intercept12
detectedStdDev := stdDev12
pearsonR13 =>
detectedPeriod := Periods.get(13)
detectedSlope := slope13
detectedIntrcpt := intercept13
detectedStdDev := stdDev13
pearsonR14 =>
detectedPeriod := Periods.get(14)
detectedSlope := slope14
detectedIntrcpt := intercept14
detectedStdDev := stdDev14
pearsonR15 =>
detectedPeriod := Periods.get(15)
detectedSlope := slope15
detectedIntrcpt := intercept15
detectedStdDev := stdDev15
pearsonR16 =>
detectedPeriod := Periods.get(16)
detectedSlope := slope16
detectedIntrcpt := intercept16
detectedStdDev := stdDev16
pearsonR17 =>
detectedPeriod := Periods.get(17)
detectedSlope := slope17
detectedIntrcpt := intercept17
detectedStdDev := stdDev17
pearsonR18 =>
detectedPeriod := Periods.get(18)
detectedSlope := slope18
detectedIntrcpt := intercept18
detectedStdDev := stdDev18
=> // pearsonR19
detectedPeriod := Periods.get(19)
detectedSlope := slope19
detectedIntrcpt := intercept19
detectedStdDev := stdDev19
// Calculate start and end price based on detected slope and intercept
float startPrice = math.exp(detectedIntrcpt + detectedSlope * (detectedPeriod -
1))
float endPrice = math.exp(detectedIntrcpt)
if na(baseLine)
baseLine := line.new(startAtBar, startPrice, bar_index, endPrice,
width=lineWidth, extend=EXTEND_STYLE,
color=color.new(colorInput, transpInput),
style=midLineStyle == "Dotted" ? line.style_dotted :
midLineStyle == "Dashed" ? line.style_dashed :
line.style_solid)
else
line.set_xy1(baseLine, startAtBar, startPrice)
line.set_xy2(baseLine, bar_index, endPrice)
if na(upperFill)
upperFill := linefill.new(upperLine, baseLine, color=color.new(colorInput,
fillTransparency))
if na(lowerFill)
lowerFill := linefill.new(baseLine, lowerLine, color=color.new(colorInput,
fillTransparency))
atr2 = ta.atr(100) / 2
dev = channelDeviation * atr2
highPrice1 = high[math.abs(ta.highestbars(amplitude))]
lowPrice1 = low[math.abs(ta.lowestbars(amplitude))]
highma = ta.sma(high, amplitude)
lowma = ta.sma(low, amplitude)
if nextTrend == 1
maxLowPrice := math.max(lowPrice1, maxLowPrice)
if trend == 0
if not na(trend[1]) and trend[1] != 0
up1 := na(down1[1]) ? down1 : down1[1]
arrowUp := up - atr2
arrowUp
else
up1 := na(up1[1]) ? maxLowPrice : math.max(maxLowPrice, up1[1])
up1
atrHigh := up1 + dev
atrLow := up1 - dev
atrLow
else
if not na(trend[1]) and trend[1] != 1
down1 := na(up1[1]) ? up1 : up1[1]
arrowDown := down1 + atr2
arrowDown
else
down1 := na(down1[1]) ? minHighPrice : math.min(minHighPrice, down1[1])
down1
atrHigh := down1 + dev
atrLow := down1 - dev
atrLow
utrend = ta.crossunder(trend, 1)
dtrend = ta.crossover(trend, 0)