[VJ] Mega Supertrend for Intraday
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vikris

//@version=4
strategy("[VJ]Mega ST - Intra Strat", overlay=true, calc_on_every_tick = false)

// ********** Strategy inputs - Start **********

// Used for intraday handling
// Session value should be from market start to the time you want to square-off 
// your intraday strategy
// Important: The end time should be at least 2 minutes before the intraday
// square-off time set by your broker
var i_marketSession = input(title="Market session", type=input.session, 
     defval="0915-1455", confirm=true)

// Make inputs that set the take profit % (optional)
longProfitPerc = input(title="Long Take Profit (%)",
     type=input.float, minval=0.0, step=0.1, defval=1) * 0.01

shortProfitPerc = input(title="Short Take Profit (%)",
     type=input.float, minval=0.0, step=0.1, defval=1) * 0.01
     
// Set stop loss level with input options (optional)
longLossPerc = input(title="Long Stop Loss (%)",
     type=input.float, minval=0.0, step=0.1, defval=0.5) * 0.01

shortLossPerc = input(title="Short Stop Loss (%)",
     type=input.float, minval=0.0, step=0.1, defval=0.5) * 0.01    

var float i_multiplier = input(title = "ST Multiplier", type = input.float, 
     defval = 3, step = 0.1, confirm=true)

var int i_atrPeriod = input(title = "ST ATR Period", type = input.integer, 
     defval = 7, confirm=true)
     
// rsiSource = input(title="RSI Source", type=input.source, defval=close)
// rsiLength = input(title="RSI Length", type=input.integer, defval=14)
// rsiOverbought = input(title="RSI Overbought Level", type=input.integer, defval=70)
// rsiOversold = input(title="RSI Oversold Level", type=input.integer, defval=30)
// // Get RSI value
// rsiValue = rsi(rsiSource, rsiLength)
// //rsi sell
// rsisell = rsiValue >= rsiOverbought

// //rsi buy
// rsibuy = rsiValue <= rsiOversold  


//Get Stochastic values
fast = 20, slow = 50, ultraSlow = 200
fastMA = sma(close, fast)
slowMA = sma(close, slow)
ultraSlowMA = sma(close, ultraSlow)
ema150 = 200
ema150MA = ema(close, ema150)

// smooth = input(3, minval=1), K = input(14, minval=1), D=input(3,minval=1)
// hh=highest(high,K)
// ll=lowest(low,K)
// k = sma((close-ll)/(hh-ll)*100, smooth)
// d = sma(k, 3)
// stochiasticHigh = 80
// stochiasticLow = 20


// ********** Strategy inputs - End **********


// ********** Supporting functions - Start **********

// A function to check whether the bar or period is in intraday session
barInSession(sess) => time(timeframe.period, sess) != 0

// ********** Supporting functions - End **********


// ********** Strategy - Start **********
// See if intraday session is active
bool intradaySession = barInSession(i_marketSession)

// Trade only if intraday session is active




[superTrend, dir] = supertrend(i_multiplier, i_atrPeriod)

colResistance = dir == 1 and dir == dir[1] ? color.new(color.red, 0) : color.new(color.red, 100)
colSupport = dir == -1 and dir == dir[1] ? color.new(color.green, 0) : color.new(color.green, 100)

// plot(superTrend, color = colResistance, linewidth=2)
// plot(superTrend, color = colSupport, linewidth=2)

// Super Trend Long/short condition
stlong = close > superTrend
stshort = close < superTrend

//Vol Confirmation
vol = volume > volume[1]

//Engulfing candle confirm
bullishEC = close > open[1] and close[1] < open[1]
bearishEC = close < open[1] and close[1] > open[1]

//Candles colors
greenCandle = (close > open)
redCandle = (close < open)

// //Stochastic computation
// selldata = close < ema150MA and k>stochiasticHigh and d>stochiasticHigh and close>open
// // plotshape(selldata, style=shape.triangledown, location=location.belowbar, color=color.red)

// buydata2 = close > ema150MA and k<stochiasticLow and d<stochiasticLow and close<open
// // plotshape(buydata2, style=shape.triangleup, location=location.abovebar, color=color.green)

// Figure out take profit price
longExitPrice  = strategy.position_avg_price * (1 + longProfitPerc)
shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)

// Determine stop loss price
longStopPrice  = strategy.position_avg_price * (1 - longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)

 
 
//Final Long/Short Condition
longCondition = stlong and redCandle and vol and vwap > vwap[1]
shortCondition =stshort and greenCandle and vol and vwap < vwap[1]


 
//Long Strategy - buy condition and exits with Take profit and SL
if (longCondition and intradaySession)
    stop_level = longStopPrice
    profit_level = longExitPrice
    strategy.entry("My Long Entry Id", strategy.long)
    strategy.exit("TP/SL", "My Long Entry Id", stop=stop_level, limit=profit_level)


//Short Strategy - sell condition and exits with Take profit and SL
if (shortCondition and intradaySession)
    stop_level = shortStopPrice
    profit_level = shortExitPrice
    strategy.entry("My Short Entry Id", strategy.short)
    strategy.exit("TP/SL", "My Short Entry Id", stop=stop_level, limit=profit_level)
 
 
// Square-off position (when session is over and position is open)
squareOff = (not intradaySession) and (strategy.position_size != 0)
strategy.close_all(when = squareOff, comment = "Square-off")

// ********** Strategy - End **********