Contrarian Indicator

RickyTSpanish · study · 439 行 · 点赞 24 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// Contrarian Indicator (was "Fear & Greed divergence"), the same rules as the Sniper Charts indicator on srus.dev:
// divergences between price and the crypto Fear & Greed index on the DAILY highs and lows, so every timeframe shows
// the same reads as the daily chart, and Money Flow (MFI on this chart's candles) as a cloud from 50:
// white = money coming in, purple = money going out, brightest where the flow peaks.
//   Bull    lower low in price, higher F&G          Bear    higher high in price, lower F&G
//   H Bull  higher (or equal) low, lower F&G        H Bear  lower (or equal) high, higher F&G
// Pane: Bull / Bear tags, hidden ones as hollow diamonds. Price chart: diamonds (filled = regular, hollow = hidden).
// Every piece has its own switch in the settings and every colour its own picker (Ricky, 28 Oct 2026).
// 29 Oct 2026, the same updates as Sniper Charts: Money Flow smoothing, and big-money beads on the Money Flow line (the
// biggest money candle of each push, the push's way), coloured by the higher timeframe's Money Flow (last closed higher
// candle): green = money coming in up there, pink = going out. TradingView draws straight lines between bars, so the
// curved "flow" look exists only on Sniper Charts; the shading by strength is the same here.
// Idea: Crypto Ryan (x.com/CryptoRyan17). Fear & Greed data: alternative.me (keep that credit next to the number).
// Pine cannot call alternative.me, so the daily index is stored in this file: first day 17563, last print day 20754
// (51). Later days hold that last print until the file is refreshed.
// Sniper Charts under 4H can use a live estimate of today's index; here every timeframe uses the published prints.
indicator("Contrarian Indicator", "Contrarian", overlay=false, max_lines_count=500, max_labels_count=500)
 
