Trigrams based on Candle Pattern
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
indicator('Trigrams', overlay=true)

bullish = close > open
bearish = close < open

body = math.abs(open - close)
lowerShadow = math.min(open, close) - low
upperShadow = high - math.max(close, open)
range_1 = high - low

///Trigram Conditions
/// Source: https://www.aguba.net/gupiao/10113.html
Heaven = bearish and math.abs(high - open) < 0.2 * body and math.abs(low - close) < 0.2 * body
Earth = bullish and math.abs(high - close) < 0.2 * body and math.abs(low - open) < 0.2 * body
Water = bullish and upperShadow >= 1.5 * body and upperShadow > 1.7 * lowerShadow
Mountain = bullish and lowerShadow >= 1.5 * body and lowerShadow > 1.7 * upperShadow
Flame = bearish and lowerShadow >= 1.5 * body and lowerShadow > 1.7 * upperShadow
Lake = bearish and upperShadow >= 1.5 * body and upperShadow > 1.7 * lowerShadow

Thunder = false
Wind = false
if not Heaven and not Earth and not Water and not Mountain and not Flame and not Lake
    Thunder := bearish and high > open and low < close
    Wind := bullish and high > close and low < open
    Wind

///Plot the Trigrams
plotchar(Heaven, char='☰', location=location.abovebar, color=color.new(color.white, 0))
plotchar(Earth, char='☷', location=location.abovebar, color=color.new(color.black, 0))
plotchar(Thunder, char='☳', location=location.abovebar, color=color.new(color.yellow, 0))
plotchar(Water, char='☵', location=location.abovebar, color=color.new(color.aqua, 0))
plotchar(Mountain, char='☶', location=location.abovebar, color=color.new(color.maroon, 0))
plotchar(Wind, char='☴', location=location.abovebar, color=color.new(color.green, 0))
plotchar(Flame, char='☲', location=location.abovebar, color=color.new(color.red, 0))
plotchar(Lake, char='☱', location=location.abovebar, color=color.new(color.gray, 0))

