Andrew/Acum
2025-09-20 12:47:20 +00:00

65 lines
2.9 KiB
Text

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Acum08
//@version=6
strategy("Institutional Flow Volume Block Analyzer", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=3)
// Inputs
lookbackPeriod = input.int(20, "Volume Lookback Period")
volumeThreshold = input.float(2.0, "Volume Spike Multiplier")
minVolumeFilter = input.float(0, "Minimum Volume Filter (0 = Off)")
impactDecayFactor = input.float(0.9, "Impact Decay Factor")
signalThreshold = input.float(0.5, "Signal Threshold for Entries")
useTrailingStop = input.bool(true, "Use Trailing Stop")
stopPercent = input.float(1.5, "Trailing Stop %")
// Calculate average volume
avgVolume = ta.sma(volume, lookbackPeriod)
// Identify high volume block trades
isHighVolume = volume > avgVolume * volumeThreshold and (minVolumeFilter == 0 or volume >= minVolumeFilter)
blockDirection = close > open ? 1 : close < open ? -1 : 0
// Calculate impact with decay for cumulative flow
var float cumulativeImpact = 0.0
cumulativeImpact := cumulativeImpact * impactDecayFactor + (isHighVolume ? blockDirection : 0)
// Entry conditions based on institutional flow momentum
longEntry = ta.crossover(cumulativeImpact, signalThreshold)
shortEntry = ta.crossunder(cumulativeImpact, -signalThreshold)
// Strategy entries
if (longEntry)
strategy.entry("Long", strategy.long)
if (shortEntry)
strategy.entry("Short", strategy.short)
// Exit condition: close opposite position if flow reverses
if (cumulativeImpact < -signalThreshold)
strategy.close("Long")
if (cumulativeImpact > signalThreshold)
strategy.close("Short")
// Trailing stop management
if useTrailingStop
trailValue = stopPercent / 100 * close
if strategy.position_size > 0
strategy.exit("Stop Long", "Long", trail_offset=trailValue, trail_points=trailValue)
if strategy.position_size < 0
strategy.exit("Stop Short", "Short", trail_offset=trailValue, trail_points=trailValue)
// Visuals: color the bars based on cumulative impact
barcolor(
cumulativeImpact > 0.5 ? color.new(color.green, 0) :
cumulativeImpact > 0.1 ? color.new(color.lime, 50) :
cumulativeImpact > 0 ? color.new(color.yellow, 80) :
cumulativeImpact == 0 ? color.gray :
cumulativeImpact > -0.1? color.new(color.orange, 80):
cumulativeImpact > -0.5? color.new(color.red, 50) :
color.new(color.red, 0))
// Plot cumulative impact line
plot(cumulativeImpact, color=color.blue, title="Institutional Buying/Selling Momentum")
// Plot volume spikes markers
plotshape(isHighVolume and blockDirection == 1, title="Buy Spike", location=location.abovebar, style=shape.triangleup, size=size.tiny, color=color.green)
plotshape(isHighVolume and blockDirection == -1, title="Sell Spike", location=location.belowbar, style=shape.triangledown, size=size.tiny, color=color.red)