OPEN-SOURCE SCRIPT

E-mini S&P 500 Momentum Bot with Enhancements

89
default_qty_value=1)

// Inputs
lookback_period = input.int(2, "Lookback Period", minval=1, step=1) // Candles to check momentum
threshold_percent = input.float(0.05, "Threshold %", minval=0.05, step=0.05) // Momentum threshold
stop_loss_points = input.float(3, "Stop Loss (Points)", minval=1, step=1) // Stop loss in points
take_profit_points = input.float(25, "Take Profit (Points)", minval=1, step=1) // Take profit in points
qty = input.int(3, "Contracts", minval=1, step=1) // Number of contracts

// Define smoothing for momentum
sma_price_change = ta.sma(close - close[lookback_period], lookback_period)
sma_price_change_percent = (sma_price_change / close[lookback_period]) * 100

// Trend Filter (using a 50-period Simple Moving Average)
trend_filter = ta.sma(close, 49)

// Define signals
buy_signal = sma_price_change_percent > threshold_percent and close > trend_filter and strategy.position_size == 0
sell_signal = sma_price_change_percent < -threshold_percent and close < trend_filter and strategy.position_size == 0

// Execute trades with explicit order management (v6 feature)
if buy_signal
strategy.order("Long Entry", strategy.long, qty, stop=close - stop_loss_points, limit=close + take_profit_points)
if sell_signal
strategy.order("Short Entry", strategy.short, qty, stop=close + stop_loss_points, limit=close - take_profit_points)

// Trailing Stop (to lock profits)
trail_offset = 5 // Trailing stop offset (in points)
trail_price = strategy.position_size > 0 ? close - trail_offset : strategy.position_size < 0 ? close + trail_offset : na
strategy.exit("Exit Long", from_entry="Long Entry", trail_price=trail_price, trail_offset=trail_offset)
strategy.exit("Exit Short", from_entry="Short Entry", trail_price=trail_price, trail_offset=trail_offset)

// Close positions based on stop-loss or take-profit (handled automatically by v6 order params)
// Optional: Manual close if needed
if strategy.position_size > 0 and sell_signal
strategy.close("Long Entry")
if strategy.position_size < 0 and buy_signal
strategy.close("Short Entry")

// Visualize signals
plotshape(buy_signal, title="Buy", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(sell_signal, title="Sell", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
plot(trend_filter, color=color.blue, linewidth=1, title="50-period SMA (Trend Filter)")

כתב ויתור

המידע והפרסומים אינם אמורים להיות, ואינם מהווים, עצות פיננסיות, השקעות, מסחר או סוגים אחרים של עצות או המלצות שסופקו או מאושרים על ידי TradingView. קרא עוד בתנאים וההגבלות.