Learning Understanding ta.change
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TBAjustTBA

//First script I have published. Constructive criticism is welcome.
// I hope this may be helpful to you.

// Initially, I did this for myself as I was getting some odd results from the ta.change function. Turns out it was my error LOL. What a surprise!

// Typical syntax: ta.change(source, value) 
// value doesn't need to be included. If it isn't, it will only calculate the change between the last and current value.

// Conclusion. ta.change simply calculates the difference between a previous value/candle and the latest value/candle current value in that series. Positive number states that the recent is higher.

// Bonus :-) Function on line 34 outputs the changes in a positive figure (so as to be able to sum the changes/volatility of the series of values)
//@version=5

indicator("Learning ta.change")

source = input(close) // input(close) is used to make it possible to change the value from the default 'close' 
// to another another value/series, e.g., high, low, or another indicator value)

changeLength = input.int(1, 'The Length for the ta.change calculation. To see how the length math works.', tooltip = 'default is 1 so you can see the effect of looking back only one candle')
changeLenthForManualCalculation = input.int(1, 'Length for the manually calculated change. In other workds, When doing the manual calculation, how many candles back should the change be calculated from?.')

changeForNoLengthInFunctionSoOnlyComparingToLastCandle =ta.change(source) //This is Just to see that without a length, it will just assume you want to get the change between the last value and the current value in the series.

changeWithLengthCalculation =ta.change(source, changeLength)

// Manual calculation to compare to the ta.change function. Length variable is used.
manualChangeCalculation = source - source[changeLength]


// Test manual calculation of change to compare to ta.change. This outputs positive figures only. Good for getting a sum of the changes.

float changeInPositiveFiguresOnly = if source[changeLenthForManualCalculation] < source == true
    source - source[changeLenthForManualCalculation]
else
    source[changeLenthForManualCalculation] - source

plot(changeForNoLengthInFunctionSoOnlyComparingToLastCandle, 'changeForNoLengthInFunctionSoOnlyComparingToLastCandle', color.teal)
plot(changeWithLengthCalculation, 'changeWithLengthCalculation', color.green)
plot(manualChangeCalculation, 'Plot of changes when calculated without using the ta.change function', color.blue, 3, plot.style_circles) // because this is in order of the expected syntax, it is not necessary to use title = and linewidth = .
plot(changeInPositiveFiguresOnly, 'changeInPositiveFiguresOnly', color.fuchsia)
hline(0, '0-Line for reference')
