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.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?
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)