커뮤니티

문의드립니다.

프로필 이미지
해암
2025-05-13 21:55:44
269
글번호 190782
답변완료
아래의 수식을 변화부탁드립니다. =================== / ~~ 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 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) //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} ================= 도와주심에 항상 감사드립니다. 수고하세요!!!
지표
답변 1
프로필 이미지

예스스탁 예스스탁 답변

2025-05-14 14:26:29

안녕하세요 예스스탁입니다. input : PriceValue(1); #1:hl2, 2:VWAP, 3:sma, 4:wma, 5:ema, 6:hma) input : maLen(5); input : TargetValue(1); #1:Price Action, 2:VWAP, 3:Volatility, 4:sma", 5:wma", 6:ema", 7:hma input : maLen_(5); input : numberOfClosestValues(3); input : smoothingPeriod(50); input : Upknn_col(lime); input : Dnknn_col(red); input : Neuknn_col(orange); input : Maknn_col(teal); input : bgcolor(false); input : Up_col(lime); input : Dn_col(red); var : windowSize(0),value_in(0),alpha(0),target_in(0); windowSize = max(numberOfClosestValues, 30); if PriceValue == 1 Then value_in = ma((h+l)/2,maLen); if PriceValue == 2 Then value_in = ma(c * volume, maLen) / ma(volume, maLen); if PriceValue == 3 Then value_in = ma(close,maLen); if PriceValue == 4 Then value_in = wma(close,maLen); if PriceValue == 5 Then value_in = ema(close,maLen); if PriceValue == 6 Then value_in = wma(2 * wma(close, maLen / 2) - wma(close, maLen), round(sqrt(maLen),0)); if PriceValue == 1 Then { alpha = 1/maLen; target_in = IFf(IsNan(target_in[1]) == true, ma(c,maLen) , alpha* (c) + (1 - alpha) * IFf(isnan(target_in[1])==true,0,target_in[1])); } if PriceValue == 2 Then target_in = ma(c * volume, maLen) / ma(volume, maLen); if PriceValue == 3 Then target_in = ATR(14); if PriceValue == 4 Then target_in = ma(close,maLen); if PriceValue == 5 Then target_in = wma(close,maLen); if PriceValue == 6 Then target_in = ema(close,maLen); if PriceValue == 7 Then target_in = wma(2 * wma(close, maLen / 2) - wma(close, maLen), round(sqrt(maLen),0)); #meanOfKClosest(value_in,target_in) => var : i(0),j(0),value(0),distance(0),maxDistIndex(0),maxDistValue(0); Array : closestDistances[100](1 * 10^10),closestValues[100](0); for i = 0 to windowSize { closestDistances[i] = 1 * 10^10; closestValues[i] = 0; } for i = 1 to windowSize { value = value_in[i]; distance = abs(target_in - value); maxDistIndex = 0 ; maxDistValue = closestDistances[0]; for j = 1 to numberOfClosestValues - 1 { if closestDistances[j] > maxDistValue Then { maxDistIndex = j; maxDistValue = closestDistances[j]; } } if distance < maxDistValue Then { closestDistances[maxDistIndex] = distance; closestValues[maxDistIndex] = value; } } var : knnMA(0),price(0),ap(0),oo(0),cc(0); knnMA = SummationArray(closestValues,3) / numberOfClosestValues ; price = (knnMA+close)/2; ap = 1/smoothingPeriod; CC = IFf(IsNan(oo[2]) == true, ma(knnMA,smoothingPeriod)[1] , ap * knnMA + (1 - ap) * IFf(isnan(CC[2])==true,0,CC[2])); oo = IFf(IsNan(oo[1]) == true, ma(knnMA,smoothingPeriod) , ap * knnMA + (1 - ap) * IFf(isnan(oo[1])==true,0,oo[1])); var : Pos_count(0),Neg_count(0),min_dist(0),nearest_index(0); var : dist(0),N(False),P(False),knn_prediction_raw(0),knn_prediction(0); var : knnMA_(0),knnMA_col(0),MAknn_(0),ap1(0); Pos_count = 0; Neg_count = 0 ; min_dist = 10 * 10^10; nearest_index = 0; for j = 1 to 10 { dist = sqrt(pow(price[j] - price, 2)); if dist < min_dist then { min_dist = dist; nearest_index = j; N = cc[nearest_index] > oo[nearest_index]; P = cc[nearest_index] < oo[nearest_index] ; if P Then Pos_count = Pos_count+ 1; if N Then Neg_count = Neg_count+ 1; } } knn_prediction_raw = iff(Pos_count>Neg_count,1,-1); knn_prediction = wma(knn_prediction_raw, 3); knnMA_ = wma(knnMA,5); knnMA_col = iff(knnMA_>knnMA_[1],Upknn_col,iff(knnMA_<knnMA_[1],Dnknn_col,Neuknn_col)); ap1 = 1/smoothingPeriod; MAknn_ = IFf(IsNan(MAknn_[1]) == true, ma(knnMA,smoothingPeriod) , ap1 * knnMA + (1 - ap1) * IFf(isnan(MAknn_[1])==true,0,MAknn_[1])); plot1(knnMA_,"Knn Classifier Line", knnMA_col); plot2(MAknn_,"Average Knn Classifier Line" ,Maknn_col); 즐거운 하루되세요 > 해암 님이 쓴 글입니다. > 제목 : 문의드립니다. > 아래의 수식을 변화부탁드립니다. =================== / ~~ 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 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) //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} ================= 도와주심에 항상 감사드립니다. 수고하세요!!!