PHP Code:
//@version=6
indicator(":] Oto-Dinamik 5 Bar + KNN Filtresi", overlay=true, max_lines_count=50, max_labels_count=500, max_bars_back=2000)
// ==========================================
// 1. AYARLAR (BİZİM OTO-DİNAMİK VALF)
// ==========================================
float ayar_genislik = input.float(1.0, "Ana Atmosfer Çarpanı", minval=0.1, maxval=2.0, step=0.1, group="Oto-Dinamik Valf")
float valf_hassasiyeti = input.float(0.7, "Dinamik Valf Hassasiyeti (Çarpan)", minval=0.1, maxval=1.5, step=0.1, group="Oto-Dinamik Valf")
// ==========================================
// 2. AYARLAR (YABANCI KNN MOTORU)
// ==========================================
group_ml = "🧠 KNN Filtre Ayarları"
int k_neighbors = input.int(10, "K-Neighbors (K)", minval=1, group=group_ml)
int sampling_window_size = input.int(1000, "Learning Window Size", minval=10, group=group_ml)
int momentum_window = input.int(10, "Stride", minval=1, group=group_ml)
float prob_threshold = input.float(0.9, "Prediction Threshold", minval=0.1, maxval=1.0, step=0.01, group=group_ml)
int st_atr_len = input.int(10, "KNN Referans ATR", group=group_ml)
float st_factor = input.float(2.0, "KNN Referans Factor", group=group_ml)
// ==========================================
// 3. MOTOR BLOKLARI VE YZ ÇEKİRDEĞİ (BİZİM SİSTEM)
// ==========================================
float s_fast_1 = ta.sar(0.08, 0.05, 0.2)
float s_fast_2 = ta.sar(0.01, 0.05, 0.2)
float s_fast_3 = ta.sar(0.04, 0.04, 0.2)
float avg_fast = (s_fast_1 + s_fast_2 + s_fast_3) / 3
tanh_fn(v) => (1 - math.exp(-2 * v)) / (1 + math.exp(-2 * v))
td_fn(s) => nz((s - nz(s[1])) / nz(s[1]))
var w_c = array.from(22.4271, -26.6917, 4.9371, 9.0349, -10.6929, -38.2880, 10.0500, -44.7063, -17.8163, 30.5662, -33.9954, 14.5017, -43.2865, -13.3874, 24.7080, -14.3929, 28.4830, -22.9793, -7.6582, -5.6505, 28.8379, -26.3544, 0.5206, 25.0049, -17.8832, -4.8113, -4.0364, -8.3327, -1.1571, 0.4667, -22.0533, 3.6525, -4.3904, 2.1030, 20.0272, 11.5101, -0.4150)
n_in = array.from(tanh_fn(td_fn(open)), tanh_fn(td_fn(high)), tanh_fn(td_fn(low)), tanh_fn(td_fn(close)))
f_calc_dl(n_arr, w_arr) =>
float out = 0.0
for i = 0 to 5
float s = 0.0
for j = 0 to 3
s += array.get(n_arr, j) * array.get(w_arr, (i * 5) + j)
out += tanh_fn(s + array.get(w_arr, (i * 5) + 4)) * array.get(w_arr, 30 + i)
tanh_fn(out + array.get(w_arr, 36))
float dl_val = f_calc_dl(n_in, w_c)
float ai_factor = (0.2 + (math.abs(dl_val) * 0.5)) * ayar_genislik
// Atmosferik Çizimler
float dist_base = math.abs(close - s_fast_1) * ai_factor
float dist_swing = dist_base * 2.0
// ==========================================
// 4. KNN FONKSİYONLARI VE ÖZELLİK MÜHENDİSLİĞİ (YABANCI SİSTEM)
// ==========================================
f_dema(s, l) =>
e1 = ta.ema(s, l)
2 * e1 - ta.ema(e1, l)
calcMA(s, l) => ta.sma(s, math.max(1, l))
f_chop(len) =>
100 * math.log10(math.sum(ta.tr, len) / (math.max(ta.highest(high, len), close[1]) - math.min(ta.lowest(low, len), close[1]))) / math.log10(len)
normalize(src, len) =>
float _mean = ta.sma(src[1], len)
float _std = ta.stdev(src[1], len)
(src - _mean) / math.max(_std, 0.00001)
[st_raw, dir_raw] = ta.supertrend(st_factor, st_atr_len)
float target = dir_raw * -1
for i = 1 to 5
if dir_raw[i] != dir_raw
target := 0
float f_rsi_s = ta.rsi(close, 20)
float f_rsi_m = ta.rsi(close[momentum_window], 20)
float f_rsi_l = ta.rsi(close[momentum_window*2], 20)
float f_ma_s_dev = (close - calcMA(close[1], 20)) / calcMA(close[1], 20)
float f_ma_m_dev = (close[momentum_window] - calcMA(close[momentum_window+1], 20)) / calcMA(close[momentum_window+1], 20)
float f_ma_l_dev = (close[momentum_window*2] - calcMA(close[momentum_window*2+1], 20)) / calcMA(close[momentum_window*2+1], 20)
float f_rsi_s_sig_dist = (f_rsi_s - ta.sma(f_rsi_s[1], 10)) / ta.sma(f_rsi_s[1], 10)
float f_rsi_m_sig_dist = (f_rsi_m - ta.sma(f_rsi_m[1], 10)) / ta.sma(f_rsi_m[1], 10)
float f_rsi_l_sig_dist = (f_rsi_l - ta.sma(f_rsi_l[1], 10)) / ta.sma(f_rsi_l[1], 10)
float f_chop_s = f_chop(14)
float f_chop_m = f_chop(14)[momentum_window]
float f_chop_l = f_chop(14)[momentum_window*2]
// Boyut İndirgeme (PCA)
float pc1 = normalize((normalize(f_rsi_s, 1000) + normalize(f_ma_s_dev, 1000) + normalize(f_rsi_s_sig_dist, 1000)*0.5), 1000)
float pc2 = normalize((normalize(f_rsi_m, 1000) + normalize(f_ma_m_dev, 1000) + normalize(f_rsi_m_sig_dist, 1000)*0.5), 1000) * 0.9
float pc3 = normalize((normalize(f_rsi_l, 1000) + normalize(f_ma_l_dev, 1000) + normalize(f_rsi_l_sig_dist, 1000)*0.5), 1000) * 0.8
float pc4 = normalize((normalize(f_chop_s, 1000) + normalize(f_chop_m, 1000)*0.9 + normalize(f_chop_l, 1000)*0.8), 1000) * 0.8
// ==========================================
// 5. KNN ÇEKİRDEĞİ: TAHMİN OLASILIĞI HESAPLAMA
// ==========================================
float prob_up = 0.0
float prob_down = 0.0
var float[] distances = array.new_float(0)
var float[] labels = array.new_float(0)
if bar_index > 1000 + momentum_window
array.clear(distances)
array.clear(labels)
for i = momentum_window to sampling_window_size + momentum_window by momentum_window
if target[i] != 0
float d1 = math.abs(pc1 - pc1[i])
float d2 = math.abs(pc2 - pc2[i])
float d3 = math.abs(pc3 - pc3[i])
float d4 = math.abs(pc4 - pc4[i])
float dist_knn = math.pow(math.pow(d1, 2.0) + math.pow(d2, 2.0) + math.pow(d3, 2.0) + math.pow(d4, 2.0), 1/2.0)
array.push(distances, dist_knn)
array.push(labels, target[i])
if array.size(distances) >= k_neighbors
int[] sorted_indices = array.sort_indices(distances, order.ascending)
float sum_w_up = 0.0, float sum_w_down = 0.0, float total_w = 0.0
float[] dist_sorted = array.copy(distances)
array.sort(dist_sorted)
float sigma = math.max(array.get(dist_sorted, math.min(int(k_neighbors/2), array.size(dist_sorted)-1)), 0.0001)
for j = 0 to k_neighbors - 1
int idx = array.get(sorted_indices, j)
float d = array.get(distances, idx)
float lbl = array.get(labels, idx)
float weight = math.exp(-math.pow(d, 2.0) / (2 * math.pow(sigma, 2)))
if lbl == 1
sum_w_up += weight
else if lbl == -1
sum_w_down += weight
total_w += weight
prob_up := total_w > 0 ? sum_w_up / total_w : 0.0
prob_down := total_w > 0 ? sum_w_down / total_w : 0.0
bool raw_long_signal = ta.crossover(prob_up, prob_threshold)
bool raw_short_signal = ta.crossover(prob_down, prob_threshold)
var int last_dir = 0
if raw_long_signal and last_dir <= 0
last_dir := 1
if raw_short_signal and last_dir >= 0
last_dir := -1
var float knn_st_dir = 0.0
if dir_raw != nz(dir_raw[1]) or raw_long_signal or raw_short_signal
if dir_raw < 0 and last_dir == 1 and prob_up > prob_threshold
knn_st_dir := dir_raw * -1
if dir_raw > 0 and last_dir == -1 and prob_down > prob_threshold
knn_st_dir := dir_raw * -1
// KNN Filtre Durumunu Arka Plana Yansıt
bgcolor(knn_st_dir > 0 ? color.new(color.lime, 90) : knn_st_dir < 0 ? color.new(color.red, 90) : na, title="KNN AI Filtresi")
// ==========================================
// 6. OTO-DİNAMİK BASINÇ VALFİ VE 5 BARLIK PATLAMA
// ==========================================
float atmosfer_genisligi_yuzde = (dist_swing / close) * 100
float ortalama_genislik = ta.sma(atmosfer_genisligi_yuzde, 100)
float dinamik_esik = ortalama_genislik * valf_hassasiyeti
bool basinc_zirvede = atmosfer_genisligi_yuzde < dinamik_esik
bool psar_al_verdi = close > s_fast_1 and close[1] <= s_fast_1[1]
bool psar_sat_verdi = close < s_fast_1 and close[1] >= s_fast_1[1]
// YENİ: KNN ONAYLI SUPERTREND FİLTRESİ (1: YUKARI, -1: AŞAĞI)
bool knn_AL_onayi = knn_st_dir > 0
bool knn_SAT_onayi = knn_st_dir < 0
// Patlama Tetiği: Dinamik Eşik + PSAR Kırılımı + KNN Makine Öğrenmesi Onayı!
bool patlama_AL = basinc_zirvede and psar_al_verdi and knn_AL_onayi
bool patlama_SAT = basinc_zirvede and psar_sat_verdi and knn_SAT_onayi
// Ekrana Şiddetli Vur-Kaç İşaretlerini Bas
plotshape(patlama_AL, title="5-BAR AL", text="5-BAR\nAL", style=shape.labelup, location=location.belowbar, color=color.rgb(0, 230, 119), textcolor=color.white, size=size.small)
plotshape(patlama_SAT, title="5-BAR SAT", text="5-BAR\nSAT", style=shape.labeldown, location=location.abovebar, color=color.rgb(255, 82, 82), textcolor=color.white, size=size.small)
// İMZA
var table ytd_table = table.new(position.bottom_center, 1, 1)
if barstate.islast
table.cell(ytd_table, 0, 0, "(Aİ) eğitim çalışmasıdır. Yatırım tavsiyesi KULLANILAMAZ.", text_color=color.new(color.white, 50), text_size=size.small)
Yer İmleri