[VJ] Viper VWAP 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] Viper VWAP Intraday", 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    
     




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


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

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


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

cilength = input(title="Length", type=input.integer, defval=14, minval=1, maxval=2000)
src = hlc3
upper = sum(volume * (change(src) <= 0 ? 0 : src), cilength)
lower = sum(volume * (change(src) >= 0 ? 0 : src), cilength)
_rsi(upper, lower) =>
    100.0 - (100.0 / (1.0 + upper / lower))
mf = _rsi(upper, lower)
ci = 100 * log10(sum(atr(1), cilength) / (highest(cilength) - lowest(cilength))) / log10(cilength)
// bcod = mf < 25 and ci <50 
// scod = mf >75 and ci <50 
citrendfactor = input(title="Trend Factor", type=input.float, defval=38.2, minval=1.0, maxval=100.0)
bcod = mf < 25 
scod = mf >75


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



// See if intraday session is active
bool intradaySession = barInSession(i_marketSession)

// Trade only if intraday session is active
 
 
//Final Long/Short Condition
longCondition = redCandle and vol and vwap > vwap[1] and ci <citrendfactor
shortCondition =greenCandle and vol and vwap < vwap[1] and ci <citrendfactor

 
// Long position
// When longCondition and intradaySession both are true
strategy.entry(id = "Long", long = strategy.long, 
     when = longCondition and intradaySession)
 
// Short position
// When shortCondition and intradaySession both are true
strategy.entry(id = "Short", long = strategy.short, 
     when = shortCondition and intradaySession)
 
// 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 **********