First of all, never put optimize() and series() calls inside of if(Train). This will screw up your script.

Next, your traincycles() call should be inside of if(Algo == "TRND"). So either tradeTrend() would have the traincycle as an argument, or you could even call traincycles() inside of tradeTrend().