Hi all,
could you please translate the following oscillator that seems quite interesting.
Thanks and regards.
https://www.tradingview.com/script/6dIWwfow-Order-Flow-Imbalance-Oscillator-StrikePriceLabs/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © StrikePriceLabs
//@version=6
indicator(“Order Flow Imbalance Oscillator [StrikePriceLabs]”, overlay = false)
// ─── INPUTS ─────────────────────────────────────────────────────────────────────
col_supply = input.color(color.orange, “Supply”)
col_demand = input.color(#009fd4, “Demand”)
var vol = array.new<float>()
var BullDelta = array.new<float>()
var BearDelta = array.new<float>()
// Zone geometry arrays instead of boxes
var bearTop = array.new<float>() // supply zone upper price
var bearBot = array.new<float>() // supply zone lower price
var bullTop = array.new<float>() // demand zone upper price
var bullBot = array.new<float>() // demand zone lower price
var count_bear = 0
var count_bull = 0
// New series for historical totals
var float totalSupply = na
var float totalDemand = na
// ─── CALCULATIONS ──────────────────────────────────────────────────────────────
vol.push(volume)
if vol.size() > 1000
vol.shift()
extra_vol = volume > vol.avg()
atr = ta.atr(200) * 2
bear_candle = close < open
bull_candle = close > open
// ── Bearish zones (Supply) ─────────────────────────────────────────────────────
if bear_candle and bear_candle[1] and bear_candle[2] and (extra_vol or extra_vol[1] or extra_vol[2]) and count_bear == 0
delta = 0.0
for i = 0 to 5
if bull_candle[i]
count_bear := 1
array.push(bearTop, low[i] + atr)
array.push(bearBot, low[i])
array.push(BearDelta, delta)
break
delta += bear_candle[i] ? -volume[i] : volume[i]
if count_bear >= 1
count_bear += 1
if count_bear >= 15
count_bear := 0
// ── Bullish zones (Demand) ─────────────────────────────────────────────────────
if bull_candle and bull_candle[1] and bull_candle[2] and (extra_vol or extra_vol[1] or extra_vol[2]) and count_bull == 0
delta = 0.0
for i = 0 to 5
if bear_candle[i]
count_bull := 1
array.push(bullTop, high[i])
array.push(bullBot, high[i] – atr)
array.push(BullDelta, delta)
break
delta += bull_candle[i] ? volume[i] : -volume[i]
if count_bull >= 1
count_bull += 1
if count_bull >= 15
count_bull := 0
// ── Supply invalidation ───────────────────────────────────────────────────────
s = 0
while s < bearTop.size()
top = array.get(bearTop, s)
bot = array.get(bearBot, s)
if close > top
array.remove(bearTop, s)
array.remove(bearBot, s)
array.remove(BearDelta, s)
else
s += 1
// ── Demand invalidation ───────────────────────────────────────────────────────
d = 0
while d < array.size(bullTop)
top = array.get(bullTop, d)
bot = array.get(bullBot, d)
if close < bot
array.remove(bullTop, d)
array.remove(bullBot, d)
array.remove(BullDelta, d)
else
d += 1
// ── Manage overlapping Supply zones ───────────────────────────────────────────
if array.size(bearTop) > 0
i = 0
while i < array.size(bearTop)
top = array.get(bearTop, i)
bot = array.get(bearBot, i)
j = 0
removed = false
while j < array.size(bearTop)
if i != j
top1 = array.get(bearTop, j)
bot1 = array.get(bearBot, j)
if top1 < top and top1 > bot
array.remove(bearTop, i)
array.remove(bearBot, i)
array.remove(BearDelta, i)
removed := true
break
j += 1
if not removed
i += 1
// Cap Supply zones
while array.size(bearTop) > 5
array.shift(bearTop)
array.shift(bearBot)
array.shift(BearDelta)
// ── Manage overlapping Demand zones ───────────────────────────────────────────
if array.size(bullTop) > 0
i = 0
while i < array.size(bullTop)
top = array.get(bullTop, i)
bot = array.get(bullBot, i)
j = 0
removed = false
while j < array.size(bullTop)
if i != j
top1 = array.get(bullTop, j)
bot1 = array.get(bullBot, j)
if bot1 < top and bot1 > bot
array.remove(bullTop, i)
array.remove(bullBot, i)
array.remove(BullDelta, i)
removed := true
break
j += 1
if not removed
i += 1
// Cap Demand zones
while array.size(bullTop) > 5
array.shift(bullTop)
array.shift(bullBot)
array.shift(BullDelta)
// ─── Historical totals (EXPANDED with full zone details in PineLogs) ───────────────────────
float supplySum = 0.0
supplySize = array.size(BearDelta)
if supplySize > 0
log.info(“🔴 SUPPLY ZONES ({0} total):”, supplySize)
for idx = 0 to supplySize – 1
delta = array.get(BearDelta, idx)
top = array.get(bearTop, idx)
bot = array.get(bearBot, idx)
supplySum += math.abs(delta)
log.info(” #{0}: delta={1,number,#} top={2,number,#.####} bot={3,number,#.####}”, idx, delta, top, bot)
else
log.info(“🔴 SUPPLY ZONES: 0”)
float demandSum = 0.0
demandSize = array.size(BullDelta)
if demandSize > 0
log.info(“🟢 DEMAND ZONES ({0} total):”, demandSize)
for idx = 0 to demandSize – 1
delta = array.get(BullDelta, idx)
top = array.get(bullTop, idx)
bot = array.get(bullBot, idx)
demandSum += math.abs(delta)
log.info(” #{0}: delta={1,number,#} top={2,number,#.####} bot={3,number,#.####}”, idx, delta, top, bot)
else
log.info(“🟢 DEMAND ZONES: 0”)
log.info(“📊 FINAL TOTALS – Supply: {0,number,#} | Demand: {1,number,#} | Delta: {2,number,#}”, supplySum, demandSum, demandSum – supplySum)
totalSupply := supplySum
totalDemand := demandSum
// ─── NORMALIZED IMBALANCE OSCILLATOR (-100 to +100) ───────────────────────────
denom = totalDemand + totalSupply
imbalanceNorm = denom != 0 ? (totalDemand – totalSupply) / denom * 100 : 0
plot(
imbalanceNorm,
title = “Order Flow Imbalance (-100 to +100)”,
style = plot.style_area,
color = imbalanceNorm >= 0 ? col_demand : col_supply
)
hline(0, “Balance”, color = color.new(color.gray, 70))
hline(50, “+50 Strong Demand”, color = color.new(col_demand, 50))
hline(-50, “-50 Strong Supply”, color = color.new(col_supply, 50))