# My indicator

NSE:TATAMOTORS   TATA MOTORS LTD
//@version=4
// l
//
strategy("Dr Om's ST Indicator v1.0", overlay = true, calc_on_every_tick=true)

atrPeriod1 = input(10, "ATR Length")
factor1 = input(3, "Factor")
rsi1input = input(50, "RSI")
wma1input = input(20, "WMA")

considerAtr = input(title="Consider ATR", type=input.bool, defval=false)
divideATRby = input(1.0, "ATR Buffer from ST (Divide ATR by)")
atrPeriod = input(14, "ATR period")

lotSize = input(100, "Lot size")
filterRsi1 = input(title="Consider RSI and WMA", type=input.bool, defval=false)

// 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")

varip holdingLong1 = false
varip holdingShort1 = false

varip exitHoldingLong1 = false
varip exitHoldingShort1 = false

varip targetPrice = 0.0
varip priceFromSL = 0.0

targetPercentage = 2

// 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

consolidatedLongPosition = getLongCondition(1)

longCondition1 = getLongCondition(1)
longCondition2 = getLongCondition(2)

consolidatedLongPosition := getLongCondition(1) or ( enterCriteriaOffset == 2 ? getLongCondition(2) : false)

// 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.

consolidatedShortPosition = getShortCondition(1)

shortCondition1 = getShortCondition(1)
shortCondition2 = getShortCondition(2)

consolidatedShortPosition := getShortCondition(1) or ( enterCriteriaOffset == 2 ? getShortCondition(2) : false)

bearishCandleHigh1 = array.new_float(1)
bullishCandleLow1 = array.new_float(1)

// GO LONG !
if consolidatedLongPosition and not isSuperTrendNegative()
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()
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")

plot(superTrendLine1 + (considerAtr ? (1 * modifiedAtr) : 0), color = considerAtr ? #FFFFFF4F : #00000000 , title = "SuperTrend Buffer Line")
plot(superTrendLine1 + (considerAtr ? (-1 * modifiedAtr) : 0), color = considerAtr ? #FFFFFF4F : #00000000 , title = "SuperTrend Buffer Line")
Disclaimer