//@version=6 strategy("Advanced MACD-RSI Strategy", "MACD-RSI", overlay=true) // Enums enum FirstCondition RSI = "RSI" MACD = "MACD" ANY = "ANY" enum SmoothingType SMA = "SMA" WMA = "WMA" EMA = "EMA" HMA = "HMA" RMA = "RMA" VWMA = "VWMA" // Inputs RSI_SETTINGS_GROUP = "RSI SETTINGS" RSI_SOURCE = input.source(close, "Source", group=RSI_SETTINGS_GROUP) RSI_LENGTH = input.int(14, "RSI Length", group=RSI_SETTINGS_GROUP) SMOOTHING_TYPE = input.enum(SmoothingType.SMA, "Smoothing Type", group=RSI_SETTINGS_GROUP) SMOOTHING_LENGTH = input.int(14, "Smoothing Length", group=RSI_SETTINGS_GROUP) MACD_SETTINGS_GROUP = "MACD SETTINGS" MACD_SOURCE = input.source(close, "Source", group=MACD_SETTINGS_GROUP) MACD_FAST_LENGTH = input.int(12, "Fast Length", group=MACD_SETTINGS_GROUP) MACD_SLOW_LENGTH = input.int(26, "Slow Length", group=MACD_SETTINGS_GROUP) MACD_SIGNAL_LENGTH = input.int(9, "Signal Length", group=MACD_SETTINGS_GROUP) STRATEGY_SETTINGS_GROUP = "Strategy Settings" FIRST_CONDITION = input.enum(FirstCondition.RSI, "First Condition", group=STRATEGY_SETTINGS_GROUP) CONDITION_DELAY = input.int(10, "Condition Delay", group=STRATEGY_SETTINGS_GROUP, tooltip="Delay for other condition met") STOPLOSS_TICK = input.int(10000, "Stoploss Tick", group=STRATEGY_SETTINGS_GROUP) * syminfo.mintick RISK_RATIO = input.float(1.5, "Risk Ratio", group=STRATEGY_SETTINGS_GROUP) PARTIAL_TP_RR = input.float(1.0, "R:R For Partial TP", group=STRATEGY_SETTINGS_GROUP) PARTIAL_TP_QTY_PERC = input.float(50.0, "Partial TP Qty Perc", group=STRATEGY_SETTINGS_GROUP) NEW_STOPLOSS_TICK = input.int(5000, "New Stoploss Tick", group=STRATEGY_SETTINGS_GROUP, tooltip="New Stoploss Tick After Partial TP") * syminfo.mintick DAILY_TRADE_LIMIT = 3 rsi = ta.rsi(RSI_SOURCE, RSI_LENGTH) [macd_line, signal_line, histogram] = ta.macd(MACD_SOURCE, MACD_FAST_LENGTH, MACD_SLOW_LENGTH, MACD_SIGNAL_LENGTH) DEBUG_MODE = input.bool(false, "DEBUG MODE (For Developer Only)") // Functions condition_check(bool condition, int window) => result = false for i=0 to window if condition[i] result := true result // Logic rsi_smoothing = switch SMOOTHING_TYPE SmoothingType.SMA => ta.sma(rsi, SMOOTHING_LENGTH) SmoothingType.EMA => ta.ema(rsi, SMOOTHING_LENGTH) SmoothingType.WMA => ta.wma(rsi, SMOOTHING_LENGTH) SmoothingType.RMA => ta.rma(rsi, SMOOTHING_LENGTH) SmoothingType.VWMA => ta.vwma(rsi, SMOOTHING_LENGTH) SmoothingType.HMA => ta.hma(rsi, SMOOTHING_LENGTH) rsi_long_condition = rsi < 50 and ta.crossover(rsi, rsi_smoothing) rsi_short_condition = rsi > 50 and ta.crossunder(rsi, rsi_smoothing) macd_long_condition = ta.crossover(macd_line, signal_line) and macd_line < 0 macd_short_condition = ta.crossunder(macd_line, signal_line) and macd_line > 0 long_condition = switch FIRST_CONDITION FirstCondition.RSI => macd_long_condition and condition_check(rsi_long_condition, CONDITION_DELAY) FirstCondition.MACD => rsi_long_condition and condition_check(macd_long_condition, CONDITION_DELAY) => (macd_long_condition and condition_check(rsi_long_condition, CONDITION_DELAY)) or (rsi_long_condition and condition_check(macd_long_condition, CONDITION_DELAY)) short_condition = switch FIRST_CONDITION FirstCondition.RSI => macd_short_condition and condition_check(rsi_short_condition, CONDITION_DELAY) FirstCondition.MACD => rsi_short_condition and condition_check(macd_short_condition, CONDITION_DELAY) => (macd_short_condition and condition_check(rsi_short_condition, CONDITION_DELAY)) or (rsi_short_condition and condition_check(macd_short_condition, CONDITION_DELAY)) qty = strategy.default_entry_qty(close) var partial_tp_taken = false var daily_trade_count = 0 var trade_dairy = array.new_bool(3) daily_condition = daily_trade_count < DAILY_TRADE_LIMIT if daily_trade_count == 2 and trade_dairy.get(0) and trade_dairy.get(1) qty := qty / 2 if daily_trade_count == 2 and not trade_dairy.get(0) and not trade_dairy.get(1) daily_condition := false if dayofweek != dayofweek[1] daily_trade_count := 0 in_position = strategy.position_size != 0 avg_price = strategy.position_avg_price in_long = strategy.position_size > 0 in_short = strategy.position_size < 0 if in_long and close > avg_price + STOPLOSS_TICK * PARTIAL_TP_RR and not partial_tp_taken and in_position strategy.close("Long", "Partial TP", qty_percent=PARTIAL_TP_QTY_PERC) strategy.cancel("Long Exit") strategy.exit("Long Exit", "Long", stop=avg_price - STOPLOSS_TICK / 2, limit=avg_price + STOPLOSS_TICK * RISK_RATIO) partial_tp_taken := true if in_short and close < avg_price - STOPLOSS_TICK * PARTIAL_TP_RR and not partial_tp_taken and in_position strategy.close("Short", "Partial TP", qty_percent=PARTIAL_TP_QTY_PERC) strategy.cancel("Short Exit") strategy.exit("Short Exit", "Short", stop=avg_price + STOPLOSS_TICK / 2, limit=avg_price - STOPLOSS_TICK * RISK_RATIO) partial_tp_taken := true if long_condition and not in_position and daily_condition strategy.entry("Long", strategy.long, qty) strategy.exit("Long Exit", "Long", stop=close - STOPLOSS_TICK, limit= close + STOPLOSS_TICK * RISK_RATIO) partial_tp_taken := false daily_trade_count += 1 if short_condition and not in_position and daily_condition strategy.entry("Short", strategy.short, qty) strategy.exit("Short Exit", "Short", stop=close + STOPLOSS_TICK, limit=close - STOPLOSS_TICK * RISK_RATIO) partial_tp_taken := false daily_trade_count += 1 if not in_position and in_position last_profit = strategy.closedtrades.profit(strategy.closedtrades - 1) trade_dairy.set(daily_trade_count - 1, last_profit > 0 ? true : false) // Display plotshape(DEBUG_MODE and rsi_long_condition, "RSI Long", shape.triangleup, location.belowbar, color.green, text="RSI", size=size.small) plotshape(DEBUG_MODE and rsi_short_condition, "RSI Short", shape.triangledown, location.abovebar, color.red, text="RSI", size=size.small) plotshape(DEBUG_MODE and macd_long_condition, "MACD Long", shape.triangleup, location.belowbar, color.green, text="MACD", size=size.small) plotshape(DEBUG_MODE and macd_short_condition, "MACD Long", shape.triangledown, location.abovebar, color.red, text="MACD", size=size.small) plotshape(DEBUG_MODE and long_condition, "Long Entry", shape.triangleup, location.belowbar, color.green, text="Long Entry", size=size.small) plotshape(DEBUG_MODE and short_condition, "Short Entry", shape.triangledown, location.abovebar, color.red, text="Short Entry", size=size.small) plot(in_position ? avg_price : na, "Avg Price", color.orange, style=plot.style_linebr) plot(in_position ? in_long ? avg_price - (partial_tp_taken ? STOPLOSS_TICK / 2 : STOPLOSS_TICK) : avg_price + (partial_tp_taken ? STOPLOSS_TICK / 2 : STOPLOSS_TICK) : na, "Stop Price", color.red, style=plot.style_linebr) plot(in_position and not partial_tp_taken ? in_long ? avg_price + STOPLOSS_TICK * PARTIAL_TP_RR : avg_price - STOPLOSS_TICK * PARTIAL_TP_RR : na, "Partial TP", color.green, style=plot.style_linebr) plot(in_position ? in_long ? avg_price + STOPLOSS_TICK * RISK_RATIO : avg_price - STOPLOSS_TICK * RISK_RATIO : na, "Tp", color.green, style=plot.style_linebr)