Ai
Ai
//@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
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)
// ~~ 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]
_
='
+-----------------------+
?????? Constants ??????
+-----------------------+
'//{
//}
_
='
+-----------------------+
?????? Inputs ??????
+-----------------------+
'//{
//}
_
='
+-----------------------+
?????? 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 ??????
+-----------------------+
'//{
p ? x : b
for k = 0 to len - 1
val = nz(src[k])
psq := sq
sq += (val - sq) / (1 + k )
sum += (val - sq) * (val - psq)
osc.new(
x ,
x > +25 ?
(x - 25) :
x < -25 ?
(x + 25) :
na )
switch
o.o > trs and u and barstate.isconfirmed =>
switch
na(d.p) =>
d := divergence.new(b.h, x, b.i)
p := false
//}
_
='
+-----------------------+
?????? Calc ??????
+-----------------------+
'//{
_
='
+-----------------------+
?????? Visuals ??????
+-----------------------+
'//{
fill(w , c , colsf)
fill(lh, hh, u.c )
fill(ll, hl, d.c )
//}
_
='
+-----------------------+
?????? Alerts ??????
+-----------------------+
'//{
a.any().notify()
//}