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

//@version=5

//#region *********** DESCRIPTION ***********

//====================================================================================================================

// The "BarChangeDelta" indicator, facilitates the calculation of price delta or percent changes between user-defined start and end points 
// within the current or between preceding and current bars. It offers several customizable options to fit various trading strategies.

// ================================================== INFO ==================================================

// This indicator provides the following key functionalities:
// - Two Modes: 
//  * PreviousToCurrentBarDelta: Compares user-selected start points from the previous bar to the end points of the current bar.
//  * CurrentBarDelta: Compares user-selected start and end points within the current bar.
// - Start Point/End Point Customization: Allows users to define the source for start and end points used in the delta calculations.
// - ABS Mode: Option to display only absolute values, reflected on the histogram drawn.
// - Show delta in percents: Enables users to calculate delta in percentage changes instead of price delta.
// - Moving Average (MA) Plot: A plot of the MA of the last user-defined number of delta prices or percents.

// ================================================== NOTES ==================================================

//The "BarChangeDelta" indicator finds practical application in various trading scenarios. It can be particularly useful for:
//- Assessing daily price changes between open/close or high/low for determining strike prices, especially for 0DTE trading.

//#endregion ========================================================================================================

//#region *********** SETUP *********** 
indicator(
     title      = "BarChangeDelta",
     shorttitle = "BarChangeDelta",
     overlay    = false
     )
//#endregion ========================================================================================================

//#region *********** LIBRARIES *********** 
import HeWhoMustNotBeNamed/enhanced_ta/14 as eta
//#endregion ========================================================================================================

//#region *********** USER_INPUT *********** 
var string INDICATOR_GROUP_STR = "Inputs"
i_mode                   = input.string   (title = "Mode",                      defval = "CurrentBarDelta",  group = INDICATOR_GROUP_STR, options = ["PreviousToCurrentBarDelta", "CurrentBarDelta"])
i_start_price            = input.source   (title = "Start Point",               defval = close,              group = INDICATOR_GROUP_STR)
i_end_price              = input.source   (title = "End  Point",                defval = open,               group = INDICATOR_GROUP_STR)
i_abs_mode               = input.bool     (title = "ABS mode",                  defval = true,               group = INDICATOR_GROUP_STR, tooltip = "Histogram plots ABS delta (only positive)")
i_delta_in_percents      = input.bool     (title = "Show delta in percents",    defval = false,              group = INDICATOR_GROUP_STR)

var string DELTA_HISTOGRAM_STR = "Histogram"
i_ma_type                = input.string   (title = "MA type",           group = DELTA_HISTOGRAM_STR,     defval = "ema", options = ["ema", "sma", "rma", "hma", "wma", "vwma", "swma"])
i_ma_length              = input.int      (title = "Fast MA Length",    group = DELTA_HISTOGRAM_STR,     defval = 12)
i_col_grow_above         = input          (#26A69A, "Above   Grow",   group=DELTA_HISTOGRAM_STR,       inline="Above")
i_col_fall_above         = input          (#B2DFDB, "Fall",           group=DELTA_HISTOGRAM_STR,       inline="Above")
i_col_grow_below         = input          (#FFCDD2, "Below Grow",     group=DELTA_HISTOGRAM_STR,       inline="Below")
i_col_fall_below         = input          (#FF5252, "Fall",           group=DELTA_HISTOGRAM_STR,       inline="Below")
//#endregion ========================================================================================================

//#region *********** LOGIC *********** 
end_price      = (i_mode == "CurrentBarDelta") ? i_end_price : i_end_price[1]
start_price    = i_start_price

delta          = start_price - end_price
delta_abs      = math.abs(delta)
delta_perc     = (start_price - end_price)/end_price * 100
delta_perc_abs = math.abs(delta_perc)

delta_plot     = (i_delta_in_percents == true  and i_abs_mode == true)  ? delta_perc_abs :
      (           i_delta_in_percents == true  and i_abs_mode == false) ? delta_perc :
      (           i_delta_in_percents == false and i_abs_mode == true)  ? delta_abs :
      delta //(i_delta_in_percents == false and i_abs_mode == false) 


delta_hist_color = i_delta_in_percents ? 
                     (delta_perc>=0 ? (delta_perc[1] < delta_perc ? i_col_grow_above : i_col_fall_above) : (delta_perc[1] < delta_perc ? i_col_grow_below : i_col_fall_below)) :
                     (delta>=0      ?      (delta[1] < delta      ? i_col_grow_above : i_col_fall_above) : (     delta[1] < delta      ? i_col_grow_below : i_col_fall_below))

ma             = eta.ma(source = i_delta_in_percents ? delta_perc_abs : delta_abs, maType = i_ma_type, length = i_ma_length)
//#endregion ========================================================================================================

//#region *********** DEBUG_&_PLOTS *********** 
hline(price = 0, title = "Zero Line", color=color.new(#787B86, 50))

plot( series   = delta_plot,
      title    = "Histogram",  
      style    = plot.style_columns, 
      color    = delta_hist_color
     )

plot( series   = ma,
      title    = "Delta MA",
      style    = plot.style_line,
      color    = color.white)
//#endregion ========================================================================================================