Hi! What you are describing could be a few different things, but to me it seems to most likely be swimbladder problem and/or constipation. Your goldfish may "flipover" or float and often have a wobbly swim, or seem to have trouble getting to the bottom. In many cases this can be corrected by fasting the fish for a few days, then feeding the goldfish only peas for a few days after that. Some fish are sensitive to certain foods. For example I have a few goldfish that will flipover if given any sort of pellet food.. But the fish are fine if given fresh veggies or any freeze dried foods or homemade gel foods.. It's a good idea to always supplement your goldies diet with veggies/fruits and give them a varied diet. You can also try soaking any flakes/pellets in water before feeding your fish.. This will allow get rid of any air trapped in the pellets, and it will allow the flakes to sink to the bottom, also eliminating air gulping.
Nitrates can also cause flipover in some sensitive fish.. I've seen fish that will flip if the nitrates go above 5... Have you tested your water recently? That would be something you would want to check..Nitrates can build up fast, especially in overstocked tanks, or tanks that aren't maintained properly. (weekly water changes, etc).
It is possible that your fish has a bacterial infection but I would only consider this in the event that the other suggestions don't help.. And in that case, it would be best if you could post information about your set up, and give test results so that someone can help you further.
Some fish will flip for good, but that fact that yours flips sometimes and is normal sometimes makes me think this is something that can be fixed with some change in diet..
I'm new to this particular forum so if I am stepping on anyones toes, or doing anything wrong, I apologize in advance. I just happened to see this question unanswered.
: )