Conversion Machine Learning Lorentzian Classification

Viewing 5 posts - 16 through 20 (of 20 total)
  • Author
    Posts
  • #235901 quote
    supertiti
    Participant
    Master

    Bonjour à tous,

    Beaucoup de travail où on aurait pu faire une économie de temps et de neurones brûlés !

    Je vous joins un graphe de ASML daily où j’ai superposé le LDC et une moyenne pondérée période 20 et qu’est ce qu’on voit

    la même chose à un poil près.

    La bonne nouvelle c’est qu’on peut utiliser la WMA20 sur PRT mobile comme on ne peut pas mettre de code perso.

    Bon trades à tous.

    Iván González and Fr7 thanked this post
    ASML-LDC-WMA.jpg ASML-LDC-WMA.jpg
    #244388 quote
    oliTR
    Participant
    New

    muchas gracias Iván

    en la línea 433 del código original figura la fórmula siguiente:
    yhat2 = kernels.gaussian(settings.source, h-lag, x)
    ¿Sería posible traducirla también?

    así como las siguientes líneas? :
    // Alert Variables
    bool alertBullish = useKernelSmoothing? isBullishCrossAlert: isBullishChange
    bool alertBearish = useKernelSmoothing? isBearishCrossAlert: isBearishChange
    // Bullish and Bearish Filters based on Kernel
    isBullish = useKernelFilter? (useKernelSmoothing? isBullishSmooth: isBullishRate): true
    isBearish = useKernelFilter? (useKernelSmoothing? isBearishSmooth: isBearishRate): true

    así como el final del código y en particular estos últimos apartados? :
    // Dynamic Exit Conditions
    // Fixed Exit Conditions
    // Alertas separadas para entradas y salidas
    // Alertas combinadas para entradas y salidas
    // Kernel Estimate Alerts

    #244409 quote
    Iván González
    Moderator
    Master

    Hola. De nada. Te dejo aquí el cálculo de yhat2.

    //Rational Quadratic
    src1=close
    startAtBar=x1
    relativeWeight=r1
    
    lookback1=h1
    lookback2=h1-lag
    
    currentWeight1=0
    cumulativeWeight1=0
    
    currentWeight2=0
    cumulativeWeight2=0
    
    size1=min(maxBarsBack,barindex)
    
    for k=0 to min(size1+startAtBar,barindex) do
    //Rational Quadratic
    w1=POW(1+(pow(k,2)/(pow(lookback1,2)*2*relativeWeight)),-relativeWeight)
    currentWeight1=currentWeight1+src1[k]*w1
    cumulativeWeight1=cumulativeWeight1+w1
    //Gaussian
    w2=EXP(-pow(k,2)/(2*pow(lookback2,2)))
    currentWeight2=currentWeight2+src1[k]*w2
    cumulativeWeight2=cumulativeWeight2+w2
    next
    
    yhat1=currentWeight1/cumulativeWeight1
    yhat2=currentWeight2/cumulativeWeight2
    oliTR thanked this post
    #255417 quote
    Pedro.lopez
    Participant
    Junior

    Hola, Iván

    Muchas gracias por la conversión de este indicador. Un trabajo Colosal.
    La razon de este mensaje es que estoy intentando hacer un SCREENER para obtener como resultados aquellas acciones con una alerta de compra/posicion larga (drawarrowUp) y con una columna de criterior en la que se reflejen los valores de la variable “prediction” pero hasta el momento ha obtengo ningun resultado, he tenido que utilizar como base el screener del compañero @supertiti porque en su screener si da resultados y la verdad no es que no veo donde estoy metiendo la pata.
    Me preguntaba si habías podido trabajar en el codigo de conversion a PRT. Te lo comento porque había visto que en la conversión del código original a PRT hay algunas variables que no se utilizan, aunque parece que no influyen en el resultado, por ejemplo

    a) en la línea 4 del codigo en PRT aparece

    featureCount = 5  // Número de características

    A cada valor de esta variable (featureCount=5/4/3/2) en el código original le corresponde un calculo diferente de distancia, sin embargo en el codigo en PRT sólo hay una formula de calculo con lo que esta variable no habria que declararla, no? o ¿estoy equivocado?

    b) en las lineas que van de la 12 a la 15 del codigo en PRT hay unas variables declaradas que se repiten  posteriormente en las lineas que van de la 118 a la 121. Entiendo que las ultimas se podrian eliminar. Las variables son,

    hh=8 //Lookback Window
    r=8 //Relative Weighting
    x=25 //Regression level
    lag=2 //Lag

    Por cierto, la variable lag tampoco se utiliza en el codigo en PRT, no?

    c) en las lineas que van de la 100 a la  106, como en el caso anterior, hay unas variables que aunque declaradas no se utilizan y que si no me equivoco se podrian eliminar, no?. Las variables a las que me refieron son:

    if signal<>signal[1] then
    barsHeld=0
    else
    barsHeld=barsHeld+1
    endif
    isHeldFourBars=barsHeld=4
    isHeldLessThanFourBars=BarsHeld>0 and BarsHeld<4

    d) lo mismo sucedería con la variable en la linea 110 del codigo en PRT

    isEarlySignalFlip=signal<>signal[1] and (isDifferentSignalType[1] or isDifferentSignalType[2] or isDifferentSignalType[3])

    Con lo que también se podria eliminar, no?

    Si tienes algo de tiempo te agradeceria pudieras darme un poco de luz.

    Muchisimas gracias!!!

    Recibe un cordial saludo,

    Pedro

    #257681 quote
    Iván González
    Moderator
    Master

    Hoy he han hecho referencia en el foro a este indicador así que aquí va una actualización. He incluido filtros de tendencia adicionales.

    // ============================================================
    // PRC_Machine Learning: Lorentzian Classification [@jdehorty]
    // version = 1
    // 09.02.2026
    // Iván González @ www.prorealcode.com
    // Sharing ProRealTime knowledge
    // ============================================================
    defparam calculateonlastbars = 1000
    // ============================================================
    // ==== PARÁMETROS GENERALES ====
    // ============================================================
    neighborsCount = 8   // Número de vecinos K
    maxBarsBack = 700    // Ventana histórica
    featureCount = 5     // Features para distancia
    
    // Filtros EMA/SMA (0=desactivado, 1=activado)
    useEmaFilter = 0
    emaPeriod = 200
    useSmaFilter = 0
    smaPeriod = 200
    
    // Filtros ML (0=desactivado, 1=activado)
    useVolatilityFilter = 1  // ATR reciente > ATR histórico
    useRegimeFilter = 1      // Detección trending/ranging con KLMF
    regimeThreshold = -0.1   // Umbral régimen (-10 a 10, default -0.1)
    useAdxFilter = 0         // ADX > umbral
    adxThreshold = 20        // Umbral ADX (0-100)
    
    // Kernel Regression
    useKernelFilter = 1
    useKernelSmoothing = 0  // 0=rate of change, 1=crossover yhat2/yhat1
    lookbackWindow = 8
    relWeight = 8
    regLevel = 25
    kLag = 2
    
    // ============================================================
    // ==== CÁLCULO DE FEATURES (NORMALIZADAS) ====
    // ============================================================
    // Feature 1: RSI(14) normalizado
    f1raw = RSI[14](close)
    f1lo = lowest[maxBarsBack](f1raw)
    f1hi = highest[maxBarsBack](f1raw)
    f1 = (f1raw - f1lo) / max(0.0001, f1hi - f1lo)
    
    // Feature 2: Wave Trend(10,11) normalizado
    ap = (high + low + close) / 3
    esa = average[10,1](ap)
    d1 = average[10,1](abs(ap - esa))
    ci = (ap - esa) / (0.015 * d1)
    f2raw = average[11,1](ci)
    f2lo = lowest[maxBarsBack](f2raw)
    f2hi = highest[maxBarsBack](f2raw)
    f2 = (f2raw - f2lo) / max(0.0001, f2hi - f2lo)
    
    // Feature 3: CCI(20) normalizado
    f3raw = CCI[20](close)
    f3lo = lowest[maxBarsBack](f3raw)
    f3hi = highest[maxBarsBack](f3raw)
    f3 = (f3raw - f3lo) / max(0.0001, f3hi - f3lo)
    
    // Feature 4: ADX(20) normalizado
    f4raw = ADX[20]
    f4lo = lowest[maxBarsBack](f4raw)
    f4hi = highest[maxBarsBack](f4raw)
    f4 = (f4raw - f4lo) / max(0.0001, f4hi - f4lo)
    
    // Feature 5: RSI(9) normalizado
    f5raw = RSI[9](close)
    f5lo = lowest[maxBarsBack](f5raw)
    f5hi = highest[maxBarsBack](f5raw)
    f5 = (f5raw - f5lo) / max(0.0001, f5hi - f5lo)
    
    // ============================================================
    // ==== NEXT BAR CLASSIFICATION (Training Labels) ====
    // ============================================================
    src = close
    IF src[4] < src[0] THEN
       yTrainSeries = -1
    ELSIF src[4] > src[0] THEN
       yTrainSeries = 1
    ELSE
       yTrainSeries = 0
    ENDIF
    
    // ============================================================
    // ==== CORE ML LOGIC: KNN con Distancia Lorentziana ====
    // ============================================================
    lastDistance = -1.0
    mySize = min(maxBarsBack - 1, barindex - 1)
    sizeLoop = min(maxBarsBack - 1, mySize)
    
    IF barindex >= maxBarsBack THEN
       nn = 0
       FOR i = 0 TO sizeLoop DO
          d = LOG(1 + ABS(f1 - f1[i])) + LOG(1 + ABS(f2 - f2[i])) + LOG(1 + ABS(f3 - f3[i])) + LOG(1 + ABS(f4 - f4[i])) + LOG(1 + ABS(f5 - f5[i]))
          IF d >= lastDistance AND (i MOD 4) <> 0 THEN
             lastDistance = d
             nn = nn + 1
             $distances[nn] = d
             $predictions[nn] = round(yTrainSeries[i])
             IF nn > neighborsCount THEN
                lastDistance = $distances[round(neighborsCount * 3 / 4)]
                FOR k = 1 TO nn - 1 DO
                   $distances[k] = $distances[k + 1]
                   $predictions[k] = $predictions[k + 1]
                NEXT
                $distances[nn] = 0
                $predictions[nn] = 0
                nn = nn - 1
             ENDIF
          ENDIF
       NEXT
       prediction = 0
       FOR j = 1 TO nn DO
          prediction = prediction + $predictions[j]
       NEXT
    ENDIF
    
    // ============================================================
    // ==== FILTROS ML ====
    // ============================================================
    
    // --- 1. Filtro de Volatilidad ---
    
    IF useVolatilityFilter THEN
       recentAtr = averagetruerange[1]
       historicalAtr = averagetruerange[10]
       filterVolatility = (recentAtr > historicalAtr)
    ELSE
       filterVolatility = 1
    ENDIF
    
    // --- 2. Filtro de Régimen (KLMF - Kaufman Adaptive) ---
    
    ohlc4src = (open + high + low + close) / 4
    IF (high - low) <> 0 THEN
       value1 = 0.2 * (ohlc4src - ohlc4src[1]) + 0.8 * value1[1]
       value2 = 0.1 * (high - low) + 0.8 * value2[1]
    ENDIF
    IF value2 <> 0 THEN
       omega = abs(value1 / value2)
    ELSE
       omega = 0
    ENDIF
    alphaK = (-1 * POW(omega, 2) + SQRT(POW(omega, 4) + 16 * POW(omega, 2))) / 8
    klmf = alphaK * ohlc4src + (1 - alphaK) * klmf[1]
    curveSlope = abs(klmf - klmf[1])
    avgSlope = average[200,1](curveSlope)
    
    IF useRegimeFilter THEN
       IF avgSlope <> 0 THEN
          normalizedSlope = (curveSlope - avgSlope) / avgSlope
          filterRegime = (normalizedSlope >= regimeThreshold)
       ELSE
          filterRegime = 1
       ENDIF
    ELSE
       filterRegime = 1
    ENDIF
    
    // --- 3. Filtro ADX ---
    
    IF useAdxFilter THEN
       adxVal = ADX[14]
       filterAdx = (adxVal > adxThreshold)
    ELSE
       filterAdx = 1
    ENDIF
    
    // --- Combinación de todos los filtros ---
    filterAll = filterVolatility AND filterRegime AND filterAdx
    
    // ============================================================
    // ==== FILTROS DE PREDICCIÓN ====
    // ============================================================
    
    // --- EMA Trend Filter ---
    IF useEmaFilter THEN
       isEmaUptrend = (close > average[emaPeriod,1](close))
       isEmaDowntrend = (close < average[emaPeriod,1](close))
    ELSE
       isEmaUptrend = 1
       isEmaDowntrend = 1
    ENDIF
    
    // --- SMA Trend Filter ---
    IF useSmaFilter THEN
       isSmaUptrend = (close > average[smaPeriod](close))
       isSmaDowntrend = (close < average[smaPeriod](close))
    ELSE
       isSmaUptrend = 1
       isSmaDowntrend = 1
    ENDIF
    
    // --- Signal GATED por filterAll ---
    
    IF prediction > 0 AND filterAll THEN
       signal = 1
    ELSIF prediction < 0 AND filterAll THEN
       signal = -1
    ENDIF
    
    // --- Bar-Count Filter ---
    IF signal <> signal[1] THEN
       barsHeld = 0
    ELSE
       barsHeld = barsHeld + 1
    ENDIF
    isHeldFourBars = (barsHeld = 4)
    isHeldLessThanFourBars = (barsHeld > 0 AND barsHeld < 4)
    
    // --- Fractal Filters ---
    isDifferentSignalType = (signal <> signal[1])
    isEarlySignalFlip = isDifferentSignalType AND ((signal[1] <> signal[2]) OR (signal[2] <> signal[3]) OR (signal[3] <> signal[4]))
    
    isBuySignal = (signal = 1) AND isEmaUptrend AND isSmaUptrend
    isSellSignal = (signal = -1) AND isEmaDowntrend AND isSmaDowntrend
    isNewBuySignal = isDifferentSignalType AND isBuySignal
    isNewSellSignal = isDifferentSignalType AND isSellSignal
    
    // ============================================================
    // ==== KERNEL REGRESSION (Nadaraya-Watson) ====
    // ============================================================
    
    // --- Rational Quadratic Kernel (yhat1) ---
    currentWeight1 = 0
    cumulativeWeight1 = 0
    FOR i = 0 TO lookbackWindow + regLevel DO
       y = src[i]
       w1 = POW(1 + (POW(i, 2) / (POW(lookbackWindow, 2) * 2 * relWeight)), -relWeight)
       currentWeight1 = currentWeight1 + y * w1
       cumulativeWeight1 = cumulativeWeight1 + w1
    NEXT
    yhat1 = currentWeight1 / cumulativeWeight1
    
    // --- Gaussian Kernel (yhat2) ---
    currentWeight2 = 0
    cumulativeWeight2 = 0
    gaussWindow = max(1, lookbackWindow - kLag)
    FOR i = 0 TO gaussWindow + regLevel DO
       y = src[i]
       w2 = EXP(-POW(i, 2) / (2 * POW(gaussWindow, 2)))
       currentWeight2 = currentWeight2 + y * w2
       cumulativeWeight2 = cumulativeWeight2 + w2
    NEXT
    yhat2 = currentWeight2 / cumulativeWeight2
    
    // --- Kernel Rates of Change ---
    wasBearishRate = (yhat1[2] > yhat1[1])
    wasBullishRate = (yhat1[2] < yhat1[1])
    isBearishRate = (yhat1[1] > yhat1)
    isBullishRate = (yhat1[1] < yhat1)
    isBearishChange = isBearishRate AND wasBullishRate
    isBullishChange = isBullishRate AND wasBearishRate
    
    // --- Kernel Crossovers ---
    isBullishSmooth = (yhat2 >= yhat1)
    isBearishSmooth = (yhat2 <= yhat1)
    
    // --- Aplicar filtro kernel según modo ---
    IF useKernelFilter THEN
       IF useKernelSmoothing THEN
          isBullish = isBullishSmooth
          isBearish = isBearishSmooth
       ELSE
          isBullish = isBullishRate
          isBearish = isBearishRate
       ENDIF
    ELSE
       isBullish = 1
       isBearish = 1
    ENDIF
    
    // --- Kernel color ---
    IF useKernelSmoothing THEN
       IF isBullishSmooth THEN
          cr = 0
          cg = 153
          cb = 136
       ELSE
          cr = 204
          cg = 51
          cb = 17
       ENDIF
    ELSE
       IF isBullishRate THEN
          cr = 0
          cg = 153
          cb = 136
       ELSE
          cr = 204
          cg = 51
          cb = 17
       ENDIF
    ENDIF
    
    // ============================================================
    // ==== ENTRY CONDITIONS ====
    // ============================================================
    startLongTrade = isNewBuySignal AND isBullish AND isEmaUptrend AND isSmaUptrend
    startShortTrade = isNewSellSignal AND isBearish AND isEmaDowntrend AND isSmaDowntrend
    
    // ============================================================
    // ==== DISPLAY ====
    // ============================================================
    myRange = high - low
    
    // Transparencia basada en fuerza de predicción (clamped 30-255)
    IF prediction <> 0 THEN
       maxPred = max(1, highest[300](abs(prediction)))
       alphaVal = min(255, max(30, round(abs(prediction) / maxPred * 255)))
    ELSE
       alphaVal = 80
    ENDIF
    
    IF startLongTrade THEN
       drawarrowup(barindex, low - myRange) coloured(0, 153, 136, alphaVal)
       drawtext("#prediction#", barindex, low - 1.5 * myRange) coloured(0, 153, 136)
    ELSIF startShortTrade THEN
       drawarrowdown(barindex, high + myRange) coloured(204, 51, 17, alphaVal)
       drawtext("#prediction#", barindex, high + 1.5 * myRange) coloured(204, 51, 17)
    ENDIF
    
    // ============================================================
    RETURN yhat1 AS "Kernel Estimate" style(line, 2) coloured(cr, cg, cb)
    


    robertogozzi thanked this post
Viewing 5 posts - 16 through 20 (of 20 total)
  • You must be logged in to reply to this topic.

TradingView to ProRealTime Translation Center

New Reply
Author
author-avatar
Fr7 @fr7 Participant
Summary

This topic contains 19 replies,
has 1 voice, and was last updated by Iván González
2 weeks, 4 days ago.

Topic Details
Forum: TradingView to ProRealTime Translation Center Forum
Started: 05/18/2024
Status: Active
Attachments: 12 files
Logo Logo
Loading...