本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// SK-Oscillator
// Unofficial tool by a fan - not affiliated with, sponsored or endorsed by Stefan Kassing or Crypto Wall Street.
//
// Three rows in one pane under the chart, each on or off:
// 1. MACD of the closes (fast / slow / signal): the line, its signal and the histogram (green rising
// above 0, faded green falling above it, red falling below 0, faded red rising below it);
// 2. RSI with 70 and 30 marked, and its moving average (yellow, an SMA of the RSI);
// 3. the Coinbase premium: Coinbase's BTC-USD close minus Binance's BTCUSDT close per candle of the
// chart's timeframe, in $ or % - Bitcoin's, whatever the chart shows.
// One pane has one scale, so each row gets its own band in it: the RSI's 20 - 80 fill its band, the MACD
// and the premium are scaled to their largest size over the last candles ("Scale over"). The numbers on the
// pane's axis are only positions - each row's newest value is written right of it, and every value is in
// the Data Window.
indicator("SK-Oscillator", shorttitle = "SK-Osc", overlay = false, precision = 2)
// ---------------------------------------------------------------- inputs
G_MACD = "MACD"
showMacd = input.bool(true, "Show", group = G_MACD)
fastIn = input.int(12, "Fast", minval = 1, group = G_MACD, inline = "m")
slowIn = input.int(26, "Slow", minval = 1, group = G_MACD, inline = "m")
signalIn = input.int(9, "Signal", minval = 1, group = G_MACD, inline = "m")
G_RSI = "RSI"
showRsi = input.bool(true, "Show", group = G_RSI)
rsiLenIn = input.int(14, "Length", minval = 1, group = G_RSI, inline = "r")
rsiMaLenIn = input.int(14, "MA length", minval = 1, group = G_RSI, inline = "r")
G_PREM = "Coinbase premium (Bitcoin)"
showPrem = input.bool(true, "Show", group = G_PREM)
unitIn = input.string("$", "Unit", options = ["$", "%"], group = G_PREM)
cbSym = input.symbol("COINBASE:BTCUSD", "Coinbase", group = G_PREM)
bnSym = input.symbol("BINANCE:BTCUSDT", "Binance", group = G_PREM)
G_LOOK = "Look"
scaleLen = input.int(200, "Scale over (candles)", minval = 20, maxval = 5000, group = G_LOOK, tooltip = "MACD and premium: their band is filled by their largest size over these candles.")
showValues = input.bool(true, "Newest values right of the rows", group = G_LOOK)
// ---------------------------------------------------------------- the rows' bands (top to bottom: MACD, RSI, premium)
float ROW = 100.0
float GAP = 5.0
int shown = (showMacd ? 1 : 0) + (showRsi ? 1 : 0) + (showPrem ? 1 : 0)
int macdSlot = 0
int rsiSlot = showMacd ? 1 : 0
int premSlot = (showMacd ? 1 : 0) + (showRsi ? 1 : 0)
// A row's bottom: the first row on top.
bandOf(int slot) => (shown - 1 - slot) * (ROW + GAP)
float macdMid = bandOf(macdSlot) + ROW / 2
float rsiBase = bandOf(rsiSlot)
float premMid = bandOf(premSlot) + ROW / 2
// A value scaled into half a band (a little room left at the edges).
fit(float v, float size) => size > 0 ? 0.48 * ROW * v / size : 0.0
// The RSI: 20 - 80 fill its band (it hardly leaves that), 30 and 70 inside; beyond it reaches into the gap.
float RSI_LO = 20.0
float RSI_HI = 80.0
rsiY(float v) => rsiBase + ROW * (v - RSI_LO) / (RSI_HI - RSI_LO)
color UP = #26a69a
color DOWN = #ef5350
color UP_FADED = color.new(#26a69a, 55)
color DOWN_FADED = color.new(#ef5350, 55)
color GUIDE = color.new(color.white, 70)
// ---------------------------------------------------------------- 1. MACD
[macdLine, macdSignal, macdHist] = ta.macd(close, fastIn, slowIn, signalIn)
float macdSize = ta.highest(math.max(math.abs(nz(macdLine)), math.abs(nz(macdSignal)), math.abs(nz(macdHist))), scaleLen)
float histPrev = nz(macdHist[1], macdHist)
color histCol = macdHist >= 0 ? (macdHist >= histPrev ? UP : UP_FADED) : (macdHist <= histPrev ? DOWN : DOWN_FADED)
plot(showMacd ? macdMid : na, "MACD zero", GUIDE)
// The histogram as bars from the band's middle (a column plot's base can't move with the bands).
float histY = showMacd ? macdMid + fit(macdHist, macdSize) : na
plotcandle(showMacd ? macdMid : na, math.max(macdMid, histY), math.min(macdMid, histY), histY, "MACD histogram", histCol, histCol, bordercolor = histCol)
plot(showMacd ? macdMid + fit(macdLine, macdSize) : na, "MACD line", #2962ff)
plot(showMacd ? macdMid + fit(macdSignal, macdSize) : na, "MACD signal", #ff6d00)
// ---------------------------------------------------------------- 2. RSI
float rsiVal = ta.rsi(close, rsiLenIn)
float rsiMa = ta.sma(rsiVal, rsiMaLenIn)
plot(showRsi ? rsiY(70) : na, "RSI 70", GUIDE)
plot(showRsi ? rsiY(30) : na, "RSI 30", GUIDE)
plot(showRsi ? rsiY(rsiVal) : na, "RSI", #7e57c2)
plot(showRsi ? rsiY(rsiMa) : na, "RSI MA", #fdd835)
// ---------------------------------------------------------------- 3. Coinbase premium
float cbClose = request.security(cbSym, timeframe.period, close, ignore_invalid_symbol = true)
float bnClose = request.security(bnSym, timeframe.period, close, ignore_invalid_symbol = true)
float gap = cbClose - bnClose
float prem = unitIn == "%" ? gap / bnClose * 100 : gap
float premSize = ta.highest(math.abs(nz(prem)), scaleLen)
plot(showPrem ? premMid : na, "Premium zero", GUIDE)
float premY = showPrem ? premMid + fit(prem, premSize) : na
color premCol = prem >= 0 ? UP : DOWN
plotcandle(showPrem ? premMid : na, math.max(premMid, premY), math.min(premMid, premY), premY, "Coinbase premium", premCol, premCol, bordercolor = premCol)
// ---------------------------------------------------------------- the real values
plot(macdLine, "MACD value", display = display.data_window)
plot(macdSignal, "MACD signal value", display = display.data_window)
plot(macdHist, "MACD histogram value", display = display.data_window)
plot(rsiVal, "RSI value", display = display.data_window)
plot(rsiMa, "RSI MA value", display = display.data_window)
plot(prem, "Coinbase premium value", display = display.data_window)
var label macdTag = na
var label rsiTag = na
var label premTag = na
tag(label old, bool on, float y, string s, color c) =>
label.delete(old)
on and showValues ? label.new(bar_index + 2, y, s, style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = size.small) : na
if barstate.islast
macdTag := tag(macdTag, showMacd, macdMid, "MACD " + str.tostring(macdLine, "#.##"), #2962ff)
rsiTag := tag(rsiTag, showRsi, rsiBase + ROW / 2, "RSI " + str.tostring(rsiVal, "#.#"), #7e57c2)
string premText = na(prem) ? "-" : unitIn == "%" ? str.tostring(prem, "#.###") + " %" : str.tostring(prem, "#") + " $"
premTag := tag(premTag, showPrem, premMid, "CB " + premText, prem >= 0 ? UP : DOWN)