OPEN-SOURCE SCRIPT
已更新

InnovateX

193
//version=6
strategy("Candle Stop Strategy Backtest", overlay=true)

// --- EMA Variables ---
ema7_length = 7
ema25_length = 25
ema99_length = 99

ema7 = ta.ema(close, ema7_length)
ema25 = ta.ema(close, ema25_length)
ema99 = ta.ema(close, ema99_length)

// --- Support and Resistance Calculation ---
lookback_support_resistance = 50
support_level = ta.lowest(low, lookback_support_resistance)
resistance_level = ta.highest(high, lookback_support_resistance)

// --- Candle Stop Function ---
is_candle_stop(trend) =>
body = math.abs(close - open)
upper_shadow = high - math.max(open, close)
lower_shadow = math.min(open, close) - low
if trend == "up"
lower_shadow >= 1.5 * body and upper_shadow < body and close > open and math.abs(ema7 - close) < body
else if trend == "down"
upper_shadow >= 1.5 * body and lower_shadow < body and close < open and math.abs(ema7 - close) < body
else
false

// --- Trend Determination ---
trend = ema25 > ema99 ? "up" : ema25 < ema99 ? "down" : "neutral"

// --- Breakout Detection ---
var bool breakout_detected = false
if trend == "up" and close > resistance_level
breakout_detected := true
alert("شکست صعودی تشخیص داده شد! منتظر پولبک 🚀", alert.freq_once_per_bar)
else if trend == "down" and close < support_level
breakout_detected := true
alert("شکست نزولی تشخیص داده شد! منتظر پولبک 📉", alert.freq_once_per_bar)

// --- Entry and Exit Conditions ---
var float position = 0.0
var float entry_price = 0.0
var float stop_loss_price = na
var bool take_profit_long = false // Declare take_profit_long
var bool stop_loss_hit_long = false // Declare stop_loss_hit_long
var bool take_profit_short = false // Declare take_profit_short
var bool stop_loss_hit_short = false// Declare stop_loss_hit_short
risk_per_trade_percent = 2.0 // Risk percentage per trade - adjustable in strategy settings


if not breakout_detected
if position == 0 and is_candle_stop(trend)
risk_amount_usd = strategy.initial_capital * (risk_per_trade_percent / 100)
if trend == "up"
stop_loss_price := math.min(low, support_level * 0.99)
if (close - stop_loss_price) != 0
position_size_usd = risk_amount_usd / (close - stop_loss_price)
amount = position_size_usd / close
strategy.entry("Long", strategy.long, qty=amount)
position := amount
entry_price := close
else if trend == "down"
stop_loss_price := math.max(high, resistance_level * 1.01)
if (stop_loss_price - close) != 0
position_size_usd = risk_amount_usd / (stop_loss_price - close)
amount = position_size_usd / close
strategy.entry("Short", strategy.short, qty=amount)
position := amount
entry_price := close

if position > 0
profit_percent_long = (close - entry_price) / entry_price * 100
profit_percent_short = (entry_price - close) / entry_price * 100
loss_percent_long = (entry_price - close) / entry_price * 100
loss_percent_short = (close - entry_price) / entry_price * 100

risk_reward_long = loss_percent_long != 0 ? profit_percent_long / loss_percent_long : profit_percent_long
risk_reward_short = loss_percent_short != 0 ? profit_percent_short / loss_percent_short : profit_percent_short

take_profit_long := profit_percent_long >= 4 and risk_reward_long >= 2 // Assign value to take_profit_long
stop_loss_hit_long := close <= stop_loss_price // Assign value to stop_loss_hit_long
take_profit_short := profit_percent_short >= 4 and risk_reward_short >= 2 // Assign value to take_profit_short
stop_loss_hit_short := close >= stop_loss_price // Assign value to stop_loss_hit_short


if (trend == "up" and (take_profit_long or stop_loss_hit_long)) or (trend == "down" and (take_profit_short or stop_loss_hit_short))
if trend == "up"
strategy.close("Long")
else
strategy.close("Short")
position := 0
entry_price := 0.0
breakout_detected := false


// --- Plotting EMAs and Support/Resistance Levels ---
plot(ema7, color=color.blue, title="EMA7")
plot(ema25, color=color.red, title="EMA25")
plot(ema99, color=color.green, title="EMA99")
plot(resistance_level, color=color.orange, style=plot.style_line, title="Resistance")
plot(support_level, color=color.orange, style=plot.style_line, title="Support")
發行說明
HELLO

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。