0% found this document useful (0 votes)
208 views4 pages

AI Trend Navigator Overview

The document outlines the 'AI Trend Navigator' indicator, which utilizes K-Nearest Neighbors (KNN) for trend prediction in financial markets. It includes various input parameters for price computation methods, smoothing periods, and color settings for visual representation of trends. Additionally, it describes the KNN classification process and the alerts generated based on trend movements.

Uploaded by

asgraphics010
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)
208 views4 pages

AI Trend Navigator Overview

The document outlines the 'AI Trend Navigator' indicator, which utilizes K-Nearest Neighbors (KNN) for trend prediction in financial markets. It includes various input parameters for price computation methods, smoothing periods, and color settings for visual representation of trends. Additionally, it describes the KNN classification process and the alerts generated based on trend movements.

Uploaded by

asgraphics010
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

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.

0
International (CC BY-NC-SA 4.0) [Link]
// © Zeiierman {

//@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 = [Link]("hl2", options = ["hl2","VWAP", "sma", "wma", "ema",
"hma"], group="", inline="Value")
maLen = [Link](5, minval=2, maxval=200, title="", group="",
inline="Value", tooltip=t1)
TargetValue = [Link]("Price Action", options = ["Price Action","VWAP",
"Volatility", "sma", "wma", "ema", "hma"], group="", inline="Target")
maLen_ = [Link](5, minval=2, maxval=200, title="", group="",
inline="Target", tooltip=t2)
// Input parameters for the KNN Moving Average
numberOfClosestValues = [Link](3, "Number of Closest Values", 2, 200,
tooltip=t3)
smoothingPeriod = [Link](50, "Smoothing Period", 2, 500, tooltip=t4)
windowSize = [Link](numberOfClosestValues, 30)

// knn Color
Upknn_col = [Link]([Link], title="", group="KNN Color", inline="knn
col")
Dnknn_col = [Link]([Link], title="", group="KNN Color", inline="knn col")
Neuknn_col = [Link]([Link], title="", group="KNN Color", inline="knn
col")
// MA knn Color
Maknn_col = [Link]([Link], title="", group="MA KNN Color", inline="MA
knn col")
// BG Color
bgcolor = [Link](false, title="Trend Prediction Color", group="BG Color",
inline="bg", tooltip=t5)
Up_col = [Link]([Link], title="", group="BG Color", inline="bg")
Dn_col = [Link]([Link], title="", group="BG Color", inline="bg")
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ kNN Classifier {
value_in = switch PriceValue
"hl2" => [Link](hl2,maLen)
"VWAP" => [Link](close[maLen])
"sma" => [Link](close,maLen)
"wma" => [Link](close,maLen)
"ema" => [Link](close,maLen)
"hma" => [Link](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 = [Link](target_ - value)
maxDistIndex = 0
maxDistValue = [Link](0)
for j = 1 to numberOfClosestValues - 1
if [Link](j) > maxDistValue
maxDistIndex := j
maxDistValue := [Link](j)
if distance < maxDistValue
[Link](maxDistIndex, distance)
[Link](maxDistIndex, value)
[Link]() / numberOfClosestValues

// Choose the target input based on user selection


target_in = switch TargetValue
"Price Action" => [Link](close,maLen_)
"VWAP" => [Link](close[maLen_])
"Volatility" => [Link](14)
"sma" => [Link](close,maLen_)
"wma" => [Link](close,maLen_)
"ema" => [Link](close,maLen_)
"hma" => [Link](close,maLen_)

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

// ~~ kNN Prediction {
// Function to calculate KNN Classifier
price = [Link](knnMA, close)
c = [Link](knnMA[1], smoothingPeriod)
o = [Link](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 = [Link]([Link](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 = [Link](knn_prediction_raw, 3)
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Plots {
// Plots for display on the chart
knnMA_ = [Link](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_ = [Link](knnMA, smoothingPeriod)
plot(MAknn_,"Average Knn Classifier Line" ,Maknn_col)
green = knn_prediction < 0.5
red = knn_prediction > -0.5
bgcolor( green and bgcolor? [Link](Dn_col,80) :
red and bgcolor ? [Link](Up_col,80) : na)
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Alerts {
knnMA_cross_Over_Ma = [Link](knnMA_,MAknn_)
knnMA_cross_Under_Ma = [Link](knnMA_,MAknn_)
knnMA_cross_Over_Close = [Link](knnMA_,close)
knnMA_cross_Under_Close = [Link](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")
//
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

You might also like