See if this works the way you are thinking
Names Default To Here( 1 );
dt = Current Data Table();
colCount = N Cols( dt );
// Loop across all of the columns
For( i = 1, i <= colCount, i++,
// Create the new column and set all values to 0
dt << New Column( "Optimal signal" || Char( i ), set each value( 0 ) );
// Get the name of the column to work on
theColName = Column( dt, i ) << get name;
// Find all of the High Points
Eval(
Parse(
"theRows = dt<<get rows where(lag(:\!"" || theColName || "\!"n,1) <= :\!"" || theColName
|| "\!"n & lag(:\!"" || theColName || "\!"n,-1) <= :\!"" || theColName ||
"\!"n |
(row()==1 & lag(:\!"" || theColName || "\!"n,-1) <= :\!"" || theColName ||
"\!"n) |
(row()==N Rows(dt) & lag(:\!"" || theColName || "\!"n,1) <= :\!"" ||
theColName || "\!"n)
);"
)
);
// Set the found rows to -1
If( N Rows( theRows ) > 0,
Column( dt, N Cols( dt ) )[theRows] = -1
);
// Find all of the Low Points
Eval(
Parse(
"theRows = dt<<get rows where(lag(:\!"" || theColName || "\!"n,1) >= :\!"" || theColName
|| "\!"n & lag(:\!"" || theColName || "\!"n,-1) >= :\!"" || theColName ||
"\!"n |
(row()==1 & lag(:\!"" || theColName || "\!"n,-1) >= :\!"" || theColName ||
"\!"n) |
(row()==N Rows(dt) & lag(:\!"" || theColName || "\!"n,1) >= :\!"" ||
theColName || "\!"n)
);"
)
);
// Set the found rows to 1
If( N Rows( theRows ) > 0,
Column( dt, N Cols( dt ) )[theRows] = 1
);
);
Jim