The script looks ok. It trades only when SAR swaps direction, so the trade on live start was probably just coincidence.
You can generally solve such problems by printing the SAR in the log or plotting it on the chart - then you can see when and why it trades.