//@version=5 strategy("MACD Enhanced Strategy MTF with Stop Loss [LTB]", overlay=true) // Input Parameters fastLength = input.int(12, title="MACD Fast Length", minval=1) slowLength = input.int(26, title="MACD Slow Length", minval=1) signalLength = input.int(9, title="MACD Signal Length", minval=1) crossscore = input.float(10, title="Cross (buy/sell) Score") indiside = input.float(8, title="Indicator Direction Score") histside = input.float(2, title="Histogram Direction Score") shotsl = input.bool(false, title="Show Stop Loss Line") Mult = input.float(1.2, title="Stop Loss Factor", minval=0.1, maxval=100) Period = input.int(10, title="Stop Loss Period", minval=1, maxval=100) lookaheadi = input.bool(true, title="Lookahead") // Higher Time Frame Selection var string HTF = '' if (timeframe.period == '1') HTF := '5' else if (timeframe.period == '3') HTF := '15' else if (timeframe.period == '5') HTF := '15' else if (timeframe.period == '15') HTF := '60' else if (timeframe.period == '30') HTF := '60' else if (timeframe.period == '45') HTF := '60' else if (timeframe.period == '60') HTF := '240' else if (timeframe.period == '120') HTF := '240' else if (timeframe.period == '180') HTF := '240' else if (timeframe.period == '240') HTF := 'D' else if (timeframe.period == 'D') HTF := 'W' else HTF := 'W' var float calc = na if (timeframe.period == '1') calc := 5 else if (timeframe.period == '3') calc := 5 else if (timeframe.period == '5') calc := 3 else if (timeframe.period == '15') calc := 4 else if (timeframe.period == '30') calc := 4 else if (timeframe.period == '45') calc := 4 else if (timeframe.period == '60') calc := 4 else if (timeframe.period == '120') calc := 3 else if (timeframe.period == '180') calc := 3 else if (timeframe.period == '240') calc := 6 else if (timeframe.period == 'D') calc := 5 else calc := 1 // Scoring Function count() => indi = ta.ema(close, fastLength) - ta.ema(close, slowLength) signal = ta.ema(indi, signalLength) hist = indi - signal Anlyse = 0.0 Anlyse += indi > indi[1] ? (hist > hist[1] ? indiside + histside : indiside) : (hist == hist[1] ? indiside : indiside - histside) Anlyse += indi < indi[1] ? (hist < hist[1] ? -(indiside + histside) : -indiside) : (hist == hist[1] ? -indiside : -(indiside - histside)) Anlyse += indi == indi[1] ? (hist > hist[1] ? histside : (hist < hist[1] ? -histside : 0)) : 0 countcross = indi >= signal and indi[1] < signal[1] ? crossscore : (indi <= signal and indi[1] > signal[1] ? -crossscore : 0.0) countcross += nz(countcross[1]) * 0.6 Anlyse += countcross nz(Anlyse) // Calculate Scores Anlys = count() AnlysHfrm = request.security(syminfo.tickerid, HTF, count(), lookahead = (lookaheadi ? barmerge.lookahead_on : barmerge.lookahead_off)) Result = (AnlysHfrm * calc + Anlys) / (calc + 1) // Entry Conditions longCondition = ta.change(Result) != 0 and Result > 0 shortCondition = ta.change(Result) != 0 and Result < 0 if (longCondition) strategy.entry("MACD Long", strategy.long) if (shortCondition) strategy.entry("MACD Short", strategy.short) // Stop Loss Calculation countstop(pos) => Upt = hl2 - Mult * ta.atr(Period) Dnt = hl2 + Mult * ta.atr(Period) var float TUp = na var float TDown = na TUp := close[1] > nz(TUp[1]) ? math.max(Upt, nz(TUp[1])) : Upt TDown := close[1] < nz(TDown[1]) ? math.min(Dnt, nz(TDown[1])) : Dnt tslmtf = pos == 1 ? TUp : TDown tslmtf pos = longCondition ? 1 : -1 var float stline = na countstop__1 = countstop(pos) security_1 = request.security(syminfo.tickerid, HTF, countstop__1) stline := ta.change(time(HTF)) != 0 or longCondition or shortCondition ? security_1 : nz(stline[1]) plot(stline, color=shotsl ? color.gray : na, style=plot.style_line, linewidth=2, title="Stop Loss")