FNG_0 = "30,15,40,24,11,8,36,30,44,54,31,42,35,55,71,67,74,63,67,74,54,44,39,31,33,37,44,41,38,47,56,44,55,59,37,39,37,39,40,41,41,40,32,33,31,29,29,37,36,36,28,32,30,31,24,24,18,12,16,16,11,22,22,17,19,20,17,21,18,20,18,23,23,23,23,26,24,25,26,32,31,28,29,64,47,55,54,61,59,56,52,55,56,63,67,56,62,53,63,41,44,40,40,40,32,31,37,31,32,41,30,26,27,25,23,19,22,16,38,25,24,27,40,41,26,42,38,40,39,24,15,19,19,17,26,22,23,27,32,34,37,28,17,15,16,21,18,20,16,22,27,27,31,33,37,34,34,38,39,37,29,33,29,29,32,36,39,42,44,47,43,46,44,49,54,53,47,54,54,53,48,39,39,36,31,23,25,25,23,19,21,18,18,21,16,18,21,19,24,27,26,19,21,18,19,22,19,18,19,19,22,17,21,18,19,26,17,14,17,18,13,15,18,14,20,23,24,28,25,21,24,24,31,35,38,43,37,37,42,42,37,34,35,33,36,29,37,34,29,26,31,28,19,13,15,18,20,24,23,26,24,21,21,27,24,23,25,29,33,35,34,31,32,29,36,36,41,42,42,48,51,47,52,54,52,56,49,28,23,24,26,28,21,15,14,12,15,9,17,11,14,18,19,13,15,17,12,19,13,11,11,14,19,15,14,14,10,11,13,17,23,21,27,35,28,31,25,33,26,29,21,24,23,26,24,30,33,48,36,31,39,39,42,37,19,22,21,16,27,24,28,29,31,35,30,27,33,37,35,41,39,35,21,22,17,23,22,19,27,21,14,18,37,42,40,46,38,48,48,43,43,38,63,65,59,59,61,63,69,47,40,39,39,42,41,44,36,35,42,56"
FNG_1 = "54,55,55,56,56,54,55,55,54,58,56,56,55,62,56,50,44,46,43,44,49,50,57,56,62,60,71,61,59,65,69,65,64,62,65,42,62,51,60,50,61,64,61,62,62,61,68,65,58,41,42,40,42,42,51,50,63,66,67,57,69,63,69,71,76,75,78,78,77,75,65,67,70,73,68,69,65,64,69,67,70,71,71,73,61,62,63,66,42,27,34,27,62,62,46,61,60,63,67,75,80,84,83,82,81,84,83,84,80,87,95,92,62,74,78,65,63,79,76,67,72,67,74,84,83,62,33,65,61,16,34,19,40,42,34,42,42,40,20,42,24,47,16,19,22,31,57,61,61,62,64,66,45,61,60,59,45,48,45,11,13,31,20,14,30,39,11,5,33,39,33,41,30,32,20,24,20,24,28,41,43,41,43,39,43,41,41,38,39,38,39,38,38,41,38,31,41,37,37,41,39,15,12,24,32,33,27,38,39,37,30,31,32,27,39,37,41,39,38,38,37,39,40,40,40,41,37,37,39,33,20,24,53,50,52,54,53,50,49,51,56,49,54,53,54,42,38,39,40,39,38,41,38,41,38,38,32,32,30,20,23,21,17,21,20,32,31,38,25,28,28,24,21,29,32,28,32,26,20,23,22,27,21,24,23,15,21,23,23,20,33,25,22,39,38,37,37,40,38,37,39,38,38,39,41,40,51,44,41,50,45,49,56,54,55,54,51,53,48,49,52,49,40,41,42,50,54,57,57,55,57,57,59,56,53,61,56,56,56,57,52,61,65,63,64,59,49,53,50,44,44,43,46,46,44,41,39,40,38,39,38,38,40,41,39,38,33,17,16,17,14,10,8,12,9,8,11,12,9,9,11,10,12,13,14,12,8,12,10,12,12,14,14,12,12,12,20,21,22,15"
FNG_2 = "15,10,11,15,18,13,15,18,16,15,17,19,19,20,24,21,28,26,26,44,40,40,45,44,40,42,49,55,56,48,40,39,41,40,44,41,40,50,50,52,49,42,40,43,41,39,39,41,48,48,51,50,56,48,54,53,54,54,53,52,54,52,38,38,40,37,39,38,40,39,38,37,38,41,50,43,40,43,40,41,44,42,42,41,40,38,40,43,44,44,41,44,41,43,43,44,43,41,44,41,44,44,50,55,53,55,55,58,76,71,76,75,75,80,75,72,75,79,77,77,79,78,84,75,75,78,79,82,83,82,80,75,81,78,76,78,75,76,75,74,79,75,75,75,83,79,40,41,41,41,41,38,38,41,41,38,39,47,43,48,49,48,52,48,39,43,39,46,45,47,43,45,49,45,41,40,42,42,47,43,46,48,53,55,52,56,53,56,52,56,55,55,56,61,73,74,73,76,75,61,70,67,74,73,72,71,71,74,72,90,88,82,90,90,86,87,89,90,86,90,86,91,94,86,91,94,90,88,94,93,86,87,89,88,95,92,92,92,93,95,94,95,86,94,89,90,91,95,91,92,92,95,93,92,92,88,93,86,94,93,91,92,91,91,95,94,94,93,94,93,95,91,93,93,94,90,84,78,83,88,84,79,79,80,78,75,40,74,70,74,71,78,55,77,76,78,77,76,78,80,81,84,86,83,95,92,93,92,92,95,93,95,95,91,93,91,91,94,94,76,79,55,56,55,38,78,78,84,77,77,76,81,81,68,73,70,74,78,76,71,71,72,71,75,73,70,66,65,60,54,65,74,72,72,76,74,74,73,74,71,75,72,73,70,70,76,74,74,75,79,78,76,79,74,73,73,65,55,37,31,27,50,59,52,51,68,66,61,68,48,65,64,67,73,72,61,68,31,26,27"
FNG_3 = "20,27,21,23,11,19,12,14,10,22,22,27,21,18,10,18,20,23,24,27,24,17,15,13,14,21,21,28,23,28,38,33,26,25,23,21,23,10,14,22,27,20,22,25,25,28,28,21,24,27,29,20,28,20,20,20,20,25,20,21,20,22,15,19,24,19,10,21,23,22,27,26,32,50,50,53,60,60,48,48,42,50,52,69,74,65,71,70,70,70,76,71,72,72,73,70,70,78,76,79,79,73,75,71,78,72,73,73,71,74,74,72,73,79,79,47,45,46,31,32,44,30,49,53,48,50,53,50,27,21,27,33,28,27,26,25,24,20,27,54,49,54,59,68,76,74,72,71,71,78,70,70,71,78,79,78,75,82,84,75,74,73,72,76,73,66,70,73,74,74,73,76,73,73,71,73,75,84,75,77,74,72,74,72,71,52,54,34,43,49,50,33,42,32,47,21,27,33,40,34,32,31,25,18,16,25,28,29,24,16,27,28,21,28,29,23,24,29,25,27,45,34,41,39,37,40,41,27,22,28,21,29,29,23,24,15,18,10,23,23,21,22,21,21,23,21,22,24,24,24,19,13,11,13,12,23,20,24,24,29,20,26,28,20,20,33,37,45,48,54,50,50,44,44,46,46,51,52,30,25,27,25,20,25,23,27,26,26,20,51,52,39,33,22,22,23,21,22,28,22,22,21,23,21,24,27,25,28,31,30,26,31,40,47,51,49,60,56,55,52,50,52,48,52,53,48,34,37,30,34,32,20,25,28,22,28,28,24,27,27,27,26,24,24,23,27,21,24,23,20,22,28,27,21,27,22,23,18,11,10,12,12,10,9,10,14,8,12,13,13,13,14,10,12,11,12,12,13,14,10,16,17,13,10,14,10,13,15,17,11,13,12,14,11,8,7,7,9,6,6"
FNG_4 = "9,9,11,11,11,14,14,12,10,13,11,11,14,11,14,19,18,18,20,24,24,22,16,15,18,15,21,24,20,30,31,34,33,31,30,30,26,28,32,39,42,39,33,31,34,30,31,31,30,30,42,31,41,42,46,47,45,44,41,30,33,29,27,29,28,25,25,27,28,28,24,27,23,20,25,21,20,23,22,24,20,22,28,26,25,34,27,28,20,22,27,21,23,23,22,20,24,24,21,20,20,22,21,20,24,24,20,25,26,23,24,22,22,24,20,20,24,24,24,20,22,23,23,23,20,23,22,20,33,32,30,34,34,31,30,30,30,30,38,40,33,31,29,22,25,21,22,24,22,23,20,23,23,24,21,22,22,20,20,22,26,28,26,29,27,27,27,26,26,25,29,25,26,27,26,27,27,30,31,29,28,26,29,29,26,28,27,29,29,28,27,28,28,28,25,26,27,26,29,29,26,25,25,25,26,26,30,31,46,52,45,51,52,45,51,53,53,50,52,51,54,55,52,55,61,51,56,60,60,58,58,56,54,58,55,48,49,50,48,50,53,62,61,60,60,58,60,59,56,53,52,51,50,53,50,51,50,50,47,48,49,50,44,34,33,33,49,56,50,52,51,64,63,66,68,62,57,61,64,64,64,59,57,60,63,61,63,63,62,62,63,64,61,61,62,68,65,61,68,68,68,69,58,63,52,50,53,56,53,53,56,59,64,64,60,63,55,64,64,61,60,64,60,51,52,52,49,48,48,50,54,50,51,48,48,52,49,50,50,51,49,48,50,52,51,51,52,50,53,52,53,44,53,50,50,49,47,47,45,46,41,47,47,49,47,49,59,65,65,62,64,55,59,62,54,56,59,63,62,64,61,56,55,58,55,56,57,64,57,60,56,57,54,56,50,56,50,52,54,55"
FNG_5 = "50,51,51,52,52,52,50,53,53,52,54,50,49,49,54,50,53,51,54,54,50,53,52,50,37,39,37,38,37,37,41,39,38,38,39,39,49,52,40,39,40,40,40,42,41,46,41,40,40,30,41,45,45,43,46,46,46,47,47,43,47,44,47,46,44,46,48,47,48,50,50,49,48,50,49,50,50,50,47,45,44,47,45,47,52,50,52,53,63,53,53,66,72,71,70,65,72,68,66,66,72,65,68,70,74,68,66,69,70,70,73,72,69,60,70,63,69,66,69,71,62,66,66,73,73,66,68,72,74,71,74,73,74,75,72,72,72,73,74,74,67,65,72,70,67,73,65,73,74,70,74,70,71,73,71,73,73,65,68,67,65,71,70,68,72,70,71,71,76,73,76,71,64,60,52,64,60,63,51,52,56,55,50,48,52,49,55,54,55,61,60,63,63,60,60,60,64,62,66,72,74,71,70,79,74,72,72,76,72,75,72,78,74,76,72,74,72,79,82,80,80,80,83,82,90,75,82,81,84,79,82,81,81,88,83,81,79,77,79,74,78,75,73,74,75,81,83,80,79,75,75,79,79,71,70,79,75,78,76,80,78,76,79,72,72,74,65,67,57,66,66,72,73,71,72,72,70,67,65,67,67,54,43,48,67,69,71,68,64,55,66,53,56,57,66,64,70,74,73,72,70,76,76,76,74,76,75,74,72,72,73,73,72,73,73,73,75,78,77,72,75,72,74,72,70,74,74,71,71,74,64,60,63,53,55,51,30,46,40,47,30,47,53,51,50,44,29,26,29,28,27,28,29,25,25,33,52,65,69,61,60,74,74,70,71,69,68,68,72,71,74,67,61,52,57,37,34,26,17,29,20,48,40,39,25,31,30,29,27,25,31,28,30,26,39,34,56,54,55,48"
FNG_6 = "30,29,34,29,26,26,26,27,29,22,23,29,26,33,37,31,32,50,51,39,33,45,49,54,54,52,50,54,59,50,61,64,63,61,50,42,37,41,49,50,50,49,49,39,32,49,50,48,65,73,71,73,72,73,72,70,71,69,72,72,74,72,72,77,77,75,72,74,70,70,70,77,75,75,78,76,80,84,88,80,86,90,83,90,83,82,94,93,80,82,79,75,77,78,84,81,80,76,78,84,72,75,79,78,78,74,83,76,83,80,83,87,81,75,74,73,73,70,73,73,79,74,72,73,65,64,66,70,74,73,72,76,78,70,69,50,69,62,61,63,70,75,75,77,77,76,76,84,75,75,75,73,71,72,72,70,76,68,60,44,72,54,49,44,44,46,43,47,46,50,48,50,54,51,47,44,49,55,49,50,49,25,21,10,16,20,26,33,15,20,25,34,28,27,20,24,34,45,27,46,30,32,34,32,49,31,32,30,45,46,47,40,44,26,32,34,34,44,25,28,30,34,23,24,18,39,25,43,45,31,38,29,30,33,32,37,39,47,72,63,60,65,61,54,60,56,53,67,65,64,52,59,67,65,73,70,70,70,70,73,70,71,74,74,74,71,70,72,78,66,74,73,74,71,74,60,50,56,64,64,62,57,45,52,62,62,71,72,71,61,63,60,61,68,52,57,54,49,42,47,65,66,74,65,65,68,66,64,63,73,73,67,66,73,65,66,71,71,79,74,74,73,70,74,73,74,72,71,72,74,71,70,72,73,75,73,74,72,65,55,53,64,60,54,62,74,67,69,70,68,73,75,60,56,64,60,56,44,50,50,60,53,47,48,51,48,50,39,48,46,49,55,51,48,48,44,51,48,49,54,57,52,55,53,52,53,52,53,48,49,45,43,44,44,28,33,37,50,50,49"
FNG_7 = "64,63,71,74,71,70,60,70,64,27,24,38,38,34,28,22,23,29,29,34,25,27,30,37,40,51,50,51,34,29,33,37,42,21,23,27,24,20,22,29,26,24,15,16,10,10,14,11,15,11,14,11,13,19,20,15,22,25,28,28,24,23,28,26,28,23,20,20,22,26,29,29,23,21,16,11,16,17,16,20,20,25,24,24,23,20,23,24,24,23,21,20,28,29,25,26,44,42,28,27,25,29,27,26,48,61,49,50,49,44,32,24,20,24,25,25,20,29,29,26,16,20,14,14,17,14,12,9,6,7,14,9,11,5,9,9,8,12,10,8,9,7,8,9,5,8,11,11,13,11,14,10,14,10,22,18,12,12,8,13,15,18,15,16,15,23,28,26,23,11,12,10,8,11,14,10,13,12,9,8,11,8,12,9,11,12,13,11,17,14,16,15,16,12,21,23,23,21,26,27,29,33,32,46,39,31,33,47,33,26,29,26,39,47,40,50,46,47,38,38,47,48,49,42,34,43,31,27,28,25,27,29,28,28,25,30,34,25,22,23,23,28,29,23,11,12,12,12,12,8,10,9,12,12,13,18,20,23,22,15,14,23,23,20,23,17,12,13,15,18,12,15,11,19,21,22,23,24,27,20,22,23,26,26,28,22,25,25,27,25,28,29,25,33,31,28,27,26,30,29,29,28,25,27,27,28,25,27,25,29,30,31,30,29,27,29,29,34,34,31,41,46,62,72,71,66,73,74,65,71,73,68,69,62,69,63,65,74,73,73,71,69,66,69,56,63,61,57,69,51,50,56,71,71,70,78,71,71,71,74,70,74,73,71,74,72,67,65,70,73,71,64,59,64,61,69,57,62,53,63,55,53,59,55,63,68,50,51,54,50,48,51"
int FIRST_DAY = 17563
string FNG_CSV = FNG_0 + "," + FNG_1 + "," + FNG_2 + "," + FNG_3 + "," + FNG_4 + "," + FNG_5 + "," + FNG_6 + "," + FNG_7
 
var int[] FG = array.new_int()
if barstate.isfirst
    for part in str.split(FNG_CSV, ",")
        array.push(FG, int(str.tonumber(part)))
 
int LAST_DAY = FIRST_DAY + array.size(FG) - 1
 
// The index published for a UTC day (days after the last stored print hold that print).
fngAt(int day) =>
    int n = array.size(FG)
    int i = day - FIRST_DAY
    n == 0 or i < 0 ? float(na) : float(array.get(FG, math.min(i, n - 1)))
 
// The print at the close of a day (published at the next 00:00 UTC): what the line shows on that daily candle.
fngEod(int day) =>
    day + 1 <= LAST_DAY ? fngAt(day + 1) : fngAt(day)
 
// The index for a candle, as on Sniper Charts: up to a day long, the last print by the time the candle ends;
// longer candles (1W, 1M) the average of the daily prints inside them.
candleFg() =>
    int endMs = na(time_close) ? timenow : time_close
    int d0 = int(time / 86400000) + 1
    int d1 = int(endMs / 86400000)
    int d1c = math.min(d1, LAST_DAY)
    float v = na
    if d1c - d0 < 1
        v := fngAt(d1)
    else
        float sum = 0.0
        int cnt = 0
        for k = d0 to d1c
            float x = fngAt(k)
            if not na(x)
                sum += x
                cnt += 1
        v := cnt > 0 ? math.round(sum / cnt * 10) / 10.0 : fngAt(d1)
    v
 
