trading view pine script ( BTCUSDT 10 MIN REAL TIME DIRECTION INDICATOR SET ON TRADING VIEW FIRST STRATEGY)
//@version=5
strategy("BTCUSDT 10m Real-Time Direction (live forming 10m) - Fixed", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=1, initial_capital=10000)
// ---------- INPUTS ----------
aggTF = input.timeframe("10", "Aggregation TF (minutes)") // keep "10" for 10-min
useShorts = input.bool(true, "Enable Shorts")
tpPct = input.float(0.25, "TP % (0.25 = 0.25%)", step=0.01)
volMult = input.float(1.5, "Volume spike multiplier")
momPeriod = input.int(5, "Momentum period")
rsiLen = input.int(14, "RSI length")
stochLen = input.int(14, "Stoch-RSI window")
// ---------- REQUEST SECURITY (REAL-TIME FORMING HIGHER-TF) ----------
// Use named args for request.security options to get forming higher-TF bar
gaps_mode = barmerge.gaps_off
lookahead_mode = barmerge.lookahead_on
// request aggregated series (open, high, low, close, volume) from aggTF as they form
[o, h, l, c, v] = request.security(syminfo.tickerid, aggTF, [open, high, low, close, volume], gaps = gaps_mode, lookahead = lookahead_mode)
// ---------- INDICATORS (ON AGGREGATED REAL-TIME 10m) ----------
ema5 = ta.ema(c, 5)
ema20 = ta.ema(c, 20)
rsiV = ta.rsi(c, rsiLen)
// StochRSI on aggregated closes
rsi_low = ta.lowest(rsiV, stochLen)
rsi_high = ta.highest(rsiV, stochLen)
stochRSI = (rsi_high - rsi_low) == 0 ? 0.0 : (rsiV - rsi_low) / (rsi_high - rsi_low)
// Parabolic SAR on aggregated series via request.security (returning scalar)
psar_v = request.security(syminfo.tickerid, aggTF, ta.sar(0.02, 0.02, 0.2), gaps = gaps_mode, lookahead = lookahead_mode)
psar_dir = c > psar_v
// VWAP: session VWAP on aggTF
vwap_agg = request.security(syminfo.tickerid, aggTF, ta.vwap, gaps = gaps_mode, lookahead = lookahead_mode)
// Momentum & Volume spike on aggregated series
mom = c - nz(c[momPeriod], c)
vol_avg = request.security(syminfo.tickerid, aggTF, ta.sma(volume, 10), gaps = gaps_mode, lookahead = lookahead_mode)
vol_spike = v > vol_avg * volMult
// ---------- SIGNAL LOGIC (same vote system) ----------
emaS = ema5 > ema20 ? 1 : -1
rsiS = rsiV > 55 ? 1 : (rsiV < 45 ? -1 : 0)
stS = stochRSI > 0.8 ? -1 : (stochRSI < 0.2 ? 1 : 0)
psS = psar_dir ? 1 : -1
vwS = c > vwap_agg ? 1 : -1
moS = mom > 0 ? 1 : -1
buyCount = (emaS>0 ? 1 : 0) + (rsiS>0 ? 1 : 0) + (stS>0 ? 1 : 0) + (psS>0 ? 1 : 0) + (vwS>0 ? 1 : 0) + (moS>0 ? 1 : 0)
sellCount = (emaS<0 ? 1 : 0) + (rsiS<0 ? 1 : 0) + (stS<0 ? 1 : 0) + (psS<0 ? 1 : 0) + (vwS<0 ? 1 : 0) + (moS<0 ? 1 : 0)
conf = math.round(100 * math.max(buyCount, sellCount) / 6.0)
signal = buyCount >= 3 ? "BUY" : (sellCount >= 3 ? "SELL" : "NEUTRAL")
targetPrice = signal == "BUY" ? c * (1 + tpPct/100.0) : (signal == "SELL" ? c * (1 - tpPct/100.0) : na)
// ---------- PLOTS ----------
plotc = request.security(syminfo.tickerid, aggTF, close, gaps = gaps_mode, lookahead = lookahead_mode) // aggregated closing series for plotting
plot(plotc, title="10m Close (forming)", linewidth=2)
plot(targetPrice, title="Predicted Target", style=plot.style_line, linewidth=2)
plot(ema5, title="EMA5", linewidth=1)
plot(ema20, title="EMA20", linewidth=1)
plot(vwap_agg, title="VWAP (agg session)", linewidth=1)
// arrows for live signals (these mark based on forming agg bar)
plotshape(signal=="BUY", title="Buy Arrow", style=shape.labelup, location=location.belowbar, text="BUY", textcolor=color.white, size=size.tiny)
plotshape(signal=="SELL", title="Sell Arrow", style=shape.labeldown, location=location.abovebar, text="SELL", textcolor=color.white, size=size.tiny)
// chart info
var table info = table.new(position.top_right, 1, 5, border_width=1)
if barstate.islast
table.cell(info, 0, 0, "Signal: " + signal + " (" + str.tostring(conf) + "%)", text_size=size.small)
table.cell(info, 0, 1, "Votes B/S: " + str.tostring(buyCount) + "/" + str.tostring(sellCount), text_size=size.small)
table.cell(info, 0, 2, "10m Close: " + str.tostring(c, format.mintick), text_size=size.small)
table.cell(info, 0, 3, "VWAP(agg): " + str.tostring(vwap_agg, format.mintick), text_size=size.small)
table.cell(info, 0, 4, "Vol Spike: " + (vol_spike ? "YES" : "NO"), text_size=size.small)
// ---------- STRATEGY (paper trade) ----------
longId = "LONG"
shortId = "SHORT"
if (signal == "BUY")
if strategy.position_size == 0
strategy.entry(longId, strategy.long)
strategy.exit("TP Long", from_entry=longId, limit=targetPrice)
else if (signal == "SELL")
if useShorts and strategy.position_size == 0
strategy.entry(shortId, strategy.short)
strategy.exit("TP Short", from_entry=shortId, limit=targetPrice)
else
if strategy.position_size != 0
strategy.close_all(comment="Neutral close")
Comments
Post a Comment