Ok. If I understood you right, your initial problem was that your trades close too early. And attempts to fix this by adding more code brought you instead more new problems.

For fixing the closing, I don't see what the problem is. Just look in the log, check why and when a trade closes, and if it's no good, change your TP or whatever else caused the closing. This is not a script problem but an optimization. Only real script problems require debugging.

If you don't understand what your trades are doing, make the script simpler, not more complicated. If the TMF gave you three new problems and is not needed anyway, delete it. Do not add new functions until you have understood what the current functions do. Also delete the if(INITRUN) line and the brackets - they probably do no harm, but are nonsense. if() is a conditional branch in a function, not a global variables section.