Three Week Tight Pattern Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © GreatStockMarketer

//@version=4
study(title="Three Week Tight Pattern", overlay=true)
showLabels = input(title="Show Labels", type=input.bool, defval=true)

// truncate() truncates a given number to a certain number of decimals
truncate(number, decimals) =>
    factor = pow(10, decimals)
    int(number * factor) / factor

thisWkC = security(syminfo.ticker, 'W', close)
oneWkBackC = security(syminfo.ticker, 'W', close[1])
TwoWkBackC = security(syminfo.ticker, 'W', close[2])
ThreeWkBackC = security(syminfo.ticker, 'W', close[3])

pcChange2wk = truncate((oneWkBackC/TwoWkBackC),3)
pcChange1wk = truncate((thisWkC/TwoWkBackC),3)
pcChange0wk = truncate((thisWkC/oneWkBackC),3)

is0wkTight = (pcChange0wk < 1.015 and pcChange0wk > 0.985)? true: false
is1wkTight = (pcChange1wk < 1.015 and pcChange1wk > 0.985)? true: false
is2wkTight = (pcChange2wk < 1.015 and pcChange2wk > 0.985)? true: false

isthreeWeekTF = is0wkTight and is1wkTight and is2wkTight

if isthreeWeekTF and showLabels
    label1 = label.new(bar_index[0], high[0], text="3W Tight", style=label.style_labeldown, color=color.white)
