Erstelle mir bitte in pinescript in der aktuellen...

创建于:2025年6月14日

使用 O3 回答 Chat01

问题

Erstelle mir bitte in pinescript in der aktuellen version für tradingview eine neue Strategie:

Nimm das als Grundlage und baue es um:

//@version=4
study(title="UT Bot Alerts", overlay = true)

// Inputs
a = input(1, title = "Key Vaule. 'This changes the sensitivity'")
c = input(10, title = "ATR Period")
h = input(false, title = "Signals from Heikin Ashi Candles")

xATR = atr(c)
nLoss = a * xATR

src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = false) : close

xATRTrailingStop = 0.0
xATRTrailingStop := iff(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
iff(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
iff(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss)))

pos = 0
pos := iff(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
iff(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))

xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue

ema = ema(src,1)
above = crossover(ema, xATRTrailingStop)
below = crossover(xATRTrailingStop, ema)

buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below

barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop

plotshape(buy, title = "Buy", text = 'Buy', style = shape.labelup, location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(sell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)

barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)

alertcondition(buy, "UT Long", "UT Long")
alertcondition(sell, "UT Short", "UT Short")

Bitte mit Stop-loss und trailing Stop (beide einstellbar).

Die Signale in der Grundlage als Handelssignale für das automatisierte Trading.

思考

Creating a strategy in Pine Script

The user is asking for a strategy built in the latest version of Pine Script (v5) based on an existing study, likely "UT Bot Alerts." They want an automated trading strategy with adjustable stop-loss and trailing stops. To do this, I'll need to convert the provided study into a strategy, add entry and exit conditions, and implement the required risk management features like stop-loss and trailing stops. I'll make sure to use the correct version directive (@version=5) and the strategy.* functions.

Designing Pine Script strategy

The user wants me to create a Pine Script strategy using the provided study, with adjustable stop-loss and trailing stop percentages or ATR multipliers. I'll use strategy.entry and strategy.exit to implement buy/sell conditions, while setting up stop-loss and trailing stop with proper offsets. For stop-loss, I can calculate the exit on a percentage basis, converting percent to absolute prices. V5 improvements like ta.crossover, ta.atr, and strategy.position_size will be used. I'll ensure the user gets the full script for their automated trading setup.

Thought for 19 seconds

回答

Hier ist eine komplette Pine-Script-Strategie in der aktuellen TradingView-Version (v5).
Sie wandelt das UT-Bot-Konzept in echte Handels-Ein- und -Ausstiege um und bietet parametrierbaren Stop-Loss und Trailing-Stop.

pinescript
//@version=5 strategy( title = "UT-Bot Strategie (mit SL & Trailing)", overlay = true, initial_capital= 10_000, default_qty_type = strategy.percent_of_equity, default_qty_value= 10) // ======= Eingaben ============================================================ sens = input.int(1, "Key-Wert (Sensitivität)", minval = 1) atrPeriod = input.int(10, "ATR-Periode", minval = 1) useHA = input.bool(false,"Heikin-Ashi-Quelle nutzen?") stopLossPct = input.float(1.5, "Fixer Stop-Loss [%]", minval = 0.1, step = 0.1) trailPct = input.float(1.0, "Trailing-Stop [%]", minval = 0.1, step = 0.1) // ======= Quellpreis wählen (Normal oder Heikin-Ashi) ========================= haClose(srcSymbol)=> request.security( heikinashi(srcSymbol), timeframe.period, close, lookahead = barmerge.lookahead_on) src = useHA ? haClose(syminfo.tickerid) : close // ======= UT-Bot Kerngleichungen ============================================== atr = ta.atr(atrPeriod) nLoss = sens * atr // gleitender ATR-Stopp var float atrStop = na atrStop := src > nz(atrStop[1], 0) and src[1] > nz(atrStop[1], 0) ? math.max(nz(atrStop[1]), src - nLoss) : src < nz(atrStop[1], 0) and src[1] < nz(atrStop[1], 0) ? math.min(nz(atrStop[1]), src + nLoss) : src > nz(atrStop[1], 0) ? src - nLoss : src + nLoss // Positionsrichtung var int pos = 0 pos := src[1] < nz(atrStop[1], 0) and src > nz(atrStop[1], 0) ? 1 : src[1] > nz(atrStop[1], 0) and src < nz(atrStop[1], 0) ? -1 : nz(pos[1]) // Zusatzfilter (EMA 1 wie im Original) ema1 = ta.ema(src, 1) above = ta.crossover(ema1, atrStop) below = ta.crossover(atrStop, ema1) // Handels-Signale longSignal = src > atrStop and above shortSignal = src < atrStop and below // ======= Strategische Ein- & Ausstiege ======================================= // LONG-Eintritt if longSignal strategy.entry(id = "Long", direction = strategy.long) // SHORT-Eintritt if shortSignal strategy.entry(id = "Short", direction = strategy.short) // Dynamischer Stop-Loss & Trailing-Stop // (wird auf jedem Balken aktualisiert, solange eine Position offen ist) if strategy.position_size > 0 // LONG-Position float stopPrice = strategy.position_avg_price * (1 - stopLossPct/100) float trailPts = (trailPct/100) * close strategy.exit(id = "L-Exit", from_entry = "Long", stop = stopPrice, trail_points = trailPts) if strategy.position_size < 0 // SHORT-Position float stopPrice = strategy.position_avg_price * (1 + stopLossPct/100) float trailPts = (trailPct/100) * close strategy.exit(id = "S-Exit", from_entry = "Short", stop = stopPrice, trail_points = trailPts) // ======= Visualisierung (optional) =========================================== plot(atrStop, "ATR-Trailing-Stop", color = color.gray) plotshape(longSignal, "Long-Signal", shape.labelup, location.belowbar, color = color.green, text = "Buy", textcolor = color.white, size = size.tiny) plotshape(shortSignal, "Short-Signal", shape.labeldown, location.abovebar, color = color.red, text = "Sell", textcolor = color.white, size = size.tiny) barcolor( pos == 1 ? color.new(color.green , 80) : pos == -1 ? color.new(color.red , 80) : na)

So passt du die Strategie an deine Bedürfnisse an

ParameterWirkung
Key-WertMultiplikator für den ATR-Abstand ⇒ höhere Werte = weniger, aber stabilere Signale
ATR-PeriodeLänge der ATR-Berechnung
Heikin-Ashi-QuelleNutzt HA-Kerzen, um Rauschen zu glätten
Stop-Loss [%]Fester Abstand vom Einstiegspreis in %
Trailing-Stop [%]Abstand, ab dem der Trailing-Stop nachgezogen wird (in Punkten ≙ % vom aktuellen Kurs)

Hinweis:

  • strategy.exit(..., trail_points = ...) zieht den Stop dynamisch nach, sobald der Kurs mindestens trail_points Punkte (≙ trailPct %) im Plus ist.
  • Für Live-Trading auf TradingView musst du die Strategie in einen Bot/Connector (z. B. via webhook) integrieren.

Viel Erfolg beim Testen und Anpassen!

分享这个问答