NewMACDand/TRAILINGSTOPLOSE
s7s
plain_text
6 months ago
3.8 kB
12
Indexable
//@version=5 indicator(title='MACD Divergence', shorttitle='MACD Divergence') //MACD Histogram source = close fastLengt = input.int(12, title='Fast Length') slowLength = input.int(26, minval=1, title='Slow Length') signalLength = input.int(9, minval=1, title='Signal Length') fastMA = ta.ema(source, fastLength) slowMA = ta.ema(source, slowLength) macd = fastMA - slowMA signal = ta.sma(macd, signalLength) hist = macd - signal outMACD = macd outSignal = signal outHist = hist histA = outHist < outHist[1] and outHist > 0 histB = outHist > outHist[1] and outHist > 0 histC = outHist > outHist[1] and outHist <= 0 histD = outHist < outHist[1] and outHist <= 0 //MACD Histogram Colors macd_above = outMACD >= outSignal macd_below = outMACD < outSignal plot_color = histA ? #B2DFDB : histB ? #26A69A : histC ? #FFCDD2 : histD ? #FF5252 : na macd_color = #2962FF signal_color = #FF6D00 plot(outMACD ? outMACD : na, title='MACD Line' , color=color.new(macd_color, 0)) plot(outSignal ? outSignal : na, title='Signal Line', color=color.new(signal_color, 0)) plot(outHist ? outHist : na, title='Histogram' , color=plot_color, style=plot.style_columns) //Bullish Divergence bullHistB = outHist bullHistB := ()outHist<0 and outHist[1] > 0 ? outHist : outHist > 0 ? 0 : outHist < bullHistB[1] ? outHist : bullHistB[1] bullHistA = bullHistB bullHistA := bullHistB == 0 and bullHistB[1] < 0 ? bullHistB[1] : bullHistA[1] bullPriceB_high = high bullPriceB_high := bullHistB == 0 ? 0 : bullHistB < bullHistB[1] ? high : bullPriceB_high[1] bullPriceB_low = low bullPriceB_low := bullHistB == 0 ? 0 : bullHistB < bullHistB[1] ? low : bullPriceB_low[1] bullPriceA_low = bullPriceB_low bullPriceA_low := bullPriceB_low == 0 and bullPriceB_low[1] > 0 ? bullPriceB_low[1] : bullPriceA_low[1] bullDiv = bullHistA[1] * 0.5 < bullHistB[1] and bullPriceA_low[1] > bullPriceB_high[1] and (bullHistB < 0 and bullHistB == bullHistB[1] and bullHistB != bullHistB[2] or bullHistB == 0 and outHist[1] == bullHistA) bgcolor(bullDiv ? color.lime : na, offset=-1, title='Bullish Divergence') // @version=5 strategy("TheArtOfTrading.com|", overlay=true, margin_long=100, margin_short=100) // Get user input int BAR_LOOKBACK = input.int(10, "Bar Lookback") int ATR_LENGTH = input.int(14, "ATR Length") float ATR_MULTIPLIER = input.float(1.0, "ATR Multiplier") // Get indicator values float atrValue = ta.atr(ATR_LENGTH) // Calculate stop loss values var float trailingStopLoss = na float longStop = ta.lowest(low, BAR_LOOKBACK) - (atrValue * ATR_MULTIPLIER) float shortStop = ta.highest(high, BAR_LOOKBACK) + (atrValue * ATR_MULTIPLIER) // Check if we can take trades bool canTakeTrades = not na(atrValue) bgcolor(canTakeTrades ? na : color.red) // Enter long trades (replace this entry condition) longCondition = canTakeTrades and ta.crossover(ta.sma(close, 14), ta.sma(close, 28)) if (longCondition and strategy.position_size == 0) strategy.entry("Long", strategy.long) // Enter short trades (replace this entry condition) shortCondition = canTakeTrades and ta.crossunder(ta.sma(close, 14), ta.sma(close, 28)) if (shortCondition and strategy.position_size == 0) strategy.entry("Short", strategy.short) // Update trailing stop if (strategy.position_size > 0) if (na(trailingStopLoss) or longStop > trailingStopLoss) trailingStopLoss := longStop else if (strategy.position_size < 0) if (na(trailingStopLoss) or shortStop < trailingStopLoss) trailingStopLoss := shortStop else trailingStopLoss := na // Exit trades with strategy.exit("Long Exit", "Long", stop=trailingStopLoss) strategy.exit("Short Exit", "Short", stop=trailingStopLoss) // Draw stop loss plot(trailingStopLoss, "Stop Loss", color.red, 1, plot.style_linebr)
Editor is loading...