Demand Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © melihtuna

//@version=5

library("DemandIndex")

export di(int period = 10, int priceRange = 2, int smooth = 10) =>
    p = (close - open) / open
    di = 0.0
    va = 0.0
    bp = 1.0
    sp = 1.0
    h = ta.highest(high, priceRange)
    l = ta.lowest(low, priceRange)
    
    va := h - l
    va := ta.sma(va, period)
    if va == 0
        va := 1
        va
    k = 3 * close / va
    p *= k
    if close > open
        bp := volume
        sp := volume / p
        sp
    else
        bp := volume / p
        sp := volume
        sp
    if math.abs(bp) > math.abs(sp)
        di := sp / bp
        di
    else
        di := bp / sp
        di
    di := ta.ema(di, smooth)
    
plot(di(), color=color.new(color.navy, 0))
plot(0, color=color.new(color.purple, 60))
