RicardoSantos

Function Bezier Curve

EXPERIMENTAL:
Function for drawing Bezier Curves.
the 4 points should act as a deformable rectangle:
B------C
|...........|
A.........D

percent of time is a value 0->1 representing the percentage of time traveled.
Script de código abierto

Siguiendo el verdadero espíritu de TradingView, el autor de este script lo ha publicado en código abierto, para que los traders puedan entenderlo y verificarlo. ¡Un hurra por el autor! Puede utilizarlo de forma gratuita, aunque si vuelve a utilizar este código en una publicación, debe cumplir con lo establecido en las Normas internas. Puede añadir este script a sus favoritos y usarlo en un gráfico.

Exención de responsabilidad

La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.

¿Quiere utilizar este script en un gráfico?
study("Function Bezier Curve", overlay=true)
useHA = input(false, title='Use Heikken Ashi Candles')
useAltTF = input(true, title='Use Alt Timeframe')
tf = input('D', title='Alt Timeframe')
zigzag() =>
    _isUp = close >= open
    _isDown = close <= open
    _direction = _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz(_direction[1])
    _zigzag = _isUp[1] and _isDown and _direction[1] != -1 ? highest(2) : _isDown[1] and _isUp and _direction[1] != 1 ? lowest(2) : na

_ticker = useHA ? heikenashi(tickerid) : tickerid
sz = useAltTF ? (change(time(tf)) != 0 ? security(_ticker, tf, zigzag()) : na) : zigzag()

plot(sz, title='zigzag', color=black, linewidth=2)


pb = valuewhen(sz, sz, 2) 
pc = valuewhen(sz, sz, 1) 
pd = valuewhen(sz, sz, 0)
tb = valuewhen(sz, n, 2) 
tc = valuewhen(sz, n, 1) 
td = valuewhen(sz, n, 0)

//  ||  Function for drawing Bezier Curves:
//  ||  y = price scale
//  ||  (percentage of time, start point, start control point, end control point, end point)
f_CalculateBezierPoint(_percent, _p0_y, _p1_y, _p2_y, _p3_y)=>
    _u = 1 - _percent
    _tt = pow(_percent, 2)
    _uu = _u * _u
    _uuu = _uu * _u
    _ttt = _tt * _percent
    _p_y1 = _uuu * _p0_y //first term
    _p_y2 = _p_y1 + 3 * _uu * _percent * _p1_y //second term
    _p_y3 = _p_y2 + 3 * _u * _tt * _p2_y //third term
    _p_y4 = _p_y3 + _ttt * _p3_y //fourth term
    _return = _p_y4

//  Input Variables:
perc = (n-td)/(td-tc)
p0_start = pd
p0_control = pd > pc ? pd+(pd-pc) : pd-(pd-pc)
p1_control = pd > pc ? pc+(pd-pc)*-2 : pc-(pd-pc)*2
p1_end = pc
//  Output Variables:
lin_y = f_CalculateBezierPoint(perc, p0_start, p0_control, p1_control, p1_end)
lin_y0 = lin_y - (pd-pc)*0.618
lin_y1 = lin_y + (pd-pc)*0.618
//  Output:
p0 = plot(change(pd)!=0?na:lin_y, style=linebr, color=navy)
p1 = plot(change(pd)!=0?na:lin_y0, style=linebr, color=navy)
p2 = plot(change(pd)!=0?na:lin_y1, style=linebr, color=navy)
fill(p0, p1, color=black, transp=25)
fill(p0, p2, color=black, transp=50)