Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joemccann/d4d51d1e2f37a1e0c2e01c1e2f2e8720 to your computer and use it in GitHub Desktop.
Save joemccann/d4d51d1e2f37a1e0c2e01c1e2f2e8720 to your computer and use it in GitHub Desktop.
Asymmetric Z-Score Histogram
//@version=5
//
// _____ __ .__
// / _ \ _________.__. _____ _____ _____/ |________|__| ____
// / /_\ \ / ___< | |/ \ / \_/ __ \ __\_ __ \ |/ ___\
// / | \\\___ \ \___ | Y Y \ Y Y \ ___/| | | | \/ \ \___
// \____|__ /____ >/ ____|__|_| /__|_| /\___ >__| |__| |__|\___ >
// \/ \/ \/ \/ \/ \/ \/
//
// Disclaimers: https://t.co/kOsUzy5pCB
indicator("Asymmetric Z-Score Histogram", overlay=false)
// Input for lookback period
lookback = input.int(50, "Lookback Period", minval=10, maxval=500)
// Calculate percentage move
price_change = close - open
candle_range = high - low
percent_move = price_change / open * 100
// Calculate mean and standard deviation
mean = ta.sma(percent_move, lookback)
variance = ta.variance(percent_move, lookback)
stdev = ta.stdev(percent_move, lookback)
// Calculate z-score
z_score = (percent_move - mean) / stdev
// Plot histogram
hline(0, "Zero Line", color=color.gray)
plot(z_score, style=plot.style_histogram, color=color.blue, title="Z-Score")
// Plot reference lines at ±2 standard deviations
hline(2, "Upper Band", color=color.red, linestyle=hline.style_dashed)
hline(-2, "Lower Band", color=color.red, linestyle=hline.style_dashed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment