declare lower; input price = hlc3; input reversalAmount = 0.05; rec state = {default init, undefined, uptrend, downtrend}; rec minMaxPrice; if (getValue(state, 1) == getValue(state.init, 0)) { minMaxPrice = price; state = state.undefined; } else if (getValue(state, 1) == getValue(state.undefined, 0)) { if (price <= getValue(minMaxPrice, 1) - getValue(minMaxPrice, 1) * reversalAmount / 100) { state = state.downtrend; minMaxPrice = price; } else if (price >= getValue(minMaxPrice, 1) + getValue(minMaxPrice, 1) * reversalAmount / 100) { state = state.uptrend; minMaxPrice = price; } else { state = state.undefined; minMaxPrice = getValue(minMaxPrice, 1); } } else if (getValue(state, 1) == getValue(state.uptrend, 0)) { if (price <= getValue(minMaxPrice, 1) - getValue(minMaxPrice, 1) * reversalAmount / 100) { state = state.downtrend; minMaxPrice = price; } else { state = state.uptrend; minMaxPrice = Max(price, getValue(minMaxPrice, 1)); } } else { if (price >= getValue(minMaxPrice, 1) + getValue(minMaxPrice, 1) * reversalAmount / 100) { state = state.uptrend; minMaxPrice = price; } else { state = state.downtrend; minMaxPrice = Min(price, getValue(minMaxPrice, 1)); } } plot “ZZT$” = if state == state.uptrend and !IsNan(close) then 1 else if state == state.downtrend and !IsNan(close) then -1 else Double.NaN; “ZZT$”.SetDefaultColor(GetColor(1)); def Vol = volume; rec countp = if “zzT$” >= 0 then countp[1] + vol else 0; plot upvol = countp; upvol.SetDefaultColor(Color.GReen); upvol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM); rec countn = if “zzT$”<= 0 then countn[1] + vol else 0; plot dnvol = countn; dnvol.SetDefaultColor(Color.red); dnvol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM); # plot VolAvg = Average(upvol + dnvol/2 , 10); # VolAvg.SetDefaultColor(GetColor(8));