Super Trend AI Clustering
Forums › ProRealTime forum Français › Support ProBuilder › Super Trend AI Clustering
- This topic has 8 replies, 2 voices, and was last updated 2 weeks ago by
Iván.
-
-
11/21/2025 at 7:53 AM #253841123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347//---------------------------------------------------------------////SuperTrend AI Clustering//version = 2//02.06.2025//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//---------------------------------------------------------------////-----Inputs----------------------------------------------------////---------------------------------------------------------------////length=10 // ATR length//minMult=1//maxMult=5//step=0.5//perfAlpha=10 // Performance memory//fromCluster=1 // 1=best 2=average 3=worstmaxiter=10 // Número máximo de iteraciones de clusteringepsilon=0.0001 // Precisión para simular convergencia//---------------------------------------------------------------////-----Average True Range----------------------------------------////---------------------------------------------------------------//atr=averagetruerange[length](close)//---------------------------------------------------------------////-----Supertrend cálculo múltiple-------------------------------////---------------------------------------------------------------//$holderUpper[0]=(high+low)/2$holderLower[0]=(high+low)/2$holderOutput[0]=0$holderPerf[0]=0$holderFactor[0]=0$holderTrend[0]=0$factors[0]=0if barindex=0 thenn=0for i=0 to floor((maxMult-minMult)/step) do$factors[n+1]=(minMult+i*step)$holderUpper[n+1]=(high+low)/2$holderLower[n+1]=(high+low)/2$holderOutput[n+1]=0$holderPerf[n+1]=0$holderFactor[n+1]=0$holderTrend[n+1]=0n=n+1nextendifk=0for factor=0 to floor((maxMult-minMult)/step) doup=(high+low)/2+atr*factordn=(high+low)/2-atr*factorif close > $holderUpper[k] then$holderTrend[k]=1elsif close < $holderUpper[k] then$holderTrend[k]=0else$holderTrend[k]=$holderTrend[k]endifif close[1]<$holderUpper[k] then$holderUpper[k]=min(up,$holderUpper[k])else$holderUpper[k]=upendifif close[1]>$holderLower[k] then$holderLower[k]=max(dn,$holderLower[k])else$holderLower[k]=dnendifif (close[1]-$holderOutput[k])>0 thendiff=1elsif (close[1]-$holderOutput[k])<0 thendiff=-1elsediff=0endif$holderPerf[k]=$holderPerf[max(0,k-1)]+2/(perfAlpha+1)*((close-close[1])*diff-$holderPerf[max(0,k-1)])if $holderTrend[k]=1 then$holderOutput[k]=$holderLower[k]else$holderOutput[k]=$holderUpper[k]endif$holderFactor[k] = $factors[k]k=k+1next//---------------------------------------------------------------////-----K-means con convergencia----------------------------------////---------------------------------------------------------------//centroid1 = 0centroid2 = 0centroid3 = 0count = kFOR i = 0 TO count - 1 DOIF i < count / 3 THENcentroid1 = centroid1 + $holderPerf[i]ELSIF i < 2 * count / 3 THENcentroid2 = centroid2 + $holderPerf[i]ELSEcentroid3 = centroid3 + $holderPerf[i]ENDIFNEXTcentroid1 = centroid1 / (count / 3)centroid2 = centroid2 / (count / 3)centroid3 = centroid3 / (count / 3)converged = 0iter = 0WHILE iter < maxiter AND converged = 0 DOold1 = centroid1old2 = centroid2old3 = centroid3$cluster1Perf[0] = 0$cluster2Perf[0] = 0$cluster3Perf[0] = 0$cluster1Factors[0] = 0$cluster2Factors[0] = 0$cluster3Factors[0] = 0FOR i = 0 TO count - 1 DOdist1 = abs($holderPerf[i] - centroid1)dist2 = abs($holderPerf[i] - centroid2)dist3 = abs($holderPerf[i] - centroid3)IF dist1 <= dist2 AND dist1 <= dist3 THEN$cluster1Perf[$cluster1Perf[0]+1] = $holderPerf[i]$cluster1Factors[$cluster1Factors[0]+1] = $holderFactor[i]$cluster1Perf[0] = $cluster1Perf[0] + 1$cluster1Factors[0] = $cluster1Factors[0] + 1ELSIF dist2 <= dist1 AND dist2 <= dist3 THEN$cluster2Perf[$cluster2Perf[0]+1] = $holderPerf[i]$cluster2Factors[$cluster2Factors[0]+1] = $holderFactor[i]$cluster2Perf[0] = $cluster2Perf[0] + 1$cluster2Factors[0] = $cluster2Factors[0] + 1ELSE$cluster3Perf[$cluster3Perf[0]+1] = $holderPerf[i]$cluster3Factors[$cluster3Factors[0]+1] = $holderFactor[i]$cluster3Perf[0] = $cluster3Perf[0] + 1$cluster3Factors[0] = $cluster3Factors[0] + 1ENDIFNEXTcentroid1 = 0centroid2 = 0centroid3 = 0IF $cluster1Perf[0] > 0 THENFOR i = 1 TO $cluster1Perf[0] DOcentroid1 = centroid1 + $cluster1Perf[i]NEXTcentroid1 = centroid1 / $cluster1Perf[0]ENDIFIF $cluster2Perf[0] > 0 THENFOR i = 1 TO $cluster2Perf[0] DOcentroid2 = centroid2 + $cluster2Perf[i]NEXTcentroid2 = centroid2 / $cluster2Perf[0]ENDIFIF $cluster3Perf[0] > 0 THENFOR i = 1 TO $cluster3Perf[0] DOcentroid3 = centroid3 + $cluster3Perf[i]NEXTcentroid3 = centroid3 / $cluster3Perf[0]ENDIFIF abs(centroid1 - old1) < epsilon AND abs(centroid2 - old2) < epsilon AND abs(centroid3 - old3) < epsilon THENconverged = 1ENDIFiter = iter + 1WEND//---------------------------------------------------------------////-----Cluster seleccionado y nuevo Supertrend-------------------////---------------------------------------------------------------//finalFactor = 0finalPerf = 0perf = 0fact = 0IF fromCluster = 1 THEN// BestIF centroid1 > max(centroid2, centroid3) THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)if lastset($cluster1Perf) > 0 thenfinalPerf = perf / lastset($cluster1Perf)elsefinalPerf = 0endifELSIF centroid2 > max(centroid1, centroid3) THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)if lastset($cluster2Perf) > 0 thenfinalPerf = perf / lastset($cluster2Perf)elsefinalPerf = 0endifELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)if lastset($cluster3Perf) > 0 thenfinalPerf = perf / lastset($cluster3Perf)elsefinalPerf = 0endifENDIFELSIF fromCluster = 2 THEN// AverageIF centroid1 > centroid2 AND centroid1 < centroid3 THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)if lastset($cluster1Perf) > 0 thenfinalPerf = perf / lastset($cluster1Perf)elsefinalPerf = 0endifELSIF centroid2 > centroid1 AND centroid2 < centroid3 THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)if lastset($cluster2Perf) > 0 thenfinalPerf = perf / lastset($cluster2Perf)elsefinalPerf = 0endifELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)if lastset($cluster3Perf) > 0 thenfinalPerf = perf / lastset($cluster3Perf)elsefinalPerf = 0endifENDIFELSIF fromCluster = 3 THEN// WorstIF centroid1 < min(centroid2, centroid3) THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)if lastset($cluster1Perf) > 0 thenfinalPerf = perf / lastset($cluster1Perf)elsefinalPerf = 0endifELSIF centroid2 < min(centroid1, centroid3) THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)if lastset($cluster2Perf) > 0 thenfinalPerf = perf / lastset($cluster2Perf)elsefinalPerf = 0endifELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)if lastset($cluster3Perf) > 0 thenfinalPerf = perf / lastset($cluster3Perf)elsefinalPerf = 0endifENDIFENDIF//---------------------------------------------------------------////-----Renderizado final del Supertrend--------------------------////---------------------------------------------------------------//newup = (high + low) / 2 + atr * finalFactornewdown = (high + low) / 2 - atr * finalFactorif close[1] < newupper thennewupper = min(newup, newupper)elsenewupper = newupendifif close[1] > newlower thennewlower = max(newdown, newlower)elsenewlower = newdownendifif close > newupper thennewOS = 1elsif close < newlower thennewOS = 0elsenewOS = newOSendifif newOS and newOS[1]=0 thennewST = newlowerr = 0g = 255transp=0elsif newOS thennewST = newlowerr = 0g = 255transp=255elsif newOS=0 and newOS[1] thennewST = newupperr = 255g = 0transp=0elsenewST = newupperr = 255g = 0transp=255endif//---------------------------------------------------------------//return newST style(line,2) coloured(r,g,0,transp)
Bonjour, j’aimerai savoir dans ce code comment s’appelle la ligne verte et rouge que l’on voit sur l’image jointe ? Initialement, je suis parti dans l’idée que c’était “newST”, cependant en l’incluant dans un Screener afin de déterminer si la tendance est haussière ou baissière, je me retrouve trop souvent avec des résultats erronés! Pourriez-vous m’indiquer avec exactitude la dénomination de cette ligne dans ce code ? Merci
11/21/2025 at 9:18 AM #253845Bonjour. La ligne verte/rouge est nouvelle. Sa couleur change en fonction des conditions définies entre les lignes 325 et 345. Si vous souhaitez modifier la couleur, par exemple du rouge au vert, vous pouvez insérer le code suivant dans le filtre :
1screener[newOS and newOS[1]=0]1 user thanked author for this post.
11/21/2025 at 10:58 AM #253851Sur le graphique voici un exemple du problème que je rencontre, la paire GBP/SEK ressort haussière sur le Screener et pourtant la tendance est baissière sur l’indicateur SuperTrendAIClusterin ! Je joins aussi le code afin de trouver ce qui cloche!!!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343//"Screener SuperTrendAI"//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////SuperTrend AI Clusterin_V2 | Indicator////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Inputs//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////length=10 // ATR lengthminMult=1maxMult=5step=0.5perfAlpha=10 // Performance memoryfromCluster=2 // 1=best 2=average 3=worstmaxiter=10 // Número máximo de iteraciones de clusteringepsilon=0.0001 // Precisión para simular convergencia////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Average True Range//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////atr=averagetruerange[length](close)////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Supertrend cálculo múltiple//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////$holderUpper[0]=(high+low)/2$holderLower[0]=(high+low)/2$holderOutput[0]=0$holderPerf[0]=0$holderFactor[0]=0$holderTrend[0]=0$factors[0]=0if barindex=0 thenn=0for i=0 to floor((maxMult-minMult)/step) do$factors[n+1]=(minMult+i*step)$holderUpper[n+1]=(high+low)/2$holderLower[n+1]=(high+low)/2$holderOutput[n+1]=0$holderPerf[n+1]=0$holderFactor[n+1]=0$holderTrend[n+1]=0n=n+1nextendifk=0for factor=0 to floor((maxMult-minMult)/step) doup=(high+low)/2+atr*factordn=(high+low)/2-atr*factorif close > $holderUpper[k] then$holderTrend[k]=1elsif close < $holderUpper[k] then$holderTrend[k]=0else$holderTrend[k]=$holderTrend[k]endifif close[1]<$holderUpper[k] then$holderUpper[k]=min(up,$holderUpper[k])else$holderUpper[k]=upendifif close[1]>$holderLower[k] then$holderLower[k]=max(dn,$holderLower[k])else$holderLower[k]=dnendifif (close[1]-$holderOutput[k])>0 thendiff=1elsif (close[1]-$holderOutput[k])<0 thendiff=-1elsediff=0endif$holderPerf[k]=$holderPerf[max(0,k-1)]+2/(perfAlpha+1)*((close-close[1])*diff-$holderPerf[max(0,k-1)])if $holderTrend[k]=1 then$holderOutput[k]=$holderLower[k]else$holderOutput[k]=$holderUpper[k]endif$holderFactor[k] = $factors[k]k=k+1next////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////K-means con convergencia//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////centroid1 = 0centroid2 = 0centroid3 = 0count = kFOR i = 0 TO count - 1 DOIF i < count / 3 THENcentroid1 = centroid1 + $holderPerf[i]ELSIF i < 2 * count / 3 THENcentroid2 = centroid2 + $holderPerf[i]ELSEcentroid3 = centroid3 + $holderPerf[i]ENDIFNEXTcentroid1 = centroid1 / (count / 3)centroid2 = centroid2 / (count / 3)centroid3 = centroid3 / (count / 3)converged = 0iter = 0WHILE iter < maxiter AND converged = 0 DOold1 = centroid1old2 = centroid2old3 = centroid3$cluster1Perf[0] = 0$cluster2Perf[0] = 0$cluster3Perf[0] = 0$cluster1Factors[0] = 0$cluster2Factors[0] = 0$cluster3Factors[0] = 0FOR i = 0 TO count - 1 DOdist1 = abs($holderPerf[i] - centroid1)dist2 = abs($holderPerf[i] - centroid2)dist3 = abs($holderPerf[i] - centroid3)IF dist1 <= dist2 AND dist1 <= dist3 THEN$cluster1Perf[$cluster1Perf[0]+1] = $holderPerf[i]$cluster1Factors[$cluster1Factors[0]+1] = $holderFactor[i]$cluster1Perf[0] = $cluster1Perf[0] + 1$cluster1Factors[0] = $cluster1Factors[0] + 1ELSIF dist2 <= dist1 AND dist2 <= dist3 THEN$cluster2Perf[$cluster2Perf[0]+1] = $holderPerf[i]$cluster2Factors[$cluster2Factors[0]+1] = $holderFactor[i]$cluster2Perf[0] = $cluster2Perf[0] + 1$cluster2Factors[0] = $cluster2Factors[0] + 1ELSE$cluster3Perf[$cluster3Perf[0]+1] = $holderPerf[i]$cluster3Factors[$cluster3Factors[0]+1] = $holderFactor[i]$cluster3Perf[0] = $cluster3Perf[0] + 1$cluster3Factors[0] = $cluster3Factors[0] + 1ENDIFNEXTcentroid1 = 0centroid2 = 0centroid3 = 0IF $cluster1Perf[0] > 0 THENFOR i = 1 TO $cluster1Perf[0] DOcentroid1 = centroid1 + $cluster1Perf[i]NEXTcentroid1 = centroid1 / $cluster1Perf[0]ENDIFIF $cluster2Perf[0] > 0 THENFOR i = 1 TO $cluster2Perf[0] DOcentroid2 = centroid2 + $cluster2Perf[i]NEXTcentroid2 = centroid2 / $cluster2Perf[0]ENDIFIF $cluster3Perf[0] > 0 THENFOR i = 1 TO $cluster3Perf[0] DOcentroid3 = centroid3 + $cluster3Perf[i]NEXTcentroid3 = centroid3 / $cluster3Perf[0]ENDIFIF abs(centroid1 - old1) < epsilon AND abs(centroid2 - old2) < epsilon AND abs(centroid3 - old3) < epsilon THENconverged = 1ENDIFiter = iter + 1WEND////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Cluster seleccionado y nuevo Supertrend//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////finalFactor = 0perf = 0fact = 0IF fromCluster = 1 THEN// BestIF centroid1 > max(centroid2, centroid3) THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)ELSIF centroid2 > max(centroid1, centroid3) THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)ELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)ENDIFELSIF fromCluster = 2 THEN// AverageIF centroid1 > centroid2 AND centroid1 < centroid3 THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)ELSIF centroid2 > centroid1 AND centroid2 < centroid3 THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)ELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)ENDIFELSIF fromCluster = 3 THEN// WorstIF centroid1 < min(centroid2, centroid3) THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)ELSIF centroid2 < min(centroid1, centroid3) THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)ELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)ENDIFENDIF////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Renderizado final del Supertrend//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////newup = (high + low) / 2 + atr * finalFactornewdown = (high + low) / 2 - atr * finalFactorif close[1] < newupper thennewupper = min(newup, newupper)elsenewupper = newupendifif close[1] > newlower thennewlower = max(newdown, newlower)elsenewlower = newdownendifif close > newupper thennewOS = 1elsif close < newlower thennewOS = 0elsenewOS = newOSendif//if newOS and newOS[1]=0 then//newST = newlower////r = 0////g = 255////transp=0//elsif newOS then//newST = newlower////r = 0////g = 255////transp=255//elsif newOS=0 and newOS[1] then//newST = newupper////r = 255////g = 0////transp=0//else//newST = newupper////r = 255////g = 0////transp=255//endif//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////if newOS[0]>newOS[1] thenHausseSTAI = 1endifif newOS[0]<newOS[1] thenHausseSTAI = 0endif//if newOS[0]<newOS[1] thenBaisseSTAI = -1endifif newOS[0]>newOS[1] thenBaisseSTAI = 0endif////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Configuration//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Up = HausseSTAIDw = BaisseSTAI//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Signal = 0IF Up THENSignal = 1ENDIF//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////IF Dw THENSignal = -1ENDIF//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Screener[Up Or Dw](Signal as "Signal")//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////11/21/2025 at 12:14 PM #253855123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274//---------------------------------------------------------------////SuperTrend AI Clustering//version = 2//02.06.2025//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//---------------------------------------------------------------////-----Inputs----------------------------------------------------////---------------------------------------------------------------//length=10 // ATR lengthminMult=1maxMult=5step=0.5perfAlpha=10 // Performance memoryfromCluster=1 // 1=best 2=average 3=worstmaxiter=10 // Número máximo de iteraciones de clusteringepsilon=0.0001 // Precisión para simular convergencia//---------------------------------------------------------------////-----Average True Range----------------------------------------////---------------------------------------------------------------//atr=averagetruerange[length](close)//---------------------------------------------------------------////-----Supertrend cálculo múltiple-------------------------------////---------------------------------------------------------------//$holderUpper[0]=(high+low)/2$holderLower[0]=(high+low)/2k=0for factor=0 to floor((maxMult-minMult)/step) doup=(high+low)/2+atr*factordn=(high+low)/2-atr*factorif close > $holderUpper[k] then$holderTrend[k]=1elsif close < $holderUpper[k] then$holderTrend[k]=0else$holderTrend[k]=$holderTrend[k]endifif close[1]<$holderUpper[k] then$holderUpper[k]=min(up,$holderUpper[k])else$holderUpper[k]=upendifif close[1]>$holderLower[k] then$holderLower[k]=max(dn,$holderLower[k])else$holderLower[k]=dnendifif (close[1]-$holderOutput[k])>0 thendiff=1elsif (close[1]-$holderOutput[k])<0 thendiff=-1elsediff=0endif$holderPerf[k]=$holderPerf[max(0,k-1)]+2/(perfAlpha+1)*((close-close[1])*diff-$holderPerf[max(0,k-1)])if $holderTrend[k]=1 then$holderOutput[k]=$holderLower[k]else$holderOutput[k]=$holderUpper[k]endif$holderFactor[k] = (minMult+k*step)k=k+1next//---------------------------------------------------------------////-----K-means con convergencia----------------------------------////---------------------------------------------------------------//centroid1 = 0centroid2 = 0centroid3 = 0count = kFOR i = 0 TO count - 1 DOIF i < count / 3 THENcentroid1 = centroid1 + $holderPerf[i]ELSIF i < 2 * count / 3 THENcentroid2 = centroid2 + $holderPerf[i]ELSEcentroid3 = centroid3 + $holderPerf[i]ENDIFNEXTcentroid1 = centroid1 / (count / 3)centroid2 = centroid2 / (count / 3)centroid3 = centroid3 / (count / 3)converged = 0iter = 0WHILE iter < maxiter AND converged = 0 DOold1 = centroid1old2 = centroid2old3 = centroid3$cluster1Perf[0] = 0$cluster2Perf[0] = 0$cluster3Perf[0] = 0$cluster1Factors[0] = 0$cluster2Factors[0] = 0$cluster3Factors[0] = 0FOR i = 0 TO count - 1 DOdist1 = abs($holderPerf[i] - centroid1)dist2 = abs($holderPerf[i] - centroid2)dist3 = abs($holderPerf[i] - centroid3)IF dist1 <= dist2 AND dist1 <= dist3 THEN$cluster1Perf[$cluster1Perf[0]+1] = $holderPerf[i]$cluster1Factors[$cluster1Factors[0]+1] = $holderFactor[i]$cluster1Perf[0] = $cluster1Perf[0] + 1$cluster1Factors[0] = $cluster1Factors[0] + 1ELSIF dist2 <= dist1 AND dist2 <= dist3 THEN$cluster2Perf[$cluster2Perf[0]+1] = $holderPerf[i]$cluster2Factors[$cluster2Factors[0]+1] = $holderFactor[i]$cluster2Perf[0] = $cluster2Perf[0] + 1$cluster2Factors[0] = $cluster2Factors[0] + 1ELSE$cluster3Perf[$cluster3Perf[0]+1] = $holderPerf[i]$cluster3Factors[$cluster3Factors[0]+1] = $holderFactor[i]$cluster3Perf[0] = $cluster3Perf[0] + 1$cluster3Factors[0] = $cluster3Factors[0] + 1ENDIFNEXTcentroid1 = 0centroid2 = 0centroid3 = 0IF $cluster1Perf[0] > 0 THENFOR i = 1 TO $cluster1Perf[0] DOcentroid1 = centroid1 + $cluster1Perf[i]NEXTcentroid1 = centroid1 / $cluster1Perf[0]ENDIFIF $cluster2Perf[0] > 0 THENFOR i = 1 TO $cluster2Perf[0] DOcentroid2 = centroid2 + $cluster2Perf[i]NEXTcentroid2 = centroid2 / $cluster2Perf[0]ENDIFIF $cluster3Perf[0] > 0 THENFOR i = 1 TO $cluster3Perf[0] DOcentroid3 = centroid3 + $cluster3Perf[i]NEXTcentroid3 = centroid3 / $cluster3Perf[0]ENDIFIF abs(centroid1 - old1) < epsilon AND abs(centroid2 - old2) < epsilon AND abs(centroid3 - old3) < epsilon THENconverged = 1ENDIFiter = iter + 1WEND//---------------------------------------------------------------////-----Cluster seleccionado y nuevo Supertrend-------------------////---------------------------------------------------------------//finalFactor = 0perf = 0fact = 0IF fromCluster = 1 THEN// BestIF centroid1 > max(centroid2, centroid3) THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)ELSIF centroid2 > max(centroid1, centroid3) THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)ELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)ENDIFELSIF fromCluster = 2 THEN// AverageIF centroid1 > centroid2 AND centroid1 < centroid3 THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)ELSIF centroid2 > centroid1 AND centroid2 < centroid3 THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)ELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)ENDIFELSIF fromCluster = 3 THEN// WorstIF centroid1 < min(centroid2, centroid3) THENFOR j = 1 TO lastset($cluster1Perf) DOperf = perf + $cluster1Perf[j]fact = fact + $cluster1Factors[j]NEXTfinalFactor = fact / lastset($cluster1Factors)ELSIF centroid2 < min(centroid1, centroid3) THENFOR j = 1 TO lastset($cluster2Perf) DOperf = perf + $cluster2Perf[j]fact = fact + $cluster2Factors[j]NEXTfinalFactor = fact / lastset($cluster2Factors)ELSEFOR j = 1 TO lastset($cluster3Perf) DOperf = perf + $cluster3Perf[j]fact = fact + $cluster3Factors[j]NEXTfinalFactor = fact / lastset($cluster3Factors)ENDIFENDIF//---------------------------------------------------------------////-----Renderizado final del Supertrend--------------------------////---------------------------------------------------------------//newup = (high + low) / 2 + atr * finalFactornewdown = (high + low) / 2 - atr * finalFactorif close[1] < newupper thennewupper = min(newup, newupper)elsenewupper = newupendifif close[1] > newlower thennewlower = max(newdown, newlower)elsenewlower = newdownendifif close > newupper thennewOS = 1elsif close < newlower thennewOS = 0elsenewOS = newOSendif//---------------------------------signalUp=(newOS and newOS[1]=0)signalDn=(newOS=0 and newOS[1]=1)screener[signalUp or signalDn](signalUp)11/21/2025 at 12:45 PM #25385911/21/2025 at 2:13 PM #25386311/21/2025 at 2:44 PM #253868@Iván Je crois que je comprends ! Le code que tu me proposes indique lorsqu’il y a un changement d’une condition baissière vers une condition haussière et inversement. Mais ma demande porte sur le fait d’identifier une tendance haussière ou baissière affirmée dans le temps. Je ne cherche pas identifier un retournement, mais une continuation de tendance !!!
11/24/2025 at 11:28 AM #25391911/25/2025 at 3:49 PM #2539541234trendUp=newOS=1trendDn=newOS=0screener[trendUP or trendDn](newOS)2 users thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on 