Super Trend AI Clustering

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #253841 quote
    Alai-n
    Participant
    Veteran
    //---------------------------------------------------------------//
    //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=worst
    maxiter=10 // Número máximo de iteraciones de clustering
    epsilon=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]=0
    
    if barindex=0 then
    n=0
    for 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]=0
    n=n+1
    next
    endif
    
    k=0
    for factor=0 to floor((maxMult-minMult)/step) do
    up=(high+low)/2+atr*factor
    dn=(high+low)/2-atr*factor
    
    if close > $holderUpper[k] then
    $holderTrend[k]=1
    elsif close < $holderUpper[k] then
    $holderTrend[k]=0
    else
    $holderTrend[k]=$holderTrend[k]
    endif
    
    if close[1]<$holderUpper[k] then
    $holderUpper[k]=min(up,$holderUpper[k])
    else
    $holderUpper[k]=up
    endif
    
    if close[1]>$holderLower[k] then
    $holderLower[k]=max(dn,$holderLower[k])
    else
    $holderLower[k]=dn
    endif
    
    if (close[1]-$holderOutput[k])>0 then
    diff=1
    elsif (close[1]-$holderOutput[k])<0 then
    diff=-1
    else
    diff=0
    endif
    
    $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+1
    next
    //---------------------------------------------------------------//
    //-----K-means con convergencia----------------------------------//
    //---------------------------------------------------------------//
    centroid1 = 0
    centroid2 = 0
    centroid3 = 0
    count = k
    
    FOR i = 0 TO count - 1 DO
    IF i < count / 3 THEN
    centroid1 = centroid1 + $holderPerf[i]
    ELSIF i < 2 * count / 3 THEN
    centroid2 = centroid2 + $holderPerf[i]
    ELSE
    centroid3 = centroid3 + $holderPerf[i]
    ENDIF
    NEXT
    
    centroid1 = centroid1 / (count / 3)
    centroid2 = centroid2 / (count / 3)
    centroid3 = centroid3 / (count / 3)
    
    converged = 0
    iter = 0
    
    WHILE iter < maxiter AND converged = 0 DO
    old1 = centroid1
    old2 = centroid2
    old3 = centroid3
    
    $cluster1Perf[0] = 0
    $cluster2Perf[0] = 0
    $cluster3Perf[0] = 0
    $cluster1Factors[0] = 0
    $cluster2Factors[0] = 0
    $cluster3Factors[0] = 0
    
    FOR i = 0 TO count - 1 DO
    dist1 = 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] + 1
    ELSIF 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] + 1
    ELSE
    $cluster3Perf[$cluster3Perf[0]+1] = $holderPerf[i]
    $cluster3Factors[$cluster3Factors[0]+1] = $holderFactor[i]
    $cluster3Perf[0] = $cluster3Perf[0] + 1
    $cluster3Factors[0] = $cluster3Factors[0] + 1
    ENDIF
    NEXT
    
    centroid1 = 0
    centroid2 = 0
    centroid3 = 0
    
    IF $cluster1Perf[0] > 0 THEN
    FOR i = 1 TO $cluster1Perf[0] DO
    centroid1 = centroid1 + $cluster1Perf[i]
    NEXT
    centroid1 = centroid1 / $cluster1Perf[0]
    ENDIF
    IF $cluster2Perf[0] > 0 THEN
    FOR i = 1 TO $cluster2Perf[0] DO
    centroid2 = centroid2 + $cluster2Perf[i]
    NEXT
    centroid2 = centroid2 / $cluster2Perf[0]
    ENDIF
    IF $cluster3Perf[0] > 0 THEN
    FOR i = 1 TO $cluster3Perf[0] DO
    centroid3 = centroid3 + $cluster3Perf[i]
    NEXT
    centroid3 = centroid3 / $cluster3Perf[0]
    ENDIF
    
    IF abs(centroid1 - old1) < epsilon AND abs(centroid2 - old2) < epsilon AND abs(centroid3 - old3) < epsilon THEN
    converged = 1
    ENDIF
    
    iter = iter + 1
    WEND
    //---------------------------------------------------------------//
    //-----Cluster seleccionado y nuevo Supertrend-------------------//
    //---------------------------------------------------------------//
    finalFactor = 0
    finalPerf = 0
    perf = 0
    fact = 0
    
    IF fromCluster = 1 THEN
    // Best
    IF centroid1 > max(centroid2, centroid3) THEN
    FOR j = 1 TO lastset($cluster1Perf) DO
    perf = perf + $cluster1Perf[j]
    fact = fact + $cluster1Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster1Factors)
    if lastset($cluster1Perf) > 0 then
    finalPerf = perf / lastset($cluster1Perf)
    else
    finalPerf = 0
    endif
    ELSIF centroid2 > max(centroid1, centroid3) THEN
    FOR j = 1 TO lastset($cluster2Perf) DO
    perf = perf + $cluster2Perf[j]
    fact = fact + $cluster2Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster2Factors)
    if lastset($cluster2Perf) > 0 then
    finalPerf = perf / lastset($cluster2Perf)
    else
    finalPerf = 0
    endif
    ELSE
    FOR j = 1 TO lastset($cluster3Perf) DO
    perf = perf + $cluster3Perf[j]
    fact = fact + $cluster3Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster3Factors)
    if lastset($cluster3Perf) > 0 then
    finalPerf = perf / lastset($cluster3Perf)
    else
    finalPerf = 0
    endif
    ENDIF
    
    ELSIF fromCluster = 2 THEN
    // Average
    IF centroid1 > centroid2 AND centroid1 < centroid3 THEN
    FOR j = 1 TO lastset($cluster1Perf) DO
    perf = perf + $cluster1Perf[j]
    fact = fact + $cluster1Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster1Factors)
    if lastset($cluster1Perf) > 0 then
    finalPerf = perf / lastset($cluster1Perf)
    else
    finalPerf = 0
    endif
    ELSIF centroid2 > centroid1 AND centroid2 < centroid3 THEN
    FOR j = 1 TO lastset($cluster2Perf) DO
    perf = perf + $cluster2Perf[j]
    fact = fact + $cluster2Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster2Factors)
    if lastset($cluster2Perf) > 0 then
    finalPerf = perf / lastset($cluster2Perf)
    else
    finalPerf = 0
    endif
    ELSE
    FOR j = 1 TO lastset($cluster3Perf) DO
    perf = perf + $cluster3Perf[j]
    fact = fact + $cluster3Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster3Factors)
    if lastset($cluster3Perf) > 0 then
    finalPerf = perf / lastset($cluster3Perf)
    else
    finalPerf = 0
    endif
    ENDIF
    
    ELSIF fromCluster = 3 THEN
    // Worst
    IF centroid1 < min(centroid2, centroid3) THEN
    FOR j = 1 TO lastset($cluster1Perf) DO
    perf = perf + $cluster1Perf[j]
    fact = fact + $cluster1Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster1Factors)
    if lastset($cluster1Perf) > 0 then
    finalPerf = perf / lastset($cluster1Perf)
    else
    finalPerf = 0
    endif
    ELSIF centroid2 < min(centroid1, centroid3) THEN
    FOR j = 1 TO lastset($cluster2Perf) DO
    perf = perf + $cluster2Perf[j]
    fact = fact + $cluster2Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster2Factors)
    if lastset($cluster2Perf) > 0 then
    finalPerf = perf / lastset($cluster2Perf)
    else
    finalPerf = 0
    endif
    ELSE
    FOR j = 1 TO lastset($cluster3Perf) DO
    perf = perf + $cluster3Perf[j]
    fact = fact + $cluster3Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster3Factors)
    if lastset($cluster3Perf) > 0 then
    finalPerf = perf / lastset($cluster3Perf)
    else
    finalPerf = 0
    endif
    ENDIF
    ENDIF
    //---------------------------------------------------------------//
    //-----Renderizado final del Supertrend--------------------------//
    //---------------------------------------------------------------//
    newup = (high + low) / 2 + atr * finalFactor
    newdown = (high + low) / 2 - atr * finalFactor
    
    if close[1] < newupper then
    newupper = min(newup, newupper)
    else
    newupper = newup
    endif
    
    if close[1] > newlower then
    newlower = max(newdown, newlower)
    else
    newlower = newdown
    endif
    
    if close > newupper then
    newOS = 1
    elsif close < newlower then
    newOS = 0
    else
    newOS = newOS
    endif
    
    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
    //---------------------------------------------------------------//
    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

    #253845 quote
    Iván González
    Moderator
    Master

    Bonjour. 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 :

    screener[newOS and newOS[1]=0]
    Alai-n thanked this post
    #253851 quote
    Alai-n
    Participant
    Veteran

    Sur 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!!!

    //"Screener SuperTrendAI"
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //SuperTrend AI Clusterin_V2 | Indicator
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Inputs
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    length=10 // ATR length
    minMult=1
    maxMult=5
    step=0.5
    perfAlpha=10 // Performance memory
    fromCluster=2 // 1=best 2=average 3=worst
    maxiter=10 // Número máximo de iteraciones de clustering
    epsilon=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]=0
    
    if barindex=0 then
    n=0
    for 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]=0
    n=n+1
    next
    endif
    
    k=0
    for factor=0 to floor((maxMult-minMult)/step) do
    up=(high+low)/2+atr*factor
    dn=(high+low)/2-atr*factor
    
    if close > $holderUpper[k] then
    $holderTrend[k]=1
    elsif close < $holderUpper[k] then
    $holderTrend[k]=0
    else
    $holderTrend[k]=$holderTrend[k]
    endif
    
    if close[1]<$holderUpper[k] then
    $holderUpper[k]=min(up,$holderUpper[k])
    else
    $holderUpper[k]=up
    endif
    
    if close[1]>$holderLower[k] then
    $holderLower[k]=max(dn,$holderLower[k])
    else
    $holderLower[k]=dn
    endif
    
    if (close[1]-$holderOutput[k])>0 then
    diff=1
    elsif (close[1]-$holderOutput[k])<0 then
    diff=-1
    else
    diff=0
    endif
    
    $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+1
    next
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //K-means con convergencia
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    centroid1 = 0
    centroid2 = 0
    centroid3 = 0
    count = k
    
    FOR i = 0 TO count - 1 DO
    IF i < count / 3 THEN
    centroid1 = centroid1 + $holderPerf[i]
    ELSIF i < 2 * count / 3 THEN
    centroid2 = centroid2 + $holderPerf[i]
    ELSE
    centroid3 = centroid3 + $holderPerf[i]
    ENDIF
    NEXT
    
    centroid1 = centroid1 / (count / 3)
    centroid2 = centroid2 / (count / 3)
    centroid3 = centroid3 / (count / 3)
    
    converged = 0
    iter = 0
    
    WHILE iter < maxiter AND converged = 0 DO
    old1 = centroid1
    old2 = centroid2
    old3 = centroid3
    
    $cluster1Perf[0] = 0
    $cluster2Perf[0] = 0
    $cluster3Perf[0] = 0
    $cluster1Factors[0] = 0
    $cluster2Factors[0] = 0
    $cluster3Factors[0] = 0
    
    FOR i = 0 TO count - 1 DO
    dist1 = 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] + 1
    ELSIF 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] + 1
    ELSE
    $cluster3Perf[$cluster3Perf[0]+1] = $holderPerf[i]
    $cluster3Factors[$cluster3Factors[0]+1] = $holderFactor[i]
    $cluster3Perf[0] = $cluster3Perf[0] + 1
    $cluster3Factors[0] = $cluster3Factors[0] + 1
    ENDIF
    NEXT
    
    centroid1 = 0
    centroid2 = 0
    centroid3 = 0
    
    IF $cluster1Perf[0] > 0 THEN
    FOR i = 1 TO $cluster1Perf[0] DO
    centroid1 = centroid1 + $cluster1Perf[i]
    NEXT
    centroid1 = centroid1 / $cluster1Perf[0]
    ENDIF
    IF $cluster2Perf[0] > 0 THEN
    FOR i = 1 TO $cluster2Perf[0] DO
    centroid2 = centroid2 + $cluster2Perf[i]
    NEXT
    centroid2 = centroid2 / $cluster2Perf[0]
    ENDIF
    IF $cluster3Perf[0] > 0 THEN
    FOR i = 1 TO $cluster3Perf[0] DO
    centroid3 = centroid3 + $cluster3Perf[i]
    NEXT
    centroid3 = centroid3 / $cluster3Perf[0]
    ENDIF
    
    IF abs(centroid1 - old1) < epsilon AND abs(centroid2 - old2) < epsilon AND abs(centroid3 - old3) < epsilon THEN
    converged = 1
    ENDIF
    
    iter = iter + 1
    WEND
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Cluster seleccionado y nuevo Supertrend
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    finalFactor = 0
    perf = 0
    fact = 0
    
    IF fromCluster = 1 THEN
    // Best
    IF centroid1 > max(centroid2, centroid3) THEN
    FOR j = 1 TO lastset($cluster1Perf) DO
    perf = perf + $cluster1Perf[j]
    fact = fact + $cluster1Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster1Factors)
          
    ELSIF centroid2 > max(centroid1, centroid3) THEN
    FOR j = 1 TO lastset($cluster2Perf) DO
    perf = perf + $cluster2Perf[j]
    fact = fact + $cluster2Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster2Factors)
          
    ELSE
    FOR j = 1 TO lastset($cluster3Perf) DO
    perf = perf + $cluster3Perf[j]
    fact = fact + $cluster3Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster3Factors)
          
    ENDIF
       
    ELSIF fromCluster = 2 THEN
    // Average
    IF centroid1 > centroid2 AND centroid1 < centroid3 THEN
    FOR j = 1 TO lastset($cluster1Perf) DO
    perf = perf + $cluster1Perf[j]
    fact = fact + $cluster1Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster1Factors)
          
    ELSIF centroid2 > centroid1 AND centroid2 < centroid3 THEN
    FOR j = 1 TO lastset($cluster2Perf) DO
    perf = perf + $cluster2Perf[j]
    fact = fact + $cluster2Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster2Factors)
          
    ELSE
    FOR j = 1 TO lastset($cluster3Perf) DO
    perf = perf + $cluster3Perf[j]
    fact = fact + $cluster3Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster3Factors)
          
    ENDIF
       
    ELSIF fromCluster = 3 THEN
    // Worst
    IF centroid1 < min(centroid2, centroid3) THEN
    FOR j = 1 TO lastset($cluster1Perf) DO
    perf = perf + $cluster1Perf[j]
    fact = fact + $cluster1Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster1Factors)
          
    ELSIF centroid2 < min(centroid1, centroid3) THEN
    FOR j = 1 TO lastset($cluster2Perf) DO
    perf = perf + $cluster2Perf[j]
    fact = fact + $cluster2Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster2Factors)
          
    ELSE
    FOR j = 1 TO lastset($cluster3Perf) DO
    perf = perf + $cluster3Perf[j]
    fact = fact + $cluster3Factors[j]
    NEXT
    finalFactor = fact / lastset($cluster3Factors)
          
    ENDIF
    ENDIF
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Renderizado final del Supertrend
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    newup = (high + low) / 2 + atr * finalFactor
    newdown = (high + low) / 2 - atr * finalFactor
    
    if close[1] < newupper then
    newupper = min(newup, newupper)
    else
    newupper = newup
    endif
    
    if close[1] > newlower then
    newlower = max(newdown, newlower)
    else
    newlower = newdown
    endif
    
    if close > newupper then
    newOS = 1
    elsif close < newlower then
    newOS = 0
    else
    newOS = newOS
    endif
    
    //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] then
    HausseSTAI = 1
    endif
    
    if newOS[0]<newOS[1] then
    HausseSTAI = 0
    endif
    //
    if newOS[0]<newOS[1] then
    BaisseSTAI = -1
    endif
    
    if newOS[0]>newOS[1] then
    BaisseSTAI = 0
    endif
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Configuration
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Up = HausseSTAI
    Dw = BaisseSTAI
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    Signal = 0
    
    IF Up THEN
    Signal = 1
    ENDIF
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    IF Dw THEN
    Signal = -1
    ENDIF
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Screener[Up Or Dw](Signal as "Signal")
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    #253855 quote
    Iván González
    Moderator
    Master
    //---------------------------------------------------------------//
    //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=worst
    maxiter=10 // Número máximo de iteraciones de clustering
    epsilon=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
    
    k=0
    for factor=0 to floor((maxMult-minMult)/step) do
       up=(high+low)/2+atr*factor
       dn=(high+low)/2-atr*factor
       
       if close > $holderUpper[k] then
          $holderTrend[k]=1
       elsif close < $holderUpper[k] then
          $holderTrend[k]=0
       else
          $holderTrend[k]=$holderTrend[k]
       endif
       
       if close[1]<$holderUpper[k] then
          $holderUpper[k]=min(up,$holderUpper[k])
       else
          $holderUpper[k]=up
       endif
       
       if close[1]>$holderLower[k] then
          $holderLower[k]=max(dn,$holderLower[k])
       else
          $holderLower[k]=dn
       endif
       
       if (close[1]-$holderOutput[k])>0 then
          diff=1
       elsif (close[1]-$holderOutput[k])<0 then
          diff=-1
       else
          diff=0
       endif
       
       $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+1
    next
    //---------------------------------------------------------------//
    //-----K-means con convergencia----------------------------------//
    //---------------------------------------------------------------//
    centroid1 = 0
    centroid2 = 0
    centroid3 = 0
    count = k
    
    FOR i = 0 TO count - 1 DO
       IF i < count / 3 THEN
          centroid1 = centroid1 + $holderPerf[i]
       ELSIF i < 2 * count / 3 THEN
          centroid2 = centroid2 + $holderPerf[i]
       ELSE
          centroid3 = centroid3 + $holderPerf[i]
       ENDIF
    NEXT
    
    centroid1 = centroid1 / (count / 3)
    centroid2 = centroid2 / (count / 3)
    centroid3 = centroid3 / (count / 3)
    
    converged = 0
    iter = 0
    
    WHILE iter < maxiter AND converged = 0 DO
       old1 = centroid1
       old2 = centroid2
       old3 = centroid3
       
       $cluster1Perf[0] = 0
       $cluster2Perf[0] = 0
       $cluster3Perf[0] = 0
       $cluster1Factors[0] = 0
       $cluster2Factors[0] = 0
       $cluster3Factors[0] = 0
       
       FOR i = 0 TO count - 1 DO
          dist1 = 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] + 1
          ELSIF 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] + 1
          ELSE
             $cluster3Perf[$cluster3Perf[0]+1] = $holderPerf[i]
             $cluster3Factors[$cluster3Factors[0]+1] = $holderFactor[i]
             $cluster3Perf[0] = $cluster3Perf[0] + 1
             $cluster3Factors[0] = $cluster3Factors[0] + 1
          ENDIF
       NEXT
       
       centroid1 = 0
       centroid2 = 0
       centroid3 = 0
       
       IF $cluster1Perf[0] > 0 THEN
          FOR i = 1 TO $cluster1Perf[0] DO
             centroid1 = centroid1 + $cluster1Perf[i]
          NEXT
          centroid1 = centroid1 / $cluster1Perf[0]
       ENDIF
       IF $cluster2Perf[0] > 0 THEN
          FOR i = 1 TO $cluster2Perf[0] DO
             centroid2 = centroid2 + $cluster2Perf[i]
          NEXT
          centroid2 = centroid2 / $cluster2Perf[0]
       ENDIF
       IF $cluster3Perf[0] > 0 THEN
          FOR i = 1 TO $cluster3Perf[0] DO
             centroid3 = centroid3 + $cluster3Perf[i]
          NEXT
          centroid3 = centroid3 / $cluster3Perf[0]
       ENDIF
       
       IF abs(centroid1 - old1) < epsilon AND abs(centroid2 - old2) < epsilon AND abs(centroid3 - old3) < epsilon THEN
          converged = 1
       ENDIF
       
       iter = iter + 1
    WEND
    //---------------------------------------------------------------//
    //-----Cluster seleccionado y nuevo Supertrend-------------------//
    //---------------------------------------------------------------//
    finalFactor = 0
    
    perf = 0
    fact = 0
    
    IF fromCluster = 1 THEN
       // Best
       IF centroid1 > max(centroid2, centroid3) THEN
          FOR j = 1 TO lastset($cluster1Perf) DO
             perf = perf + $cluster1Perf[j]
             fact = fact + $cluster1Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster1Factors)
          
       ELSIF centroid2 > max(centroid1, centroid3) THEN
          FOR j = 1 TO lastset($cluster2Perf) DO
             perf = perf + $cluster2Perf[j]
             fact = fact + $cluster2Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster2Factors)
          
       ELSE
          FOR j = 1 TO lastset($cluster3Perf) DO
             perf = perf + $cluster3Perf[j]
             fact = fact + $cluster3Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster3Factors)
          
       ENDIF
       
    ELSIF fromCluster = 2 THEN
       // Average
       IF centroid1 > centroid2 AND centroid1 < centroid3 THEN
          FOR j = 1 TO lastset($cluster1Perf) DO
             perf = perf + $cluster1Perf[j]
             fact = fact + $cluster1Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster1Factors)
          
       ELSIF centroid2 > centroid1 AND centroid2 < centroid3 THEN
          FOR j = 1 TO lastset($cluster2Perf) DO
             perf = perf + $cluster2Perf[j]
             fact = fact + $cluster2Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster2Factors)
          
       ELSE
          FOR j = 1 TO lastset($cluster3Perf) DO
             perf = perf + $cluster3Perf[j]
             fact = fact + $cluster3Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster3Factors)
          
       ENDIF
       
    ELSIF fromCluster = 3 THEN
       // Worst
       IF centroid1 < min(centroid2, centroid3) THEN
          FOR j = 1 TO lastset($cluster1Perf) DO
             perf = perf + $cluster1Perf[j]
             fact = fact + $cluster1Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster1Factors)
          
       ELSIF centroid2 < min(centroid1, centroid3) THEN
          FOR j = 1 TO lastset($cluster2Perf) DO
             perf = perf + $cluster2Perf[j]
             fact = fact + $cluster2Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster2Factors)
          
       ELSE
          FOR j = 1 TO lastset($cluster3Perf) DO
             perf = perf + $cluster3Perf[j]
             fact = fact + $cluster3Factors[j]
          NEXT
          finalFactor = fact / lastset($cluster3Factors)
          
       ENDIF
    ENDIF
    //---------------------------------------------------------------//
    //-----Renderizado final del Supertrend--------------------------//
    //---------------------------------------------------------------//
    newup = (high + low) / 2 + atr * finalFactor
    newdown = (high + low) / 2 - atr * finalFactor
    
    if close[1] < newupper then
       newupper = min(newup, newupper)
    else
       newupper = newup
    endif
    
    if close[1] > newlower then
       newlower = max(newdown, newlower)
    else
       newlower = newdown
    endif
    
    if close > newupper then
       newOS = 1
    elsif close < newlower then
       newOS = 0
    else
       newOS = newOS
    endif
    
    //---------------------------------
    signalUp=(newOS and newOS[1]=0)
    signalDn=(newOS=0 and newOS[1]=1)
    screener[signalUp or signalDn](signalUp)
    
    #253859 quote
    Alai-n
    Participant
    Veteran

    @Iván Il ne se passe rien, aucune valeur n’est retournée! Le Screener reste vide!

    #253863 quote
    Iván González
    Moderator
    Master

    Je ne peux plus rien faire. Ça me convient pourtant…

    #253868 quote
    Alai-n
    Participant
    Veteran

    @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 !!!

    #253919 quote
    Alai-n
    Participant
    Veteran

    UP

    #253954 quote
    Iván González
    Moderator
    Master
    trendUp=newOS=1
    trendDn=newOS=0
    
    screener[trendUP or trendDn](newOS)
    robertogozzi and Alai-n thanked this post
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Super Trend AI Clustering


ProBuilder : Indicateurs & Outils Personnalisés

New Reply
Author
author-avatar
Alai-n @alai-n Participant
Summary

This topic contains 8 replies,
has 2 voices, and was last updated by Iván González
2 months, 1 week ago.

Topic Details
Forum: ProBuilder : Indicateurs & Outils Personnalisés
Language: French
Started: 11/21/2025
Status: Active
Attachments: 3 files
Logo Logo
Loading...