Pump_Detection_price_and_volume_15m
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TMOUM
//@version=4
// Inpired from Pump_Doctor of LARP_capital : 
//Drop me a line if you decide to use this code in any of your scripts.  Enjoy :D
study("Pump_Doctor_price", shorttitle="Pump_detection")

lookback = input(14)
pump_limit = input(1000)
lookback_period = input(48, title="Give value in hours")
ebc=input(false, title="Enable bar colors")

cumulativeup = 0.0
countup = 0
cumulativedown = 0.0
countdown = 0
price = high - low
// designed for 15m timeframe
weekly_average_price = sma(price, 499)
weekly_average_volume = sma(volume, 499)
for i=0 to lookback-1
    if close[i] > open[i]
        cumulativeup := cumulativeup + volume[i] * price[i]
        countup := countup + 1
    else
        cumulativedown := cumulativedown + volume[i] * price[i]
        countdown := countdown + 1

averagevolumeup = (cumulativeup / countup)
averagevolumedown = -(cumulativedown / countdown)

sigup = (averagevolumeup) <= volume and close > open
sigdown = (averagevolumedown) >= -volume and close < open
regup = averagevolumeup >= volume and close > open
regdown = averagevolumedown <= -volume and close < open
volplot = close > open ? volume : close < open ? -volume : na
volcolor = sigup ? color.lime : sigdown ? #FF0000 : regup ? color.green : regdown ? #990000 : na

flow_price = cumulativeup - cumulativedown
flow_price_normalized = flow_price / (weekly_average_volume * weekly_average_price)
flowcolor = flow_price > 0 ? color.lime : flow_price <=0 ? color.red : na
duration_lookback = lookback_period * 4
max_flow_price_normalized = highest(flow_price_normalized, duration_lookback)

pumpsignal = (max_flow_price_normalized > pump_limit and close > 1.1 * weekly_average_price)

max_flow_price_color = pumpsignal ? color.red : color.lime

purgatory = (flow_price > 0 and flow_price[1] > 0) and sigdown
purgatory1 = (flow_price < 0 and flow_price[1] < 0) and sigup

barc = ebc ? ((flow_price > 0 and not sigdown) ? color.lime : (flow_price < 0 and not sigup) ? color.red : purgatory ? color.silver : purgatory1 ? color.silver : color.silver) : na

plot(flow_price_normalized, color=volcolor, style= plot.style_histogram, linewidth=1)
plot(flow_price_normalized, color=flowcolor, linewidth=2)
plot(max_flow_price_normalized, color=max_flow_price_color, linewidth=2)
barcolor(barc)

plot(close)