Price Deviation Indicator (PDI)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bashseo, development by "DimArt" company

//@version=5
indicator(title="Price Deviation Indicator (PDI)", overlay=false)

// Define the number of bars to calculate the average value
length = input(20, title="Length")

// Calculate the average price over the period
avg_price = ta.sma(close, length)

// Calculate the relative difference between the current price and the average value
price_diff = ((close - avg_price) / avg_price) * 100

// Determine levels to change the histogram color
histogram_color = price_diff > 5 ? color.red : price_diff < -5 ? color.green : color.blue

// Draw a histogram of the price deviation from the average value
plot(title="Price Deviation Indicator (PDI)", series=price_diff, color=histogram_color, style=plot.style_histogram)
