Macbook swaps external displays when lid closed or opened
You need to set the display layout in each scenario. For example set the display arrangement when the macbook screen is opened and you should do it once again for when the laptop display is shut.
That should do the trick.