Papercuts Price Distance Travelled
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © joelly3d
//

//@version=5
indicator("Papercuts Price Distance Travelled")
grpin = '⚙=====-----'
grpout = '-----=====⚙'

colorGroup = grpin + 'Color Options' + grpout
colorDayPlus = input.color(defval=#2196F3, title='Bullish Color: ', inline='line2',group=colorGroup) //Light Blue
colorDayPlusAvg = input.color(defval=#dbdbdb, title='Wait Color', inline='line2',group=colorGroup) //light grey


grprsvaema = grpin + 'RSVAEMA Options' + grpout
calc_RSVAEMA = input.bool(true, title='RSVAEMA Calculation?', group=grprsvaema)
int periods = input.int(9,    'EMA Length1:', minval=1, group=grprsvaema)
int     pds = input.int(9,     'VS Length1:', minval=1, group=grprsvaema)
float  mltp = input.int(5, 'VS Multiplier1:', minval=0, group=grprsvaema)

rsvaema(float    source = close, simple int  emaPeriod = 50, simple int   vsPeriod = 50, float multiplier = 10.0  ) =>
    var float mltp1 = 2.0 / (emaPeriod + 1.0)
    var float coef1 = 2.0 /  (vsPeriod + 1.0)
    var float coef2 = 1.0 - coef1
    float pv   = source > source[1] ? volume : 0.0
    float nv   = source < source[1] ? volume : 0.0
    float apv  = na, apv := coef1 * pv + coef2 * nz(apv[1])
    float anv  = na, anv := coef1 * nv + coef2 * nz(anv[1])
    float vs   = math.abs(apv - anv) / (apv + anv)
    float rate = mltp1 * (1.0 + nz(vs, 0.00001) * multiplier)
    float rsma = na
    rsma := rate * source + (1.0 - rate) * nz(rsma[1],source)
    rsma

float rsvaema = rsvaema(hlcc4, periods, pds, mltp)




//------------------------------------------------------------------------Daily Distance Travelled 
//------------------------------------------------------------------------Daily Distance Travelled
//------------------------------------------------------------------------Daily Distance Travelled
daysBack = input.int(20,title='Days Back To Average: ')
var float[] dayMaxArray = array.new_float()
var float[] dayPlusMaxArray = array.new_float()
var PriceDistanceDay = 0.0
var PriceDistanceDayPlus = 0.0
float distanceTraveled = math.abs(rsvaema[1] - rsvaema[2])
//aggregate
PriceDistanceDayPlus := distanceTraveled + PriceDistanceDayPlus[1]
//reset
if dayofweek != dayofweek[1]
    dayPlusMaxArray.unshift(PriceDistanceDayPlus[1])
    if dayPlusMaxArray.size() > daysBack
        dayPlusMaxArray.pop()
    PriceDistanceDay := 0
    PriceDistanceDayPlus := 0 

PriceDistanceDayAverage = array.avg(dayPlusMaxArray)
//------------------------------------------------------------------------Daily Distance Travelled
//------------------------------------------------------------------------Daily Distance Travelled
//------------------------------------------------------------------------Daily Distance Travelled



var labelResults = label.new(bar_index, 0, color=colorDayPlus, yloc=yloc.price, textcolor=color.black, size=size.small, style=label.style_label_left, textalign=text.align_left)
if barstate.islast
    label.set_text( labelResults, str.tostring(daysBack)+" Day Avg:   "+str.tostring(math.round(PriceDistanceDayAverage,2))+"\nCurrent Day:  "+str.tostring(math.round(PriceDistanceDayPlus,2)))
    label.set_y( labelResults, PriceDistanceDayPlus)
    label.set_x(labelResults, bar_index+4)


plot(PriceDistanceDayAverage, color=colorDayPlusAvg, linewidth=2, style=plot.style_stepline)
plot(PriceDistanceDayPlus,color=colorDayPlus, linewidth=1, style=plot.style_histogram)