Vortex with TSI strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hydrelev

//@version=4
strategy("Vortex TSI strategy", overlay=false)
///////////////////INDICATOR TSI
long = input(title="Long Length", type=input.integer, defval=25)
short = input(title="Short Length", type=input.integer, defval=13)
signal = input(title="Signal Length", type=input.integer, defval=13)
price = close
double_smooth(src, long, short) =>
    fist_smooth = ema(src, long)
    ema(fist_smooth, short)
pc = change(price)
double_smoothed_pc = double_smooth(pc, long, short)
double_smoothed_abs_pc = double_smooth(abs(pc), long, short)
tsi_blue = 100 * (double_smoothed_pc / double_smoothed_abs_pc)
tsi_red = ema(tsi_blue, signal)
// plot(tsi_blue, color=#3BB3E4)
// plot(tsi_red, color=#FF006E)
// hline(0, title="Zero")

/////////////////INDICATOR VI
period_ = input(14, title="Period", minval=2)
VMP = sum( abs( high - low[1]), period_ )
VMM = sum( abs( low - high[1]), period_ )
STR = sum( atr(1), period_ )
VIP_blue = VMP / STR
VIM_red = VMM / STR
// plot(VIP_blue, title="VI +", color=#3BB3E4)
// plot(VIM_red, title="VI -", color=#FF006E)

////////////////////STRATEGY
bar=input(1, title="Close after x bar", minval=1, maxval=50)

tsi_long = crossover(tsi_blue, tsi_red)
tsi_short = crossunder(tsi_blue, tsi_red)
vi_long = crossover(VIP_blue, VIM_red)
vi_short = crossunder(VIP_blue, VIM_red)

LongConditionOpen = tsi_long and vi_long ? true : false
LongConditionClose = tsi_long[bar] and vi_long[bar] ? true : false
ShortConditionOpen = tsi_short and vi_short ? true : false
ShortConditionClose = tsi_short[bar] and vi_short[bar] ? true : false

if (LongConditionOpen)
    strategy.entry("Long Entry", strategy.long)
if (LongConditionClose)
    strategy.close("Long Entry")

if (ShortConditionOpen)
    strategy.entry("Short Entry", strategy.short)
if (ShortConditionClose)
    strategy.close("Short Entry")