Wick-off Check Moving Average [Misu]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Misu

//@version=5
indicator("Wick-off Check Moving Average [Misu]", shorttitle="Wickoff Check MA [Misu]",  overlay = true, max_lines_count = 500, max_labels_count = 500) //, initial_capital=50000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

import Fontiramisu/fontilab/12 as fontilab

// ] —————— Input Vars —————— [
// General
src                     = input(close, title="Source")
// MA.
var maTypeTooltip       = "SMA: Simple moving average // EMA: Exponential MA // SMMA (RMA): Running MA // WMA: Weighted MA // VWMA: Volume weighted MA // DEMA: Double exponential MA " 
                                     + "// TEMA:  Triple exponential MA// ZLSMA: Zero lag sma //ZLDEMA: Zero lag dema// ZLTEMA: Zero lag tema // McGinley-D: McGinley Dynamic// HMA: Hull ma"
typeMA                  = input.string(title = "Method Multi MA", defval = "EMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "DEMA", "TEMA", "ZLSMA", "ZLDEMA", "ZLTEMA", "McGinley-D", "HMA"]
                                 , tooltip=maTypeTooltip
                                 , group="MA")
srcMA                   = input(close, title="MA Source", group="MA")
lenMA                   = input.int(36, minval=1, title="Length Multi MA", group="MA")
// Trend.
lenBarTrendValidation   = input.int(10, title="Length Bar - Trend Validation", minval=1, group="Trend", tooltip="Define the number of bar needed to validate a trend. When price is above the MA, trend is up. When price is under MA, trend is down.")
// Wick-off.
wickoffMode             = input.string("continuation pattern", title="Wickoff Mode", options=["no trend in progress", "continuation pattern", "both"], group="Wickoff Mode")
lenWickValidation       = input.int(10, title="Lenght Avg Wick Validation", group="Wickoff Settings")
factorWickValidation    = input.float(1, step=0.1, title="Factor Avg Wick Validation", group="Wickoff Settings")

// Wick off cond.
isWickoffUpBull          = false
isWickoffUpBear          = false
isWickoffDownBull        = false
isWickoffDownBear        = false

// ] —————— Util Functions —————— [
// Cond Vars.
_bodyHi                 = math.max(close, open)
_bodyLo                 = math.min(close, open)
_body                   = _bodyHi - _bodyLo
_upperWick              = high - _bodyHi
_greenBody              = open < close
_redBody                = open > close
_highWick               = high - _bodyHi
_lowWick                = _bodyLo - low
_avgHighWick            = ta.sma(_highWick, lenWickValidation)
_avgLowWick             = ta.sma(_lowWick, lenWickValidation)
// Wick-off & MA conf.
multiMA                 = fontilab.multiMa(srcMA, lenMA, typeMA)
var upTCounter          = 0
var downTCounter        = 0
upTCounter              := src > multiMA ? upTCounter + 1 : 0
downTCounter            := src < multiMA ? downTCounter + 1 : 0

_uptInProgress          = src > multiMA and upTCounter >= lenBarTrendValidation
_downtInProgress        = src < multiMA and downTCounter >= lenBarTrendValidation

// @function detect wickoff.
isWickCrossPattern (src, isCrossUp, factor) =>
    isCross = false
    if isCrossUp
        isCross := _bodyHi < src and src < high and _highWick > _avgHighWick * factor
    else
        isCross := low < src and src < _bodyLo and _lowWick > _avgLowWick * factor
    isCross

// ] —————— Logic —————— [
// Invalidate wickoff other conditions
isWickoffUpBull      := _uptInProgress and isWickCrossPattern(multiMA, false, factorWickValidation)
isWickoffDownBear    := _downtInProgress and isWickCrossPattern(multiMA, true, factorWickValidation)
isWickoffDownBull    := not _downtInProgress and not _uptInProgress and isWickCrossPattern(multiMA, false, factorWickValidation)
isWickoffUpBear      := not _downtInProgress and not _uptInProgress and isWickCrossPattern(multiMA, true, factorWickValidation)

// -----
// BUY / SELL COND.
buyCond             = wickoffMode == "continuation pattern" ? isWickoffUpBull : wickoffMode == "both" ? isWickoffUpBull or isWickoffDownBull : isWickoffDownBull 
sellCond            = wickoffMode  == "continuation pattern" ? isWickoffDownBear : wickoffMode == "both" ? isWickoffUpBear or isWickoffDownBear : isWickoffUpBear

// ] —————— Strategy & alerts Part —————— [
// if buyCond
//     strategy.entry("L", strategy.long, alert_message="Buy Signal")

// if sellCond
//     strategy.entry("S", strategy.short, alert_message="Sell Signal")

alertcondition(buyCond, title = "Long", message = "Wick-off check MA [Misu]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(sellCond, title = "Short", message = "Wick-off check MA [Misu]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

// ] —————— Plot —————— [
plot(multiMA, title = "Moving Average", color=color.aqua, linewidth = 2)

if isWickoffDownBull and wickoffMode != "continuation pattern"
    label.new(x = bar_index, y = low - (ta.atr(30) * 0.2), xloc = xloc.bar_index, text = "W", style = label.style_label_up, color = color.green, size = size.small, textcolor = color.white, textalign = text.align_center)
else if isWickoffUpBull and wickoffMode != "no trend in progress"
    label.new(x = bar_index, y = low - (ta.atr(30) * 0.2), xloc = xloc.bar_index, text = "W", style = label.style_label_up, color = color.green, size = size.small, textcolor = color.white, textalign = text.align_center)
else if isWickoffUpBear and wickoffMode != "continuation pattern"
    label.new(x = bar_index, y = high + (ta.atr(30) * 0.2), xloc = xloc.bar_index, text = "W", style = label.style_label_down, color = color.red, size = size.small, textcolor = color.white, textalign = text.align_center)
else if isWickoffDownBear and wickoffMode != "no trend in progress"
    label.new(x = bar_index, y = high + (ta.atr(30) * 0.2), xloc = xloc.bar_index, text = "W", style = label.style_label_down, color = color.red, size = size.small, textcolor = color.white, textalign = text.align_center)

// ]
