本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("SMA 250 Status, Trades und Warnzone (ohne Puffer)", overlay=true)
laenge = input.int(250, "SMA-Länge", minval=1)
schwelle = input.float(2.0, "Warnschwelle in Prozent", step=0.5, minval=0.1)
sma = ta.sma(close, laenge)
// Zustand merken: 1 = investiert, 0 = raus
var int zustand = 1
if close > sma
zustand := 1
if close < sma
zustand := 0
kaufen = zustand == 1 and zustand[1] == 0
verkaufen = zustand == 0 and zustand[1] == 1
// Trades zählen
var int anzahlKaeufe = 0
var int anzahlVerkaeufe = 0
if kaufen
anzahlKaeufe += 1
if verkaufen
anzahlVerkaeufe += 1
// Startzeitpunkt = erster Balken, an dem der SMA existiert
var int startZeit = na
if na(startZeit) and not na(sma)
startZeit := time
// Warnzone
oben = sma * (1 + schwelle / 100)
unten = sma * (1 - schwelle / 100)
abstandAbs = math.abs(close / sma - 1) * 100
nah = abstandAbs < schwelle
naheEintritt = nah and not nah[1]
// Linien, Band und Signale
plot(sma, "SMA", color.gray, linewidth=2)
pOben = plot(oben, "Warnzone oben", color.new(color.orange, 50))
pUnten = plot(unten, "Warnzone unten", color.new(color.orange, 50))
fill(pOben, pUnten, color.new(color.orange, 85), "Warnzone")
plotshape(kaufen, "Kaufen", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(verkaufen, "Verkaufen", shape.triangledown, location.abovebar, color.red, size=size.small)
// Hintergrund je nach Zustand
bgcolor(zustand == 1 ? color.new(color.green, 92) : color.new(color.red, 92))
// Statusanzeige oben rechts
var tbl = table.new(position.top_right, 1, 5, border_width=1)
if barstate.islast
txt = zustand == 1 ? "INVESTIERT" : "RAUS"
col = zustand == 1 ? color.green : color.red
abstand = (close / sma - 1) * 100
gesamt = anzahlKaeufe + anzahlVerkaeufe
jahre = na(startZeit) ? 0.0 : (time - startZeit) / 31557600000.0
proJahr = jahre > 0 ? gesamt / jahre : 0.0
table.cell(tbl, 0, 0, txt, text_color=color.white, bgcolor=col)
table.cell(tbl, 0, 1, "Abstand zum SMA: " + str.tostring(abstand, "#.##") + " %", text_color=color.white, bgcolor=color.gray)
table.cell(tbl, 0, 2, "Käufe: " + str.tostring(anzahlKaeufe) + " | Verkäufe: " + str.tostring(anzahlVerkaeufe) + " | Gesamt: " + str.tostring(gesamt), text_color=color.white, bgcolor=color.gray)
table.cell(tbl, 0, 3, "Wechsel pro Jahr: " + str.tostring(proJahr, "#.#"), text_color=color.white, bgcolor=color.gray)
table.cell(tbl, 0, 4, nah ? "WARNZONE (< " + str.tostring(schwelle, "#.#") + " %)" : "Außerhalb der Warnzone", text_color=color.white, bgcolor=nah ? color.orange : color.gray)
// Alerts
alertcondition(kaufen, "Kaufsignal", "SMA-Strategie: Kaufen")
alertcondition(verkaufen, "Verkaufssignal", "SMA-Strategie: Verkaufen")
alertcondition(naheEintritt, "SMA-Naehe", "SMA 250: Kurs weniger als die Warnschwelle vom SMA entfernt")