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.
Remove from Favorite Scripts Add to Favorite Scripts
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)
You constantly amaze us with your skill and creativity. This reminds me of a polynomial channel. Similar concept?
Reply
RicardoSantos PRO coondawg71
a year ago
kinda, polynomial would need to repaint to realign the points at new bars (impossible in pine atm)
Reply
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out