Okay thank you for the trade delay fix! Modified the code to include this and it works great now:
Length1 = Optimize ("Length1", 10, 1, 20, 1);
Length2 = Optimize("Length2", 10, 1, 20, 1);
Pivot1 = HHV(Close,Length1);
Pivot2 = LLV(Close,Length2);
Buy = Close > Ref( Pivot1, -1 )...