grp = "Divergence"
minPts = input.int(5, "Min F&G gap (points)", minval=0, maxval=60, group=grp)
eqBand = input.float(0.5, "Price counts as equal within ±%", minval=0, maxval=10, step=0.1, group=grp)
useZone = input.bool(true, "Only in fear / greed", group=grp)
fearLvl = input.int(45, "Fear: F&G below", minval=0, maxval=100, group=grp)
greedLvl = input.int(55, "Greed: F&G above", minval=0, maxval=100, group=grp)
left = input.int(5, "Swing: days to the left", minval=1, maxval=30, group=grp)
right = input.int(5, "Swing: days to the right", minval=1, maxval=30, group=grp)
dmin = input.int(1, "Swings at least (days apart)", minval=0, maxval=120, group=grp)
dmaxIn = input.int(60, "Swings at most (days apart)", minval=1, maxval=400, group=grp)
showHBull = input.bool(true, "Hidden bullish (H Bull)", group=grp)
showBull = input.bool(true, "Regular bullish (Bull)", group=grp)
showHBear = input.bool(true, "Hidden bearish (H Bear)", group=grp)
showBear = input.bool(true, "Regular bearish (Bear)", group=grp)
paneLines = input.bool(false, "Divergence lines in the pane", group=grp)
paneMarks = input.bool(true, "Bull / Bear tags in the pane", group=grp)
priceAll = input.bool(true, "Price chart: all signals on / off (the pane keeps its own)", group=grp)
priceLines = input.bool(false, "Divergence lines on the price chart", group=grp)
signals = input.bool(true, "Signals on the price chart (diamonds)", group=grp)
showLine = input.bool(true, "Show the F&G line", group=grp)
showRule = input.bool(true, "Two-month rule on the chart", group=grp)
look = input.int(60, "Compare with (days back)", minval=5, maxval=365, group=grp)
rulePts = input.int(30, "Two-month F&G gap", minval=0, maxval=80, group=grp)
ruleBand = input.float(5.0, "Two-month price within ±%", minval=0, maxval=30, step=0.5, group=grp)
 
