[KL] Relative Volume Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DojiEmoji (kevinhhl)

//@version=4
strategy("[KL] Relative Volume Strategy",overlay=true,pyramiding=1)
ENUM_LONG = "Long"

// Timeframe {
backtest_timeframe_start = input(defval = timestamp("01 Apr 2016 13:30 +0000"), title = "Backtest Start Time", type = input.time)
USE_ENDTIME = input(false,title="Define backtest end-time (If false, will test up to most recent candle)")
backtest_timeframe_end = input(defval = timestamp("01 May 2021 19:30 +0000"), title = "Backtest End Time (if checked above)", type = input.time)
within_timeframe = time >= backtest_timeframe_start and (time <= backtest_timeframe_end or not USE_ENDTIME)
// }

// Volatility Indicators {
BOLL_length = 20, BOLL_src = close, SMA20 = sma(BOLL_src, BOLL_length), BOLL_sDEV_x2 = 2 * stdev(BOLL_src, BOLL_length)
BOLL_upper = SMA20 + BOLL_sDEV_x2, BOLL_lower = SMA20 - BOLL_sDEV_x2
plot(SMA20, "Basis", color=#872323, offset = 0)
BOLL_p1 = plot(BOLL_upper, "BOLL Upper", color=color.new(color.navy,50), offset = 0)
BOLL_p2 = plot(BOLL_lower, "BOLL Lower", color=color.new(color.navy,50), offset = 0)
fill(BOLL_p1, BOLL_p2, title = "Background", color=color.new(#198787,90))

ATR_X2_volat = atr(input(10,title="Length of ATR to determine volality"))*2
plot(SMA20+ATR_X2_volat, "SMA20 + ATR_X2_volat", color=color.new(color.gray,75), offset = 0)
plot(SMA20-ATR_X2_volat, "SMA20 - ATR_X2_volat", color=color.new(color.gray,75), offset = 0)
plotchar(ATR_X2_volat, "ATR_X2_volat", "", location = location.bottom)
// }

// Trailing stop loss {
ATR_X2_TSL = atr(input(10,title="Length of ATR for trailing stop loss")) * input(2.0,title="ATR Multiplier for trailing stop loss",type=input.float)
TSL_source = low
var stop_loss_price = float(0)
TSL_line_color = color.green, TSL_transp = 100
if strategy.position_size == 0 or not within_timeframe
    TSL_line_color := color.black
    stop_loss_price := TSL_source - ATR_X2_TSL 
else if strategy.position_size > 0
    stop_loss_price := max(stop_loss_price, TSL_source - ATR_X2_TSL)
    TSL_transp := 0
plot(stop_loss_price, color=color.new(TSL_line_color, TSL_transp))
// }

// Signals for entry {
_avg_vol = sma(volume,input(5, title="SMA(volume) length (for relative comparison)"))
_relative_vol = _avg_vol * input(1,title="Multiple of avg vol to consider relative volume as being high",type=input.float)
__lowerOfOpenClose = min(open,close)
_wickRatio_lower = (__lowerOfOpenClose - low) / (high - low)
entry_signal1 = volume > _relative_vol
entry_signal2 = ATR_X2_volat > BOLL_sDEV_x2
entry_signal3 = _wickRatio_lower > input(0.1,title="Wick ratio (lower:high-low) of entry bar",type=input.float)
// }

// MAIN:
if within_timeframe

    // ENTRY :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	if entry_signal1 and entry_signal2 and entry_signal3
		entry_msg = strategy.position_size > 0 ? "adding" : "initial"

		strategy.entry(ENUM_LONG, strategy.long, comment=entry_msg)

    // EXIT ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	exit_msg = close <= strategy.position_avg_price ? "stop loss" : "take profit"
	if strategy.position_size > 0
		exit_condition = TSL_source <= stop_loss_price
        strategy.close(ENUM_LONG, when=exit_condition, comment=exit_msg)

// CLEAN UP:
if strategy.position_size == 0
	stop_loss_price := float(0)
