Flexible S/R Channels indicatore di curve paraboliche

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #259856 quote
    okeus
    Participant
    Senior

    ciao ho trovato questo indicatore che permette di tracciare delle curve paraboliche a partire da tre punti sul grafico, molto utile come previsione di tendenza e come supporti e resistenze . Grazie del vostro supporto

    //@version=6

    indicator(‘Flexible S/R Channels’, overlay=true, shorttitle=’FlexSR’)


    // =========================== TOOLTIP STRINGS ===========================

    ttA      = ‘Resistance: Initial Swing High (1 of 6)’

    ttB      = ‘Resistance: Intermediate Swing High (2 of 6)’

    ttC      = ‘Resistance: Recent Swing High (3 of 6)’

    ttD      = ‘Support: Initial Swing Low (4 of 6)’

    ttE      = ‘Support: Intermediate Swing Low (5 of 6)’

    ttF      = ‘Support: Recent Swing Low (6 of 6)’

    ttCurve    = ‘Mathematical method for fitting curves through anchor points.’

    ttPro     = ‘Number of bars to project the curves into the future. (0 to 500)’

    ttGrip    = ‘Size of the draggable handles displayed at each curve anchor point. (0 = hidden, 40 = largest)’

    ttLW     = ‘Width of the support and resistance curve lines.’

    ttSCol    = ‘Color for the support curve and its projection.’

    ttRCol    = ‘Color for the resistance curve and its projection.’

    ttInfo    = ‘Toggle display of the info table in the chart corner.’


    // =============================== INPUTS ================================

    CurveMethod  = input.string (‘Quadratic’,       ‘Curve Method’,   options=[‘Quadratic’,’Quadratic-Linear’,’Weighted Linear’, ‘Natural Cubic Spline’], tooltip=ttCurve)   

    Projection  = input.int   (100,           ‘Projection Length’, minval=0, maxval=500,     tooltip=ttPro)

    grip_size   = input.int   (20,           ‘Grip Size’,     minval=0, maxval=40,      tooltip=ttGrip)

    line_width  = input.int   (4,            ‘Line Width’,    minval=1, maxval=25,      tooltip=ttLW)

    col_support  = input.color  (color.new(#008000, 0),  ‘Support Color’,                  tooltip=ttSCol)

    col_resist  = input.color  (color.new(#FF0000, 0),  ‘Resistance Color’,                 tooltip=ttRCol)

    show_info   = input.bool  (true,          ‘Info Table’,                    tooltip=ttInfo)


    TP_R     = ‘Resistance Time/Price’  

    A_Time_    = input.time  (timestamp(‘2024-08-12′), ”,   confirm=true,  group=TP_R,   inline=’A’, tooltip=ttA)

    A_Price_   = input.price  (0, ”,              confirm=true,  group=TP_R,   inline=’A’, tooltip=ttA)

    B_Time_    = input.time  (timestamp(‘2024-08-12′), ”,   confirm=true,  group=TP_R,   inline=’B’, tooltip=ttB)

    B_Price_   = input.price  (0, ”,              confirm=true,  group=TP_R,   inline=’B’, tooltip=ttB)

    C_Time_    = input.time  (timestamp(‘2024-08-12′), ”,   confirm=true,  group=TP_R,   inline=’C’, tooltip=ttC)

    C_Price_   = input.price  (0, ”,              confirm=true,  group=TP_R,   inline=’C’, tooltip=ttC)


    TP_S     = ‘Support Time/Price’

    D_Time_    = input.time  (timestamp(‘2024-08-12′), ”,   confirm=true,  group=TP_S,   inline=’D’, tooltip=ttD)  

    D_Price_   = input.price  (0, ”,              confirm=true,  group=TP_S,   inline=’D’, tooltip=ttD)  

    E_Time_    = input.time  (timestamp(‘2024-08-12′), ”,   confirm=true,  group=TP_S,   inline=’E’, tooltip=ttE)  

    E_Price_   = input.price  (0, ”,              confirm=true,  group=TP_S,   inline=’E’, tooltip=ttE)  

    F_Time_    = input.time  (timestamp(‘2024-08-12′), ”,   confirm=true,  group=TP_S,   inline=’F’, tooltip=ttF)  

    F_Price_   = input.price  (0, ”,              confirm=true,  group=TP_S,   inline=’F’, tooltip=ttF)  


    // ============================= FUNCTIONS ===============================

    // Ensures time-price points are in chronological order, even if user inputs or drags them out of order.

    f_sort_points(int t1, float p1, int t2, float p2, int t3, float p3) =>

      int  sT1 = t1

      int  sT2 = t2

      int  sT3 = t3

      float sP1 = p1

      float sP2 = p2

      float sP3 = p3

       

      if sT1 > sT2

        int  tempT = sT1

        float tempP = sP1

        sT1    := sT2

        sP1    := sP2

        sT2    := tempT

        sP2    := tempP

       

      if sT2 > sT3

        int  tempT = sT2

        float tempP = sP2

        sT2    := sT3

        sP2    := sP3

        sT3    := tempT

        sP3    := tempP

       

      if sT1 > sT2

        int  tempT = sT1

        float tempP = sP1

        sT1    := sT2

        sP1    := sP2

        sT2    := tempT

        sP2    := tempP

       

      [sT1, sP1, sT2, sP2, sT3, sP3]


    // Displays draggable grip points at specified time-price locations

    f_show_point(x, y, Text, Color, x_location, size) =>

      box.new(x, y, x, y, xloc=x_location, border_width=0, text_valign=text.align_center,

         border_color=na, text=Text, text_color=Color, text_size=size, text_halign=text.align_center)


    // Standard quadratic formula: y = ax² + bx + c

    f_quadratic(float x, float a, float b, float c) =>

      a * x * x + b * x + c


    // Quadratic curve that transitions to linear projection beyond x3

    f_quadratic_linear(float x, float x3_, float y3_, float a, float b, float c, float slope) =>

      x <= x3_ ? a * x * x + b * x + c : y3_ + slope * (x – x3_)


    // Piecewise linear interpolation with weighted slope for projection

    f_weighted_linear(float x, float x1_, float x2_, float x3_, float y1_, float y2_, float y3_, float sAB, float sBC, float sW) =>

      x <= x2_ ? y1_ + sAB * (x – x1_) : x <= x3_ ? y2_ + sBC * (x – x2_) : y3_ + sW * (x – x3_)


    // Natural cubic spline: smooth curve with continuous second derivatives

    f_natural_cubic_spline(float x, float x1, float y1, float x2, float y2, float x3, float y3) =>

      float h1 = x2 – x1

      float h2 = x3 – x2

      float d1 = (y2 – y1) / h1

      float d2 = (y3 – y2) / h2

      float m2 = (d1 + d2) / 2

      float m1 = d1 – (m2 – d1) / 2

      float m3 = d2 + (d2 – m2) / 2

       

      if x <= x2

        float a = y1

        float b = m1

        float c = (3 * d1 – 2 * m1 – m2) / h1

        float d = (m1 + m2 – 2 * d1) / (h1 * h1)

        a + b * (x – x1) + c * math.pow(x – x1, 2) + d * math.pow(x – x1, 3)

      else

        float a = y2

        float b = m2

        float c = (3 * d2 – 2 * m2 – m3) / h2

        float d = (m2 + m3 – 2 * d2) / (h2 * h2)

        a + b * (x – x2) + c * math.pow(x – x2, 2) + d * math.pow(x – x2, 3)


    // Coefficient calculation functions

    f_calc_quadratic_coefs(float x1, float y1, float x2, float y2, float x3, float y3) =>

      float denom = (x1 – x2) * (x1 – x3) * (x2 – x3)

      float a   = (x3 * (y2 – y1) + x2 * (y1 – y3) + x1 * (y3 – y2)) / denom

      float b   = (x3 * x3 * (y1 – y2) + x2 * x2 * (y3 – y1) + x1 * x1 * (y2 – y3)) / denom

      float c   = (x2 * x3 * (x2 – x3) * y1 + x3 * x1 * (x3 – x1) * y2 + x1 * x2 * (x1 – x2) * y3) / denom

      [a, b, c]


    // Calculates segment slopes and extrapolates weighted slope for projection

    f_calc_weighted_linear_coefs(float x1, float y1, float x2, float y2, float x3, float y3) =>

      float sAB  = (y2 – y1) / (x2 – x1)

      float sBC  = (y3 – y2) / (x3 – x2)

      float sW   = sBC + (sBC – sAB) * 0.5

      [sAB, sBC, sW]


    // General function to get curve value based on selected method

    f_get_curve_value(float x, string method, float a, float b, float c,

             float x1, float y1, float x2, float y2, float x3, float y3,

             float sAB, float sBC, float sW, float slope_end) =>

      float y = na

      if method == ‘Quadratic’

        y := f_quadratic(x, a, b, c)

      if method == ‘Quadratic-Linear’

        y := f_quadratic_linear(x, x3, y3, a, b, c, slope_end)

      if method == ‘Weighted Linear’

        y := f_weighted_linear(x, x1, x2, x3, y1, y2, y3, sAB, sBC, sW)

      if method == ‘Natural Cubic Spline’

        y := f_natural_cubic_spline(x, x1, y1, x2, y2, x3, y3)

      y


    // Function to draw backfit polyline

    f_draw_backfit(int start_idx, int end_idx, string method, float a, float b, float c,

            float x1, float y1, float x2, float y2, float x3, float y3,

            float sAB, float sBC, float sW, float slope_end, color col) =>

      int  span      = int(math.abs(end_idx – start_idx))

      int  sample_interval = math.max(1, math.ceil(span / 500))

      var array<chart.point> pts = array.new<chart.point>()

      array.clear(pts)

       

      for i = start_idx to end_idx by sample_interval

        float y = f_get_curve_value(i, method, a, b, c, x1, y1, x2, y2, x3, y3, sAB, sBC, sW, slope_end)

        array.push(pts, chart.point.from_index(i, y))

       

      // Ensure endpoint is included

      float end_y = f_get_curve_value(end_idx, method, a, b, c, x1, y1, x2, y2, x3, y3, sAB, sBC, sW, slope_end)

      array.push(pts, chart.point.from_index(end_idx, end_y))

       

      polyline.new(pts, line_color=col, line_width=line_width, line_style=line.style_solid)


    // Function to draw projection polyline

    f_draw_projection(int start_bar, int proj_span, string method, float a, float b, float c,

             float x1, float y1, float x2, float y2, float x3, float y3,

             float sAB, float sBC, float sW, float slope_end, color col) =>

      int  proj_interval  = math.max(1, math.ceil(proj_span / 100))

      var array<chart.point> pts = array.new<chart.point>()

      array.clear(pts)

       

      for i = 0 to proj_span by proj_interval

        int  proj_index = start_bar + i

        float y      = f_get_curve_value(proj_index, method, a, b, c, x1, y1, x2, y2, x3, y3, sAB, sBC, sW, slope_end)

        array.push(pts, chart.point.from_index(proj_index, y))

       

      polyline.new(pts, line_color=col, line_width=line_width, line_style=line.style_dotted)



    // ======================= SORT INPUT POINTS =============================

    // All time-price points are sorted into chronological order. Support and Resistance are handled separately.

    [A_Time, A_Price, B_Time, B_Price, C_Time, C_Price] = f_sort_points(A_Time_, A_Price_, B_Time_, B_Price_, C_Time_, C_Price_)

    [D_Time, D_Price, E_Time, E_Price, F_Time, F_Price] = f_sort_points(D_Time_, D_Price_, E_Time_, E_Price_, F_Time_, F_Price_)



    // ================= TIME AND BAR INDEX VARIABLES ========================

    // Variables to track bar indices corresponding to each time-price point

    var int   A_Index     = na

    var int   B_Index     = na

    var int   C_Index     = na

    var int   D_Index     = na

    var int   E_Index     = na

    var int   F_Index     = na

    bool    is_C_Time    = time > C_Time and time[1] <= C_Time

    bool    is_F_Time    = time > F_Time and time[1] <= F_Time

    bool    After_C_Time  = time >= C_Time

    bool    After_F_Time  = time >= F_Time


    // Capture bar indices for each time point. Must be done in real-time as bars progress.

    if time > A_Time and time[1] <= A_Time

      A_Index   := bar_index

    if time > B_Time and time[1] <= B_Time

      B_Index   := bar_index

    if time > C_Time and time[1] <= C_Time

      C_Index   := bar_index

    if time > D_Time and time[1] <= D_Time

      D_Index   := bar_index

    if time > E_Time and time[1] <= E_Time

      E_Index   := bar_index

    if time > F_Time and time[1] <= F_Time

      F_Index   := bar_index


       

    // ================= CURVE CALCULATION VARIABLES =========================

    // Pre-calculated coefficients and slopes stored for reuse across bars

    var float coef_a_r      = na

    var float coef_b_r      = na

    var float coef_c_r      = na

    var float coef_a_s      = na

    var float coef_b_s      = na

    var float coef_c_s      = na

    var float slope_AB      = na

    var float slope_BC      = na

    var float slope_at_C     = na

    var float slope_weighted_r  = na

    var float slope_DE      = na

    var float slope_EF      = na

    var float slope_at_F     = na

    var float slope_weighted_s  = na

    var float curve_value_r    = na

    var float curve_value_s    = na

    var polyline backfit_curve_r = na

    var polyline backfit_curve_s = na



    // =========== RESISTANCE CURVE CALCULATIONS & BACKFIT ===================

    // Polylines are redrawn when the last points C is set. All values for resistance are then known. 

    // Due to Auto Sorting, C is always the most recent Resistance point.

    if is_C_Time and not na(A_Index) and not na(B_Index) and not na(C_Index) 

      if CurveMethod == ‘Quadratic’ or CurveMethod == ‘Quadratic-Linear’

        [a_r, b_r, c_r]       = f_calc_quadratic_coefs(A_Index, A_Price, B_Index, B_Price, C_Index, C_Price)

        coef_a_r          := a_r

        coef_b_r          := b_r

        coef_c_r          := c_r

        slope_at_C         := 2 * coef_a_r * C_Index + coef_b_r

       

      if CurveMethod == ‘Weighted Linear’

        [sAB, sBC, sW]        = f_calc_weighted_linear_coefs(A_Index, A_Price, B_Index, B_Price, C_Index, C_Price)

        slope_AB          := sAB

        slope_BC          := sBC

        slope_weighted_r      := sW


      backfit_curve_r       := f_draw_backfit(A_Index, C_Index, CurveMethod,

                      coef_a_r, coef_b_r, coef_c_r,

                      A_Index, A_Price, B_Index, B_Price, C_Index, C_Price,

                      slope_AB, slope_BC, slope_weighted_r, slope_at_C, col_resist)



    // ============ SUPPORT CURVE CALCULATIONS & BACKFIT =====================

    // Polylines are redrawn when the last point F is set. All values for support are then known.

    // Due to Auto Sorting, F is always the most recent Support point.

    if is_F_Time and not na(D_Index) and not na(E_Index) and not na(F_Index) 

      if CurveMethod == ‘Quadratic’ or CurveMethod == ‘Quadratic-Linear’

        [a_s, b_s, c_s]       = f_calc_quadratic_coefs(D_Index, D_Price, E_Index, E_Price, F_Index, F_Price)

        coef_a_s          := a_s

        coef_b_s          := b_s

        coef_c_s          := c_s

        slope_at_F         := 2 * coef_a_s * F_Index + coef_b_s

       

      if CurveMethod == ‘Weighted Linear’

        [sDE, sEF, sWs]       = f_calc_weighted_linear_coefs(D_Index, D_Price, E_Index, E_Price, F_Index, F_Price)

        slope_DE          := sDE

        slope_EF          := sEF

        slope_weighted_s      := sWs   

      

      backfit_curve_s       := f_draw_backfit(D_Index, F_Index, CurveMethod,

                         coef_a_s, coef_b_s, coef_c_s,

                         D_Index, D_Price, E_Index, E_Price, F_Index, F_Price,

                         slope_DE, slope_EF, slope_weighted_s, slope_at_F, col_support)


    // ========== REAL-TIME CURVE VALUE CALCULATION AND PLOTS ================

    // Plot-based workaround for polyline limitations: Polylines cannot span large bar ranges

    // without causing overflow errors, and time-based coordinates fail during market closures.

    // Solution: Use plot() for real-time updates (unlimited range), polylines only for backfit

    // (known range from first to last point) and forward projection (user-defined length).


    if After_C_Time and not na(C_Index)

      curve_value_r := f_get_curve_value(bar_index, CurveMethod,

                coef_a_r, coef_b_r, coef_c_r,

                A_Index, A_Price, B_Index, B_Price, C_Index, C_Price,

                slope_AB, slope_BC, slope_weighted_r, slope_at_C)


    if After_F_Time and not na(F_Index)

      curve_value_s := f_get_curve_value(bar_index, CurveMethod,

                coef_a_s, coef_b_s, coef_c_s,

                D_Index, D_Price, E_Index, E_Price, F_Index, F_Price,

                slope_DE, slope_EF, slope_weighted_s, slope_at_F)


    plot(curve_value_r,  ‘Resistance’, color=col_resist, linewidth=line_width)

    plot(curve_value_s,  ‘Support’,  color=col_support, linewidth=line_width)



    // ====================== PROJECTION POLYLINE ============================

    // polyline projections are redrawn on the last bar to extend into the future

    var polyline projection_curve_r = na

    var polyline projection_curve_s = na


    if barstate.islast and not na(C_Index)

      if not na(projection_curve_r)

        projection_curve_r.delete()

       

      projection_curve_r := f_draw_projection(bar_index, Projection, CurveMethod,

                  coef_a_r, coef_b_r, coef_c_r,

                  A_Index, A_Price, B_Index, B_Price, C_Index, C_Price,

                  slope_AB, slope_BC, slope_weighted_r, slope_at_C, col_resist)


    if barstate.islast and not na(F_Index)

      if not na(projection_curve_s)

        projection_curve_s.delete()

       

      projection_curve_s := f_draw_projection(bar_index, Projection, CurveMethod,

                  coef_a_s, coef_b_s, coef_c_s,

                  D_Index, D_Price, E_Index, E_Price, F_Index, F_Price,

                  slope_DE, slope_EF, slope_weighted_s, slope_at_F, col_support)



    // ================= INFO TABLE & GRIP POINTS ============================

    // Display info table and grip points on the first bar. No need to redraw on subsequent bars.

    var table InfoTable   = table.new(position.top_right, 1, 2)

    if barstate.isfirst 

      f_show_point(A_Time, A_Price, ‘🞒’, col_resist, xloc.bar_time, grip_size)

      f_show_point(B_Time, B_Price, ‘🞒’, col_resist, xloc.bar_time, grip_size)

      f_show_point(C_Time, C_Price, ‘🞒’, col_resist, xloc.bar_time, grip_size)

      f_show_point(D_Time, D_Price, ‘🞒’, col_support, xloc.bar_time, grip_size)

      f_show_point(E_Time, E_Price, ‘🞒’, col_support, xloc.bar_time, grip_size)

      f_show_point(F_Time, F_Price, ‘🞒’, col_support, xloc.bar_time, grip_size)

       

      if show_info

        table.cell(InfoTable, 0, 0,   text=CurveMethod, text_color=color.white, bgcolor=color.black)

    #259920 quote
    Iván González
    Moderator
    Legend

    Ciao! Questa è una versione adattata. In questa versione, calcolo i punti di rotazione e disegniamo le curve seguendo quei punti.

    //--------------------------------------------
    // PRC_Flexible-SR-Channels
    // version = 0
    // 07.04.2026
    // Ivan Gonzalez @ www.prorealcode.com
    // Sharing ProRealTime knowledge
    //--------------------------------------------
    defparam drawonlastbaronly = true
    //=== PARAMETERS ===
    //pivotPrd = 25      // Pivot lookback period
    //curveMethod = 0    // 0=Quadratic, 1=Quad-Linear, 2=Weighted Linear, 3=Cubic Spline
    //projLen = 100      // Projection bars into future
    //skipPivots = 0     // Skip N most recent pivots (0=use latest)
    //minSep = 10        // Min bars between selected pivots
    
    //=== PIVOT DETECTION ===
    once phCount = 0
    once plCount = 0
    
    IF barindex >= 2 * pivotPrd THEN
       // Pivot High
       IF high[pivotPrd] = highest[2*pivotPrd+1](high) THEN
          newBar = barindex - pivotPrd
          addPH = 1
          IF phCount > 0 THEN
             IF $phBar[phCount-1] = newBar THEN
                addPH = 0
             ENDIF
          ENDIF
          IF addPH = 1 THEN
             $phBar[phCount] = newBar
             $phPrice[phCount] = high[pivotPrd]
             phCount = phCount + 1
          ENDIF
       ENDIF
       
       // Pivot Low
       IF low[pivotPrd] = lowest[2*pivotPrd+1](low) THEN
          newBar = barindex - pivotPrd
          addPL = 1
          IF plCount > 0 THEN
             IF $plBar[plCount-1] = newBar THEN
                addPL = 0
             ENDIF
          ENDIF
          IF addPL = 1 THEN
             $plBar[plCount] = newBar
             $plPrice[plCount] = low[pivotPrd]
             plCount = plCount + 1
          ENDIF
       ENDIF
    ENDIF
    
    //=== DRAWING ===
    IF islastbarupdate THEN
       
       //---------------------------------------------
       // SELECT 3 PIVOT HIGHS (RESISTANCE)
       //---------------------------------------------
       rSel = 0
       rSkip = 0
       rLastBar = barindex + minSep + 1
       
       FOR i = phCount - 1 DOWNTO 0 DO
          IF rSel >= 3 THEN
             BREAK
          ENDIF
          IF rSkip < skipPivots THEN
             rSkip = rSkip + 1
          ELSE
             IF rSel = 0 OR rLastBar - $phBar[i] >= minSep THEN
                IF rSel = 0 THEN
                   rx3 = $phBar[i]
                   ry3 = $phPrice[i]
                ELSIF rSel = 1 THEN
                   rx2 = $phBar[i]
                   ry2 = $phPrice[i]
                ELSIF rSel = 2 THEN
                   rx1 = $phBar[i]
                   ry1 = $phPrice[i]
                ENDIF
                rLastBar = $phBar[i]
                rSel = rSel + 1
             ENDIF
          ENDIF
       NEXT
       
       //---------------------------------------------
       // DRAW RESISTANCE CURVE
       //---------------------------------------------
       IF rSel = 3 THEN
          x1 = rx1
          y1 = ry1
          x2 = rx2
          y2 = ry2
          x3 = rx3
          y3 = ry3
          
          IF x1 < x2 AND x2 < x3 THEN
             // --- Coefficients ---
             IF curveMethod = 0 OR curveMethod = 1 THEN
                den = (x1-x2) * (x1-x3) * (x2-x3)
                cA = (x3*(y2-y1) + x2*(y1-y3) + x1*(y3-y2)) / den
                cB = (x3*x3*(y1-y2) + x2*x2*(y3-y1) + x1*x1*(y2-y3)) / den
                cC = (x2*x3*(x2-x3)*y1 + x3*x1*(x3-x1)*y2 + x1*x2*(x1-x2)*y3) / den
                sEnd = 2 * cA * x3 + cB
             ENDIF
             IF curveMethod = 2 THEN
                slAB = (y2-y1) / (x2-x1)
                slBC = (y3-y2) / (x3-x2)
                slW = slBC + (slBC-slAB) * 0.5
             ENDIF
             IF curveMethod = 3 THEN
                hh1 = x2 - x1
                hh2 = x3 - x2
                dd1 = (y2-y1) / hh1
                dd2 = (y3-y2) / hh2
                mm2 = (dd1+dd2) / 2
                mm1 = dd1 - (mm2-dd1) / 2
                mm3 = dd2 + (dd2-mm2) / 2
                s1a = y1
                s1b = mm1
                s1c = (3*dd1-2*mm1-mm2) / hh1
                s1d = (mm1+mm2-2*dd1) / (hh1*hh1)
                s2a = y2
                s2b = mm2
                s2c = (3*dd2-2*mm2-mm3) / hh2
                s2d = (mm2+mm3-2*dd2) / (hh2*hh2)
             ENDIF
             
             // --- Draw curve ---
             endX = barindex + projLen
             totalSpan = endX - x1
             nStep = max(2, round(totalSpan / 80))
             nTotal = round(totalSpan / nStep) + 2
             prevX = x1
             prevY = y1
             
             FOR j = 1 TO nTotal DO
                xi = min(x1 + j * nStep, endX)
                IF xi <= prevX THEN
                   BREAK
                ENDIF
                
                IF curveMethod = 0 THEN
                   currY = cA*xi*xi + cB*xi + cC
                ELSIF curveMethod = 1 THEN
                   IF xi <= x3 THEN
                      currY = cA*xi*xi + cB*xi + cC
                   ELSE
                      currY = y3 + sEnd*(xi-x3)
                   ENDIF
                ELSIF curveMethod = 2 THEN
                   IF xi <= x2 THEN
                      currY = y1 + slAB*(xi-x1)
                   ELSIF xi <= x3 THEN
                      currY = y2 + slBC*(xi-x2)
                   ELSE
                      currY = y3 + slW*(xi-x3)
                   ENDIF
                ELSIF curveMethod = 3 THEN
                   IF xi <= x2 THEN
                      dx = xi - x1
                      currY = s1a + s1b*dx + s1c*dx*dx + s1d*dx*dx*dx
                   ELSE
                      dx = xi - x2
                      currY = s2a + s2b*dx + s2c*dx*dx + s2d*dx*dx*dx
                   ENDIF
                ENDIF
                
                IF prevX >= barindex THEN
                   drawsegment(prevX, prevY, xi, currY) coloured(255,0,0,140) style(dottedline, 2)
                ELSE
                   drawsegment(prevX, prevY, xi, currY) coloured(255,0,0) style(line, 2)
                ENDIF
                prevX = xi
                prevY = currY
             NEXT
             
             drawtext("●", x1, y1) coloured(255,0,0)
             drawtext("●", x2, y2) coloured(255,0,0)
             drawtext("●", x3, y3) coloured(255,0,0)
          ENDIF
       ENDIF
       
       //---------------------------------------------
       // SELECT 3 PIVOT LOWS (SUPPORT)
       //---------------------------------------------
       sSel = 0
       sSkip = 0
       sLastBar = barindex + minSep + 1
       
       FOR i = plCount - 1 DOWNTO 0 DO
          IF sSel >= 3 THEN
             BREAK
          ENDIF
          IF sSkip < skipPivots THEN
             sSkip = sSkip + 1
          ELSE
             IF sSel = 0 OR sLastBar - $plBar[i] >= minSep THEN
                IF sSel = 0 THEN
                   sx3 = $plBar[i]
                   sy3 = $plPrice[i]
                ELSIF sSel = 1 THEN
                   sx2 = $plBar[i]
                   sy2 = $plPrice[i]
                ELSIF sSel = 2 THEN
                   sx1 = $plBar[i]
                   sy1 = $plPrice[i]
                ENDIF
                sLastBar = $plBar[i]
                sSel = sSel + 1
             ENDIF
          ENDIF
       NEXT
       
       //---------------------------------------------
       // DRAW SUPPORT CURVE
       //---------------------------------------------
       IF sSel = 3 THEN
          x1 = sx1
          y1 = sy1
          x2 = sx2
          y2 = sy2
          x3 = sx3
          y3 = sy3
          
          IF x1 < x2 AND x2 < x3 THEN
             IF curveMethod = 0 OR curveMethod = 1 THEN
                den = (x1-x2) * (x1-x3) * (x2-x3)
                cA = (x3*(y2-y1) + x2*(y1-y3) + x1*(y3-y2)) / den
                cB = (x3*x3*(y1-y2) + x2*x2*(y3-y1) + x1*x1*(y2-y3)) / den
                cC = (x2*x3*(x2-x3)*y1 + x3*x1*(x3-x1)*y2 + x1*x2*(x1-x2)*y3) / den
                sEnd = 2 * cA * x3 + cB
             ENDIF
             IF curveMethod = 2 THEN
                slAB = (y2-y1) / (x2-x1)
                slBC = (y3-y2) / (x3-x2)
                slW = slBC + (slBC-slAB) * 0.5
             ENDIF
             IF curveMethod = 3 THEN
                hh1 = x2 - x1
                hh2 = x3 - x2
                dd1 = (y2-y1) / hh1
                dd2 = (y3-y2) / hh2
                mm2 = (dd1+dd2) / 2
                mm1 = dd1 - (mm2-dd1) / 2
                mm3 = dd2 + (dd2-mm2) / 2
                s1a = y1
                s1b = mm1
                s1c = (3*dd1-2*mm1-mm2) / hh1
                s1d = (mm1+mm2-2*dd1) / (hh1*hh1)
                s2a = y2
                s2b = mm2
                s2c = (3*dd2-2*mm2-mm3) / hh2
                s2d = (mm2+mm3-2*dd2) / (hh2*hh2)
             ENDIF
             
             endX = barindex + projLen
             totalSpan = endX - x1
             nStep = max(2, round(totalSpan / 80))
             nTotal = round(totalSpan / nStep) + 2
             prevX = x1
             prevY = y1
             
             FOR j = 1 TO nTotal DO
                xi = min(x1 + j * nStep, endX)
                IF xi <= prevX THEN
                   BREAK
                ENDIF
                
                IF curveMethod = 0 THEN
                   currY = cA*xi*xi + cB*xi + cC
                ELSIF curveMethod = 1 THEN
                   IF xi <= x3 THEN
                      currY = cA*xi*xi + cB*xi + cC
                   ELSE
                      currY = y3 + sEnd*(xi-x3)
                   ENDIF
                ELSIF curveMethod = 2 THEN
                   IF xi <= x2 THEN
                      currY = y1 + slAB*(xi-x1)
                   ELSIF xi <= x3 THEN
                      currY = y2 + slBC*(xi-x2)
                   ELSE
                      currY = y3 + slW*(xi-x3)
                   ENDIF
                ELSIF curveMethod = 3 THEN
                   IF xi <= x2 THEN
                      dx = xi - x1
                      currY = s1a + s1b*dx + s1c*dx*dx + s1d*dx*dx*dx
                   ELSE
                      dx = xi - x2
                      currY = s2a + s2b*dx + s2c*dx*dx + s2d*dx*dx*dx
                   ENDIF
                ENDIF
                
                IF prevX >= barindex THEN
                   drawsegment(prevX, prevY, xi, currY) coloured(0,128,0,140) style(dottedline, 2)
                ELSE
                   drawsegment(prevX, prevY, xi, currY) coloured(0,128,0) style(line, 2)
                ENDIF
                prevX = xi
                prevY = currY
             NEXT
             
             drawtext("●", x1, y1) coloured(0,128,0)
             drawtext("●", x2, y2) coloured(0,128,0)
             drawtext("●", x3, y3) coloured(0,128,0)
          ENDIF
       ENDIF
       
    ENDIF
    
    RETURN
    


Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

TradingView to ProRealTime Translation Center

New Reply
Author
author-avatar
okeus @okeus Participant
Summary

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

Topic Details
Forum: TradingView to ProRealTime Translation Center Forum
Started: 04/04/2026
Status: Active
Attachments: No files
Logo Logo
Loading...