gmf = "Money Flow"
mfiOn = input.bool(true, "Money Flow (white in, purple out)", group=gmf)
mfiStyle = input.string("cloud", "Money Flow as", options=["cloud", "columns"], group=gmf)
mfiLen = input.int(14, "Money Flow length", minval=2, maxval=100, group=gmf)
mfPeaks = input.bool(true, "Money Flow peaks in the pane (and \"peaking\" live)", group=gmf)
mfPeakPrice = input.bool(true, "Money Flow peaks on the price chart", group=gmf)
mfSmooth = input.int(1, "Money Flow smoothing (bars; 1 = none)", minval=1, maxval=10, group=gmf)
mfBeads = input.bool(true, "Big money beads", group=gmf)
mfBig = input.float(2.5, "Big money: at least this many times the average bar", minval=1.2, maxval=10, step=0.1, group=gmf)
mfBeadMode = input.string("biggest per push", "Big money beads", options=["biggest per push", "every big candle"], group=gmf)
mfHtf = input.string("auto", "Beads coloured by the higher timeframe", options=["auto", "1H", "4H", "1D", "1W", "off"], group=gmf)
guides = input.bool(true, "Fear / greed guide lines (25 · 50 · 75)", group=gmf)
 
gst = "Colours"
fgCol = input.color(#4cc9f0, "F&G line", group=gst)
mfInC = input.color(#f4f7fb, "Money in", group=gst)
mfOutC = input.color(#b14dff, "Money out", group=gst)
BULL = input.color(#22c55e, "Bullish", group=gst)
BEAR = input.color(#a855f7, "Bearish", group=gst)
greedC = input.color(color.new(#22c55e, 70), "Greed line (75)", group=gst)
midC = input.color(color.new(#e8ecf4, 86), "Mid line (50)", group=gst)
fearC = input.color(color.new(#f43f5e, 70), "Fear line (25)", group=gst)
noteC = input.color(#8b95a8, "Two-month text", group=gst)
htfBullC = input.color(#22c55e, "Bead: higher timeframe money in", group=gst)
htfBearC = input.color(#f472b6, "Bead: higher timeframe money out", group=gst)
 
int dLo = math.min(dmin, dmaxIn)
int dHi = math.max(dmin, dmaxIn)
int tfSec = timeframe.in_seconds()
 
// ── Money Flow: a cloud from 50, brighter the stronger the flow; the top of a push past 80 (bottom past 20) is marked
float mfRaw = ta.mfi(hlc3, mfiLen)
float mf = mfSmooth > 1 ? ta.wma(mfRaw, mfSmooth) : mfRaw      // weighted, newest bar heaviest: Sniper Charts' smoothing
bool cloud = mfiStyle == "cloud"
float strength = na(mf) ? na : math.min(1.0, math.abs(mf - 50) / 50)
color mfBase = mf >= 50 ? mfInC : mfOutC
color fillC = na(mf) ? na : color.new(mfBase, 100 - (5 + 60 * math.pow(strength, 1.4)))
color edgeC = na(mf) ? na : color.new(mfBase, 100 - (30 + 70 * math.min(1.0, math.abs(mf - 50) / 40)))
pMf = plot(mfiOn and cloud ? mf : na, "Money Flow", edgeC, 1)
pBase = plot(mfiOn and cloud ? 50 : na, "Money Flow base", color.new(#000000, 100), display=display.none)
fill(pMf, pBase, fillC, "Money Flow cloud")
plot(mfiOn and not cloud ? mf : na, "Money Flow columns", na(mf) ? na : color.new(mfBase, mf >= 80 or mf <= 20 ? 45 : 74), style=plot.style_columns, histbase=50)
// peaks: the top of a push past 80 (bottom past 20), the most extreme within 5 bars each side. Confirmed 5 bars later,
// so they are drawn back on their own bar; the newest bars show "peaking" while the extreme is still forming.
float pkH = ta.pivothigh(mf, 5, 5)
float pkL = ta.pivotlow(mf, 5, 5)
// the pane's peaks and the price chart's peaks have their own switches (Ricky 29 Oct)
bool pkTop = mfiOn and pkH >= 80
bool pkBot = mfiOn and pkL <= 20
bool pkPrice = priceAll and mfPeakPrice
plot(pkTop and mfPeaks ? pkH : na, "Money Flow peak", mfInC, 4, plot.style_circles, offset=-5)
plot(pkBot and mfPeaks ? pkL : na, "Money Flow low", mfOutC, 4, plot.style_circles, offset=-5)
plotshape(pkTop and pkPrice, "Money Flow peak (price)", shape.triangledown, location.abovebar, mfInC, offset=-5, size=size.tiny, force_overlay=true)
plotshape(pkBot and pkPrice, "Money Flow low (price)", shape.triangleup, location.belowbar, mfOutC, offset=-5, size=size.tiny, force_overlay=true)
// big-money beads: each bar's money (typical price x volume, in when it rose, out when it fell) against the 20 bars before
float flowV = hlc3 > hlc3[1] ? hlc3 * volume : hlc3 < hlc3[1] ? -hlc3 * volume : 0.0
float avgAbs = ta.sma(math.abs(flowV), 20)[1]
float ratio = avgAbs > 0 ? flowV / avgAbs : na
// the higher timeframe (as Sniper Charts picks it): 1H up to 10m, 4H up to 1H, 1D up to 4H, 1W up to 1D; its last CLOSED bar, so nothing repaints
string htfAuto = tfSec <= 600 ? "60" : tfSec <= 3600 ? "240" : tfSec <= 14400 ? "D" : tfSec <= 86400 ? "W" : ""
string htfRes = mfHtf == "auto" ? htfAuto : mfHtf == "off" ? "" : mfHtf == "1H" ? "60" : mfHtf == "4H" ? "240" : mfHtf == "1D" ? "D" : "W"
string htfUse = htfRes == "" ? timeframe.period : htfRes
bool htfOk = htfRes != "" and timeframe.in_seconds(htfUse) > tfSec
float htfMf = request.security(syminfo.tickerid, htfUse, ta.mfi(hlc3, mfiLen)[1], lookahead=barmerge.lookahead_on)
string htfName = htfUse == "60" ? "1H" : htfUse == "240" ? "4H" : htfUse == "D" ? "1D" : htfUse == "W" ? "1W" : htfUse
color beadC = htfOk and not na(htfMf) ? (htfMf >= 50 ? htfBullC : htfBearC) : (ratio > 0 ? mfInC : mfOutC)
bool beadsOn = mfiOn and cloud and mfBeads
var int pushSide = 0
var label pushLbl = na
var float pushBest = 0.0
int sdNow = na(mf) ? 0 : mf >= 50 ? 1 : -1
if sdNow != pushSide
    pushSide := sdNow
    pushLbl := na
    pushBest := 0.0
// a small dot on the line, as on Sniper Charts (label.style_circle is a big badge even at its smallest, Ricky 30 Oct):
// a "●" glyph on a see-through label, one size up when the candle is twice the big-money bar
newBead(float y, color c, string sz) =>
    label.new(bar_index, y, "●", color=color.new(#000000, 100), textcolor=c, style=label.style_label_center, size=sz)
if beadsOn and not na(ratio) and math.abs(ratio) >= mfBig
    string bSize = math.abs(ratio) >= mfBig * 2 ? size.small : size.tiny
    if mfBeadMode == "every big candle"
        newBead(mf, beadC, bSize)
    else if sdNow != 0 and math.sign(ratio) == sdNow and math.abs(ratio) > pushBest
        // the biggest so far in this push: the push's one bead moves here
        pushBest := math.abs(ratio)
        if na(pushLbl)
            pushLbl := newBead(mf, beadC, bSize)
        else
            label.set_xy(pushLbl, bar_index, mf)
            label.set_textcolor(pushLbl, beadC)
            label.set_size(pushLbl, bSize)
bool recent = bar_index > last_bar_index - 5
bool liveTop = mfiOn and recent and mf >= 80 and mf == ta.highest(mf, 6)
bool liveBot = mfiOn and recent and mf <= 20 and mf == ta.lowest(mf, 6)
plotshape(liveTop and mfPeaks ? mf : na, "Money Flow peaking", shape.diamond, location.absolute, mfInC, size=size.tiny, text="peaking", textcolor=mfInC)
plotshape(liveBot and mfPeaks ? mf : na, "Money Flow bottoming", shape.diamond, location.absolute, mfOutC, size=size.tiny, text="peaking", textcolor=mfOutC)
plotshape(liveTop and pkPrice, "Money Flow peaking (price)", shape.circle, location.abovebar, mfInC, size=size.tiny, force_overlay=true)
plotshape(liveBot and pkPrice, "Money Flow bottoming (price)", shape.circle, location.belowbar, mfOutC, size=size.tiny, force_overlay=true)
 
// ── Fear & Greed line and guides
float fgv = candleFg()
plot(showLine ? fgv : na, "F&G", fgCol, 1)
hline(75, "Greed", greedC, hline.style_dashed, display=guides ? display.all : display.none)
hline(50, "Mid", midC, hline.style_dashed, display=guides ? display.all : display.none)
hline(25, "Fear", fearC, hline.style_dashed, display=guides ? display.all : display.none)
 
// ── Two-month reading in the corner (Crypto Ryan's rule): today's index and price against `look` days back
ptsText(float d) =>
    (d > 0 ? "+" : d < 0 ? "−" : "") + str.tostring(math.abs(math.round(d)), "#")
float pxThen = request.security(syminfo.tickerid, "D", close[look], lookahead=barmerge.lookahead_off)
var table panel = table.new(position.top_right, 1, 1, bgcolor=color.new(#000000, 100), border_width=0)
if barstate.islast
    string head = ""
    if showRule
        float fn = fngAt(LAST_DAY)
        float fp = fngAt(LAST_DAY - look)
        float dF = fn - fp
        float dP = not na(pxThen) and pxThen > 0 ? (close / pxThen - 1.0) * 100.0 : na
        if not na(dF) and not na(dP)
            string call = math.abs(dP) <= ruleBand and dF <= -rulePts ? " · H Bull" : math.abs(dP) <= ruleBand and dF >= rulePts ? " · H Bear" : ""
            head := str.tostring(look) + "d: F&G " + ptsText(dF) + " · price " + (dP >= 0 ? "+" : "−") + str.tostring(math.abs(dP), "#.0") + "%" + call + " · alternative.me"
    // the higher timeframe's Money Flow, as Sniper Charts adds it ("4H MF in 61")
    if beadsOn and htfOk and not na(htfMf)
        head := head + (head == "" ? "" : " · ") + htfName + " MF " + (htfMf >= 50 ? "in " : "out ") + str.tostring(math.round(htfMf), "#")
    if head != ""
        table.cell(panel, 0, 0, head, text_color=noteC, text_size=size.small, text_halign=text.align_right)
 
// ── Divergences on the DAILY highs and lows, so every timeframe shows the same reads as the daily chart
var int[] loDay = array.new_int()
var int[] loWhen = array.new_int()
var float[] loPx = array.new_float()
var float[] loFg = array.new_float()
var int[] hiDay = array.new_int()
var int[] hiWhen = array.new_int()
var float[] hiPx = array.new_float()
var float[] hiFg = array.new_float()
var float[] loY = array.new_float()
var float[] hiY = array.new_float()
var line[] keptL = array.new_line()
var label[] keptB = array.new_label()
 
keepLine(line ln) =>
    array.push(keptL, ln)
    if array.size(keptL) > 240
        line.delete(array.shift(keptL))
 
keepLabel(label lb) =>
    array.push(keptB, lb)
    if array.size(keptB) > 240
        label.delete(array.shift(keptB))
 
// What one candle shows for one side: a weekly / monthly candle holding several swings keeps the largest F&G gap
type Mark
    float gap
    label pane
    label price
    line pl
    line pp
var map<int, Mark> marksLo = map.new<int, Mark>()
var map<int, Mark> marksHi = map.new<int, Mark>()
 
// A new daily swing against earlier swings in the day window; the largest Fear & Greed gap is the one drawn.
// fg: the close-of-day print the rules compare. whenMs: the candle on this chart the day lands on, yLine: the line there.
noteSwing(bool isLow, float px, float fg, int day, int whenMs, float yLine) =>
    int[] days = isLow ? loDay : hiDay
    int[] whens = isLow ? loWhen : hiWhen
    float[] pxs = isLow ? loPx : hiPx
    float[] fgs = isLow ? loFg : hiFg
    float[] ys = isLow ? loY : hiY
    float bestGap = 0.0
    int bestJ = -1
    string bestKind = ""
    int n = array.size(days)
    if n > 0
        for j = n - 1 to 0
            int gap = day - array.get(days, j)
            if gap < dLo
                continue
            if gap > dHi
                break
            float prevFg = array.get(fgs, j)
            float prevPx = array.get(pxs, j)
            if na(prevFg) or na(prevPx) or prevPx <= 0
                continue
            float df = fg - prevFg
            string kind = ""
            if isLow
                if px >= prevPx * (1.0 - eqBand / 100.0) and df < 0 and -df >= minPts and (not useZone or fg < fearLvl)
                    kind := "hbull"
                else if px < prevPx * (1.0 - eqBand / 100.0) and df > 0 and df >= minPts and (not useZone or prevFg < fearLvl)
                    kind := "bull"
            else
                if px <= prevPx * (1.0 + eqBand / 100.0) and df > 0 and df >= minPts and (not useZone or fg > greedLvl)
                    kind := "hbear"
                else if px > prevPx * (1.0 + eqBand / 100.0) and df < 0 and -df >= minPts and (not useZone or prevFg > greedLvl)
                    kind := "bear"
            bool allowed = kind == "hbull" ? showHBull : kind == "bull" ? showBull : kind == "hbear" ? showHBear : kind == "bear" and showBear
            if allowed and math.abs(df) > bestGap
                bestGap := math.abs(df)
                bestJ := j
                bestKind := kind
    map<int, Mark> seen = isLow ? marksLo : marksHi
    Mark was = map.get(seen, whenMs)
    if bestJ >= 0 and not na(was) and was.gap >= bestGap
        bestJ := -1
    if bestJ >= 0 and not na(was)
        label.delete(was.pane)
        label.delete(was.price)
        line.delete(was.pl)
        line.delete(was.pp)
    if bestJ >= 0
        bool bull = bestKind == "hbull" or bestKind == "bull"
        bool hidden = bestKind == "hbull" or bestKind == "hbear"
        color col = bull ? BULL : BEAR
        int t0 = array.get(whens, bestJ)
        float p0 = array.get(pxs, bestJ)
        float y0 = array.get(ys, bestJ)
        lnStyle = hidden ? line.style_dashed : line.style_solid
        Mark mk = Mark.new(bestGap, na, na, na, na)
        if paneLines and t0 != whenMs
            mk.pl := line.new(t0, y0, whenMs, yLine, xloc=xloc.bar_time, color=col, style=lnStyle, width=1)
            keepLine(mk.pl)
        // the pane, on the line: hidden = hollow diamond, regular = its Bull / Bear tag
        if paneMarks
            if hidden
                mk.pane := label.new(whenMs, yLine, "◇", xloc=xloc.bar_time, style=bull ? label.style_label_up : label.style_label_down, color=color.new(col, 100), textcolor=col, size=size.normal)
            else
                mk.pane := label.new(whenMs, yLine, bull ? "Bull" : "Bear", xloc=xloc.bar_time, style=bull ? label.style_label_up : label.style_label_down, color=col, textcolor=color.white, size=size.tiny)
            keepLabel(mk.pane)
        if priceAll and priceLines and t0 != whenMs
            mk.pp := line.new(t0, p0, whenMs, px, xloc=xloc.bar_time, color=color.new(col, 55), style=lnStyle, width=1, force_overlay=true)
            keepLine(mk.pp)
        // the price chart, same candle: filled diamond = regular, hollow = hidden
        if priceAll and signals
            mk.price := label.new(whenMs, px, hidden ? "◇" : "◆", xloc=xloc.bar_time, yloc=bull ? yloc.belowbar : yloc.abovebar, style=label.style_none, textcolor=col, size=size.normal, force_overlay=true)
            keepLabel(mk.price)
        map.put(seen, whenMs, mk)
    array.push(days, day)
    array.push(whens, whenMs)
    array.push(pxs, px)
    array.push(fgs, fg)
    array.push(ys, yLine)
    if array.size(days) > 400
        array.shift(days)
        array.shift(whens)
        array.shift(pxs)
        array.shift(fgs)
        array.shift(ys)
 
// Daily candles: day, high, low and the chart candle that printed each
var int[] dDay = array.new_int()
var float[] dH = array.new_float()
var float[] dL = array.new_float()
var int[] dHT = array.new_int()
var int[] dLT = array.new_int()
var float[] dHY = array.new_float()
var float[] dLY = array.new_float()
var int[] lastPushed = array.from(-1)       // a function can't reassign a global, so it lives in an array
 
pushDay(int day, float h, float l, int ht, int lt, float hy, float ly) =>
    if day > array.get(lastPushed, 0) and day <= LAST_DAY and not na(h) and not na(l)
        array.set(lastPushed, 0, day)
        array.push(dDay, day)
        array.push(dH, h)
        array.push(dL, l)
        array.push(dHT, ht)
        array.push(dLT, lt)
        array.push(dHY, hy)
        array.push(dLY, ly)
        if array.size(dDay) > 900
            array.shift(dDay)
            array.shift(dH)
            array.shift(dL)
            array.shift(dHT)
            array.shift(dLT)
            array.shift(dHY)
            array.shift(dLY)
        // the day `right` days back is a swing once `right` more days have printed
        int n = array.size(dDay)
        int i = n - 1 - right
        if i >= left
            float xl = array.get(dL, i)
            float xh = array.get(dH, i)
            bool isLo = true
            bool isHi = true
            for k = i - left to i + right
                if k != i
                    float yl = array.get(dL, k)
                    float yh = array.get(dH, k)
                    if k < i ? yl <= xl : yl < xl
                        isLo := false
                    if k < i ? yh >= xh : yh > xh
                        isHi := false
            int sd = array.get(dDay, i)
            float sfg = fngEod(sd)
            if isLo and not na(sfg)
                noteSwing(true, xl, sfg, sd, array.get(dLT, i), nz(array.get(dLY, i), sfg))
            if isHi and not na(sfg)
                noteSwing(false, xh, sfg, sd, array.get(dHT, i), nz(array.get(dHY, i), sfg))
 
int dayNow = int(time / 86400000)
if tfSec < 86400
    // intraday: build each UTC day from this chart's candles (the first, partial day is skipped)
    var int curD = na
    var bool firstDay = true
    var float cH = na
    var float cL = na
    var int cHT = na
    var int cLT = na
    var float cHY = na
    var float cLY = na
    if na(curD) or dayNow != curD
        if not na(curD) and not firstDay
            pushDay(curD, cH, cL, cHT, cLT, cHY, cLY)
        if not na(curD)
            firstDay := false
        curD := dayNow
        cH := high
        cL := low
        cHT := time
        cLT := time
        cHY := fgv
        cLY := fgv
    else
        if high > cH
            cH := high
            cHT := time
            cHY := fgv
        if low < cL
            cL := low
            cLT := time
            cLY := fgv
else if tfSec == 86400
    if bar_index > 0
        pushDay(int(time[1] / 86400000), high[1], low[1], time[1], time[1], fgv[1], fgv[1])
 
// weekly / monthly: the daily candles inside each bar; a swing day sits on the bar that holds it
[lh, ll, lt] = request.security_lower_tf(syminfo.tickerid, tfSec > 86400 ? "D" : timeframe.period, [high, low, time])
if tfSec > 86400
    int m = array.size(lt)
    if m > 0
        for j = 0 to m - 1
            bool developing = barstate.isrealtime and j == m - 1
            if not developing
                pushDay(int(array.get(lt, j) / 86400000), array.get(lh, j), array.get(ll, j), time, time, fgv, fgv)

← 返回列表