I11L - Better Buy Low Volatility or High Volatility?
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © I11L

//@version=5
strategy("I11L - Better Buy Low Volatility or High Volatility?", overlay=false, pyramiding=1, default_qty_value=1000000, initial_capital=1000000, default_qty_type=strategy.cash,process_orders_on_close=false, calc_on_every_tick=false)

mode = input.string("Buy low Volatility",options = ["Buy low Volatility","Buy high Volatility"])
volatilityTargetRatio = input.float(1,minval = 0, maxval = 100,step=0.1, tooltip="1 equals the average atr for the security, a lower value means that the volatility is lower")
atrLength = input.int(14)

atr = ta.atr(atrLength) / close
avg_atr = ta.sma(atr,atrLength*5)
ratio = atr / avg_atr

sellAfterNBarsLength = input.int(5, step=5, minval=0)


var holdingBarsCounter = 0

if(strategy.opentrades > 0)
    holdingBarsCounter := holdingBarsCounter + 1


isBuy = false

if(mode == "Buy low Volatility")
    isBuy := ratio < volatilityTargetRatio
else
    isBuy := ratio > volatilityTargetRatio

isClose = holdingBarsCounter > sellAfterNBarsLength



if(isBuy)
    strategy.entry("Buy",strategy.long)

if(isClose)
    holdingBarsCounter := 0
    strategy.exit("Close",limit=close)

plot(ratio, color=isBuy[1] ? color.green : isClose[1] ? color.red : color.white)
plot(1, color=color.white)

