I'm trying to convert the lite C version of MMI into thinkscript so that I can use MMI with think or swim.

I've coded up the following but I'm not 100% sure it's doing what it's supposed to do as the iterator logic is different in thinkscript than anything I've seen. Thinkscript uses fold instead of for loops. See https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold.html

The trickiest part was calculating nh and nl as these are iterated for each price in the lookback series.

The input mmiLength section lets the user define how far back you want to look. I choose 150 as a default value based off of a previous post from JCL that said 100-200 bars is a good compromise.

Here's my solution. Anyone see any problems with it?

input mmiLength = 150;
def m = Median(close, mmiLength);

def nh = fold i = 1 to mmiLength+1 with p = 0 do if close[i] > m and close[i] > getValue(close, i + 1) then p+1 else p+0;

def nl = fold j = 1 to mmiLength+1 with q = 0 do if close[j] < m and close[j] < getValue(close, j + 1) then q+1 else q+0;

plot mmi= 100*(nl+nh)/(mmiLength-1);