Don't Trade in Sideway and Up Trend Market, Trade Only When market is Strong Bearish,…
- User
Don't Trade in Sideway and Up Trend Market, Trade Only When market is Strong Bearish, Write Code Properly, Prevent from issues like Mismatched input 'var' expecting 'end of line without line continuation' : //@version=5 indicator("EMA5 Break Strategy with Correct Trade Outcome Highlighting", overlay=true)
// Parameters ema_length = input.int(5, minval=1, title="EMA Length") atr_length = input.int(14, minval=1, title="ATR Length")
// Risk-Reward Ratio Input risk_reward_ratio = input.float(2.5, minval=0.1, step=0.1, title="Risk-Reward Ratio")
// Time Filter Settings use_time_filter = input.bool(true, title="Use Time Filter?")
session1 = input.session("0930-1600", "Session 1 Time", group="Time Filter Settings") session2 = input.session("2000-2300", "Session 2 Time", group="Time Filter Settings") timezone = "America/New_York"
// Compute EMA ema5 = ta.ema(close, ema_length)
// Compute Daily ATR daily_atr = request.security(syminfo.tickerid, 'D', ta.atr(atr_length))
// Time filter logic var bool is_in_time = true
if use_time_filter is_in_session1 = not na(time(timeframe.period, session1, timezone)) is_in_session2 = not na(time(timeframe.period, session2, timezone)) is_in_time := is_in_session1 or is_in_session2 else is_in_time := true
// Conditions // Current candle's low is above EMA5 and the previous candle's low was at or below EMA5 first_candle_not_touching_ema = low[1] > ema5[1] and low[2] <= ema5[2]
// Price is above EMA5 price_above_ema = close[1] > ema5[1]
// Current candle breaks the low of the previous candle (the first candle not touching EMA) breaks_prev_low = low < low[1]
// Signal when the conditions are met and time is within session signal = first_candle_not_touching_ema and price_above_ema and breaks_prev_low and is_in_time
// Entry, Stop Loss, and Take Profit Prices entry_price = low[1] stop_loss_price = high[1] + (daily_atr * 0.02) risk = stop_loss_price - entry_price take_profit_price = entry_price - (risk * risk_reward_ratio)
// Variables to track trade status var bool in_trade = false var bool trade_won = false var float trade_entry_price = na var float trade_stop_loss = na var float trade_take_profit = na var int trade_entry_bar = na var int trade_exit_bar = na var bool trade_closed_on_this_bar = false
// Variables to track trade statistics var int total_trades = 0 var int wins = 0 var int losses = 0
if signal and not in_trade in_trade := true trade_won := false trade_entry_price := entry_price trade_stop_loss := stop_loss_price trade_take_profit := take_profit_price trade_entry_bar := bar_index trade_exit_bar := na trade_closed_on_this_bar := false
if in_trade // Check if the trade is exited on this bar if high >= trade_stop_loss and low <= trade_take_profit // Both levels were hit in the same bar; we need to decide which was hit first // Since intrabar price movement is unknown, we'll assume worse case (stop loss hit first) trade_won := false in_trade := false trade_exit_bar := bar_index trade_closed_on_this_bar := true else if high >= trade_stop_loss // Stop loss was hit trade_won := false in_trade := false trade_exit_bar := bar_index trade_closed_on_this_bar := true else if low <= trade_take_profit // Take profit was hit trade_won := true in_trade := false trade_exit_bar := bar_index trade_closed_on_this_bar := true // Optional: Exit trade after a maximum number of bars else if bar_index - trade_entry_bar >= 100 in_trade := false trade_won := false trade_exit_bar := bar_index trade_closed_on_this_bar := true else trade_closed_on_this_bar := false else trade_closed_on_this_bar := false
- Assistant