本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Titpaud
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © varreyfrederic
//
// Credits (code reused under its original open-source license):
// - Core WaveTrend dots, fractal divergences (WT / RSI / Stoch RSI), RSI+MFI area, OB/OS levels
// and colors: "VuManChu Cipher B + Divergences" © vumanchu, Mozilla Public License 2.0.
// Original code by vumanchu, migrated here from Pine v4 to v6 and modified.
// - WaveTrend oscillator formula: LazyBear ("Indicator: WaveTrend Oscillator [WT]").
// - MFI Vumanchu ("MFI+RSI Area"): andreholanda73, as used in VuManChu Cipher B.
// - Stochastic RSI: standard formula (Chong & Kroll); ALMA (Arnaud Legoux) and HMA (Alan Hull) through Pine built-ins.
// - Original work by varreyfrederic: BUY/SELL signal logic, VWAP (WT momentum) divergences, channel (WT envelopes)
// + smoothed MFI + extended lines, alert() system, multi-timeframe signal grid (up to 36 TFs, no repaint).
//@version=6
// Titpaud Omniscient Cipher B (2026-10-24, formerly "Omniscient cipher") = Cipher on Steroids + Titpaud signal grid
// - The oscillator pane is Cipher on Steroids unchanged (WaveTrend, MFI, divergences, channel, BUY/SELL signals).
// - The 4 MTF levels of Cipher on Steroids (×4 ÷4 ×16 ÷16) and its 8-row table are replaced by the signal grid:
// up to 36 TFs (default list "Fine 1m → 3M", 36 TFs), one request.security per TF (36 of the 40 allowed by Pine).
// - Every signal of every TF is drawn, on the price chart (labels with force_overlay) AND in the oscillator pane:
// one triangle per TF and per direction (no grouping), "×N" when one TF gives several signals inside the same
// chart candle (lower TFs). The chart TF signal is computed on the chart itself (no delay).
// - A signal of another TF appears on the chart candle where its own candle closes (where it is seen live): no repaint.
// - Limits: 500 labels max (pane + price chart together; the oldest are deleted first), the last 3000 chart candles
// are computed, and each TF only over its last N candles ("History computed per TF") to stay within memory.
// - Signal (all TFs): regular WT2 divergence (pivot ≤ −45 / ≥ +45) → wt1×wt2 dot with WT2 ≤ −45 / ≥ +45 → MFI turns,
// within 20 bars; one signal per setup; an opposite divergence cancels; VWAP, RSI and Stoch RSI divergences required (on, window 10 bars).
// - Options "Require an RSI divergence" / "Require a Stoch RSI divergence" (both on by default): same rule as the VWAP
// divergence (regular divergence, same direction, from N bars before the WT divergence to the signal bar),
// applied to every TF (triangles, table, alerts).
// - Two MFI: MFI Cryptoface (Cipher on Steroids) and MFI Vumanchu (original VuManChu Cipher B). Display MFI Cryptoface, MFI Vumanchu or both;
// the signals use MFI Cryptoface, MFI Vumanchu, both (both must turn) or either one.
// History - Cipher on Steroids = Fred MTF signals + Fred Waves (merged on 2026-10-12)
// - the waves and the smoothed MFI reuse the WaveTrend and the MFI of the oscillator (same settings, no duplicate)
// - 64 plots limit: small dots alerts sent with alert() (selectable)
// MFI bar drawn with two hline() + fill() (hlines do not count)
// - 2026-10-22: multi-TF table: for each TF of the list, last BUY/SELL signal (same rules as the triangles),
// age in bars of that TF and in time, setup in progress.
// No repaint: only closed candles of each TF count. The table uses no plot.
// - 2026-10-23: option "Require a VWAP divergence" (on by default): a BUY/SELL signal also requires a regular VWAP
// divergence in the same direction, between N bars (default 10) before the WT divergence and the signal bar.
// Applies to the signals of every TF (triangles and table).
// Migration from Pine v4 to v6:
// - typed inputs (input.int / input.float / input.bool / input.color / input.source)
// - prefixed functions (ta.*, math.*, request.*), 'transp' replaced by color.new()
// - strictly boolean conditions (not na(...) instead of a float used as a condition)
// - fixes: RSI OB/OS bounds, color of the BUY divergence dots, dead MACD Colors code,
// useless request.security calls on the chart timeframe
// - utility plots hidden from the status line / scale (display.pane)
// - option: BUY/SELL also on the price chart (force_overlay)
indicator(title = 'Titpaud Omniscient Cipher B', shorttitle = 'Omniscient Cipher B', overlay = false, calc_bars_count = 3000, max_labels_count = 500, max_lines_count = 50)
// PARAMETERS {
// BUY / SELL signals
// Logic: 1) WT divergence whose pivot is far enough from the zero line
// 2) confirming dot (wt1/wt2 cross = VWAP crossing zero), also far enough from the zero line
// 3) the signal prints as soon as the MFI turns the right way (up for BUY, down for SELL)
sigShow = input.bool(true, title = 'Show BUY/SELL signals', group = 'Signals Settings')
sigOnPrice = input.bool(true, title = 'Also show the signals (all TFs) on the price chart', group = 'Signals Settings')
sigDivDist = input.float(45.0, title = 'Min distance from zero line - divergence (+/-)', minval = 0, group = 'Signals Settings')
sigDotDist = input.float(45.0, title = 'Min distance from zero line - dot (+/-)', minval = 0, group = 'Signals Settings')
sigPivDot = input.bool(true, title = 'The dot on the pivot bar of the divergence counts', group = 'Signals Settings', tooltip = 'A WT divergence is only confirmed 2 bars after its pivot (fractal). A dot 1 or 2 bars after the pivot always counts. Checked: a dot on the pivot bar itself also counts (it often happens, because WT1 tends to cross WT2 right at the WT2 low/high). Unchecked: a dot on the pivot bar is ignored and a second dot is needed. No repaint in both cases: that dot is already closed when the divergence is confirmed.')
sigMaxBars = input.int(20, title = 'Max delay (bars) between divergence and signal', minval = 1, group = 'Signals Settings')
sigOnPane = input.bool(true, title = 'Show the signals in the indicator pane', group = 'Signals Settings')
sigVwapReq = input.bool(true, title = 'Require a VWAP divergence', group = 'Signals Settings', tooltip = 'A signal only prints if a regular VWAP (WT momentum) divergence in the same direction was detected between N bars before the WT divergence and the signal bar. Uses the VWAP divergence levels of "VWAP Divergence Settings".')
sigRsiReq = input.bool(true, title = 'Require an RSI divergence', group = 'Signals Settings', tooltip = 'A signal only prints if a regular RSI divergence in the same direction was detected between N bars before the WT divergence and the signal bar. Uses the RSI settings and divergence levels of "RSI Settings" (the RSI does not need to be shown).')
sigStochReq = input.bool(true, title = 'Require a Stoch RSI divergence', group = 'Signals Settings', tooltip = 'A signal only prints if a regular Stochastic RSI (K line) divergence in the same direction was detected between N bars before the WT divergence and the signal bar. Uses the settings of "Stoch Settings" (the Stoch RSI does not need to be shown).')
sigVwapLook = input.int(10, title = 'VWAP divergence: max bars before the WT divergence', minval = 0, group = 'Signals Settings')
sigOscLook = input.int(10, title = 'RSI / Stoch RSI divergence: max bars before the WT divergence', minval = 0, group = 'Signals Settings')
// Signals of all TFs (formerly "Titpaud signal grid"): one request per TF of the list
G_LST = 'Timeframe list'
tfList = input.string('Fine 1m → 3M (36 TFs)', 'List', options = ['Fine 1m → 3M (36 TFs)', 'Wide 1m → 1M (22 TFs)', '1m → 1h (24 TFs)', '1h15 → 1M (19 TFs)', 'Custom list'], group = G_LST, tooltip = 'Pine allows 40 request calls per script: 36 slots max. The table only shows the TFs with a recent signal or a setup.\nFine 1m → 3M: 1m, 2m, 3m, 4m, 5m, 6m, 8m, 10m, 12m, 15m, 20m, 25m, 30m, 40m, 45m, 1h, 1h15, 1h30, 1h45, 2h, 2h30, 3h, 4h, 6h, 8h, 12h, 1D, 2D, 3D, 4D, 5D, 1W, 2W, 3W, 1M, 3M.\nWide 1m → 1M: 1m, 3m, 5m, 10m, 15m, 30m, 45m, 1h, 1h30, 2h, 3h, 4h, 6h, 8h, 12h, 1D, 2D, 3D, 1W, 2W, 3W, 1M.\n1m → 1h: 1 to 15 min, then every 5 min up to 1h.\n1h15 → 1M: 1h15, 1h30, 1h45, 2h, 2h30, 3h, 4h, 6h, 8h, 12h, 1D, 2D, 3D, 4D, 5D, 1W, 2W, 3W, 1M.\nCustom list: the 24 slots of the "Custom list" group.')
tfBars = input.int(1500, 'History computed per TF (bars)', minval = 300, maxval = 5000, group = G_LST, tooltip = 'Only the last N candles of each TF are computed, to stay within the memory limit. On a 1h chart, 1500 candles of 1m = about 25 hours: older 1m signals are not drawn. Lower it if a memory error appears.')
G_CH = 'Signals of all TFs (price chart + pane)'
sigChartShow = input.bool(true, 'Chart TF signals (computed on the chart, no delay)', group = G_CH)
sigChSz = input.string('TF class', 'Chart TF: size', options = ['TF class', 'tiny', 'small', 'normal', 'large', 'huge'], inline = 'sc', group = G_CH)
sigChColB = input.color(#3fff00, 'buy', inline = 'sc', group = G_CH)
sigChColS = input.color(#ff0000, 'sell', inline = 'sc', group = G_CH)
chShow = input.bool(true, 'Signals of the other TFs of the list', group = G_CH, tooltip = 'One triangle per TF and per direction, on the chart candle where the candle of that TF closes (no repaint). "×3" = that TF gave 3 signals inside this chart candle (lower TFs).')
chMin = input.timeframe('1', 'Only TFs ≥', group = G_CH)
chLower = input.bool(true, 'Also show TFs lower than the chart TF', group = G_CH)
chRatioS = input.string('÷16', 'Hide TFs smaller than chart TF ÷', options = ['No filter', '÷4', '÷8', '÷16', '÷32', '÷64'], group = G_CH, tooltip = 'Proportional filter: hides the triangles of the TFs smaller than the chart TF divided by this ratio. With ÷16: on a 1D chart, TFs below 1h30 are hidden; 4h chart: below 15m; 1h chart: below 4m (3m45); 15m chart: nothing hidden. Only the triangles: the table and the alerts are not affected.')
chStack = input.string('Offset', 'Several TFs on the same candle', options = ['Offset', 'Stacked'], group = G_CH, tooltip = 'Offset: each TF at its own distance, the smallest TF closest to the candle. Stacked: all at the same place, the smallest drawn over the largest, all TF names written on the largest.')
chText = input.string('TF name', 'Text', options = ['TF name', 'No text'], group = G_CH, tooltip = 'The TF and the count are always in the tooltip (hover the triangle).')
chTxMin = input.timeframe('1', 'Text only for TFs ≥', group = G_CH)
chBuyTx = input.int(1, 'BUY text: empty lines between triangle and text', minval = 0, maxval = 5, group = G_CH, tooltip = 'BUY texts are written under the triangles (a separate text label, so a BUY with text uses 2 labels of the 500). Raise it if the text touches a large triangle.')
chColB = input.color(#3fff00, 'Other TFs: buy', inline = 'oc', group = G_CH)
chColS = input.color(#ff0000, 'sell', inline = 'oc', group = G_CH)
chBig = input.timeframe('240', 'Big TF colors from', inline = 'big', group = G_CH)
chBigB = input.color(#00e5ff, 'buy', inline = 'big', group = G_CH)
chBigS = input.color(#ff00e5, 'sell', inline = 'big', group = G_CH)
chAtr = input.float(2.0, 'Price chart: distance (ATR)', minval = 0, step = 0.1, inline = 'pa', group = G_CH)
chStep = input.float(1.0, 'step between TFs', minval = 0, step = 0.1, inline = 'pa', group = G_CH)
paBase = input.float(110, 'Pane: height (±)', step = 1, inline = 'pp', group = G_CH)
paStep = input.float(12, 'step between TFs', minval = 0, step = 1, inline = 'pp', group = G_CH)
chT1On = input.bool(true, '< 15m', inline = 'k1', group = G_CH)
chT1Sz = input.string('tiny', '', options = ['tiny', 'small', 'normal', 'large', 'huge'], inline = 'k1', group = G_CH)
chT1Tr = input.int(70, 'transparency', minval = 0, maxval = 100, inline = 'k1', group = G_CH)
chT2On = input.bool(true, '15m – 1h', inline = 'k2', group = G_CH)
chT2Sz = input.string('small', '', options = ['tiny', 'small', 'normal', 'large', 'huge'], inline = 'k2', group = G_CH)
chT2Tr = input.int(65, 'transparency', minval = 0, maxval = 100, inline = 'k2', group = G_CH)
chT3On = input.bool(true, '1h – 4h', inline = 'k3', group = G_CH)
chT3Sz = input.string('normal', '', options = ['tiny', 'small', 'normal', 'large', 'huge'], inline = 'k3', group = G_CH)
chT3Tr = input.int(60, 'transparency', minval = 0, maxval = 100, inline = 'k3', group = G_CH)
chT4On = input.bool(true, '4h – 1D', inline = 'k4', group = G_CH)
chT4Sz = input.string('large', '', options = ['tiny', 'small', 'normal', 'large', 'huge'], inline = 'k4', group = G_CH)
chT4Tr = input.int(55, 'transparency', minval = 0, maxval = 100, inline = 'k4', group = G_CH)
chT5On = input.bool(true, '≥ 1D', inline = 'k5', group = G_CH)
chT5Sz = input.string('huge', '', options = ['tiny', 'small', 'normal', 'large', 'huge'], inline = 'k5', group = G_CH)
chT5Tr = input.int(55, 'transparency', minval = 0, maxval = 100, inline = 'k5', group = G_CH)
chBuyTrX = input.int(10, 'Extra transparency for BUY signals', minval = 0, maxval = 100, group = G_CH, tooltip = 'Added to the transparency of every BUY triangle and its text (pane and price chart). SELL signals are not affected.')
chRelSz = input.bool(true, 'Triangle size relative to the chart TF', group = G_CH, tooltip = 'On: the size depends on the ratio between the signal TF and the chart TF: smaller than the chart = tiny, 1 to 4× = small, 4 to 16× = normal, 16 to 64× = large, 64× and more = huge. A 1D signal stays huge on a 15m chart but is only small on a 1D chart. Off: the size of each TF class above is used. Transparency and colors always follow the TF class.')
// WaveTrend
wtShow = input.bool(true, title = 'Show WaveTrend', group = 'WaveTrend Settings')
vwapShow = input.bool(true, title = 'Show Fast WT', group = 'WaveTrend Settings')
wtChannelLen = input.int(9, title = 'WT Channel Length', group = 'WaveTrend Settings')
wtAverageLen = input.int(12, title = 'WT Average Length', group = 'WaveTrend Settings')
wtMASource = input.source(hlc3, title = 'WT MA Source', group = 'WaveTrend Settings')
wtMALen = input.int(3, title = 'WT MA Length', group = 'WaveTrend Settings')
// WaveTrend Overbought & Oversold lines
obLevel = input.int(53, title = 'WT Overbought Level 1', group = 'WaveTrend Settings')
obLevel2 = input.int(60, title = 'WT Overbought Level 2', group = 'WaveTrend Settings')
obLevel3 = input.int(100, title = 'WT Overbought Level 3', group = 'WaveTrend Settings')
osLevel = input.int(-53, title = 'WT Oversold Level 1', group = 'WaveTrend Settings')
osLevel2 = input.int(-60, title = 'WT Oversold Level 2', group = 'WaveTrend Settings')
osLevel3 = input.int(-100, title = 'WT Oversold Level 3', group = 'WaveTrend Settings')
// Divergence WT
wtShowDiv = input.bool(true, title = 'Show WT Regular Divergences', group = 'WaveTrend Settings')
wtShowHiddenDiv = input.bool(false, title = 'Show WT Hidden Divergences', group = 'WaveTrend Settings')
showHiddenDiv_nl = input.bool(false, title = 'Not apply OB/OS Limits on Hidden Divergences', group = 'WaveTrend Settings')
wtDivOBLevel = input.int(45, title = 'WT Bearish Divergence min', group = 'WaveTrend Settings')
wtDivOSLevel = input.int(-65, title = 'WT Bullish Divergence min', group = 'WaveTrend Settings')
// Divergence extra range
wtDivOBLevel_addshow = input.bool(true, title = 'Show 2nd WT Regular Divergences', group = 'WaveTrend Settings')
wtDivOBLevel_add = input.int(15, title = 'WT 2nd Bearish Divergence', group = 'WaveTrend Settings')
wtDivOSLevel_add = input.int(-40, title = 'WT 2nd Bullish Divergence 15 min', group = 'WaveTrend Settings')
// RSI+MFI
rsiMFIShow = input.bool(true, title = 'Show MFI', group = 'MFI Settings')
rsiMFIperiod = input.int(60, title = 'MFI Period', group = 'MFI Settings')
rsiMFIPosY = input.float(2.5, title = 'MFI Area Y Pos', group = 'MFI Settings')
// Two MFI: MFI Cryptoface = Cipher on Steroids (hlc3 deviation), MFI Vumanchu = original VuManChu Cipher B (candle body / range, andreholanda73)
mfiView = input.string('Both MFI overlaid', title = 'MFI displayed', options = ['MFI Cryptoface', 'MFI Vumanchu', 'Both MFI overlaid'], group = 'MFI Settings', tooltip = 'MFI Cryptoface = Cipher on Steroids MFI: SMA(60) of the hlc3 deviation (CCI-like).\nMFI Vumanchu = original VuManChu Cipher B MFI: SMA(60) of (close − open) / (high − low) × multiplier.\nBoth MFI overlaid = the two areas overlaid. The MFI bar follows MFI Cryptoface, or MFI Vumanchu when it is displayed alone. Each MFI has its own smoothed line (group SMOOTHED MFI).')
mfiSig = input.string('Both must turn', title = 'MFI used by the signals', options = ['MFI Cryptoface', 'MFI Vumanchu', 'Both must turn', 'Either one turns'], group = 'MFI Settings', tooltip = 'Last step of a BUY (SELL) signal: the MFI turns up (down).\nBoth must turn = MFI Cryptoface AND MFI Vumanchu turn the right way on the same bar (stricter).\nEither one turns = MFI Cryptoface OR MFI Vumanchu is enough (more signals).\nApplies to every TF (triangles, table, alerts).')
rsiMFIMultiplier = input.float(150, title = 'MFI Vumanchu: area multiplier', group = 'MFI Settings')
mfi2ColA = input.color(color.new(#26c6da, 50), 'MFI Vumanchu color > 0', inline = 'm2', group = 'MFI Settings')
mfi2ColB = input.color(color.new(#ffa726, 50), '< 0', inline = 'm2', group = 'MFI Settings')
// RSI
rsiShow = input.bool(false, title = 'Show RSI', group = 'RSI Settings')
rsiSRC = input.source(close, title = 'RSI Source', group = 'RSI Settings')
rsiLen = input.int(14, title = 'RSI Length', group = 'RSI Settings')
rsiOversold = input.int(30, title = 'RSI Oversold', minval = 0, maxval = 50, group = 'RSI Settings')
rsiOverbought = input.int(60, title = 'RSI Overbought', minval = 50, maxval = 100, group = 'RSI Settings')
// Divergence RSI
rsiShowDiv = input.bool(true, title = 'Show RSI Regular Divergences', group = 'RSI Settings')
rsiShowHiddenDiv = input.bool(false, title = 'Show RSI Hidden Divergences', group = 'RSI Settings')
rsiDivOBLevel = input.int(60, title = 'RSI Bearish Divergence min', group = 'RSI Settings')
rsiDivOSLevel = input.int(30, title = 'RSI Bullish Divergence min', group = 'RSI Settings')
// RSI Stochastic
stochShow = input.bool(false, title = 'Show Stochastic RSI', group = 'Stoch Settings')
stochUseLog = input.bool(true, title = ' Use Log?', group = 'Stoch Settings')
stochAvg = input.bool(false, title = 'Use Average of both K & D', group = 'Stoch Settings')
stochSRC = input.source(close, title = 'Stochastic RSI Source', group = 'Stoch Settings')
stochLen = input.int(14, title = 'Stochastic RSI Length', group = 'Stoch Settings')
stochRsiLen = input.int(14, title = 'RSI Length ', group = 'Stoch Settings')
stochKSmooth = input.int(3, title = 'Stochastic RSI K Smooth', group = 'Stoch Settings')
stochDSmooth = input.int(3, title = 'Stochastic RSI D Smooth', group = 'Stoch Settings')
// Divergence stoch
stochShowDiv = input.bool(true, title = 'Show Stoch Regular Divergences', group = 'Stoch Settings')
stochShowHiddenDiv = input.bool(false, title = 'Show Stoch Hidden Divergences', group = 'Stoch Settings')
// Divergence VWAP (WT momentum)
vwapShowDiv = input.bool(true, title = 'Show VWAP Regular Divergences', group = 'VWAP Divergence Settings')
vwapShowHiddenDiv = input.bool(false, title = 'Show VWAP Hidden Divergences', group = 'VWAP Divergence Settings')
vwapDivOBLevel = input.float(0.0, title = 'VWAP Bearish Divergence min (top >=)', group = 'VWAP Divergence Settings')
vwapDivOSLevel = input.float(0.0, title = 'VWAP Bullish Divergence max (bottom <=)', group = 'VWAP Divergence Settings')
G_TAB = 'Table'
tabShow = input.bool(true, 'Show table', group = G_TAB)
tabOnPrice = input.bool(false, 'Display on the price chart (otherwise in the indicator pane)', group = G_TAB)
tabPos = input.string('Bottom left', 'Position', options = ['Top left', 'Top center', 'Top right', 'Middle left', 'Middle right', 'Bottom left', 'Bottom center', 'Bottom right'], inline = 'tp', group = G_TAB)
tabSize = input.string('tiny', 'Text size', options = ['tiny', 'small', 'normal', 'large'], inline = 'tp', group = G_TAB)
tabBlocks = input.int(2, 'Side-by-side blocks', minval = 1, maxval = 4, group = G_TAB, tooltip = 'Splits the rows into N blocks placed side by side, so that many TFs do not make a very tall table.')
tabOrder = input.string('Largest first', 'Order', options = ['Largest first', 'Smallest first', 'Most recent signal first'], group = G_TAB, tooltip = 'Most recent signal first: rows sorted by the close time of their last signal, the most recent at the top, the oldest at the bottom; TFs without a signal come last (largest first).')
tabMaxOn = input.bool(true, 'Remove signals older than', inline = 'mx', group = G_TAB)
tabMax = input.int(30, '', minval = 1, inline = 'mx', group = G_TAB)
tabMaxU = input.string('bars of their TF', '', options = ['bars of their TF', 'hours (all TFs)'], inline = 'mx', group = G_TAB, tooltip = 'An older signal is removed from the table (cell shown as "—"). Bars of their TF: 30 = 30 min on 1m, 30 hours on 1h, 30 days on 1D. Hours (all TFs): the same limit for every TF (big TF signals are then removed quickly too).')
tabOnlySig = input.bool(true, 'Hide TFs without any signal or setup', group = G_TAB)
tabFresh = input.int(3, 'Recent signal = within N bars of its TF', minval = 1, group = G_TAB, tooltip = 'Recent signal: bright cell. Older signal: faded cell. 1 = the signal is on the last closed candle of that TF.')
tabColBars = input.bool(true, 'Column: bars ago', inline = 'tc', group = G_TAB)
tabColAge = input.bool(true, 'Column: time ago', inline = 'tc', group = G_TAB)
tabColSetup = input.bool(true, 'Column: setup in progress', group = G_TAB, tooltip = 'Div = regular WT divergence found, waiting for the dot. Div+Dot = dot found, waiting for the MFI to turn (and for the VWAP / RSI / Stoch RSI divergences that are required). VWAP / RSI / Stoch = divergences of these oscillators already found for this setup (same direction, within the N bars window).')
tabSummary = input.bool(true, 'Summary row (recent BUY / SELL count)', group = G_TAB)
tabColB = input.color(#3fff00, 'Buy', inline = 'tcol', group = G_TAB)
tabColS = input.color(#ff0000, 'Sell', inline = 'tcol', group = G_TAB)
G_CU = 'Custom list (List = Custom list)'
cOn1 = input.bool(true, '', inline = 'c1', group = G_CU)
cTf1 = input.timeframe('1', 'Slot 1', inline = 'c1', group = G_CU)
cOn2 = input.bool(true, '', inline = 'c2', group = G_CU)
cTf2 = input.timeframe('2', 'Slot 2', inline = 'c2', group = G_CU)
cOn3 = input.bool(true, '', inline = 'c3', group = G_CU)
cTf3 = input.timeframe('3', 'Slot 3', inline = 'c3', group = G_CU)
cOn4 = input.bool(true, '', inline = 'c4', group = G_CU)
cTf4 = input.timeframe('4', 'Slot 4', inline = 'c4', group = G_CU)
cOn5 = input.bool(true, '', inline = 'c5', group = G_CU)
cTf5 = input.timeframe('5', 'Slot 5', inline = 'c5', group = G_CU)
cOn6 = input.bool(true, '', inline = 'c6', group = G_CU)
cTf6 = input.timeframe('6', 'Slot 6', inline = 'c6', group = G_CU)
cOn7 = input.bool(true, '', inline = 'c7', group = G_CU)
cTf7 = input.timeframe('7', 'Slot 7', inline = 'c7', group = G_CU)
cOn8 = input.bool(true, '', inline = 'c8', group = G_CU)
cTf8 = input.timeframe('8', 'Slot 8', inline = 'c8', group = G_CU)
cOn9 = input.bool(true, '', inline = 'c9', group = G_CU)
cTf9 = input.timeframe('9', 'Slot 9', inline = 'c9', group = G_CU)
cOn10 = input.bool(true, '', inline = 'c10', group = G_CU)
cTf10 = input.timeframe('10', 'Slot 10', inline = 'c10', group = G_CU)
cOn11 = input.bool(true, '', inline = 'c11', group = G_CU)
cTf11 = input.timeframe('11', 'Slot 11', inline = 'c11', group = G_CU)
cOn12 = input.bool(true, '', inline = 'c12', group = G_CU)
cTf12 = input.timeframe('12', 'Slot 12', inline = 'c12', group = G_CU)
cOn13 = input.bool(true, '', inline = 'c13', group = G_CU)
cTf13 = input.timeframe('13', 'Slot 13', inline = 'c13', group = G_CU)
cOn14 = input.bool(true, '', inline = 'c14', group = G_CU)
cTf14 = input.timeframe('14', 'Slot 14', inline = 'c14', group = G_CU)
cOn15 = input.bool(true, '', inline = 'c15', group = G_CU)
cTf15 = input.timeframe('15', 'Slot 15', inline = 'c15', group = G_CU)
cOn16 = input.bool(true, '', inline = 'c16', group = G_CU)
cTf16 = input.timeframe('20', 'Slot 16', inline = 'c16', group = G_CU)
cOn17 = input.bool(true, '', inline = 'c17', group = G_CU)
cTf17 = input.timeframe('25', 'Slot 17', inline = 'c17', group = G_CU)
cOn18 = input.bool(true, '', inline = 'c18', group = G_CU)
cTf18 = input.timeframe('30', 'Slot 18', inline = 'c18', group = G_CU)
cOn19 = input.bool(true, '', inline = 'c19', group = G_CU)
cTf19 = input.timeframe('35', 'Slot 19', inline = 'c19', group = G_CU)
cOn20 = input.bool(true, '', inline = 'c20', group = G_CU)
cTf20 = input.timeframe('40', 'Slot 20', inline = 'c20', group = G_CU)
cOn21 = input.bool(true, '', inline = 'c21', group = G_CU)
cTf21 = input.timeframe('45', 'Slot 21', inline = 'c21', group = G_CU)
cOn22 = input.bool(true, '', inline = 'c22', group = G_CU)
cTf22 = input.timeframe('50', 'Slot 22', inline = 'c22', group = G_CU)
cOn23 = input.bool(true, '', inline = 'c23', group = G_CU)
cTf23 = input.timeframe('55', 'Slot 23', inline = 'c23', group = G_CU)
cOn24 = input.bool(true, '', inline = 'c24', group = G_CU)
cTf24 = input.timeframe('60', 'Slot 24', inline = 'c24', group = G_CU)
// Channel (formerly "waves", from "Fred Waves"): envelopes of the WaveTrend + smoothed MFI
G_WV = 'CHANNEL'
wvShow = input.bool(true, 'Show channel', group = G_WV)
wLine = input.string('WT2 (dark wave)', 'Line to envelope', options = ['WT1 (light wave)', 'WT2 (dark wave)'], group = G_WV)
wvMode = input.string('Live (no repaint)', 'Mode', options = ['Live (no repaint)', 'Centered (history, repaints)'], group = G_WV)
envLen = input.int(30, 'Envelope length (bars)', minval = 3, group = G_WV)
smLen = input.int(60, 'Smoothing length (bars)', minval = 1, group = G_WV)
smType = input.string('ALMA', 'Smoothing type (Live mode)', options = ['SMA', 'EMA', 'WMA', 'ALMA', 'HMA'], group = G_WV)
showMid = input.bool(true, 'Show mid-line', group = G_WV)
showFill = input.bool(false, 'Fill inside the channel', group = G_WV)
G_MF = 'SMOOTHED MFI'
mfiOn = input.bool(false, 'Show smoothed MFI Cryptoface', group = G_MF)
mfiOn2 = input.bool(false, 'Show smoothed MFI Vumanchu', group = G_MF, tooltip = 'Uses the colors of MFI Vumanchu (MFI Settings), more opaque. Same smoothing length and mode as the other smoothed MFI.')
mfiSm = input.int(15, 'MFI smoothing length (bars)', minval = 1, group = G_MF)
colMfU = input.color(color.new(#a5d6a7, 50), 'Above 0', inline = 'mf', group = G_MF)
colMfD = input.color(color.new(#ef9a9a, 50), 'Below 0', inline = 'mf', group = G_MF)
mfW = input.int(2, 'Width', minval = 1, maxval = 6, inline = 'mf', group = G_MF)
G_WST = 'CHANNEL STYLE'
colUp = input.color(color.new(#4caf50, 50), 'Upper band', inline = 'c', group = G_WST)
colDn = input.color(color.new(#4caf50, 50), 'Lower band', inline = 'c', group = G_WST)
lw = input.int(1, 'Width', minval = 1, maxval = 6, inline = 'c', group = G_WST)
colFill = input.color(color.new(#4caf50, 92), 'Fill', group = G_WST)
G_EX = 'EXTENDED LINES'
extOn = input.bool(true, 'Extend the curves to the right', group = G_EX)
extBars = input.int(30, 'Bars beyond the end of the curve', minval = 0, maxval = 300, group = G_EX)
extLook = input.int(1, 'Slope measured over (bars)', minval = 1, maxval = 50, group = G_EX)
extSty = input.string('Dotted line', 'Line style', options = ['Solid line', 'Dashed line', 'Dotted line'], inline = 'ex', group = G_EX)
extW = input.int(2, 'Width', minval = 1, maxval = 6, inline = 'ex', group = G_EX)
// Alerts sent with alert(): create ONE "Any alert() function call" alert on the indicator,
// the boxes below choose what it sends
G_AL = 'Alerts ("Any alert() function call")'
alOn = input.bool(true, 'BUY/SELL signal of each TF of the list', group = G_AL, tooltip = 'Create ONE alert on this indicator with the condition "Any alert() function call". The alert fires when the candle of that TF closes with a signal.')
alMin = input.timeframe('1', 'Only TFs ≥', group = G_AL, tooltip = 'Ignores the smallest TFs of the list for the alerts (e.g. 5 = only 5m and above).')
alDots = input.bool(false, 'Small dots (every WT cross, very frequent)', group = G_AL)
darkMode = input.bool(false, title = 'Dark mode', group = 'Mode Settings')
// Colors
colorRed = #ff0000
colorPurple = #e600e6
colorGreen = #3fff00
colorOrange = #e2a400
colorYellow = #ffe500
colorWhite = #ffffff
colorPink = #ff00f0
colorBluelight = #31c0ff
colorWT2 = #0d47a1
colorWT2_ = #131722
// } PARAMETERS
// FUNCTIONS {
// Divergences
f_top_fractal(src) => src[4] < src[2] and src[3] < src[2] and src[2] > src[1] and src[2] > src[0]
f_bot_fractal(src) => src[4] > src[2] and src[3] > src[2] and src[2] < src[1] and src[2] < src[0]
f_fractalize(src) =>
isTop = f_top_fractal(src)
isBot = f_bot_fractal(src)
isTop ? 1 : isBot ? -1 : 0
// fractalTop / fractalBot = src[2] on a pivot, na otherwise
f_findDivs(src, topLimit, botLimit, useLimits) =>
fractal = f_fractalize(src)
fractalTop = fractal > 0 and (useLimits ? src[2] >= topLimit : true) ? src[2] : na
fractalBot = fractal < 0 and (useLimits ? src[2] <= botLimit : true) ? src[2] : na
isTop = not na(fractalTop)
isBot = not na(fractalBot)
highPrev = ta.valuewhen(isTop, src[2], 0)[2]
highPrice = ta.valuewhen(isTop, high[2], 0)[2]
lowPrev = ta.valuewhen(isBot, src[2], 0)[2]
lowPrice = ta.valuewhen(isBot, low[2], 0)[2]
bearSignal = isTop and high[2] > highPrice and src[2] < highPrev
bullSignal = isBot and low[2] < lowPrice and src[2] > lowPrev
bearDivHidden = isTop and high[2] < highPrice and src[2] > highPrev
bullDivHidden = isBot and low[2] > lowPrice and src[2] < lowPrev
[fractalTop, fractalBot, lowPrev, bearSignal, bullSignal, bearDivHidden, bullDivHidden]
// Money Flow
f_rsimfi(_period) =>
ta.sma((hlc3 - ta.sma(hlc3, 5)) / (0.015 * ta.sma(math.abs(hlc3 - ta.sma(hlc3, 5)), 5)), _period) - rsiMFIPosY
// MFI Vumanchu: original VuManChu Cipher B "MFI+RSI Area" (andreholanda73), candle body over candle range (0 on a flat candle)
f_rsimfi2(_period, _multiplier) =>
ta.sma((high - low) == 0 ? 0.0 : (close - open) / (high - low) * _multiplier, _period) - rsiMFIPosY
// MFI turn used by the signals, according to "MFI used by the signals"
f_mfiTurn(float m1, float m2) =>
bool u1 = m1 > m1[1]
bool d1 = m1 < m1[1]
bool u2 = m2 > m2[1]
bool d2 = m2 < m2[1]
bool up = mfiSig == 'MFI Cryptoface' ? u1 : mfiSig == 'MFI Vumanchu' ? u2 : mfiSig == 'Both must turn' ? u1 and u2 : u1 or u2
bool dn = mfiSig == 'MFI Cryptoface' ? d1 : mfiSig == 'MFI Vumanchu' ? d2 : mfiSig == 'Both must turn' ? d1 and d2 : d1 or d2
[up, dn]
// WaveTrend
f_wavetrend(src, chlen, avg, malen) =>
esa = ta.ema(src, chlen)
de = ta.ema(math.abs(src - esa), chlen)
ci = (src - esa) / (0.015 * de)
wt1 = ta.ema(ci, avg)
wt2 = ta.sma(wt1, malen)
wtVwap = wt1 - wt2
wtOversold = wt2 <= osLevel
wtOverbought = wt2 >= obLevel
wtCross = ta.cross(wt1, wt2)
wtCrossUp = wt2 - wt1 <= 0
wtCrossDown = wt2 - wt1 >= 0
wtCrosslast = ta.cross(wt1[2], wt2[2])
wtCrossUplast = wt2[2] - wt1[2] <= 0
wtCrossDownlast = wt2[2] - wt1[2] >= 0
[wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCrosslast, wtCrossUplast, wtCrossDownlast, wtVwap]
// Stochastic RSI
f_stochrsi(_src, _stochlen, _rsilen, _smoothk, _smoothd, _log, _avg) =>
src = _log ? math.log(_src) : _src
_rsi = ta.rsi(src, _rsilen)
kk = ta.sma(ta.stoch(_rsi, _rsi, _rsi, _stochlen), _smoothk)
d1 = ta.sma(kk, _smoothd)
avg_1 = math.avg(kk, d1)
k = _avg ? avg_1 : kk
[k, d1]
// BUY / SELL signals wrapped in a function (to compute them on other TFs) - same logic as below
// also returns the setup state: 0 = none, 1 = divergence waiting for the dot, 2 = divergence + dot waiting for the MFI (and for the required divergences)
f_sigFull() =>
[w1, w2, _o1, _o2, _c1, _c2, _c3, _c4, _c5, _c6, vw] = f_wavetrend(wtMASource, wtChannelLen, wtAverageLen, wtMALen)
float mf = f_rsimfi(rsiMFIperiod)
float mf2 = f_rsimfi2(rsiMFIperiod, rsiMFIMultiplier)
[fT, fB, _lp, bearD, bullD, _bh, _bbh] = f_findDivs(w2, 0, 0, false)
bool bDiv = bullD and fB <= -sigDivDist
bool sDiv = bearD and fT >= sigDivDist
bool dB = ta.crossover(vw, 0) and w2 <= -sigDotDist
bool dS = ta.crossunder(vw, 0) and w2 >= sigDotDist
[mu, md] = f_mfiTurn(mf, mf2)
[_vT, _vB, _vlp, vBear, vBull, _vbh, _vbbh] = f_findDivs(vw, vwapDivOBLevel, vwapDivOSLevel, true)
// optional RSI / Stoch RSI divergences (same rule as the VWAP divergence, own window)
float rs = ta.rsi(rsiSRC, rsiLen)
[stK, _stD] = f_stochrsi(stochSRC, stochLen, stochRsiLen, stochKSmooth, stochDSmooth, stochUseLog, stochAvg)
[_rT, _rB, _rlp, rBear, rBull, _rbh, _rbbh] = f_findDivs(rs, rsiDivOBLevel, rsiDivOSLevel, true)
[_kT, _kB, _klp, kBear, kBull, _kbh, _kbbh] = f_findDivs(stK, 0, 0, false)
var int rB = na
var int rS = na
var int kB = na
var int kS = na
if rBull
rB := bar_index
if rBear
rS := bar_index
if kBull
kB := bar_index
if kBear
kS := bar_index
var int vB = na
var int vS = na
if vBull
vB := bar_index
if vBear
vS := bar_index
var int bS = na
var bool bC = false
var int sS = na
var bool sC = false
if bDiv
bS := bar_index
bC := dB or dB[1] or (sigPivDot and dB[2])
sS := na
sC := false
if sDiv
sS := bar_index
sC := dS or dS[1] or (sigPivDot and dS[2])
bS := na
bC := false
if not na(bS) and bar_index - bS > sigMaxBars
bS := na
bC := false
if not na(sS) and bar_index - sS > sigMaxBars
sS := na
sC := false
if not na(bS) and dB
bC := true
if not na(sS) and dS
sC := true
bool vOkB = not sigVwapReq or (not na(bS) and not na(vB) and vB >= bS - sigVwapLook)
bool vOkS = not sigVwapReq or (not na(sS) and not na(vS) and vS >= sS - sigVwapLook)
bool rOkB = not sigRsiReq or (not na(bS) and not na(rB) and rB >= bS - sigOscLook)
bool rOkS = not sigRsiReq or (not na(sS) and not na(rS) and rS >= sS - sigOscLook)
bool kOkB = not sigStochReq or (not na(bS) and not na(kB) and kB >= bS - sigOscLook)
bool kOkS = not sigStochReq or (not na(sS) and not na(kS) and kS >= sS - sigOscLook)
bool bT = not na(bS) and bC and mu and vOkB and rOkB and kOkB
bool sT = not na(sS) and sC and md and vOkS and rOkS and kOkS
if bT
bS := na
bC := false
if sT
sS := na
sC := false
int bSt = na(bS) ? 0 : bC ? 2 : 1
int sSt = na(sS) ? 0 : sC ? 2 : 1
// divergences already found for the setup in progress: 1 = VWAP, 2 = RSI, 4 = Stoch RSI
int bDv = na(bS) ? 0 : (not na(vB) and vB >= bS - sigVwapLook ? 1 : 0) + (not na(rB) and rB >= bS - sigOscLook ? 2 : 0) + (not na(kB) and kB >= bS - sigOscLook ? 4 : 0)
int sDv = na(sS) ? 0 : (not na(vS) and vS >= sS - sigVwapLook ? 1 : 0) + (not na(rS) and rS >= sS - sigOscLook ? 2 : 0) + (not na(kS) and kS >= sS - sigOscLook ? 4 : 0)
[bT, sT, bSt, sSt, bDv, sDv]
// Signal grid: state of the requested TF, seen from its last CLOSED candle ([1] values: no repaint)
// dir = 1 BUY / -1 SELL / 0 none; n = candles elapsed (1 = signal on the last closed candle);
// tm = close time of the signal candle; st = setup in progress (+ buy / − sell);
// cB / cS = cumulative number of BUY / SELL signals: the difference between two chart candles gives every signal
// of that TF inside the chart candle (several signals of a lower TF are all counted, none is lost)
f_tabReq() =>
[b, s, bSt, sSt, bDv, sDv] = f_sigFull()
var int dir = 0
var int sBar = na
var int sTm = na
var int cB = 0
var int cS = 0
if b
dir := 1
sBar := bar_index
sTm := time_close
cB += 1
if s
dir := -1
sBar := bar_index
sTm := time_close
cS += 1
int st = bSt > 0 ? bSt : sSt > 0 ? -sSt : 0
int dv = bSt > 0 ? bDv : sSt > 0 ? sDv : 0
[nz(dir[1]), bar_index - sBar[1], sTm[1], nz(st[1]), cB[1], cS[1], nz(dv[1])]
// TF lists: "simple" strings (no array) so that the lookahead choice stays "simple"
f_tfA(int k) =>
switch k
1 => '1'
2 => '2'
3 => '3'
4 => '4'
5 => '5'
6 => '6'
7 => '7'
8 => '8'
9 => '9'
10 => '10'
11 => '11'
12 => '12'
13 => '13'
14 => '14'
15 => '15'
16 => '20'
17 => '25'
18 => '30'
19 => '35'
20 => '40'
21 => '45'
22 => '50'
23 => '55'
24 => '60'
=> ''
f_tfW(int k) =>
switch k
1 => '1'
2 => '3'
3 => '5'
4 => '10'
5 => '15'
6 => '30'
7 => '45'
8 => '60'
9 => '90'
10 => '120'
11 => '180'
12 => '240'
13 => '360'
14 => '480'
15 => '720'
16 => 'D'
17 => '2D'
18 => '3D'
19 => 'W'
20 => '2W'
21 => '3W'
22 => 'M'
=> ''
f_tfB(int k) =>
switch k
1 => '75'
2 => '90'
3 => '105'
4 => '120'
5 => '150'
6 => '180'
7 => '240'
8 => '360'
9 => '480'
10 => '720'
11 => 'D'
12 => '2D'
13 => '3D'
14 => '4D'
15 => '5D'
16 => 'W'
17 => '2W'
18 => '3W'
19 => 'M'
=> ''
f_tfC(int k) =>
switch k
1 => cOn1 ? cTf1 : ''
2 => cOn2 ? cTf2 : ''
3 => cOn3 ? cTf3 : ''
4 => cOn4 ? cTf4 : ''
5 => cOn5 ? cTf5 : ''
6 => cOn6 ? cTf6 : ''
7 => cOn7 ? cTf7 : ''
8 => cOn8 ? cTf8 : ''
9 => cOn9 ? cTf9 : ''
10 => cOn10 ? cTf10 : ''
11 => cOn11 ? cTf11 : ''
12 => cOn12 ? cTf12 : ''
13 => cOn13 ? cTf13 : ''
14 => cOn14 ? cTf14 : ''
15 => cOn15 ? cTf15 : ''
16 => cOn16 ? cTf16 : ''
17 => cOn17 ? cTf17 : ''
18 => cOn18 ? cTf18 : ''
19 => cOn19 ? cTf19 : ''
20 => cOn20 ? cTf20 : ''
21 => cOn21 ? cTf21 : ''
22 => cOn22 ? cTf22 : ''
23 => cOn23 ? cTf23 : ''
24 => cOn24 ? cTf24 : ''
=> ''
f_tfF(int k) =>
switch k
1 => '1'
2 => '2'
3 => '3'
4 => '4'
5 => '5'
6 => '6'
7 => '8'
8 => '10'
9 => '12'
10 => '15'
11 => '20'
12 => '25'
13 => '30'
14 => '40'
15 => '45'
16 => '60'
17 => '75'
18 => '90'
19 => '105'
20 => '120'
21 => '150'
22 => '180'
23 => '240'
24 => '360'
25 => '480'
26 => '720'
27 => 'D'
28 => '2D'
29 => '3D'
30 => '4D'
31 => '5D'
32 => 'W'
33 => '2W'
34 => '3W'
35 => 'M'
36 => '3M'
=> ''
f_tf(int k) =>
tfList == 'Custom list' ? f_tfC(k) : tfList == 'Fine 1m → 3M (36 TFs)' ? f_tfF(k) : tfList == '1h15 → 1M (19 TFs)' ? f_tfB(k) : tfList == 'Wide 1m → 1M (22 TFs)' ? f_tfW(k) : f_tfA(k)
// one slot = one request.security call (an empty TF is not requested)
// TF ≥ chart: lookahead_on + [1] = last closed candle as soon as it closes;
// TF < chart: lookahead_off + [1] = last closed small candle inside the chart candle
f_slot(int k) =>
string tf = f_tf(k)
int d = 0
int n = na
int t = na
int p = 0
int cb = na
int cs = na
int dv = 0
if tf != ''
[a, b, c, e, f, g, h] = request.security(syminfo.tickerid, tf, f_tabReq(), lookahead = timeframe.in_seconds(tf) >= timeframe.in_seconds() ? barmerge.lookahead_on : barmerge.lookahead_off, calc_bars_count = tfBars)
d := a
n := b
t := c
p := e
cb := f
cs := g
dv := h
[d, n, t, p, cb, cs, dv]
f_tfName(string t) =>
int sec = timeframe.in_seconds(t)
int mn = int(math.round(sec / 60))
string r = ''
if sec >= 2419200 and str.endswith(t, 'M')
r := (t == 'M' ? '1' : str.replace(t, 'M', '')) + 'M'
else if sec < 60
r := str.tostring(sec) + 's'
else if mn < 60
r := str.tostring(mn) + 'm'
else if mn < 1440
r := str.tostring(int(mn / 60)) + 'h' + (mn % 60 > 0 ? (mn % 60 < 10 ? '0' : '') + str.tostring(mn % 60) : '')
else if mn % 10080 == 0
r := str.tostring(int(mn / 10080)) + 'W'
else
r := str.tostring(int(mn / 1440)) + 'D'
r
// size class of a TF: 1 = < 15m, 2 = 15m–1h, 3 = 1h–4h, 4 = 4h–1D, 5 = ≥ 1D
f_tier(int sec) =>
sec < 900 ? 1 : sec < 3600 ? 2 : sec < 14400 ? 3 : sec < 86400 ? 4 : 5
f_tierOn(int t) =>
t == 1 ? chT1On : t == 2 ? chT2On : t == 3 ? chT3On : t == 4 ? chT4On : chT5On
f_tierTr(int t) =>
t == 1 ? chT1Tr : t == 2 ? chT2Tr : t == 3 ? chT3Tr : t == 4 ? chT4Tr : chT5Tr
f_tierSz(int t) =>
string z = t == 1 ? chT1Sz : t == 2 ? chT2Sz : t == 3 ? chT3Sz : t == 4 ? chT4Sz : chT5Sz
z == 'tiny' ? size.tiny : z == 'small' ? size.small : z == 'normal' ? size.normal : z == 'large' ? size.large : size.huge
f_tabPos(string p) =>
switch p
'Top left' => position.top_left
'Top center' => position.top_center
'Middle left' => position.middle_left
'Middle right' => position.middle_right
'Bottom left' => position.bottom_left
'Bottom center' => position.bottom_center
'Bottom right' => position.bottom_right
=> position.top_right
f_tabSz(string s) =>
s == 'tiny' ? size.tiny : s == 'normal' ? size.normal : s == 'large' ? size.large : size.small
// time elapsed since the close of the signal candle
f_age(int ms) =>
int mn = int(ms / 60000)
mn < 60 ? str.tostring(mn) + 'm' : mn < 1440 ? str.tostring(int(mn / 60)) + 'h' + (mn % 60 > 0 ? (mn % 60 < 10 ? '0' : '') + str.tostring(mn % 60) : '') : mn < 10080 ? str.tostring(int(mn / 1440)) + 'd ' + str.tostring(int((mn % 1440) / 60)) + 'h' : str.tostring(int(mn / 1440)) + 'd'
// size relative to the chart TF: ratio signal TF / chart TF
f_relSz(int sec, int secChart) =>
float r = float(sec) / math.max(secChart, 1)
r < 1 ? size.tiny : r < 4 ? size.small : r < 16 ? size.normal : r < 64 ? size.large : size.huge
f_szStr(string z) =>
z == 'tiny' ? size.tiny : z == 'small' ? size.small : z == 'normal' ? size.normal : z == 'large' ? size.large : size.huge
// Channel: smoothing of the Live mode
f_ma(float src, int l) =>
switch smType
'SMA' => ta.sma(src, l)
'EMA' => ta.ema(src, l)
'WMA' => ta.wma(src, l)
'ALMA' => ta.alma(src, l, 0.85, 6)
=> ta.hma(src, l)
f_lineStyle(string t) =>
t == 'Solid line' ? line.style_solid : t == 'Dotted line' ? line.style_dotted : line.style_dashed
// } FUNCTIONS
// CALCULATE INDICATORS {
// RSI
rsi = ta.rsi(rsiSRC, rsiLen)
rsiobcolor = input.color(#e13e3e, 'RSI OverBought', group = 'Color Settings')
rsioscolor = input.color(#3ee145, 'RSI OverSold', group = 'Color Settings')
rsinacolor = input.color(#c33ee1, 'RSI InBetween', group = 'Color Settings')
rsiColor = rsi <= rsiOversold ? rsioscolor : rsi >= rsiOverbought ? rsiobcolor : rsinacolor
// RSI + MFI Area
rsiMFI = f_rsimfi(rsiMFIperiod)
rsiMFIColorAbove = input.color(#3ee145, 'MFI Color > 0', group = 'Color Settings')
rsiMFIColorBelow = input.color(#ff3d2e, 'MFI Color < 0', group = 'Color Settings')
rsiMFI2 = f_rsimfi2(rsiMFIperiod, rsiMFIMultiplier)
bool mfiShow1 = mfiView != 'MFI Vumanchu'
bool mfiShow2 = mfiView != 'MFI Cryptoface'
// MFI followed by the MFI bar and the smoothed MFI: MFI Vumanchu only when it is displayed alone
float mfiMain = mfiView == 'MFI Vumanchu' ? rsiMFI2 : rsiMFI
rsiMFIColor = mfiMain > 0 ? rsiMFIColorAbove : rsiMFIColorBelow
// Calculates WaveTrend
[wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCross_last, wtCrossUp_last, wtCrossDown_last, wtVwap] = f_wavetrend(wtMASource, wtChannelLen, wtAverageLen, wtMALen)
// Stochastic RSI
[stochK, stochD] = f_stochrsi(stochSRC, stochLen, stochRsiLen, stochKSmooth, stochDSmooth, stochUseLog, stochAvg)
// WT Divergences
[wtFractalTop, wtFractalBot, wtLow_prev, wtBearDiv, wtBullDiv, wtBearDivHidden, wtBullDivHidden] = f_findDivs(wt2, wtDivOBLevel, wtDivOSLevel, true)
[wtFractalTop_add, wtFractalBot_add, wtLow_prev_add, wtBearDiv_add, wtBullDiv_add, wtBearDivHidden_add, wtBullDivHidden_add] = f_findDivs(wt2, wtDivOBLevel_add, wtDivOSLevel_add, true)
[wtFractalTop_nl, wtFractalBot_nl, wtLow_prev_nl, wtBearDiv_nl, wtBullDiv_nl, wtBearDivHidden_nl, wtBullDivHidden_nl] = f_findDivs(wt2, 0, 0, false)
wtBearDivHidden_ = showHiddenDiv_nl ? wtBearDivHidden_nl : wtBearDivHidden
wtBullDivHidden_ = showHiddenDiv_nl ? wtBullDivHidden_nl : wtBullDivHidden
WTBearDivColorDown = input.color(#e60000, 'WT Bear Div', group = 'Color Settings')
wtBullDivColorUp = input.color(#00e676, 'WT Bull Div', group = 'Color Settings')
wtBearDivColor = (wtShowDiv and wtBearDiv) or (wtShowHiddenDiv and wtBearDivHidden_) ? WTBearDivColorDown : na
wtBullDivColor = (wtShowDiv and wtBullDiv) or (wtShowHiddenDiv and wtBullDivHidden_) ? wtBullDivColorUp : na
wtBearDivColor_add = (wtShowDiv and (wtDivOBLevel_addshow and wtBearDiv_add)) or (wtShowHiddenDiv and (wtDivOBLevel_addshow and wtBearDivHidden_add)) ? WTBearDivColorDown : na
wtBullDivColor_add = (wtShowDiv and (wtDivOBLevel_addshow and wtBullDiv_add)) or (wtShowHiddenDiv and (wtDivOBLevel_addshow and wtBullDivHidden_add)) ? wtBullDivColorUp : na
// RSI Divergences
[rsiFractalTop, rsiFractalBot, rsiLow_prev, rsiBearDiv, rsiBullDiv, rsiBearDivHidden, rsiBullDivHidden] = f_findDivs(rsi, rsiDivOBLevel, rsiDivOSLevel, true)
[rsiFractalTop_nl, rsiFractalBot_nl, rsiLow_prev_nl, rsiBearDiv_nl, rsiBullDiv_nl, rsiBearDivHidden_nl, rsiBullDivHidden_nl] = f_findDivs(rsi, 0, 0, false)
rsiBearDivHidden_ = showHiddenDiv_nl ? rsiBearDivHidden_nl : rsiBearDivHidden
rsiBullDivHidden_ = showHiddenDiv_nl ? rsiBullDivHidden_nl : rsiBullDivHidden
rsiBearColor = #e60000
rsiBullColor = #38ff42
rsiBearDivColor = (rsiShowDiv and rsiBearDiv) or (rsiShowHiddenDiv and rsiBearDivHidden_) ? rsiBearColor : na
rsiBullDivColor = (rsiShowDiv and rsiBullDiv) or (rsiShowHiddenDiv and rsiBullDivHidden_) ? rsiBullColor : na
// Stoch Divergences
[stochFractalTop, stochFractalBot, stochLow_prev, stochBearDiv, stochBullDiv, stochBearDivHidden, stochBullDivHidden] = f_findDivs(stochK, 0, 0, false)
stochbearcolor = #e60000
stochbullcolor = #38ff42
stochBearDivColor = (stochShowDiv and stochBearDiv) or (stochShowHiddenDiv and stochBearDivHidden) ? stochbearcolor : na
stochBullDivColor = (stochShowDiv and stochBullDiv) or (stochShowHiddenDiv and stochBullDivHidden) ? stochbullcolor : na
// VWAP (WT momentum) Divergences
[vwapFractalTop, vwapFractalBot, vwapLow_prev, vwapBearDiv, vwapBullDiv, vwapBearDivHidden, vwapBullDivHidden] = f_findDivs(wtVwap, vwapDivOBLevel, vwapDivOSLevel, true)
vwapBearColor = #e60000
vwapBullColor = #38ff42
// Displayed VWAP divergence conditions (regular and/or hidden according to the settings) - used for the plot AND the alerts
vwapBearDivSignal = (vwapShowDiv and vwapBearDiv) or (vwapShowHiddenDiv and vwapBearDivHidden)
vwapBullDivSignal = (vwapShowDiv and vwapBullDiv) or (vwapShowHiddenDiv and vwapBullDivHidden)
vwapBearDivColor = vwapBearDivSignal ? vwapBearColor : na
vwapBullDivColor = vwapBullDivSignal ? vwapBullColor : na
// Small Circles WT Cross
signalcolorup = input.color(#00e676, 'WT Buy Dot', group = 'Color Settings')
signalcolordown = input.color(#ff5252, 'WT Sell Dot', group = 'Color Settings')
signalColor = wt2 - wt1 > 0 ? signalcolordown : signalcolorup
// BUY / SELL signals {
// WT divergences without OB/OS limit (the distance from the zero line is filtered by the "Signals Settings")
// 1) Regular WT divergence whose pivot is far enough from the zero line
sigBullDiv = wtBullDiv_nl and wtFractalBot_nl <= -sigDivDist
sigBearDiv = wtBearDiv_nl and wtFractalTop_nl >= sigDivDist
// 2) Confirming dot = wt1/wt2 cross (VWAP crossing zero), with the WT far enough from the zero line
sigDotBull = ta.crossover(wtVwap, 0) and wt2 <= -sigDotDist
sigDotBear = ta.crossunder(wtVwap, 0) and wt2 >= sigDotDist
// 3) MFI direction
[sigMfiUp, sigMfiDown] = f_mfiTurn(rsiMFI, rsiMFI2)
var int bullSetupBar = na
var bool bullCrossed = false
var int bearSetupBar = na
var bool bearCrossed = false
// New divergence: the setup (re)starts. The divergence is detected 2 bars after the pivot,
// so a dot during these 2 bars also counts (and the dot on the pivot bar itself if sigPivDot). An opposite divergence cancels the setup in progress.
if sigBullDiv
bullSetupBar := bar_index
bullCrossed := sigDotBull or sigDotBull[1] or (sigPivDot and sigDotBull[2])
bearSetupBar := na
bearCrossed := false
if sigBearDiv
bearSetupBar := bar_index
bearCrossed := sigDotBear or sigDotBear[1] or (sigPivDot and sigDotBear[2])
bullSetupBar := na
bullCrossed := false
// Expired setup
if not na(bullSetupBar) and bar_index - bullSetupBar > sigMaxBars
bullSetupBar := na
bullCrossed := false
if not na(bearSetupBar) and bar_index - bearSetupBar > sigMaxBars
bearSetupBar := na
bearCrossed := false
// Confirming dot after the divergence
if not na(bullSetupBar) and sigDotBull
bullCrossed := true
if not na(bearSetupBar) and sigDotBear
bearCrossed := true
// Signal: complete setup + MFI turning the right way
// 4) Option: regular VWAP divergence in the same direction, between sigVwapLook bars before the WT divergence and the signal
var int vwapBullBar = na
var int vwapBearBar = na
if vwapBullDiv
vwapBullBar := bar_index
if vwapBearDiv
vwapBearBar := bar_index
bool sigVwapOkB = not sigVwapReq or (not na(bullSetupBar) and not na(vwapBullBar) and vwapBullBar >= bullSetupBar - sigVwapLook)
bool sigVwapOkS = not sigVwapReq or (not na(bearSetupBar) and not na(vwapBearBar) and vwapBearBar >= bearSetupBar - sigVwapLook)
// 5) Options: regular RSI and/or Stoch RSI divergence in the same direction (same rule, window sigOscLook)
var int rsiBullBar = na
var int rsiBearBar = na
var int stBullBar = na
var int stBearBar = na
if rsiBullDiv
rsiBullBar := bar_index
if rsiBearDiv
rsiBearBar := bar_index
if stochBullDiv
stBullBar := bar_index
if stochBearDiv
stBearBar := bar_index
bool sigRsiOkB = not sigRsiReq or (not na(bullSetupBar) and not na(rsiBullBar) and rsiBullBar >= bullSetupBar - sigOscLook)
bool sigRsiOkS = not sigRsiReq or (not na(bearSetupBar) and not na(rsiBearBar) and rsiBearBar >= bearSetupBar - sigOscLook)
bool sigStOkB = not sigStochReq or (not na(bullSetupBar) and not na(stBullBar) and stBullBar >= bullSetupBar - sigOscLook)
bool sigStOkS = not sigStochReq or (not na(bearSetupBar) and not na(stBearBar) and stBearBar >= bearSetupBar - sigOscLook)
buyTrigger = not na(bullSetupBar) and bullCrossed and sigMfiUp and sigVwapOkB and sigRsiOkB and sigStOkB
sellTrigger = not na(bearSetupBar) and bearCrossed and sigMfiDown and sigVwapOkS and sigRsiOkS and sigStOkS
// One signal per setup
if buyTrigger
bullSetupBar := na
bullCrossed := false
if sellTrigger
bearSetupBar := na
bearCrossed := false
// } BUY / SELL signals
// Signal grid: one request per slot of the list {
[d1, n1, t1, p1, b1, s1, v1] = f_slot(1)
[d2, n2, t2, p2, b2, s2, v2] = f_slot(2)
[d3, n3, t3, p3, b3, s3, v3] = f_slot(3)
[d4, n4, t4, p4, b4, s4, v4] = f_slot(4)
[d5, n5, t5, p5, b5, s5, v5] = f_slot(5)
[d6, n6, t6, p6, b6, s6, v6] = f_slot(6)
[d7, n7, t7, p7, b7, s7, v7] = f_slot(7)
[d8, n8, t8, p8, b8, s8, v8] = f_slot(8)
[d9, n9, t9, p9, b9, s9, v9] = f_slot(9)
[d10, n10, t10, p10, b10, s10, v10] = f_slot(10)
[d11, n11, t11, p11, b11, s11, v11] = f_slot(11)
[d12, n12, t12, p12, b12, s12, v12] = f_slot(12)
[d13, n13, t13, p13, b13, s13, v13] = f_slot(13)
[d14, n14, t14, p14, b14, s14, v14] = f_slot(14)
[d15, n15, t15, p15, b15, s15, v15] = f_slot(15)
[d16, n16, t16, p16, b16, s16, v16] = f_slot(16)
[d17, n17, t17, p17, b17, s17, v17] = f_slot(17)
[d18, n18, t18, p18, b18, s18, v18] = f_slot(18)
[d19, n19, t19, p19, b19, s19, v19] = f_slot(19)
[d20, n20, t20, p20, b20, s20, v20] = f_slot(20)
[d21, n21, t21, p21, b21, s21, v21] = f_slot(21)
[d22, n22, t22, p22, b22, s22, v22] = f_slot(22)
[d23, n23, t23, p23, b23, s23, v23] = f_slot(23)
[d24, n24, t24, p24, b24, s24, v24] = f_slot(24)
[d25, n25, t25, p25, b25, s25, v25] = f_slot(25)
[d26, n26, t26, p26, b26, s26, v26] = f_slot(26)
[d27, n27, t27, p27, b27, s27, v27] = f_slot(27)
[d28, n28, t28, p28, b28, s28, v28] = f_slot(28)
[d29, n29, t29, p29, b29, s29, v29] = f_slot(29)
[d30, n30, t30, p30, b30, s30, v30] = f_slot(30)
[d31, n31, t31, p31, b31, s31, v31] = f_slot(31)
[d32, n32, t32, p32, b32, s32, v32] = f_slot(32)
[d33, n33, t33, p33, b33, s33, v33] = f_slot(33)
[d34, n34, t34, p34, b34, s34, v34] = f_slot(34)
[d35, n35, t35, p35, b35, s35, v35] = f_slot(35)
[d36, n36, t36, p36, b36, s36, v36] = f_slot(36)
var int NS = 36
array<int> aD = array.from(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16, d17, d18, d19, d20, d21, d22, d23, d24, d25, d26, d27, d28, d29, d30, d31, d32, d33, d34, d35, d36)
array<int> aN = array.from(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16, n17, n18, n19, n20, n21, n22, n23, n24, n25, n26, n27, n28, n29, n30, n31, n32, n33, n34, n35, n36)
array<int> aT = array.from(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32, t33, t34, t35, t36)
array<int> aP = array.from(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33, p34, p35, p36)
array<string> aTf = array.from(f_tf(1), f_tf(2), f_tf(3), f_tf(4), f_tf(5), f_tf(6), f_tf(7), f_tf(8), f_tf(9), f_tf(10), f_tf(11), f_tf(12), f_tf(13), f_tf(14), f_tf(15), f_tf(16), f_tf(17), f_tf(18), f_tf(19), f_tf(20), f_tf(21), f_tf(22), f_tf(23), f_tf(24), f_tf(25), f_tf(26), f_tf(27), f_tf(28), f_tf(29), f_tf(30), f_tf(31), f_tf(32), f_tf(33), f_tf(34), f_tf(35), f_tf(36))
array<int> aCB = array.from(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36)
array<int> aCS = array.from(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, s21, s22, s23, s24, s25, s26, s27, s28, s29, s30, s31, s32, s33, s34, s35, s36)
array<int> aDV = array.from(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36)
int secC = timeframe.in_seconds()
string nmCh = f_tfName(timeframe.period)
// }
// Channel {
// same wt1 / wt2 / MFI as the oscillator above
float wvSrc = wLine == 'WT1 (light wave)' ? wt1 : wt2
bool centered = wvMode == 'Centered (history, repaints)'
float rawUp = ta.highest(wvSrc, envLen)
float rawDn = ta.lowest(wvSrc, envLen)
int wvShift = int(math.round((envLen - 1) / 2.0 + (smLen - 1) / 2.0))
int wvOff = centered ? -wvShift : 0
float wvUp = centered ? ta.sma(rawUp, smLen) : f_ma(rawUp, smLen)
float wvDn = centered ? ta.sma(rawDn, smLen) : f_ma(rawDn, smLen)
int mShift = int(math.round((mfiSm - 1) / 2.0))
int mOff = centered ? -mShift : 0
float mfs = centered ? ta.sma(rsiMFI, mfiSm) : f_ma(rsiMFI, mfiSm)
float mfs2 = centered ? ta.sma(rsiMFI2, mfiSm) : f_ma(rsiMFI2, mfiSm)
// }
// } CALCULATE INDICATORS
// DRAW {
bgcolor(darkMode ? #000000 : na)
// Fixed lines with hline(): they do not count in the 64 plots limit
hline(0, title = 'Zero Line', color = color.new(colorWhite, 50), linestyle = hline.style_solid)
// MFI BAR
// two invisible hlines (outside the 64 plots limit) + fill
rsiMfiBarTopLine = hline(-95, title = 'MFI Bar TOP Line', color = color.new(colorWhite, 100))
rsiMfiBarBottomLine = hline(-99, title = 'MFI Bar BOTTOM Line', color = color.new(colorWhite, 100))
fill(rsiMfiBarTopLine, rsiMfiBarBottomLine, title = 'MFI Bar Colors', color = rsiMFIShow ? color.new(rsiMFIColor, 75) : na)
// WT Areas
colorWT1blue = input.color(#4994ec, 'WT1 Fill', group = 'Color Settings')
colorWT2purple = input.color(#1f1559, 'WT2 Fill', group = 'Color Settings')
plot(wtShow ? wt1 : na, style = plot.style_area, title = 'WT Wave 1', color = color.new(colorWT1blue, 30))
plot(wtShow ? wt2 : na, style = plot.style_area, title = 'WT Wave 2', color = color.new(colorWT2purple, 25))
// VWAP
VWAPColor = input.color(color.new(#ffffff, 50), 'VWAP', group = 'Color Settings')
plot(vwapShow ? wtVwap : na, title = 'VWAP', color = VWAPColor, style = plot.style_area, linewidth = 1)
// MFI AREA
plot(rsiMFIShow and mfiShow1 ? rsiMFI : na, style = plot.style_area, title = 'MFI Cryptoface', color = color.new(rsiMFI > 0 ? rsiMFIColorAbove : rsiMFIColorBelow, 50))
plot(rsiMFIShow and mfiShow2 ? rsiMFI2 : na, style = plot.style_area, title = 'MFI Vumanchu', color = rsiMFI2 > 0 ? mfi2ColA : mfi2ColB)
// CHANNEL + SMOOTHED MFI
pWvUp = plot(wvShow ? wvUp : na, 'Upper band', colUp, lw, offset = wvOff)
pWvDn = plot(wvShow ? wvDn : na, 'Lower band', colDn, lw, offset = wvOff)
plot(wvShow and showMid ? (wvUp + wvDn) / 2 : na, 'Mid-line', color.new(colUp, 50), 1, offset = wvOff)
fill(pWvUp, pWvDn, color = wvShow and showFill ? colFill : na, title = 'Channel Fill')
plot(mfiOn ? mfs : na, 'Smoothed MFI Cryptoface', mfs >= 0 ? colMfU : colMfD, mfW, offset = mOff)
plot(mfiOn2 ? mfs2 : na, 'Smoothed MFI Vumanchu', mfs2 >= 0 ? color.new(mfi2ColA, 20) : color.new(mfi2ColB, 20), mfW, offset = mOff)
// WT Div
plot(not na(wtFractalTop) ? wt2[2] : na, title = 'WT Bearish Divergence', color = wtBearDivColor, linewidth = 1, offset = -2, display = display.pane)
plot(not na(wtFractalBot) ? wt2[2] : na, title = 'WT Bullish Divergence', color = wtBullDivColor, linewidth = 1, offset = -2, display = display.pane)
// WT 2nd Div
plot(not na(wtFractalTop_add) ? wt2[2] : na, title = 'WT 2nd Bearish Divergence', color = wtBearDivColor_add, linewidth = 1, offset = -2, display = display.pane)
plot(not na(wtFractalBot_add) ? wt2[2] : na, title = 'WT 2nd Bullish Divergence', color = wtBullDivColor_add, linewidth = 1, offset = -2, display = display.pane)
// RSI
plot(rsiShow ? rsi : na, title = 'RSI', color = color.new(rsiColor, 25), linewidth = 1)
// RSI Div
plot(not na(rsiFractalTop) ? rsi[2] : na, title = 'RSI Bearish Divergence', color = rsiBearDivColor, linewidth = 1, offset = -2, display = display.pane)
plot(not na(rsiFractalBot) ? rsi[2] : na, title = 'RSI Bullish Divergence', color = rsiBullDivColor, linewidth = 1, offset = -2, display = display.pane)
// Stochastic RSI
stochkcolor = input.color(color.new(#21baf3, 70), 'Stoch K', group = 'Color Settings')
stochdcolor = input.color(color.new(#673ab7, 90), 'Stoch D', group = 'Color Settings')
stochKplot = plot(stochShow ? stochK : na, title = 'Stoch K', color = stochkcolor, linewidth = 1)
stochDplot = plot(stochShow ? stochD : na, title = 'Stoch D', color = stochdcolor, linewidth = 1)
stochFillColor = stochK >= stochD ? color.new(#21baf3, 95) : color.new(#673ab7, 90)
fill(stochKplot, stochDplot, title = 'KD Fill', color = stochFillColor)
// Stoch Div
plot(not na(stochFractalTop) ? stochK[2] : na, title = 'Stoch Bearish Divergence', color = stochBearDivColor, linewidth = 1, offset = -2, display = display.pane)
plot(not na(stochFractalBot) ? stochK[2] : na, title = 'Stoch Bullish Divergence', color = stochBullDivColor, linewidth = 1, offset = -2, display = display.pane)
// VWAP Div
plot(not na(vwapFractalTop) ? wtVwap[2] : na, title = 'VWAP Bearish Divergence', color = vwapBearDivColor, linewidth = 1, offset = -2, display = display.pane)
plot(not na(vwapFractalBot) ? wtVwap[2] : na, title = 'VWAP Bullish Divergence', color = vwapBullDivColor, linewidth = 1, offset = -2, display = display.pane)
// Draw Overbought & Oversold lines
oblvl2color = #ffffff
oblvl3color = #ffffff
oslvl2color = #ffffff
hline(obLevel2, title = 'Over Bought Level 2', color = color.new(oblvl2color, 85), linestyle = hline.style_solid)
hline(obLevel3, title = 'Over Bought Level 3', color = color.new(oblvl3color, 95), linestyle = hline.style_dotted)
hline(osLevel2, title = 'Over Sold Level 2', color = color.new(oslvl2color, 85), linestyle = hline.style_solid)
// Circles
plot(wtCross ? wt2 : na, title = 'Buy and sell circle', color = color.new(signalColor, 15), style = plot.style_circles, linewidth = 3, display = display.pane)
float sigAtr = ta.atr(14)
// Signals of all TFs (labels: they do not count in the 64 plots limit) {
// one triangle per TF and per direction on each chart candle, in the pane and on the price chart
var array<int> prevCB = array.new<int>(NS, na)
var array<int> prevCS = array.new<int>(NS, na)
int alMinSec = timeframe.in_seconds(alMin)
int chRatio = chRatioS == 'No filter' ? 0 : int(str.tonumber(str.substring(chRatioS, 1)))
int chMinSec = math.max(timeframe.in_seconds(chMin), chLower ? 0 : secC, chRatio > 0 ? int(secC / chRatio) : 0)
int chTxSec = timeframe.in_seconds(chTxMin)
int chBigSec = timeframe.in_seconds(chBig)
// signals to draw on this candle: TF duration, direction, count inside the chart candle, TF name, chart TF or not
array<float> eSec = array.new<float>()
array<int> eDir = array.new<int>()
array<int> eCnt = array.new<int>()
array<string> eNm = array.new<string>()
array<bool> eCh = array.new<bool>()
for i = 0 to NS - 1
string tf = array.get(aTf, i)
if tf != ''
int sec = timeframe.in_seconds(tf)
int cb = array.get(aCB, i)
int cs = array.get(aCS, i)
int pb = array.get(prevCB, i)
int ps = array.get(prevCS, i)
int nB = na(cb) or na(pb) ? 0 : cb - pb
int nS = na(cs) or na(ps) ? 0 : cs - ps
if alOn and sec >= alMinSec
if nB > 0
alert('BUY signal on TF ' + f_tfName(tf) + (nB > 1 ? ' (x' + str.tostring(nB) + ')' : '') + ' - ' + syminfo.ticker, alert.freq_once_per_bar)
if nS > 0
alert('SELL signal on TF ' + f_tfName(tf) + (nS > 1 ? ' (x' + str.tostring(nS) + ')' : '') + ' - ' + syminfo.ticker, alert.freq_once_per_bar)
// the chart TF is drawn from the chart's own calculation below (no delay)
bool drawOk = sigShow and chShow and sec != secC and sec >= chMinSec and f_tierOn(f_tier(sec))
if drawOk and nB > 0
array.push(eSec, float(sec))
array.push(eDir, 1)
array.push(eCnt, nB)
array.push(eNm, f_tfName(tf))
array.push(eCh, false)
if drawOk and nS > 0
array.push(eSec, float(sec))
array.push(eDir, -1)
array.push(eCnt, nS)
array.push(eNm, f_tfName(tf))
array.push(eCh, false)
if not na(cb)
array.set(prevCB, i, cb)
if not na(cs)
array.set(prevCS, i, cs)
if sigShow and sigChartShow and (buyTrigger or sellTrigger)
array.push(eSec, float(secC))
array.push(eDir, buyTrigger ? 1 : -1)
array.push(eCnt, 1)
array.push(eNm, nmCh)
array.push(eCh, true)
int nE = array.size(eSec)
if nE > 0
bool stk = chStack == 'Stacked'
array<int> o = array.sort_indices(eSec, order.ascending)
for dir = -1 to 1 by 2
// signals of this direction, from the smallest TF to the largest
array<int> lst = array.new<int>()
string allNm = ''
for k = 0 to nE - 1
int i = array.get(o, k)
if array.get(eDir, i) == dir
array.push(lst, i)
int c0 = array.get(eCnt, i)
string one = array.get(eNm, i) + (c0 > 1 ? ' ×' + str.tostring(c0) : '')
allNm := allNm == '' ? one : one + ' ' + allNm
int nL = array.size(lst)
if nL > 0
// drawn from the largest TF to the smallest: the smallest is created last, so it is visible on top
for j = nL - 1 to 0
int i = array.get(lst, j)
int sec = int(array.get(eSec, i))
int cnt = array.get(eCnt, i)
bool isCh = array.get(eCh, i)
string nm = array.get(eNm, i) + (cnt > 1 ? ' ×' + str.tostring(cnt) : '')
int tr = f_tier(sec)
color c = color.new(isCh ? (dir > 0 ? sigChColB : sigChColS) : sec >= chBigSec ? (dir > 0 ? chBigB : chBigS) : (dir > 0 ? chColB : chColS), math.min(f_tierTr(tr) + (dir > 0 ? chBuyTrX : 0), 100))
color tc = color.new(c, math.max(color.t(c) - 10, 0))
string sz = isCh and sigChSz != 'TF class' ? f_szStr(sigChSz) : chRelSz ? f_relSz(sec, secC) : f_tierSz(tr)
int pos = stk ? 0 : j
string txt = chText == 'No text' or sec < chTxSec ? '' : stk ? (j == nL - 1 ? allNm : '') : nm
string tip = (dir > 0 ? 'BUY ' : 'SELL ') + (stk ? allNm : nm) + (isCh ? ' (chart TF)' : '')
string st = dir > 0 ? label.style_triangleup : label.style_triangledown
// BUY: text under the triangle (TradingView writes it above by default) = triangle without text + text label
bool bTx = dir > 0 and txt != ''
string tTri = bTx ? '' : txt
string tLow = str.repeat('\n', chBuyTx) + txt
float yPa = (dir > 0 ? -1 : 1) * (paBase + paStep * pos)
float dy = (chAtr + chStep * pos) * sigAtr
float yPr = dir > 0 ? low - dy : high + dy
if sigOnPane
label.new(bar_index, yPa, tTri, style = st, color = c, textcolor = tc, size = sz, tooltip = tip)
if bTx
label.new(bar_index, yPa, tLow, style = label.style_label_up, color = color.new(c, 100), textcolor = tc, size = sz, tooltip = tip)
if sigOnPrice
label.new(bar_index, yPr, tTri, style = st, color = c, textcolor = tc, size = sz, tooltip = tip, force_overlay = true)
if bTx
label.new(bar_index, yPr, tLow, style = label.style_label_up, color = color.new(c, 100), textcolor = tc, size = sz, tooltip = tip, force_overlay = true)
// }
// Signal grid table (does not count in the 64 plots limit) {
// at most 4 blocks × 5 columns; 1 header + 36 rows + 1 summary row
var table sigTab = tabOnPrice ? table.new(f_tabPos(tabPos), 20, 40, border_width = 1, frame_width = 1, frame_color = color.new(#787b86, 50), border_color = color.new(#787b86, 70), force_overlay = true) : table.new(f_tabPos(tabPos), 20, 40, border_width = 1, frame_width = 1, frame_color = color.new(#787b86, 50), border_color = color.new(#787b86, 70))
if tabShow and barstate.islast
table.clear(sigTab, 0, 0, 19, 39)
color hdBg = color.new(#131722, 10)
color rwBg = color.new(#131722, 25)
color txt = color.new(#ffffff, 0)
color dim = color.new(#ffffff, 50)
string tsz = f_tabSz(tabSize)
// signals too old are removed (shown as "no signal")
array<int> aDv = array.copy(aD)
if tabMaxOn
for i = 0 to NS - 1
int nn = array.get(aN, i)
int tt = array.get(aT, i)
bool old = tabMaxU == 'hours (all TFs)' ? (not na(tt) and timenow - tt > tabMax * 3600000) : (not na(nn) and nn > tabMax)
if old
array.set(aDv, i, 0)
// displayed TFs, sorted by duration
array<float> aSec = array.new<float>(NS, -1.0)
for i = 0 to NS - 1
string tf = array.get(aTf, i)
bool keep = tf != '' and (not tabOnlySig or array.get(aDv, i) != 0 or array.get(aP, i) != 0)
if keep
array.set(aSec, i, float(timeframe.in_seconds(tf)))
// sort key: TF duration, or close time of the last signal (+ tiny TF term so ties stay largest first)
array<float> aKey = array.copy(aSec)
if tabOrder == 'Most recent signal first'
for i = 0 to NS - 1
float sc0 = array.get(aSec, i)
int tt = array.get(aT, i)
if sc0 >= 0
array.set(aKey, i, array.get(aDv, i) != 0 and not na(tt) ? float(tt) + sc0 / 1e7 : sc0 - 1e13)
array<int> ord = array.sort_indices(aKey, order.descending)
array<int> shown = array.new<int>()
for k = 0 to NS - 1
int i = array.get(ord, k)
if array.get(aSec, i) >= 0
array.push(shown, i)
if tabOrder == 'Smallest first'
array.reverse(shown)
int nSh = array.size(shown)
// columns of a block: TF | Signal | Bars | Ago | Setup (unchecked columns are skipped)
int cBars = 2
int cAge = tabColBars ? 3 : 2
int cSetup = cAge + (tabColAge ? 1 : 0)
int nc = cSetup + (tabColSetup ? 1 : 0)
int nb = math.max(1, math.min(tabBlocks, nSh))
int perB = math.max(1, int(math.ceil(nSh / nb)))
int nRecB = 0
int nRecS = 0
for b = 0 to nb - 1
int x = b * nc
table.cell(sigTab, x, 0, 'TF', text_color = txt, bgcolor = hdBg, text_size = tsz)
table.cell(sigTab, x + 1, 0, 'Signal', text_color = txt, bgcolor = hdBg, text_size = tsz)
if tabColBars
table.cell(sigTab, x + cBars, 0, 'Bars', text_color = txt, bgcolor = hdBg, text_size = tsz)
if tabColAge
table.cell(sigTab, x + cAge, 0, 'Ago', text_color = txt, bgcolor = hdBg, text_size = tsz)
if tabColSetup
table.cell(sigTab, x + cSetup, 0, 'Setup', text_color = txt, bgcolor = hdBg, text_size = tsz)
if nSh > 0
for k = 0 to nSh - 1
int i = array.get(shown, k)
int x = int(k / perB) * nc
int row = k % perB + 1
int d = array.get(aDv, i)
int n = array.get(aN, i)
int tm = array.get(aT, i)
int p = array.get(aP, i)
int dvb = array.get(aDV, i)
bool isCh = array.get(aSec, i) == secC
bool fresh = d != 0 and not na(n) and n <= tabFresh
if fresh
nRecB += d > 0 ? 1 : 0
nRecS += d < 0 ? 1 : 0
color sc = d > 0 ? tabColB : tabColS
color sBg = d == 0 ? rwBg : fresh ? color.new(sc, 15) : color.new(sc, 80)
color sTx = d == 0 ? dim : fresh ? color.new(#000000, 0) : sc
// the chart TF is shown in yellow
table.cell(sigTab, x, row, f_tfName(array.get(aTf, i)), text_color = isCh ? #FFC400 : txt, bgcolor = rwBg, text_size = tsz)
table.cell(sigTab, x + 1, row, d > 0 ? 'BUY ▲' : d < 0 ? 'SELL ▼' : '—', text_color = sTx, bgcolor = sBg, text_size = tsz)
if tabColBars
table.cell(sigTab, x + cBars, row, d != 0 and not na(n) ? str.tostring(n) : '', text_color = d != 0 ? txt : dim, bgcolor = rwBg, text_size = tsz)
if tabColAge
table.cell(sigTab, x + cAge, row, d != 0 and not na(tm) ? f_age(math.max(0, timenow - tm)) : '', text_color = d != 0 ? txt : dim, bgcolor = rwBg, text_size = tsz)
if tabColSetup
string pt = p == 1 ? 'Div ▲' : p == 2 ? 'Div+Dot ▲' : p == -1 ? 'Div ▼' : p == -2 ? 'Div+Dot ▼' : ''
// divergences of the other oscillators already found for this setup
if p != 0 and dvb > 0
pt += ' ' + str.trim((dvb % 2 == 1 ? 'VWAP ' : '') + (int(dvb / 2) % 2 == 1 ? 'RSI ' : '') + (int(dvb / 4) % 2 == 1 ? 'Stoch' : ''))
color pc = p > 0 ? tabColB : p < 0 ? tabColS : dim
table.cell(sigTab, x + cSetup, row, pt, text_color = pc, bgcolor = rwBg, text_size = tsz)
// summary row: number of TFs with a recent signal
if tabSummary
int ry = perB + 1
table.cell(sigTab, 0, ry, 'Recent', text_color = txt, bgcolor = hdBg, text_size = tsz)
table.cell(sigTab, 1, ry, str.tostring(nRecB) + ' ▲ ' + str.tostring(nRecS) + ' ▼', text_color = nRecB > nRecS ? tabColB : nRecS > nRecB ? tabColS : txt, bgcolor = hdBg, text_size = tsz)
if nSh == 0
table.cell(sigTab, 2, ry, 'no TF', text_color = dim, bgcolor = hdBg, text_size = tsz)
// }
// Extended lines: start from the last displayed point of each curve
var array<line> wvLines = array.new<line>()
if barstate.islast
for l in wvLines
line.delete(l)
array.clear(wvLines)
if extOn and extBars > 0
string exSt = f_lineStyle(extSty)
if wvShow
int wEnd = bar_index + wvOff
float sU = (wvUp - wvUp[extLook]) / extLook
float sD = (wvDn - wvDn[extLook]) / extLook
array.push(wvLines, line.new(wEnd, wvUp, wEnd + extBars, wvUp + sU * extBars, color = colUp, width = extW, style = exSt))
array.push(wvLines, line.new(wEnd, wvDn, wEnd + extBars, wvDn + sD * extBars, color = colDn, width = extW, style = exSt))
if showMid
float mid = (wvUp + wvDn) / 2
array.push(wvLines, line.new(wEnd, mid, wEnd + extBars, mid + (sU + sD) / 2 * extBars, color = color.new(colUp, 50), width = 1, style = exSt))
if mfiOn
int mEnd = bar_index + mOff
float sM = (mfs - mfs[extLook]) / extLook
float yF = mfs + sM * extBars
array.push(wvLines, line.new(mEnd, mfs, mEnd + extBars, yF, color = yF >= 0 ? colMfU : colMfD, width = extW, style = exSt))
if mfiOn2
int mEnd2 = bar_index + mOff
float sM2 = (mfs2 - mfs2[extLook]) / extLook
float yF2 = mfs2 + sM2 * extBars
array.push(wvLines, line.new(mEnd2, mfs2, mEnd2 + extBars, yF2, color = yF2 >= 0 ? color.new(mfi2ColA, 20) : color.new(mfi2ColB, 20), width = extW, style = exSt))
// } DRAW
// ALERTS {
// VWAP DIVERGENCES
// Fire when a VWAP divergence is displayed (regular, and hidden if enabled in the settings).
// The divergence is confirmed 2 bars after the pivot (fractal), as for the plot.
alertcondition(vwapBullDivSignal, 'VWAP Bullish Divergence', 'Bullish divergence on the VWAP (WT momentum) - {{ticker}} {{interval}}')
alertcondition(vwapBearDivSignal, 'VWAP Bearish Divergence', 'Bearish divergence on the VWAP (WT momentum) - {{ticker}} {{interval}}')
// BUY / SELL SIGNALS
alertcondition(buyTrigger, 'Signal BUY', 'BUY signal: divergence + dot far from the zero line + MFI rising - {{ticker}} {{interval}}')
alertcondition(sellTrigger, 'Signal SELL', 'SELL signal: divergence + dot far from the zero line + MFI falling - {{ticker}} {{interval}}')
// alert() ALERTS: they do not count in the 64 plots limit.
// To receive them: create ONE alert on the indicator with the condition "Any alert() function call".
// The signals of each TF of the list are sent above (section "Signals of all TFs").
if alDots and wtCross and wtCrossUp
alert('Small green dot (bullish WT cross) - ' + syminfo.ticker, alert.freq_once_per_bar)
if alDots and wtCross and wtCrossDown
alert('Small red dot (bearish WT cross) - ' + syminfo.ticker, alert.freq_once_per_bar)
// } ALERTS