INVITE-ONLY SCRIPT

OBV Divergence Indicator [TradingFinder] On Balance Vol Reversal

26
//version=6
indicator('OBV Divergence Indicator [TradingFinder] On Balance Vol Reversal', shorttitle = 'TFlab OBV Divergence', overlay = true, max_bars_back = 5000, max_labels_count = 500, max_lines_count = 500)

// User Inputs
n = input.int(title = 'Fractal Periods', defval = 4, minval = 2, group = 'Logic Setting')
emaLength = input.int(50, title = 'EMA Length', minval = 1, group = 'Trend Settings')
Show_Table = input.string('Yes', 'Show Table', options = ['Yes', 'No'], group = 'Display Setting')
Show_Label = input.string('Yes', 'Show Label', options = ['Yes', 'No'], group = 'Display Setting')

// Indicators
ATR = ta.atr(55)
Hist = ta.obv
EMA = ta.ema(close, emaLength)

// Trend Identification
up_trend = close > EMA
down_trend = close < EMA

// Fractal Detection (Fix: Use `not na` to check valid pivots)
UpPivot = ta.pivothigh(n, n)
DownPivot = ta.pivotlow(n, n)
upFractal = not na(UpPivot)
downFractal = not na(DownPivot)

// Bearish Divergence Calculation (Sell Signal Fix)
High_Last_Price = ta.valuewhen(upFractal, high, 0)
High_Per_Price = ta.valuewhen(upFractal, high, 1)
High_Last_Hist = ta.valuewhen(upFractal, Hist, 0)
High_Per_Hist = ta.valuewhen(upFractal, Hist, 1)
High_Last_Bar = ta.valuewhen(upFractal, bar_index, 0)
High_Per_Bar = ta.valuewhen(upFractal, bar_index, 1)

Time_Condition_Bear = High_Last_Bar + 30 > bar_index
Last_Bearish_Divergece = if Time_Condition_Bear and High_Last_Bar - High_Per_Bar < 30
High_Last_Price > High_Per_Price and High_Last_Hist < High_Per_Hist
else
false

// Bullish Divergence Calculation (Buy Signal)
Low_Last_Price = ta.valuewhen(downFractal, low, 0)
Low_Per_Price = ta.valuewhen(downFractal, low, 1)
Low_Last_Hist = ta.valuewhen(downFractal, Hist, 0)
Low_Per_Hist = ta.valuewhen(downFractal, Hist, 1)
Low_Last_Bar = ta.valuewhen(downFractal, bar_index, 0)
Low_Per_Bar = ta.valuewhen(downFractal, bar_index, 1)

Time_Condition_Bull = Low_Last_Bar + 30 > bar_index
Last_Bullish_Divergece = if Time_Condition_Bull and Low_Last_Bar - Low_Per_Bar < 30
Low_Last_Price < Low_Per_Price and Low_Last_Hist > Low_Per_Hist
else
false

// ✅ Fixed: Plot Divergence Lines & Labels
plotDivergence() =>
if Last_Bullish_Divergece
line.new(x1 = Low_Per_Bar, y1 = Low_Per_Price, x2 = Low_Last_Bar, y2 = Low_Last_Price, color = color.green, width = 2)
if Show_Label == 'Yes'
label.new(x = Low_Last_Bar, y = Low_Last_Price, text = '+RD', color = color.green, textcolor = color.white, size = size.tiny, style = label.style_label_up)

if Last_Bearish_Divergece
line.new(x1 = High_Per_Bar, y1 = High_Per_Price, x2 = High_Last_Bar, y2 = High_Last_Price, color = color.red, width = 2)
if Show_Label == 'Yes'
label.new(x = High_Last_Bar, y = High_Last_Price, text = '-RD', color = color.red, textcolor = color.white, size = size.tiny, style = label.style_label_down)

plotDivergence()

כתב ויתור

המידע והפרסומים אינם אמורים להיות, ואינם מהווים, עצות פיננסיות, השקעות, מסחר או סוגים אחרים של עצות או המלצות שסופקו או מאושרים על ידי TradingView. קרא עוד בתנאים וההגבלות.