The Error 040 is because you called optimize() only in the first run. It is not allowed to have a different number of optimize calls in different runs.

As to why you got identical results, I'll have to ask. Maybe once the bars are built, subsequent changes to BarPeriod have no effect. If so, then you have to use TimeFrame instead of BarPeriod for optimizing the bar period:

Code:
function run()
{
	BarPeriod = 60;
	set(PARAMETERS);
	TimeFrame = optimize(4,1,24,1);
	Stop =	10 * PIP;
	Trail =	10 * PIP;
	enterLong();
}