OPEN-SOURCE SCRIPT
OBV Osc (No Same-Bar Exit)

//version=5
strategy("OBV Osc (No Same-Bar Exit)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// === JSON ALERT STRINGS ===
callBuyJSON = 'ANSHUL \n[{"TT":"BUY","E":"NFO","TS":"NIFTY2570326200CE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
callExtJSON = 'ANSHUL \n[{"TT":"SELL","E":"NFO","TS":"NIFTY2570326200CE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
putBuyJSON = 'ANSHUL \n[{"TT":"BUY","E":"NFO","TS":"NIFTY2570325000PE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
putExtJSON = 'ANSHUL \n[{"TT":"SELL","E":"NFO","TS":"NIFTY2570325000PE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
// === INPUTS ===
length = input.int(20, title="OBV EMA Length")
sl_pct = input.float(1.0, title="Stop Loss %", minval=0.1)
tp_pct = input.float(2.0, title="Take Profit %", minval=0.1)
trail_pct = input.float(0.5, title="Trailing Stop %", minval=0.1)
// === OBV OSCILLATOR CALC ===
src = close
obv = ta.cum(ta.change(src) > 0 ? volume : ta.change(src) < 0 ? -volume : 0)
obv_ema = ta.ema(obv, length)
obv_osc = obv - obv_ema
// === SIGNALS ===
longCondition = ta.crossover(obv_osc, 0) and strategy.position_size == 0
shortCondition = ta.crossunder(obv_osc, 0) and strategy.position_size == 0
// === RISK SETTINGS ===
longStop = close * (1 - sl_pct / 100)
longTarget = close * (1 + tp_pct / 100)
shortStop = close * (1 + sl_pct / 100)
shortTarget = close * (1 - tp_pct / 100)
trailPoints = close * trail_pct / 100
// === ENTRY BAR TRACKING TO PREVENT SAME-BAR EXIT ===
var int entryBar = na
// === STRATEGY ENTRY ===
if longCondition
strategy.entry("Long", strategy.long)
entryBar := bar_index
alert(callBuyJSON, alert.freq_all)
label.new(bar_index, low, text="BUY CALL", style=label.style_label_up, color=color.new(color.green, 85), textcolor=color.black)
if shortCondition
strategy.entry("Short", strategy.short)
entryBar := bar_index
alert(putBuyJSON, alert.freq_all)
label.new(bar_index, high, text="BUY PUT", style=label.style_label_down, color=color.new(color.red, 85), textcolor=color.black)
// === EXIT ONLY IF BAR_INDEX > entryBar (NO SAME-BAR EXIT) ===
canExitLong = strategy.position_size > 0 and bar_index > entryBar
canExitShort = strategy.position_size < 0 and bar_index > entryBar
if canExitLong
strategy.exit("Exit Long", from_entry="Long", stop=longStop, limit=longTarget, trail_points=trailPoints, trail_offset=trailPoints)
if canExitShort
strategy.exit("Exit Short", from_entry="Short", stop=shortStop, limit=shortTarget, trail_points=trailPoints, trail_offset=trailPoints)
// === TRACK ENTRY/EXIT FOR ALERTS ===
posNow = strategy.position_size
posPrev = nz(strategy.position_size[1])
longExit = posPrev == 1 and posNow == 0
shortExit = posPrev == -1 and posNow == 0
if longExit
alert(callExtJSON, alert.freq_all)
label.new(bar_index, high, text="EXIT CALL", style=label.style_label_down, color=color.new(color.blue, 85), textcolor=color.black)
if shortExit
alert(putExtJSON, alert.freq_all)
label.new(bar_index, low, text="EXIT PUT", style=label.style_label_up, color=color.new(color.orange, 85), textcolor=color.black)
// === PLOTS ===
plot(obv_osc, title="OBV Oscillator", color=obv_osc > 0 ? color.green : color.red, linewidth=2)
hline(0, color=color.gray)
strategy("OBV Osc (No Same-Bar Exit)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// === JSON ALERT STRINGS ===
callBuyJSON = 'ANSHUL \n[{"TT":"BUY","E":"NFO","TS":"NIFTY2570326200CE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
callExtJSON = 'ANSHUL \n[{"TT":"SELL","E":"NFO","TS":"NIFTY2570326200CE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
putBuyJSON = 'ANSHUL \n[{"TT":"BUY","E":"NFO","TS":"NIFTY2570325000PE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
putExtJSON = 'ANSHUL \n[{"TT":"SELL","E":"NFO","TS":"NIFTY2570325000PE","Q":"75","OT":"MARKET","P":"NRML","VL":"DAY","AT":"DHANHQ"}]'
// === INPUTS ===
length = input.int(20, title="OBV EMA Length")
sl_pct = input.float(1.0, title="Stop Loss %", minval=0.1)
tp_pct = input.float(2.0, title="Take Profit %", minval=0.1)
trail_pct = input.float(0.5, title="Trailing Stop %", minval=0.1)
// === OBV OSCILLATOR CALC ===
src = close
obv = ta.cum(ta.change(src) > 0 ? volume : ta.change(src) < 0 ? -volume : 0)
obv_ema = ta.ema(obv, length)
obv_osc = obv - obv_ema
// === SIGNALS ===
longCondition = ta.crossover(obv_osc, 0) and strategy.position_size == 0
shortCondition = ta.crossunder(obv_osc, 0) and strategy.position_size == 0
// === RISK SETTINGS ===
longStop = close * (1 - sl_pct / 100)
longTarget = close * (1 + tp_pct / 100)
shortStop = close * (1 + sl_pct / 100)
shortTarget = close * (1 - tp_pct / 100)
trailPoints = close * trail_pct / 100
// === ENTRY BAR TRACKING TO PREVENT SAME-BAR EXIT ===
var int entryBar = na
// === STRATEGY ENTRY ===
if longCondition
strategy.entry("Long", strategy.long)
entryBar := bar_index
alert(callBuyJSON, alert.freq_all)
label.new(bar_index, low, text="BUY CALL", style=label.style_label_up, color=color.new(color.green, 85), textcolor=color.black)
if shortCondition
strategy.entry("Short", strategy.short)
entryBar := bar_index
alert(putBuyJSON, alert.freq_all)
label.new(bar_index, high, text="BUY PUT", style=label.style_label_down, color=color.new(color.red, 85), textcolor=color.black)
// === EXIT ONLY IF BAR_INDEX > entryBar (NO SAME-BAR EXIT) ===
canExitLong = strategy.position_size > 0 and bar_index > entryBar
canExitShort = strategy.position_size < 0 and bar_index > entryBar
if canExitLong
strategy.exit("Exit Long", from_entry="Long", stop=longStop, limit=longTarget, trail_points=trailPoints, trail_offset=trailPoints)
if canExitShort
strategy.exit("Exit Short", from_entry="Short", stop=shortStop, limit=shortTarget, trail_points=trailPoints, trail_offset=trailPoints)
// === TRACK ENTRY/EXIT FOR ALERTS ===
posNow = strategy.position_size
posPrev = nz(strategy.position_size[1])
longExit = posPrev == 1 and posNow == 0
shortExit = posPrev == -1 and posNow == 0
if longExit
alert(callExtJSON, alert.freq_all)
label.new(bar_index, high, text="EXIT CALL", style=label.style_label_down, color=color.new(color.blue, 85), textcolor=color.black)
if shortExit
alert(putExtJSON, alert.freq_all)
label.new(bar_index, low, text="EXIT PUT", style=label.style_label_up, color=color.new(color.orange, 85), textcolor=color.black)
// === PLOTS ===
plot(obv_osc, title="OBV Oscillator", color=obv_osc > 0 ? color.green : color.red, linewidth=2)
hline(0, color=color.gray)
Açık kaynak kodlu komut dosyası
Gerçek TradingView ruhuna uygun olarak, bu komut dosyasının oluşturucusu bunu açık kaynaklı hale getirmiştir, böylece yatırımcılar betiğin işlevselliğini inceleyip doğrulayabilir. Yazara saygı! Ücretsiz olarak kullanabilirsiniz, ancak kodu yeniden yayınlamanın Site Kurallarımıza tabi olduğunu unutmayın.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.
Açık kaynak kodlu komut dosyası
Gerçek TradingView ruhuna uygun olarak, bu komut dosyasının oluşturucusu bunu açık kaynaklı hale getirmiştir, böylece yatırımcılar betiğin işlevselliğini inceleyip doğrulayabilir. Yazara saygı! Ücretsiz olarak kullanabilirsiniz, ancak kodu yeniden yayınlamanın Site Kurallarımıza tabi olduğunu unutmayın.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.