-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathAI Trend Navigator [K-Neighbor].pine
More file actions
274 lines (273 loc) · 9.87 KB
/
AI Trend Navigator [K-Neighbor].pine
File metadata and controls
274 lines (273 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
Script Name: AI Trend Navigator [K-Neighbor]
Author: Zeiierman
Description: █ Overview
In the evolving landscape of trading and investment, the demand for sophisticated and reliable tools is ever-growing. The AI Trend Navigator is an indicator designed to meet this demand, providing valuable insights into market trends and potential future price movements. The AI Trend Navigator indicator is designed to predict market trends using...
PineScript code:
Pine Script™ indicator
AI Trend Navigator [K-Neighbor]
Copy code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © 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 = 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")
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
Expand (139 lines)