本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Scalping Tool with Dynamic TP, EMA, and ATR [v6]", overlay=true)
// Input parameters
fast_length = input.int(5, title="Fast Length", minval=1)
slow_length = input.int(13, title="Slow Length", minval=1)
atr_length = input.int(14, title="ATR Length", minval=1)
// Take profit and stop loss inputs for Buy orders
buy_take_profit_enabled = input.bool(true, title="Enable Buy Take Profit")
buy_take_profit_percent = input.float(0.3, title="Buy Take Profit (%)", step=0.1) / 100
buy_stop_loss_enabled = input.bool(true, title="Enable Buy Stop Loss")
buy_stop_loss_multiplier = input.float(1.5, title="Buy Stop Loss ATR Multiplier", step=0.1)
// Take profit and stop loss inputs for Sell orders
sell_take_profit_enabled = input.bool(true, title="Enable Sell Take Profit")
sell_take_profit_percent = input.float(0.3, title="Sell Take Profit (%)", step=0.1) / 100
sell_stop_loss_enabled = input.bool(true, title="Enable Sell Stop Loss")
sell_stop_loss_multiplier = input.float(1.5, title="Sell Stop Loss ATR Multiplier", step=0.1)
// Calculate Fast and Slow Moving Averages
fast_ma = ta.ema(close, fast_length)
slow_ma = ta.ema(close, slow_length)
// Calculate ATR
atr = ta.atr(atr_length)
// Determine trend direction
trend_up = ta.crossover(fast_ma, slow_ma)
trend_down = ta.crossunder(fast_ma, slow_ma)
// Track entry price and trade state
var float long_entry_price = na
var float short_entry_price = na
var bool in_long_trade = false
var bool in_short_trade = false
// Track dynamic TP levels
var float long_take_profit = na
var float short_take_profit = na
// Generate signals
long_signal = trend_up and not in_long_trade
short_signal = trend_down and not in_short_trade
// Update entry price and trade state for long trades
if long_signal
long_entry_price := close
in_long_trade := true
long_take_profit := buy_take_profit_enabled ? long_entry_price * (1 + buy_take_profit_percent) : na
// Update entry price and trade state for short trades
if short_signal
short_entry_price := close
in_short_trade := true
short_take_profit := sell_take_profit_enabled ? short_entry_price * (1 - sell_take_profit_percent) : na
// Reset long trade state when trend reverses
if trend_down and in_long_trade
in_long_trade := false
long_entry_price := na
long_take_profit := na
// Reset short trade state when trend reverses
if trend_up and in_short_trade
in_short_trade := false
short_entry_price := na
short_take_profit := na
// Dynamic TP recalculation for long trades
if in_long_trade and buy_take_profit_enabled
if close >= long_take_profit
// Recalculate TP based on the new close price
long_take_profit := close * (1 + buy_take_profit_percent)
// Dynamic TP recalculation for short trades
if in_short_trade and sell_take_profit_enabled
if close <= short_take_profit
// Recalculate TP based on the new close price
short_take_profit := close * (1 - sell_take_profit_percent)
// Calculate stop loss levels for Buy and Sell orders
buy_stop_loss = buy_stop_loss_enabled and in_long_trade ? long_entry_price - atr * buy_stop_loss_multiplier : na
sell_stop_loss = sell_stop_loss_enabled and in_short_trade ? short_entry_price + atr * sell_stop_loss_multiplier : na
// Plot Moving Averages
plot(fast_ma, color=color.new(color.blue, 0), title="Fast MA", linewidth=1)
plot(slow_ma, color=color.new(color.red, 0), title="Slow MA", linewidth=1)
// Plot buy/sell signals
plotshape(series=long_signal, title="Buy Signal", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, text="BUY", size=size.small)
plotshape(series=short_signal, title="Sell Signal", location=location.abovebar, color=color.new(color.red, 0), style=shape.labeldown, text="SELL", size=size.small)
// Plot take profit and stop loss levels for Buy orders
plot(series=in_long_trade ? long_take_profit : na, title="Buy Take Profit", color=color.new(color.green, 0), linewidth=1, style=plot.style_linebr)
plot(series=in_long_trade ? buy_stop_loss : na, title="Buy Stop Loss", color=color.new(color.orange, 0), linewidth=1, style=plot.style_linebr)
// Plot take profit and stop loss levels for Sell orders
plot(series=in_short_trade ? short_take_profit : na, title="Sell Take Profit", color=color.new(color.red, 0), linewidth=1, style=plot.style_linebr)
plot(series=in_short_trade ? sell_stop_loss : na, title="Sell Stop Loss", color=color.new(color.purple, 0), linewidth=1, style=plot.style_linebr)
// Alerts
alertcondition(long_signal, title="Buy Signal", message="Buy Signal")
alertcondition(short_signal, title="Sell Signal", message="Sell Signal")