I learned that prices cannot be erroneous since the market is always right laugh.

I don't know how you wanted your trades to close, but if they behave odd, I can not give another advice than find the reason and fix it. Check the prices and your entry/exit conditions in detail in the log. Follow a trade from start to end. Use the debugger and watch your variables. Once you've got used to examine and debug your scripts systematically, all those issues will be gone.