74 lines
3.3 KiB
Plaintext
74 lines
3.3 KiB
Plaintext
//@version=6
|
|
// Trend + Volume strategy for Yote-In-Vest.
|
|
// Long-only (works for spot crypto and stocks).
|
|
//
|
|
// Entry: fast EMA above slow EMA, price above the fast EMA,
|
|
// and volume above its average (the trend has conviction).
|
|
// Exit: fast EMA crosses back under slow EMA, OR an ATR trailing stop is hit.
|
|
//
|
|
// Each order sends a JSON message to the webhook server.
|
|
// Set WEBHOOK_SECRET below to the same value as in your .env file.
|
|
|
|
strategy("Yote-In-Vest Trend + Volume", overlay = true,
|
|
initial_capital = 10000, default_qty_type = strategy.percent_of_equity,
|
|
default_qty_value = 10, commission_type = strategy.commission.percent,
|
|
commission_value = 0.15, slippage = 2, pyramiding = 0)
|
|
|
|
// ---------- Inputs ----------
|
|
secret = input.string("change-me", "Webhook secret")
|
|
fastLen = input.int(21, "Fast EMA", minval = 2)
|
|
slowLen = input.int(55, "Slow EMA", minval = 5)
|
|
volLen = input.int(20, "Volume average length", minval = 2)
|
|
volMult = input.float(1.2, "Volume must exceed average x", step = 0.1)
|
|
atrLen = input.int(14, "ATR length")
|
|
atrMult = input.float(3.0, "ATR trailing stop x", step = 0.1)
|
|
useAdx = input.bool(true, "Require trend strength (ADX)")
|
|
adxMin = input.int(20, "Minimum ADX")
|
|
|
|
// ---------- Indicators ----------
|
|
fastEma = ta.ema(close, fastLen)
|
|
slowEma = ta.ema(close, slowLen)
|
|
volAvg = ta.sma(volume, volLen)
|
|
atr = ta.atr(atrLen)
|
|
[_, _, adx] = ta.dmi(14, 14)
|
|
|
|
trendUp = fastEma > slowEma and close > fastEma
|
|
volumeOk = volume > volAvg * volMult
|
|
strengthOk = not useAdx or adx >= adxMin
|
|
|
|
// A fresh trigger is required (trend just started, or price reclaims the fast EMA),
|
|
// so the bot does not re-buy on every bar of an old trend after a stop-out.
|
|
freshTrigger = (trendUp and not trendUp[1]) or ta.crossover(close, fastEma)
|
|
longSignal = trendUp and volumeOk and strengthOk and freshTrigger
|
|
trendExit = ta.crossunder(fastEma, slowEma)
|
|
|
|
// ---------- ATR trailing stop ----------
|
|
var float trailStop = na
|
|
if strategy.position_size > 0
|
|
trailStop := math.max(nz(trailStop, close - atr * atrMult), close - atr * atrMult)
|
|
else
|
|
trailStop := na
|
|
stopHit = strategy.position_size > 0 and close < trailStop
|
|
|
|
// ---------- Webhook message ----------
|
|
makeMsg(string action, string reason) =>
|
|
'{"secret":"' + secret + '","symbol":"' + syminfo.ticker + '","exchange":"' +
|
|
syminfo.prefix + '","action":"' + action + '","price":' + str.tostring(close) +
|
|
',"reason":"' + reason + '","timeframe":"' + timeframe.period + '"}'
|
|
|
|
// ---------- Orders ----------
|
|
if longSignal and strategy.position_size == 0 and barstate.isconfirmed
|
|
strategy.entry("Long", strategy.long, alert_message = makeMsg("buy", "trend_up_volume"))
|
|
|
|
if strategy.position_size > 0 and barstate.isconfirmed
|
|
if trendExit
|
|
strategy.close("Long", comment = "trend exit", alert_message = makeMsg("sell", "trend_reversal"))
|
|
else if stopHit
|
|
strategy.close("Long", comment = "ATR stop", alert_message = makeMsg("sell", "atr_stop"))
|
|
|
|
// ---------- Plots ----------
|
|
plot(fastEma, "Fast EMA", color = color.new(color.teal, 0), linewidth = 2)
|
|
plot(slowEma, "Slow EMA", color = color.new(color.orange, 0), linewidth = 2)
|
|
plot(trailStop, "Trailing stop", color = color.new(color.red, 30), style = plot.style_linebr)
|
|
bgcolor(trendUp ? color.new(color.teal, 92) : na)
|