A TMF is its own function. You write it separately, and then you assign the function to a trade upon entry.

There are many excellent examples in the manual:
http://zorro-project.com/manual/en/trade.htm

Looking at your example, you can put the for(open_trades) logic in a TMF and assign the TMF to enterLong(TMF_name) and enterShort(TMF_name).