Hey everyone, could someone help me transform this code into a scan? I’m basically looking for stocks that have printed a higher high (HH). @antwerks any ideas?
Python:
# TMO
def TMO_length = 14;
def TMO_calcLength = 5;
def TMO_smoothLength = 3;
def pivotBars = 3;
def na = Double.NaN;
script FindPivot {
input src = close;
input bars = 5; # lookback window
input isHigh = yes; # yes = high, no = low
def bn = BarNumber();
def na = Double.NaN;
# check that src is extreme vs. the last 'length' bars
def isExtreme = fold i = 1 to bars
with ok = yes
while ok
do if isHigh
then src > GetValue(src, -i)
else src < GetValue(src, -i);
def pivot =
if bn > bars
&& isExtreme
&& ( if isHigh
then src == Highest(src, bars)
else src == Lowest(src, bars))
then src
else na;
plot pPivot = pivot;
}
# ##################################################
# True Momentum Oscilator
def TMO_os = -Round(TMO_length * 0.7);
def TMO_ob = Round(TMO_length * 0.7);
def TMO_Data =
fold i = 0 to TMO_length
with acc
do acc + if close > GetValue(open, i) then 1
else if close < GetValue(open, i) then -1
else 0;
def EMA5 = ExpAverage(close, TMO_calcLength);
def TMO_main = ExpAverage(EMA5, TMO_smoothLength);
def crossedUpOB = TMO_main crosses above TMO_ob;
def crossedDownOB = TMO_main crosses below TMO_ob;
rec inOBZone = CompoundValue(1,
if crossedUpOB then 1
else if crossedDownOB then 0
else inOBZone[1],
0);
def pivotOffset = Floor(pivotBars / 2);
def rawHighPivot = FindPivot(src=TMO_main, bars=pivotBars, isHigh=yes).pPivot;
# only accept highs in overbought, lows in oversold
def lastHighVal= if inOBZone then rawHighPivot else na;
# When a fresh pivot occurs, shift last→prev then capture new pivot
def isNewHighPivot = !IsNaN(lastHighVal) && IsNaN(lastHighVal[1]);
# high data
rec rlastHighVal = if isNewHighPivot then lastHighVal else rlastHighVal[1];
rec prevHighVal = if isNewHighPivot then rlastHighVal[1] else prevHighVal[1];
# High pivots (higher‐high or lower‐high)
def TMO_HH = lastHighVal > prevHighVal;
plot scan = (TMO_HH within 7 bars);