0% found this document useful (0 votes)
12 views12 pages

Ai

Ai trend Navigator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

Ai

Ai trend Navigator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

code for indicator 1

//@version=5
indicator('AI Trend Navigator', overlay=true)

// ~~ Tooltips {
t1 ="PriceValue selects the method of price computation. \n\nSets the smoothing
period for the PriceValue. \n\nAdjusting these settings will change the input
values for the K-Nearest Neighbors algorithm, influencing how the trend is
calculated."
t2 = "TargetValue specifies the target to evaluate. \n\nSets the smoothing period
for the TargetValue."
t3 ="numberOfClosestValues sets the number of closest values that are considered
when calculating the KNN Moving Average. Adjusting this number will affect the
sensitivity of the trend line, with a higher value leading to a smoother line and a
lower value resulting in a line that is more responsive to recent price changes."
t4 ="smoothingPeriod sets the period for the moving average applied to the KNN
classifier. Adjusting the smoothing period will affect how rapidly the trend line
responds to price changes, with a larger smoothing period leading to a smoother
line that may lag recent price movements, and a smaller smoothing period resulting
in a line that more closely tracks recent changes."
t5 ="This option controls the background color for the trend prediction. Enabling
it will change the background color based on the prediction, providing visual cues
on the direction of the trend. A green color indicates a positive prediction, while
red indicates a negative prediction."
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Inputs {
PriceValue = input.string("hl2", options = ["hl2","VWAP", "sma", "wma", "ema",
"hma"], group="", inline="Value")
maLen = input.int(5, minval=2, maxval=200, title="", group="",
inline="Value", tooltip=t1)
TargetValue = input.string("Price Action", options = ["Price Action","VWAP",
"Volatility", "sma", "wma", "ema", "hma"], group="", inline="Target")
maLen_ = input.int(5, minval=2, maxval=200, title="", group="",
inline="Target", tooltip=t2)
// Input parameters for the KNN Moving Average
numberOfClosestValues = input.int(3, "Number of Closest Values", 2, 200,
tooltip=t3)
smoothingPeriod = input.int(50, "Smoothing Period", 2, 500, tooltip=t4)
windowSize = math.max(numberOfClosestValues, 30)

// knn Color
Upknn_col = input.color(color.lime, title="", group="KNN Color", inline="knn
col")
Dnknn_col = input.color(color.red, title="", group="KNN Color", inline="knn col")
Neuknn_col = input.color(color.orange, title="", group="KNN Color", inline="knn
col")
// MA knn Color
Maknn_col = input.color(color.teal, title="", group="MA KNN Color", inline="MA
knn col")
// BG Color
bgcolor = input.bool(false, title="Trend Prediction Color", group="BG Color",
inline="bg", tooltip=t5)
Up_col = input.color(color.lime, title="", group="BG Color", inline="bg")
Dn_col = input.color(color.red, title="", group="BG Color", inline="bg")
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ kNN Classifier {
value_in = switch PriceValue
"hl2" => ta.sma(hl2,maLen)
"VWAP" => ta.vwap(close[maLen])
"sma" => ta.sma(close,maLen)
"wma" => ta.wma(close,maLen)
"ema" => ta.ema(close,maLen)
"hma" => ta.hma(close,maLen)

meanOfKClosest(value_,target_) =>
closestDistances = array.new_float(numberOfClosestValues, 1e10)
closestValues = array.new_float(numberOfClosestValues, 0.0)
for i = 1 to windowSize
value = value_[i]
distance = math.abs(target_ - value)
maxDistIndex = 0
maxDistValue = closestDistances.get(0)
for j = 1 to numberOfClosestValues - 1
if closestDistances.get(j) > maxDistValue
maxDistIndex := j
maxDistValue := closestDistances.get(j)
if distance < maxDistValue
closestDistances.set(maxDistIndex, distance)
closestValues.set(maxDistIndex, value)
closestValues.sum() / numberOfClosestValues

// Choose the target input based on user selection


target_in = switch TargetValue
"Price Action" => ta.rma(close,maLen_)
"VWAP" => ta.vwap(close[maLen_])
"Volatility" => ta.atr(14)
"sma" => ta.sma(close,maLen_)
"wma" => ta.wma(close,maLen_)
"ema" => ta.ema(close,maLen_)
"hma" => ta.hma(close,maLen_)

knnMA = meanOfKClosest(value_in,target_in)
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ kNN Prediction {
// Function to calculate KNN Classifier
price = math.avg(knnMA, close)
c = ta.rma(knnMA[1], smoothingPeriod)
o = ta.rma(knnMA, smoothingPeriod)

// Defines KNN function to perform classification


knn(price) =>
Pos_count = 0
Neg_count = 0
min_distance = 10e10
nearest_index = 0
for j = 1 to 10
distance = math.sqrt(math.pow(price[j] - price, 2))
if distance < min_distance
min_distance := distance
nearest_index := j
Neg = c[nearest_index] > o[nearest_index]
Pos = c[nearest_index] < o[nearest_index]
if Pos
Pos_count += 1
if Neg
Neg_count += 1
output = Pos_count>Neg_count?1:-1

// Calls KNN function and smooths the prediction


knn_prediction_raw = knn(price)
knn_prediction = ta.wma(knn_prediction_raw, 3)
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Plots {
// Plots for display on the chart
knnMA_ = ta.wma(knnMA,5)
knnMA_col = knnMA_>knnMA_[1]?Upknn_col:knnMA_<knnMA_[1]?Dnknn_col:Neuknn_col
Classifier_Line = plot(knnMA_,"Knn Classifier Line", knnMA_col)
MAknn_ = ta.rma(knnMA, smoothingPeriod)
plot(MAknn_,"Average Knn Classifier Line" ,Maknn_col)
green = knn_prediction < 0.5
red = knn_prediction > -0.5
bgcolor( green and bgcolor? color.new(Dn_col,80) :
red and bgcolor ? color.new(Up_col,80) : na)
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Alerts {
knnMA_cross_Over_Ma = ta.crossover(knnMA_,MAknn_)
knnMA_cross_Under_Ma = ta.crossunder(knnMA_,MAknn_)
knnMA_cross_Over_Close = ta.crossover(knnMA_,close)
knnMA_cross_Under_Close = ta.crossunder(knnMA_,close)
knnMA_Switch_Up = knnMA_[1]<knnMA_ and knnMA_[1]<=knnMA_[2]
knnMA_Switch_Dn = knnMA_[1]>knnMA_ and knnMA_[1]>=knnMA_[2]
knnMA_Neutral = knnMA_col==Neuknn_col and knnMA_col[1]!=Neuknn_col
greenBG = green and not green[1]
redBG = red and not red[1]

alertcondition(knnMA_cross_Over_Ma, title = "Knn Crossover Average Knn", message


= "Knn Crossover Average Knn")
alertcondition(knnMA_cross_Under_Ma, title = "Knn Crossunder Average Knn", message
= "Knn Crossunder Average Knn")
alertcondition(knnMA_cross_Over_Close, title = "Knn Crossover Close", message =
"Knn Crossover Close")
alertcondition(knnMA_cross_Under_Close, title = "Knn Crossunder Close", message =
"Knn Crossunder Close")
alertcondition(knnMA_Switch_Up, title = "Knn Switch Up", message = "Knn Switch
Up")
alertcondition(knnMA_Switch_Dn, title = "Knn Switch Dn", message = "Knn Switch Dn")
alertcondition(knnMA_Neutral, title = "Knn is Neutral", message = "Knn is Neutral")
alertcondition(greenBG, title = "Positive Prediction", message = "Positive
Prediction")
alertcondition(redBG, title = "Negative Prediction", message = "Negative
Prediction")
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

code for indicator 2

indicator("Squeeze Momentum Deluxe", "[?] -? Squeeze Deluxe", false,


explicit_plot_zorder = true, max_lines_count = 500, max_labels_count = 500)

_
='
+-----------------------+
?????? Constants ??????
+-----------------------+
'//{

const string tg = 'Choose how to display the Confluence Gauges.'


const string tm = 'Calibrates the length of the main oscillator ribbon as well as
the period for the squeeze algorithm.'
const string ts = 'Controls the width of the ribbon.\nLower values result in
faster responsiveness at the cost of premature positives.'
const string td = 'Adjusts a threshold to limit the amount of divergences detected
based on strength.\nHigher values result in less detections.'
const string go = 'Squeeze Momentum'
const string gd = 'Directional Flux'
const string gl = 'Divergences'
const string gg = 'Gauges'
const string ss = 'Sell Signal'
const string sb = 'Buy Signal'
const string sob = 'Momentum Bullish'
const string sos = 'Momentum Bearish'
const string sfb = 'Flux Bullish'
const string sfs = 'Flux Bearish'
const string ssb = 'Bullish Swing'
const string sss = 'Bearish Swing'
const string sgb = 'Strong Bull Gauge'
const string sgs = 'Strong Bear Gauge'
const string swb = 'Weak Bull Gauge'
const string sws = 'Weak Bear Gauge'
const string shs = 'High Squeeze'
const string sms = 'Normal Squeeze'
const string sls = 'Low Squeeze'
const string sds = 'Bearish Divergence'
const string sdb = 'Bullish Divergence'

const color colup = #ffcfa6


const color coldn = #419fec
const color colpf = #ffd0a6
const color coldf = #4683b4
const color colps = #169b5d
const color colng = #970529
const color colpo = #11cf77
const color colno = #d11645
const color colsh = #ff1100
const color colsm = #ff5e00
const color colsl = #ffa600
const color colnt = #787b8635
var color coltx = chart.fg_color
var color trnsp = chart.bg_color

//}

_
='
+-----------------------+
?????? Inputs ??????
+-----------------------+
'//{

dfb = input.bool (true , "" ,


inline = '1', group = gd)
dfl = input.int (30 , "Length???" , 7 , 50, 1 ,
inline = '1', group = gd)
dfh = input.bool (false , "Trend Bias" ,
group = gd)
cps = input.color (colps , "" ,
inline = '2', group = gd)
cng = input.color (colng , "" ,
inline = '2', group = gd)
cpo = input.color (colpo , "" ,
inline = '3', group = gd)
cno = input.color (colno , "" ,
inline = '3', group = gd)
smb = input.bool (true , "" ,
inline = '1', group = go)
len = input.int (20 , "Length???" , 7 , 50, 1 , tm,
'1', go)
sig = input.int (3 , "Signal???????" , 2 , 7, 1 , ts,
group = go)
cup = input.color (colup , "" ,
inline = '2', group = go)
cdn = input.color (coldn , "" ,
inline = '2', group = go)
cpf = input.color (colpf , "" ,
inline = '3', group = go)
cdf = input.color (coldf , "" ,
inline = '3', group = go)
trs = input.int (25 , "Sensitivity???", 20, 40, 1 , td,
group = gl)
dbl = input.bool (true , "Lines" ,
group = gl)
dbs = input.bool (true , "Labels" ,
group = gl)
cdu = input.color (colpo , "" ,
inline = '1', group = gl)
cdd = input.color (colno , "" ,
inline = '1', group = gl)
gds = input.string('Both', "Showcase?????" , ['Both', 'Bull', 'Bear', 'None'], tg,
'4', gg)
cgp = input.color (colps , "" ,
inline = '2', group = gg)
cgn = input.color (colng , "" ,
inline = '2', group = gg)

//}
_
='
+-----------------------+
?????? UDTs ??????
+-----------------------+
'//{

type bar
float o = open
float h = high
float l = low
float c = close
int i = bar_index

type osc
float o = na
float s = na

type squeeze
bool h = false
bool m = false
bool l = false

type gauge
float u = na
float l = na
color c = chart.fg_color
bool p = true

type divergence
float p = na
float s = na
int i = na

type alerts
bool b = false
bool s = false
bool u = false
bool d = false
bool p = false
bool n = false
bool x = false
bool y = false
bool a = false
bool c = false
bool q = false
bool w = false
bool h = false
bool m = false
bool l = false
bool e = false
bool f = false

type prompt
string s = ''
bool c = false

//}
_
='
+-----------------------+
?????? Methods ??????
+-----------------------+
'//{

method notify(prompt p) =>


if p.c
alert(p.s, alert.freq_once_per_bar_close)

method any(alerts a) =>


string s = switch
a.s => ss
a.b => sb
a.u => sob
a.d => sos
a.p => sfb
a.n => sfs
a.x => sss
a.y => ssb
a.q => sgb
a.w => sgs
a.a => swb
a.c => sws
a.h => shs
a.m => sms
a.l => sls
a.e => sds
a.f => sdb
=> na

prompt.new(s, not na(s))

method src(bar b, simple string src) =>


float x = switch src
'oc2' => math.avg(b.o, b.c )
'hl2' => math.avg(b.h, b.l )
'hlc3' => math.avg(b.h, b.l, b.c )
'ohlc4' => math.avg(b.o, b.h, b.l, b.c)
'hlcc4' => math.avg(b.h, b.l, b.c, b.c)

method ha(bar b, simple bool p = true) =>


var bar x = bar.new( )
x.c := b .src('ohlc4')
x := bar.new(
na(x.o[1]) ? b.src('oc2') : nz(x.src('oc2')[1]),
math.max(b.h, math.max(x.o, x.c)) ,
math.min(b.l, math.min(x.o, x.c)) ,
x.c )

p ? x : b

method atr(bar b, simple int len = 1) =>


float tr =
na ( b.h[1] ) ?
b.h - b.l :
math.max(
math.max(
b.h - b.l ,
math.abs( b.h - b.c[1])) ,
math.abs( b.l - b.c[1]))

len == 1 ? tr : ta.rma(tr, len)

method stdev(float src, simple int len) =>


float sq = 0.
float psq = 0.
float sum = 0.

for k = 0 to len - 1
val = nz(src[k])
psq := sq
sq += (val - sq) / (1 + k )
sum += (val - sq) * (val - psq)

math.sqrt(sum / (len - 1))

method osc(bar b, simple int sig, simple int len) =>


float av = ta .sma(b.src('hl2'), len)
bar z = bar.new(
b.o ,
ta.highest(len),
ta.lowest (len),
b.c )

float x = ta.linreg((z.c - math.avg(z.src('hl2'), av)) / z.atr() * 100, len,


0)

osc.new(x, ta.sma(x, sig))

method dfo(bar b, simple int len) =>


float tr = b .atr ( len)
float up = ta.rma (
math.max (
ta.change(b.h) , 0), len) / (tr)
float dn = ta.rma (
math.max (
ta.change(b.l) * -1, 0), len) / (tr)
float x = ta.rma (
(up - dn) / (up + dn) , len / 2) * 100

osc.new(
x ,
x > +25 ?
(x - 25) :
x < -25 ?
(x + 25) :
na )

method sqz(bar b, simple int len) =>


array<bool> sqz = array.new<bool>( )
float dev = b.c .stdev (len)
float atr = b .atr (len)
for i = 2 to 4
sqz.unshift(dev < (atr * 0.25 * i))

squeeze.new(sqz.pop(), sqz.pop(), sqz.pop())

method draw(bar b, osc o, simple int trs, simple bool s) =>


var divergence d = divergence.new( )
bool u = ta.crossunder (o.o , o.s)
bool l = ta.crossover (o.o , o.s)
float x = o.s
bool p = false

switch
o.o > trs and u and barstate.isconfirmed =>
switch
na(d.p) =>
d := divergence.new(b.h, x, b.i)
p := false

not na(d.p) =>


if b.h > d.p and x < d.s
if s
line.new(d.i, d.s, b.i, x, xloc.bar_index, extend.none,
cdd)
d := divergence.new( )
p := true
else
d := divergence.new(b.h, x, b.i)
p := false

o.o < -trs and l and barstate.isconfirmed =>


switch
na(d.p) =>
d := divergence.new(b.l, x, b.i)
p := false

not na(d.p) =>


if b.l < d.p and x > d.s
if s
line.new(d.i, d.s, b.i, x, xloc.bar_index, extend.none,
cdu)
d := divergence.new( )
p := true
else
d := divergence.new(b.l, x, b.i)
p := false

//}

_
='
+-----------------------+
?????? Calc ??????
+-----------------------+
'//{

bar b = bar .new ( )


squeeze s = b .sqz ( len)
osc o = b .osc (sig, len)
osc v = b.ha(dfh).dfo ( dfl)
bool p = b .draw(o, trs, dbl)
gauge u = gauge .new (
+75 ,
+70 ,
v.o > 0 and o.o > 0 ?
cgp :
v.o > 0 or o.o > 0 ?
color.new(cgp , 40) :
colnt ,
gds == 'Both' or gds == 'Bull' )
gauge d = gauge .new (
-75 ,
-70 ,
v.o < 0 and o.o < 0 ?
cgn :
v.o < 0 or o.o < 0 ?
color.new(cgn , 40) :
colnt ,
gds == 'Both' or gds == 'Bear' )

alerts a = alerts .new (


ta.crossover (o.o, o.s) and o.o < -40 and v.o < 0
,
ta.crossunder(o.o, o.s) and o.o > +40 and v.o > 0
,
ta.crossover (o.o, 0)
,
ta.crossunder(o.o, 0)
,
ta.crossover (v.o, 0)
,
ta.crossunder(v.o, 0)
,
ta.crossunder(o.o, o.s) and smb
,
ta.crossover (o.o, o.s) and smb
,
ta.change (u.c == colnt) and u.c == color.new(cgp,
40),
ta.change (d.c == colnt) and d.c == color.new(cgn,
40),
ta.change (u.c == colnt) and u.c == cgp
,
ta.change (d.c == colnt) and d.c == cng
,
ta.change (s.h ) and s.h
,
ta.change (s.m ) and s.m
,
ta.change (s.l ) and s.l
,
p and o.o > trs
,
p and o.o < -trs
)
//}

_
='
+-----------------------+
?????? Visuals ??????
+-----------------------+
'//{

color colsq = s.h ? colsh : s.m ? colsm : colsl


color colvf = v.o > 0 ? color.new(cps , 70) : color.new(cng , 70)
color colof = v.s > 0 ? color.new(cpo , 70) : color.new(cno , 70)
color colsf = o.o > o.s ? color.new(cpf , 50) : color.new(cdf , 50)
color colzf = o.o > o.s ? cup : cdn

m = hline(0 , "Mid-Line" , color.new(coltx, 70),


hline.style_dashed )
l = plot (dfb ? v.o : na, "Flux" , colvf , 1, plot.style_area
)
z = plot (dfb ? v.s : na, "OverFlux" , colof , 1,
plot.style_areabr )
w = plot (smb ? o.o : na, "Mom" , colzf , 1, plot.style_line
)
q = plot (s.l ? 1 : na, "Squeeze" , colsq , 1,
plot.style_columns, false, -1, display = display.pane)
j = plot (a.x ? o.s : na, "Trend Shift", cdf , 2,
plot.style_circles, display = display.pane)
k = plot (a.y ? o.o : na, "Trend Shift", cpf , 2,
plot.style_circles, display = display.pane)
c = plot (smb ? o.s : na, "Signal" ,
display = display.none)
ll = plot (d.p ? d.l : na, "Lower Gauge",
display = display.none)
hl = plot (d.p ? d.u : na, "Lower Gauge",
display = display.none)
hh = plot (u.p ? u.u : na, "Upper Gauge",
display = display.none)
lh = plot (u.p ? u.l : na, "Upper Gauge",
display = display.none)

plotshape(u.p and a.s ? u.u + 10 : na, "Confluence Sell" , shape.triangledown,


location.absolute, colno, size = size.tiny)
plotshape(d.p and a.b ? d.l - 15 : na, "Confluence Buy " , shape.triangleup ,
location.absolute, colpo, size = size.tiny)
plotshape(dbs and a.e ? o.s + 3 : na, "Bearish Divergence", shape.labeldown ,
location.absolute, colnt, 0, '???', colsm )
plotshape(dbs and a.f ? o.s - 3 : na, "Bullish Divergence", shape.labelup ,
location.absolute, colnt, 0, '???', colsm )

fill(w , c , colsf)
fill(lh, hh, u.c )
fill(ll, hl, d.c )

//}

_
='
+-----------------------+
?????? Alerts ??????
+-----------------------+
'//{

alertcondition(a.s, "Confluence Sell" , ss )


alertcondition(a.b, "Confluence Buy " , sb )
alertcondition(a.u, "Momentum Midline Crossover ", sob)
alertcondition(a.d, "Momentum Midline Crossunder", sos)
alertcondition(a.p, "Flux Midline Crossover ", sfb)
alertcondition(a.n, "Flux Midline Crossunder", sfs)
alertcondition(a.y, "Momentum Swing Crossover ", ssb)
alertcondition(a.x, "Momentum Swing Crossunder", sss)
alertcondition(a.q, "Strong Bullish Confluence" , sgb)
alertcondition(a.w, "Strong Bearish Confluence" , sgs)
alertcondition(a.a, "Weak Bullish Confluence" , swb)
alertcondition(a.c, "Weak Bearish Confluence" , sws)
alertcondition(a.h, "High Squeeze" , shs)
alertcondition(a.m, "Normal Squeeze" , sms)
alertcondition(a.l, "Low Squeeze" , sls)
alertcondition(a.e, "Bearish Divergence" , sds)
alertcondition(a.f, "Bullish Divergence" , sdb)

a.any().notify()

//}

You might also like