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

//@version=5

// @description FS: This library helps you scale your data to certain ranges or standarize, normalize, unit scale or min-max scale your data in your prefered way. Mostly used for normalization purposes.
library("Feature_Scaling")

// @function minmaxscale Min-max normalization scales your data to set minimum and maximum range
// @param source Source data you want to use
// @param min Minimum value you want
// @param max Maximum value you want
// @param length Length of the data you want taken into account
// @returns res Data scaled to the set minimum and maximum range
export minmaxscale(float source, float min, float max, int length) =>
	lowest = ta.lowest(source, length)
	highest = ta.highest(source, length)
	res = min + (source - lowest) * (max - min) / (highest - lowest)
	res

// @function meanscale Mean normalization of your data
// @param source Source data you want to use
// @param length Length of the data you want taken into account
// @returns res Mean normalization result of the source
export meanscale(float source, int length) =>
	lowest = ta.lowest(source, length)
	highest = ta.highest(source, length)
	mean = ta.sma(source, length)
	res = (source - mean) / (highest - lowest)
	res

// @function standarize Standarization of your data
// @param source Source data you want to use
// @param length Length of the data you want taken into account
// @param biased Whether to do biased calculation while taking standard deviation, default is true
// @returns res Standarized data
export standarize(float source, int length, bool biased = true) =>
	mean = ta.sma(source, length)
	stdev = ta.stdev(source, length, biased)
	res = (source - mean) / stdev
	res

// @function unitlength Scales your data into overall unit length
// @param source Source data you want to use
// @param length Length of the data you want taken into account
// @returns res Your data scaled to the unit length
export unitlength(float source, int length) =>
	lowest = ta.lowest(source, length)
	highest = ta.highest(source, length)
	res = source / (highest - lowest)
	res