Conversion de l’indicateur “Machine Learning Adaptative” pour PRT
Forums › ProRealTime forum Français › Support ProBuilder › Conversion de l’indicateur “Machine Learning Adaptative” pour PRT
- This topic has 5 replies, 2 voices, and was last updated 3 weeks ago by
Gilou34.
-
-
08/06/2025 at 10:38 AM #249432
Bonjour,
Je souhaite utiliser l’indicateur “Machine Learning Adaptative” de AlgoAlpha qui est en open source.
Serait-il possible de convertir le code en pj en langage ProBuilder pour ProRealTime.
En vous remerciant par avance.
Gilles
08/26/2025 at 8:31 AM #250069voici
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244//---------------------------------------------------------------//// PRC_Machine Learning Adaptive SuperTrend (byAlgoAlpha)// version = 0// 26.08.2025// Iván González @ www.prorealcode.com// Sharing ProRealTime knowledge//---------------------------------------------------------------////========================// Inputs (configuración)//========================atrLen = 10 // ATR Length (Pine: atr_len)fact = 3.0 // SuperTrend Factor (Pine: fact)trainingPeriod = 100 // Training Data Length (Pine: training_data_period)highPct = 0.75 // Percentil alto (Pine: highvol)midPct = 0.50 // Percentil medio (Pine: midvol)lowPct = 0.25 // Percentil bajo (Pine: lowvol)maxIter = 20 // Tope de iteraciones K-means para evitar bucles largoseps = 0.00001 // Umbral de convergencia//========================// Volatilidad (ATR)//========================atr = AverageTrueRange[atrLen](close)// Rango de ATR en ventana de entrenamientoupATR = Highest[trainingPeriod](atr)lowATR = Lowest[trainingPeriod](atr)// Inicialización de centroides por percentil (igual que Pine)aMean = lowATR + (upATR - lowATR) * highPctbMean = lowATR + (upATR - lowATR) * midPctcMean = lowATR + (upATR - lowATR) * lowPct//========================// Estructuras para K-means// (usamos la convención $arr[0] = contador)//========================$hv[0] = 0 // elementos cercanos a aMean (alta vol)$mv[0] = 0 // elementos cercanos a bMean (media vol)$lv[0] = 0 // elementos cercanos a cMean (baja vol)//========================// Bucle K-means//========================iter = 0converged = 0IF atr > 0 AND BarIndex >= trainingPeriod - 1 THEN// Para comparar convergencia guardamos anterioresprevA = aMeanprevB = bMeanprevC = cMeanWHILE converged = 0 DO// Limpiar clustersUnSet($hv)UnSet($mv)UnSet($lv)$hv[0] = 0$mv[0] = 0$lv[0] = 0// Asignación de cada ATR de la ventana al centroide más cercanoFOR i = 0 TO trainingPeriod - 1 DOv = atr[i]d1 = ABS(v - aMean)d2 = ABS(v - bMean)d3 = ABS(v - cMean)IF d1 <= d2 AND d1 <= d3 THEN$hv[$hv[0] + 1] = v$hv[0] = $hv[0] + 1ELSIF d2 <= d1 AND d2 <= d3 THEN$mv[$mv[0] + 1] = v$mv[0] = $mv[0] + 1ELSE$lv[$lv[0] + 1] = v$lv[0] = $lv[0] + 1ENDIFNEXT// Recalcular centroides (medias de cada cluster)// Si algún cluster queda vacío, mantenemos el valor previo para estabilidadIF $hv[0] > 0 THENsumA = 0FOR j = 1 TO $hv[0] DOsumA = sumA + $hv[j]NEXTaNew = sumA / $hv[0]ELSEaNew = aMeanENDIFIF $mv[0] > 0 THENsumB = 0FOR j = 1 TO $mv[0] DOsumB = sumB + $mv[j]NEXTbNew = sumB / $mv[0]ELSEbNew = bMeanENDIFIF $lv[0] > 0 THENsumC = 0FOR j = 1 TO $lv[0] DOsumC = sumC + $lv[j]NEXTcNew = sumC / $lv[0]ELSEcNew = cMeanENDIF// Comprobación de convergencia (cambios muy pequeños)da = ABS(aNew - aMean)db = ABS(bNew - bMean)dc = ABS(cNew - cMean)aMean = aNewbMean = bNewcMean = cNewiter = iter + 1IF (da < eps AND db < eps AND dc < eps) OR iter >= maxIter THENconverged = 1ENDIFWENDENDIF//========================// Asignación del centroide actual//========================vdistA = ABS(atr - aMean)vdistB = ABS(atr - bMean)vdistC = ABS(atr - cMean)cluster = 0 // 0=high, 1=medium, 2=lowassigned = aMeanIF vdistB < vdistA AND vdistB <= vdistC THENcluster = 1assigned = bMeanELSIF vdistC < vdistA AND vdistC < vdistB THENcluster = 2assigned = cMeanENDIF//========================// SuperTrend con ATR adaptativo//========================src = (high + low) / 2up = src + fact * assigneddn = src - fact * assignedONCE upperBand = upONCE lowerBand = dnprevUpper = upperBand[1]prevLower = lowerBand[1]candUpper = upcandLower = dnIF (candLower <= prevLower AND close[1] >= prevLower) THENlowerBand = prevLowerELSElowerBand = candLowerENDIFIF (candUpper >= prevUpper AND close[1] <= prevUpper) THENupperBand = prevUpperELSEupperBand = candUpperENDIF// Dirección y SuperTrendONCE isuperTrend = srcprevST = isuperTrend[1]IF BarIndex = 0 THENdir = 1ELSEIF prevST = prevUpper THEN// Si venimos de banda superior, cambio a bajista solo si close > upperBandIF close > upperBand THENdir = -1ELSEdir = 1ENDIFELSE// Si venimos de banda inferior, cambio a alcista solo si close < lowerBandIF close < lowerBand THENdir = 1ELSEdir = -1ENDIFENDIFENDIFIF dir = -1 THENisuperTrend = lowerBandELSEisuperTrend = upperBandENDIF//========================// Color dinámico//========================IF dir = 1 THENr = 255g = 0ELSEr = 0g = 255ENDIFcolorbetween(isupertrend,src,r,g,0,30)//========================// Señales de entrada//========================if dir=1 and dir<>dir[1] thendrawarrowdown(barindex,isupertrend)coloured("red")elsif dir=-1 and dir<>dir[1] thendrawarrowup(barindex,isupertrend)coloured("green")endifif islastbarupdate thenatr1=round(atr,2)if cluster=0 thendrawtext("Current Volatility: HIGH(ATR:#atr1#)",-165,-100)anchor(topright,xshift,yshift)elsif cluster=1 thendrawtext("Current Volatility: MEDIUM(ATR:#atr1#)",-165,-100)anchor(topright,xshift,yshift)elsif cluster=2 thendrawtext("Current Volatility: LOW(ATR:#atr1#)",-165,-100)anchor(topright,xshift,yshift)endifdrawrectangle(-330,-80,-10,-120)anchor(topright,xshift,yshift)endif//========================// Salida//========================RETURN isuperTrend STYLE(Point,2) COLOURED(r,g,0)08/28/2025 at 2:39 PM #25016209/02/2025 at 3:48 PM #250279Bonjour,
J’ai utilisé lindicateur traduit en PRT et le résultat est identique à l’original, cependant les symboles d’achat et de vente, les fleches vertes et rouge ne sont pas bien visibles car elles ressemblent trop aux bougies (voir fichier joint).
Ce serait parfait si vous pouviez les rendre plus visibles soit en mettant des flèches plus grosses ou en remplacant les fleches par des triangles ou autres symboles et/ou en mettant des couleurs differentes des bougies (jaune et bleu par exemple).
En vous remerciant par avance.
Gilou34.
09/03/2025 at 6:57 AM #250294Bonjour. On ne peut pas agrandir les flèches. À la place, on peut les remplacer par des triangles, par exemple en utilisant l’expression
1DRAWTEXT("text", x1, y1, <font>, <style>, <size>) COLOURED(R,V,B,a)Cherche les lignes où apparaissent drawarrowup / draarrowdown et remplace-les par celles-ci.
12345if dir=1 and dir<>dir[1] thendrawtext("▼",barindex,isupertrend, dialog,bold,30)coloured("red")elsif dir=-1 and dir<>dir[1] thendrawtext("▲",barindex,isupertrend,dialog,bold,30)coloured("green")endif09/03/2025 at 3:45 PM #250335 -
AuthorPosts
Find exclusive trading pro-tools on