Last active
March 26, 2022 14:50
-
-
Save bhaskarmelkani/ea0718d0a91c4a08be24cf5af8aea5b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@version=5 | |
indicator(title='Smoothed Mode', shorttitle='Consensus Price', overlay=true) | |
//User Inputs | |
percentageToleranceInput = input.int(title="Percentage Tolerance", defval=25) // The +/- tolerance in comparing values to compare them equal to get their mode | |
lengthInput = input.int(title="Length", defval=20) | |
source = input.source(title="Source", defval=close) | |
//getSmoothedModeForArray(array) => | |
// return a value that is +/- x% similar to all numbers | |
// It returns a number that has most number of closest numbers | |
// Its like mode but it smoothes the values, for conflict returns the smallest number, it can also return mean of the similar numbers | |
// Example Input: [1, 10, 11, 9, 20, 33, 12 ], X = 10%, Output: 10, Explanation: 10 has maximum ()2 number of closest numbers (9, 11). It had a conflict with 11 as also has 2 closest numbers (10, 12). But we return minimum of it. Mean would be 10,5 | |
// Update: Now it returns 3 numbers, Max, Min and Avg of the above numbers. So for above example it will be [10, 10.5, 11] | |
getSmoothedModeForArray(inputArray, percentageTolerance) => | |
output = array.new_float() | |
outputFrequency = 0 | |
length = array.size(inputArray) | |
currentFrequency = 0 | |
for i = 0 to length - 1 | |
currentItem = array.get(inputArray, i) | |
lowerBound = currentItem - (percentageTolerance*currentItem/100) | |
upperBound = currentItem + (percentageTolerance*currentItem/100) | |
currentFrequency := 0 | |
for j = 0 to length - 1 | |
temporaryItem = array.get(inputArray, j) | |
if(lowerBound <= temporaryItem and temporaryItem <= upperBound) | |
currentFrequency := currentFrequency + 1 | |
if currentFrequency > outputFrequency | |
output := array.new_float() | |
outputFrequency := currentFrequency | |
if currentFrequency >= outputFrequency | |
array.push(output, currentItem) | |
[array.min(output), array.avg(output), array.max(output)] | |
getMode(source, length, percentageTolerance) => | |
memory = array.new_float(length) | |
for i = 0 to length - 1 | |
array.push(memory, source[i]) | |
getSmoothedModeForArray(memory, percentageTolerance) | |
[min, avg, max] = getMode(source, lengthInput, percentageToleranceInput) | |
plotMin = plot(series=min, title='Smoothed Mode - Min', color=color.green, linewidth=1, style=plot.style_line) | |
plotAvg = plot(series=avg, title='Smoothed Mode - Avg', color=color.blue, linewidth=2, style=plot.style_line) | |
plotMax = plot(series=max, title='Smoothed Mode - Max', color=color.red, linewidth=1, style=plot.style_line) | |
fill(plotMin, plotMax, color=color.new(color.lime, 90)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment