// Number of candles that need to be considered after the SuperTrend Candle has been formed. enterCriteriaOffset = input(1, "Enter Criteria Offset from Supertrend Candle", options=)
rewardToRiskRatio = input(1.5, "Reward to Risk Ratio")
// This is the buffer between the ST line and the candle. We can consider candles which are at a distance of atr/2 from the ST. modifiedAtr = considerAtr ? (atr(atrPeriod) / divideATRby) : 0
// Supertrend line and direction of super trend = supertrend(factor1, atrPeriod1)
// These are used to make additional checks on entry conditions. Depends on input toggle `filterRsi1` wma1 = wma ( rsi (close, rsi1input), wma1input) rsi1 = rsi (close, rsi1input)
// if direction1 is false, then trend is positive and vice versa direction1 = dir == -1 ? false : true
isSuperTrendNegative() => dir == -1 ? false : true
getLongCondition(offset) => close >= superTrendLine1 and low <= (superTrendLine1 + modifiedAtr) and high >= high and (filterRsi1 ? (rsi1 > wma1) : true) and holdingLong1 == false
getShortCondition(offset) => close <= superTrendLine1 and high >= (superTrendLine1 - modifiedAtr) and low <= low and (filterRsi1 ? (rsi1 < wma1) : true) and holdingShort1 == false
// Super Trend Bullish candle (STBullCandle) is a candle where low is lower than supertrend and close is greater than super trend. // Conditions for going Long - // 1. High of current candle should be higher than high of STBullCandle. // 2. We can consider 2 candles after the STBullCandle for this condition. // 3. If filterRsi1 is enabled, then we make an additional check of RSI being greater than the weighted moving average . // 4. Should not be holding an existing long position // 5. Supertrend should be positive
// Super Trend Bearish candle (STBearCandle) is a candle where high is higher than supertrend and close is lower than super trend.
// Conditions for going Short - // 1. Low of current candle should be lower than low of STBullCandle. // 2. We can consider 2 candles after the STBearCandle for this condition. // 3. If filterRsi1 is enabled, then we make an additional check of RSI being lower than the weighted moving average . // 4. Should not be holding an existing short position // 5. Supertrend should be negative.
// GO LONG ! if consolidatedLongPosition and not isSuperTrendNegative() tradeNumber1 := tradeNumber1 + 1 label.new(bar_index, low, text = tostring(tradeNumber1) + " " +tostring(atrPeriod1) + "." + tostring(factor1) + " Buy above " + tostring(longCondition2 ? high : high ), style = label.style_labelup, color = color.green, yloc = yloc.belowbar, textcolor = color.white) array.set(bullishCandleLow1, 0, low) holdingLong1 := true exitHoldingLong1 := false strategy.entry(id="Long", long=true, qty = lotSize) priceFromSL := close - superTrendLine1 targetPrice := close + (priceFromSL * rewardToRiskRatio)
// GO SHORT ! if shortCondition1 and isSuperTrendNegative() tradeNumber1 := tradeNumber1 + 1 label.new(bar_index, high, text = tostring(tradeNumber1) + " " +tostring(atrPeriod1) + "." + tostring(factor1) + " Sell below " + tostring(shortCondition2 ? low : low), style = label.style_labeldown, color = color.red, yloc = yloc.abovebar, textcolor = color.white) array.set(bearishCandleHigh1, 0, high) holdingShort1 := true exitHoldingShort1 := false strategy.entry(id="Short", long=false, qty = lotSize) priceFromSL := superTrendLine1 - close targetPrice := close - (priceFromSL * rewardToRiskRatio)
// If holding long, EXIT LONG Position Criteria - // 1. SL would be if close is lesser than the ST candle low // 2. Direction of ST becomes Negative
if (holdingLong1 == true and exitHoldingLong1 == false and direction1 == true) or (holdingLong1 == true and close >= targetPrice )
if (close < array.get(bullishCandleLow1, 0)) or (close < (superTrendLine1 - modifiedAtr)) or (holdingLong1 == true and close >= targetPrice ) label.new(bar_index, close, text = tostring(tradeNumber1) + " " +tostring(atrPeriod1) + "." + tostring(factor1) + " Exit long @ " + tostring(close), style = label.style_labeldown, color = color.blue, yloc = yloc.abovebar, textcolor = color.white) holdingLong1 := false exitHoldingLong1 := true strategy.close(id="Long")
// If holding short, EXIT SHORT Position Criteria - // 1. SL would be if close is higher than the ST candle high // 2. Direction of ST becomes +ve if (holdingShort1 == true and exitHoldingShort1 == false and direction1 == false) or (holdingShort1 == true and close <= targetPrice )
if close > array.get(bearishCandleHigh1, 0) or (close > (superTrendLine1 + modifiedAtr)) or (holdingShort1 == true and close <= targetPrice ) label.new(bar_index, high, text = tostring(tradeNumber1) + " " +tostring(atrPeriod1) + "." + tostring(factor1) + " Exit short @ " + tostring(close), style = label.style_labelup, color = color.purple, yloc = yloc.belowbar, textcolor = color.white) holdingShort1 := false exitHoldingShort1 := true strategy.close(id="Short")
superTrendColor = isSuperTrendNegative() ? color.red : color.green plot(superTrendLine1, color = superTrendColor, title = "SuperTrend")