d=IIf( C> Ref( Max(
Max(H, Ref(H,-20)), // max of high and high 20bar back
Max(Ref(H,-10), Ref(H,-15)) // max of high ten bars back and high 15 bar back
// max of high, high 10 bars back, 15 bars back and 20 bars back
), -1), // not counting current bar
Min(
Min(L,Ref(L,-20)),
Min(Ref(L,-10),Ref(L,-15))), // then min of low of current 10 and 15 and 20 bars back
//else
IIf( C< Ref( Min(
Min(L, Ref(L,-20)),
Min(Ref(L,-10),Ref(L,-15))
),-1),
Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),
PREV));
so it means
if ( C > max of high of prev bar, 10+1 bars back, 15+1 bars back and 20+1 bars back)
then
d = min of low of current bar, 10 bars, 15 bars and 20 bars back
else
if ( C < min of low of prev bar, 10+1 bars back, 15+1 bars back and 20+1 bars back)
then
d = max of high of current bar, 10 bars, 15 bars and 20 bars back
else // that is neither of above conditions is satisfied
d = PREV ( which was set to AMA2(C, 1, 0)