Skip to content

Instantly share code, notes, and snippets.

@pequet
Created September 4, 2018 21:06
Show Gist options
  • Save pequet/0ae247a9f4765e5b3e59451fed8efe58 to your computer and use it in GitHub Desktop.
Save pequet/0ae247a9f4765e5b3e59451fed8efe58 to your computer and use it in GitHub Desktop.
// Created by @pequet (https://www.tradingview.com/u/pequet) September 4 2018
// https://github.com/pequet/
// MIT License
// @version=3
//
study(title="Clusters", shorttitle="Clusters")
// show count of consecutive up and down periods: on the last period, on multiples of 4 (settings) or when there is a top/bottom
// apply this to the MACD, VWMACD, etc
// change the line to a histogram if you are plotting the source
// inputs
src = input(close, type=source, title="Source")
length = input(4, minval=1, title="Multiple")
count = 0
count := change(src) > 0 and change(src[1]) < 0 ? 1 : change(src) < 0 and change(src[1]) > 0 ? -1 : change(src) > 0 ? count[1] + 1 : change(src) < 0 ? count[1] - 1 : count[1]
// running out of plots
// show source or changes
displayInput = input(false, title="Plot Source (or Changes)")
plotValue = displayInput ? src : count==1 or count == -1 ? src[1] : na
plotColor = displayInput and count > 0 ? yellow : displayInput and count < 0 ? red : not displayInput and count < 0 ? yellow : not displayInput and count > 0 ? red : blue
plot(plotValue, color=plotColor, style=line, linewidth=2, transp=0, offset=displayInput == 1 ? 0 : -1, title="Count")
// potential bars
plotchar(count == 1 ? src : na, char="", text="1", location=location.top, color=orange, show_last=1)
plotchar(count == 2 ? src : na, char="", text="2", location=location.top, color=orange, show_last=1)
plotchar(count == 3 ? src : na, char="", text="3", location=location.top, color=orange, show_last=1)
plotchar(count == 4 ? src : na, char="", text="4", location=location.top, color=orange, show_last=1)
plotchar(count == 5 ? src : na, char="", text="5", location=location.top, color=orange, show_last=1)
plotchar(count == 6 ? src : na, char="", text="6", location=location.top, color=orange, show_last=1)
plotchar(count == 7 ? src : na, char="", text="7", location=location.top, color=orange, show_last=1)
plotchar(count == 8 ? src : na, char="", text="8", location=location.top, color=orange, show_last=1)
plotchar(count == 9 ? src : na, char="", text="9", location=location.top, color=orange, show_last=1)
plotchar(count == 10 ? src : na, char="", text="10", location=location.top, color=orange, show_last=1)
plotchar(count == 11 ? src : na, char="", text="11", location=location.top, color=orange, show_last=1)
plotchar(count == 12 ? src : na, char="", text="12", location=location.top, color=orange, show_last=1)
plotchar(count == 13 ? src : na, char="", text="13", location=location.top, color=orange, show_last=1)
plotchar(count == 14 ? src : na, char="", text="14", location=location.top, color=orange, show_last=1)
plotchar(count == 15 ? src : na, char="", text="15", location=location.top, color=orange, show_last=1)
// plotchar(count == 16 ? src : na, char="", text="16", location=location.top, color=orange, show_last=1)
plotchar(count == -1 ? src : na, char="", text="1", location=location.bottom, color=orange, show_last=1)
plotchar(count == -2 ? src : na, char="", text="2", location=location.bottom, color=orange, show_last=1)
plotchar(count == -3 ? src : na, char="", text="3", location=location.bottom, color=orange, show_last=1)
plotchar(count == -4 ? src : na, char="", text="4", location=location.bottom, color=orange, show_last=1)
plotchar(count == -5 ? src : na, char="", text="5", location=location.bottom, color=orange, show_last=1)
plotchar(count == -6 ? src : na, char="", text="6", location=location.bottom, color=orange, show_last=1)
plotchar(count == -7 ? src : na, char="", text="7", location=location.bottom, color=orange, show_last=1)
plotchar(count == -8 ? src : na, char="", text="8", location=location.bottom, color=orange, show_last=1)
plotchar(count == -9 ? src : na, char="", text="9", location=location.bottom, color=orange, show_last=1)
plotchar(count == -10 ? src : na, char="", text="10", location=location.bottom, color=orange, show_last=1)
plotchar(count == -11 ? src : na, char="", text="11", location=location.bottom, color=orange, show_last=1)
plotchar(count == -12 ? src : na, char="", text="12", location=location.bottom, color=orange, show_last=1)
plotchar(count == -13 ? src : na, char="", text="13", location=location.bottom, color=orange, show_last=1)
plotchar(count == -14 ? src : na, char="", text="14", location=location.bottom, color=orange, show_last=1)
plotchar(count == -15 ? src : na, char="", text="15", location=location.bottom, color=orange, show_last=1)
// plotchar(count == -16 ? src : na, char="", text="16", location=location.bottom, color=orange, show_last=1)
// confirmed bars
plotchar((count[1] % length == 0 or count < 0) and count[1] == 1 ? src : na, char="", text="1", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 2 ? src : na, char="", text="2", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 3 ? src : na, char="", text="3", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 4 ? src : na, char="", text="4", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 5 ? src : na, char="", text="5", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 6 ? src : na, char="", text="6", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 7 ? src : na, char="", text="7", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 8 ? src : na, char="", text="8", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 9 ? src : na, char="", text="9", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 10 ? src : na, char="", text="10", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 11 ? src : na, char="", text="11", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 12 ? src : na, char="", text="12", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 13 ? src : na, char="", text="13", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 14 ? src : na, char="", text="14", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] == 15 ? src : na, char="", text="15", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count < 0) and count[1] > 15 ? src : na, char="", text="+", location=location.top, color=yellow, offset=-1)
// plotchar((count[1] % length == 0 or count < 0) and count[1] == 16 ? src : na, char="", text="16", location=location.top, color=yellow, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -1 ? src : na, char="", text="1", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -2 ? src : na, char="", text="2", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -3 ? src : na, char="", text="3", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -4 ? src : na, char="", text="4", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -5 ? src : na, char="", text="5", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -6 ? src : na, char="", text="6", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -7 ? src : na, char="", text="7", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -8 ? src : na, char="", text="8", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -9 ? src : na, char="", text="9", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -10 ? src : na, char="", text="10", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -11 ? src : na, char="", text="11", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -12 ? src : na, char="", text="12", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -13 ? src : na, char="", text="13", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -14 ? src : na, char="", text="14", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] == -15 ? src : na, char="", text="15", location=location.bottom, color=red, offset=-1)
plotchar((count[1] % length == 0 or count > 0) and count[1] < -15 ? src : na, char="", text="+", location=location.bottom, color=red, offset=-1)
// plotchar((count[1] % length == 0 or count > 0) and count[1] == -16 ? src : na, char="", text="16", location=location.bottom, color=red, offset=-1)
// -
@pequet
Copy link
Author

pequet commented Sep 4, 2018

clusters_v0 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment