Smart FİBO

mail@pastecode.io avatar
unknown
plain_text
2 months ago
25 kB
68
Indexable
Never
//@version=5

indicator(title="Smart Fibonacci", overlay=true, precision=4, linktoseries=true, max_bars_back=1000, 
 max_lines_count=500)

///                     
//      Pre-DEFINE     
//                     
//---------------------------------------------------
var COLOR_TRANSP        = color.new(#ffffff,100)
var COLOR_BLACK         = color.new(#000000,0)  
//---------------------------------------------------
var basicgr             = "Smart Fibonacci" 
var fb_tooltip     = "\nDepth ; The minimum number of bars that will be taken into account when calculating the indicator.\n\nDeviation ; a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot.\n"


//     INPUT                                                                                                                                                                     
fb_depth            = input.int   (10   ,minval=1,maxval=20     ,title = "How Far   "                     ,inline='FBR1'       ,group=basicgr)
fb_dev_ratio        = input.float (3.0  ,minval=1,step=0.5      ,title = "Wave Size "                     ,inline='FBR2'       ,group=basicgr   ,tooltip=fb_tooltip)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_dev_threshold    = (ta.atr(20)/close) * 100 * fb_dev_ratio
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------


fb236=input(false,"236 ",inline='fbl'   ,group=basicgr)
fb382=input(true ,"382 ",inline='fbl'   ,group=basicgr)
fb500=input(true ,"500 ",inline='fbl'   ,group=basicgr)
fb618=input(true ,"618 ",inline='fbl'   ,group=basicgr)
fb786=input(false,"786 ",inline='fbl'   ,group=basicgr)
fb886=input(false,"886" ,inline='fbl'   ,group=basicgr)


//---------------------------------------------------------------------------------------------------------------------------------------------------
view_gr = '🔴🟠🟡🟢🔵'

draw_pmark_c       = input.color (color.new(#ffffff,0)        ,""                       ,inline='pmark'    ,group=view_gr)
draw_pmark         = input.bool  (false                          ,'◯  Pivot Point'        ,inline='pmark'    ,group=view_gr)

draw_tline_c       = input.color (color.new(#9598a1,0)        ,""                       ,inline='tline'    ,group=view_gr)
draw_tline         = input.bool  (true                          ,'-- Trend Line'         ,inline='tline'    ,group=view_gr)

fb_guide_color      = input.color (color.new(#ffff00,0)       ,title = ""               ,inline='retlg'    ,group=view_gr )
fb_guide_draw       = input.bool  (false                         ,'▮ Retracement Meter '  ,inline='retlg'    ,group=view_gr)

fb_tag1             = input.color (color.new(#00dbff,0)       ,title = "General Label Color"     ,inline='tag'    ,group=view_gr )

fb_col_1618N        = input.color (color.new(#00dbff,0)       ,title = "Level:   -1.618"     ,inline='g1'    ,group=view_gr )
fb_col_1000N        = input.color (color.new(#00dbff,0)       ,title = "Level:   -1.000"                 ,inline='g1'    ,group=view_gr )
fb_col_618N         = input.color (color.new(#00dbff,0)       ,title = "Level:   -0.618"     ,inline='g2'    ,group=view_gr )
fb_col_0            = input.color (color.new(#5d606b,0)       ,title = "Level:   0"                      ,inline='g2'    ,group=view_gr )
fb_col_236          = input.color (color.new(#00dbff,0)       ,title = "Level:   0.236"      ,inline='g3'    ,group=view_gr )
fb_col_382          = input.color (color.new(#00dbff,0)       ,title = "Level:   0.382"                  ,inline='g3'    ,group=view_gr )
fb_col_500          = input.color (color.new(#00dbff,0)       ,title = "Level:   0.500"      ,inline='g4'    ,group=view_gr )
fb_col_618          = input.color (color.new(#00dbff,0)       ,title = "Level:   0.618"                  ,inline='g4'    ,group=view_gr )
fb_col_786          = input.color (color.new(#00dbff,0)       ,title = "Level:   0.786"      ,inline='g5'    ,group=view_gr )
fb_col_886          = input.color (color.new(#00dbff,0)       ,title = "Level:   0.886"                  ,inline='g5'    ,group=view_gr )
fb_col_1000         = input.color (color.new(#b22833,0)       ,title = "Level:   1.000 "     ,inline='g6'    ,group=view_gr )
fb_col_1618         = input.color (color.new(#00dbff,0)       ,title = "Projection    1.618 "     ,inline='g7'    ,group=view_gr )
fb_col_2618         = input.color (color.new(#00dbff,0)       ,title = "Projection    2.618 "     ,inline='g7'    ,group=view_gr )

f_getLabelColor(string _num) =>
    switch _num
        '-1618'   => fb_col_1618N
        '-1000'   => fb_col_1000N
        '-618'    => fb_col_618N
        '0'       => fb_col_0
        '236'     => fb_col_236
        '500'     => fb_col_500
        '618'     => fb_col_618
        '786'     => fb_col_786
        '886'     => fb_col_886
        '1000'    => fb_col_1000
        '1618'    => fb_col_1618
        '2618'    => fb_col_2618
       

fb_color3           = input.color (color.new(#ffffff,0)       ,title = " Level: 0.5"    ,inline='g6'    ,group=view_gr )
fb_color2           = input.color (color.new(#ffffff,0)       ,title = "Color Scheme"    ,inline='color1'    ,group=view_gr )
fb_color1           = input.color (color.new(#ffffff,0)       ,title = "Color Framework"                      ,inline='color1'    ,group=view_gr )
fb_label_position   = input.int   (23                           ,title = "Ruller position (X-axis Offset)"       ,inline='position'    ,group=view_gr )
draw_simplelabel    = input.bool  (false                          ,' Extended Info. (Price, Delta Rate)'  ,inline=''    ,group=view_gr)
//---------------------------------------------------------------------------------------------------------------------------------------------------




//---------------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------------
[k_volume,k_open,k_close,k_high,k_low,k_hlc3,k_hlcc4,k_ohlc4,k_hl2,k_close_mintick,k_trtrue,k_atr14,k_atr14_mintick,k_rsipvt14] 
     = request.security(syminfo.tickerid ,'', [volume,open,close,high,low,hlc3,hlcc4,ohlc4,hl2,str.tostring(close,format.mintick),ta.tr(true),ta.atr(14),str.tostring(ta.atr(14),format.mintick),ta.rsi(ta.pvt,14)] )






//                                                                                                                                                                                                                     
//       Background                                                                                                                                                                                                    
//                                                                                                                                                                                                                   

//                                                                                                                                                                                                                     
//      Fibonacci Retracement  - Logic Performance Tuned & Re-Designed Fresh-Drawing, Automatic Line on/off                                                                                                            
//                                                                                                                                                                                                                     


// pre-define --------------------------------------------------------------------------------------
var line  fb_lineLast           = na
var label fb_label_start_last   = na
var label fb_label_end_last     = na
var int   fb_iLast              = 0
var int   fb_iPrev              = 0
var float fb_pLast              = 0
var fb_isHighLast               = false     // otherwise the last pivot is a low pivot
// pivot --------------------------------------------------------------------------------------
high_none   = ta.highest(high,fb_depth) > nz(high[fb_depth/2])
fb_iH       = high_none ?   int(na) : bar_index[fb_depth/2]    
fb_pH       = high_none ? float(na) :   nz(high[fb_depth/2])
low_none    = ta.lowest (low ,fb_depth) < nz(low [fb_depth/2])
fb_iL       = low_none  ?   int(na) : bar_index[fb_depth/2]
fb_pL       = low_none  ? float(na) :   nz(low [fb_depth/2])
//--------------------------------------------------------------------------------------
calc_dev(_base_price, _price) => 100 * (_price - _base_price) / _price
//--------------------------------------------------------------------------------------





pivotFound(dev, isHigh, index, price) =>
    if fb_isHighLast == isHigh and not na(fb_lineLast) 
        // same direction
        if fb_isHighLast ? price > fb_pLast : price < fb_pLast
            line.set_xy2(fb_lineLast, index, price)
            label.set_xy(fb_label_end_last, index, price)
            [fb_lineLast, fb_isHighLast,fb_label_start_last,fb_label_end_last]
        else
            [line(na), bool(na), label(na), label(na)]
    else // reverse the direction (or create the very first line)
        if math.abs(dev) > fb_dev_threshold 
            // price move is significant
            id_fb_line = line.new(fb_iLast, fb_pLast, index, price, color=color.new(draw_tline_c,draw_tline?0:100), width=1, style=line.style_dashed)       // ------  FBR start -> end ; slopped line
            id_fb_label_start = label.new(fb_iLast, fb_pLast ,color=COLOR_TRANSP, size=size.huge ,style=label.style_label_center, textcolor=color.new(draw_pmark_c,draw_pmark?0:100)  ,text= '◯' )    
            id_fb_label_end   = label.new(index   , price    ,color=COLOR_TRANSP, size=size.huge ,style=label.style_label_center, textcolor=color.new(draw_pmark_c,draw_pmark?0:100)  ,text= '◯' )    
            [id_fb_line, isHigh, id_fb_label_start, id_fb_label_end]
        else
            [line(na), bool(na),label(na),label(na)]
//--------------------------------------------------------------------------------------
fb_new_line = false
if not na(fb_iH) 
    fb_dev = calc_dev(fb_pLast, fb_pH)
    [fb_id, fb_isHigh, fb_la_s_id, fb_la_e_id] = pivotFound(fb_dev, true, fb_iH, fb_pH)
    if not na(fb_id)
        if fb_id != fb_lineLast
            line.delete (fb_lineLast)
            label.delete(fb_label_start_last)
            label.delete(fb_label_end_last)
            fb_new_line     := true
        fb_lineLast         := fb_id
        fb_label_start_last := fb_la_s_id
        fb_label_end_last   := fb_la_e_id
        fb_isHighLast       := fb_isHigh
        fb_iPrev            := fb_iLast
        fb_iLast            := fb_iH
        fb_pLast            := fb_pH
else if not na(fb_iL) 
    fb_dev = calc_dev(fb_pLast, fb_pL)
    [fb_id, fb_isHigh, fb_la_s_id, fb_la_e_id] = pivotFound(fb_dev, false, fb_iL, fb_pL)
    if not na(fb_id)
        if fb_id != fb_lineLast
            line.delete (fb_lineLast)
            label.delete(fb_label_start_last)
            label.delete(fb_label_end_last)
            fb_new_line     := true
        fb_lineLast         := fb_id
        fb_label_start_last := fb_la_s_id
        fb_label_end_last   := fb_la_e_id                
        fb_isHighLast       := fb_isHigh
        fb_iPrev            := fb_iLast
        fb_iLast            := fb_iL
        fb_pLast            := fb_pL
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_retracement (_show, _fib_level, _col ,_switch) =>
    _price   = fb_pLast + ((line.get_y1(fb_lineLast)-fb_pLast))* _fib_level
    _fbfb_id_line_show = false
    _position           = bar_index -  line.get_x2(fb_lineLast)
    _fbr_max_highest    = math.max( ta.highest(high, nz(math.max(1,_position),1))  , fb_pLast)
    _fbr_min_lowest     = math.min( ta.lowest (low , nz(math.max(1,_position),1))  , fb_pLast)
    _fbr_max            = fb_pLast < close ? _fbr_max_highest : fb_pLast > close  ? _fbr_min_lowest : fb_pLast
    _fbr_line_max       = fb_pLast < _price? _fbr_max_highest : fb_pLast > _price ? _fbr_min_lowest : fb_pLast
    _fbfb_id_line_show  := fb_pLast < _price and _price-(k_atr14/4) <= _fbr_line_max  ? true 
                         : fb_pLast > _price and _price+(k_atr14/4) >= _fbr_line_max  ? true 
                         : false 
    if _switch =='auto' and not na(fb_lineLast)
        fbfb_id_auto      = line.new(line.get_x2(fb_lineLast) ,_price ,bar_index + 6                    ,_price ,style=line.style_solid, color=color.new(_col, _fbfb_id_line_show ? 30 : 100) , width=1, extend=extend.none)    
        fbfb_id_ext_auto  = line.new(bar_index+fb_label_position-4,_price ,bar_index+fb_label_position+1  ,_price , color=color.new(_col,0), width=1, extend=extend.none)     
        fb_label_auto     = label.new(x=bar_index+fb_label_position+1 ,y=_price, text=str.tostring(_fib_level,"0.000")+(draw_simplelabel ? ('    ' + str.tostring(_price, format.mintick) + '   ' + str.tostring(((_price - fb_pLast)/fb_pLast)*100,format.percent )):''),textcolor=color.new(color.white,0) ,style=label.style_label_left  ,textalign=text.align_left , color=color.new(_col,0), size=size.normal)
        line.delete(fbfb_id_auto[1]     )
        line.delete(fbfb_id_ext_auto[1] )
        label.delete(fb_label_auto[1]   )
        if not _show 
            line.delete(fbfb_id_auto     )
            line.delete(fbfb_id_ext_auto )
            label.delete(fb_label_auto   )
    if _show and _switch ==''
        var fbfb_id      = line.new(fb_iLast, _price, bar_index, _price, color=_col , width=1, extend=extend.none  ,style=line.style_solid )     //--- FBR | line = start to now 
        var fbfb_id_ext  = line.new(fb_iLast, _price, bar_index, _price, color=_col , width=1, extend=extend.none)     //--- FBR | line = now to label position
        var fb_label     = label.new(x=bar_index ,y=_price, text=''  ,textcolor=color.new(#ffffff,0) ,style=label.style_label_left  ,textalign=text.align_left , color=_col, size=size.normal) //aqui
        if not na(fb_lineLast)
            line.set_xy1  (fbfb_id  ,_fib_level==1.0? line.get_x1(fb_lineLast):line.get_x2(fb_lineLast), _price)             
            line.set_xy2  (fbfb_id  ,_fib_level==1.0 or _fib_level==0.0? bar_index + fb_label_position-4 : bar_index + 6           , _price)                           
            line.set_color(fbfb_id  ,color=_col)
            line.set_style(fbfb_id  ,_fib_level==0.0 or _fib_level==1.0 ? line.style_dotted : line.style_solid)
            //---------------------------------------------------------------------------------------------------    
            line.set_xy1   (fbfb_id_ext    ,bar_index+fb_label_position-4  ,_price)                        
            line.set_xy2   (fbfb_id_ext    ,bar_index+fb_label_position+1  ,_price)
            line.set_width (fbfb_id_ext    ,1)
            //---------------------------------------------------------------------------------------------------    
            label.set_xy        (fb_label ,bar_index+fb_label_position+1  ,_price)
            label.set_text      (fb_label , str.tostring(_fib_level,"0.000")+(draw_simplelabel ? '    ' + str.tostring(_price, format.mintick) + '   ' + str.tostring(((_price - fb_pLast)/fb_pLast)*100,format.percent ):''))
            // label.set_tooltip   (fb_label ,str.tostring(((_price - fb_pLast)/fb_pLast)*100,format.percent ))
            label.set_textcolor (fb_label ,color.white)    
            
            if _fib_level == 1.0 and fb_guide_draw == true
                var fbfb_box_max  = box.new  (bar_index, close   ,bar_index, fb_pLast, border_color=color.new(fb_guide_color,80) ,border_width=1, bgcolor=color.new(fb_guide_color,90) )
                var fbfb_line_max = line.new (bar_index, close   ,bar_index, fb_pLast, color=fb_guide_color ,width=2)
                var fbfb_line_min = line.new (bar_index, close   ,bar_index, fb_pLast, color=fb_guide_color ,width=2)
                var fbfb_box_ext  = box.new  (bar_index, close   ,bar_index, fb_pLast, border_color=color.new(fb_guide_color,80) ,border_width=0, bgcolor=color.new(fb_guide_color,70) )
                var fbfb_line_ext = line.new (bar_index, close   ,bar_index, fb_pLast, color=fb_guide_color ,width=3)
                //---------------------------------------------------------------------------------------------------    
                box.set_lefttop     (fbfb_box_max  ,bar_index+fb_label_position-3 ,_fbr_max_highest)
                box.set_rightbottom (fbfb_box_max  ,bar_index+fb_label_position-0 ,_fbr_min_lowest) 
                //---------------------------------------------------------------------------------------------------    
                line.set_xy1        (fbfb_line_max ,bar_index+fb_label_position-3 ,_fbr_max_highest)
                line.set_xy2        (fbfb_line_max ,bar_index+fb_label_position-0 ,_fbr_max_highest)
                line.set_xy1        (fbfb_line_min ,bar_index+fb_label_position-3 ,_fbr_min_lowest)
                line.set_xy2        (fbfb_line_min ,bar_index+fb_label_position-0 ,_fbr_min_lowest)
                //---------------------------------------------------------------------------------------------------    
                box.set_lefttop     (fbfb_box_ext  ,bar_index+fb_label_position-3 ,close)
                box.set_rightbottom (fbfb_box_ext  ,bar_index+fb_label_position-1 ,fb_pLast)
                //---------------------------------------------------------------------------------------------------    
                line.set_xy1        (fbfb_line_ext ,bar_index+fb_label_position-3 ,close)
                line.set_xy2        (fbfb_line_ext ,bar_index+fb_label_position-1 ,close)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_new = fb_pLast[1] != fb_pLast or fb_new_line or fb_iLast[1] != fb_iLast
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_over_1618    = false
fb_over_1       = false
fb_over_0786    = false
fb_under_0      = false
fb_under_0382   = false
fb_under_0618   = false
fb_under_m1     = false
//-------------------------
fb_height       = line.get_y1(fb_lineLast) - fb_pLast

fb_k_low_cr     = ta.lowest (k_low,2)
fb_k_high_cr    = ta.highest(k_high,2)

fb_over_1618   := (fb_height < 0 and fb_k_low_cr < (fb_height  * 1.618) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr > (fb_height  * 1.618) + fb_pLast   ) 
fb_over_1      := (fb_height < 0 and fb_k_low_cr < line.get_y1(fb_lineLast)         ) or (fb_height > 0 and fb_k_high_cr > line.get_y1(fb_lineLast)          ) 
fb_over_0786   := (fb_height < 0 and fb_k_low_cr < (fb_height  * 0.786) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr > (fb_height  * 0.786) + fb_pLast   ) 
fb_under_0     := (fb_height < 0 and fb_k_low_cr > fb_pLast                         ) or (fb_height > 0 and fb_k_high_cr < fb_pLast                          )
// fb_under_0382  := (fb_height < 0 and fb_k_low_cr > (fb_height  *-0.382) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr < (fb_height  *-0.382) + fb_pLast   )
fb_under_0618  := (fb_height < 0 and fb_k_low_cr > (fb_height  *-0.618) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr < (fb_height  *-0.618) + fb_pLast   )
fb_under_m1    := (fb_height < 0 and fb_k_low_cr > (fb_height  *-1    ) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr < (fb_height  *-1    ) + fb_pLast   ) 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_retracement (fb_under_m1   ,-1.618 ,f_getLabelColor('-1618') ,'auto')
fb_retracement (fb_under_0618 ,-1.000 ,f_getLabelColor('-1000') ,'auto')
fb_retracement (fb_under_0382 ,-0.618 ,f_getLabelColor('-618') ,'auto')
// fb_retracement (fb_under_0    ,-0.382 ,fb_color1 ,'auto')
// fb_retracement (true          ,-0.236 ,fb_color1 ,'auto')
fb_retracement (true          , 0.000 ,f_getLabelColor('0') ,''    )
// fb_retracement (fb236         , 0.236 ,fb_color1 ,''    )
// fb_retracement (fb382         , 0.382 ,fb_color2 ,''    )
fb_retracement (fb500         , 0.500 ,f_getLabelColor('500')  ,''   )
fb_retracement (fb618         , 0.618 ,f_getLabelColor('618') ,''    )
fb_retracement (fb786         , 0.786 ,f_getLabelColor('786') ,''    )
// fb_retracement (fb886         , 0.886 ,fb_color1 ,''    )
fb_retracement (true                          , 1.000 ,f_getLabelColor('1000') ,''    )
// fb_retracement (fb_over_0786  , 1.272 ,fb_color1 ,'auto')

fb_retracement (fb_over_1     , 1.618 ,f_getLabelColor('1618') ,'auto')
fb_retracement (fb_over_1618  , 2.618 ,f_getLabelColor('2618') ,'auto')


Leave a Comment