There is an excellent example of how to do this. Read this blog article by jcl:
https://financial-hacker.com/trend-and-exploiting-it/

The function filter() is redefined many times in a large scale test, using batch scripting. Definitely give it a look.