strategy.close("LONG", when = _close)
this means if the conditions for 'long' are true, open a trade.
the next line means if the conditions for 'long' are true, and the conditions for '_close' are also true, to close the trade
Looking at how '_close' is defined, it is true when a long or short was closed 2 bars ago.
So to open a long trade it asks did we close a long or short trade 2 bars ago and have a long signal now? I tried to straighten that out, but it dropped the profit factor of your indicator. What it does in the version I posted below, is it asks, "if we have a close signal and we were already long, then close the long."
If you want the signals it to perform like how your version shown above is trading, I can change the longswitch and shortswitch to behave like that, but I've dropped you a link to how I think your code was intended to perform. I put a green bar when a long starts, and green area for the duration it's in a long, and red for shorts, so it's a little easier to see what is happening.
Hope all that makes sense.