You sound incredibly frustrated and desperate about this situation, and I can't blame you for feeling like that, but really you need to try to take a step back from this and ask yourself what is really going on.
No child on this planet ever does something just to piss his or her parents off, especially not something as big a deal as this. When children do something like this it is a cry for the people they trust most in the world, ie. their parents, to please pay attention to something big that is causing them real pain. Most children don't even know what it is that's hurting them, but something is, and, as the parent, it is your job to be with her in her pain, and help her to either figure it out, or hold her and love her enough so she can work her way through it herself.
What your daughter is doing is not deliberate, it is not conscious, and she can't tell you why. She probably doesn't even know why, but you have to understand that this is far more profound than just trying to piss you off. The sweets issue is the same.
Imagine if you were stuck in a glass box in the middle of a busy high street, and you kept banging on the glass to get people to pay attention and help you get out, but they just kept yelling at you to shut up and stop annoying them. This is what you're unintentionally doing to your daughter.
She is saying 'please help me, I'm hurting and I don't know why and I don't know what to do about it' and you're shouting 'shut up and stop annoying me'. I know that's not nice to hear, but that is how it is.
Every time you berate her, it feeds her pain. It tells her you don't love her when she poos herself, and she gets even more insecure and frightened. Unconsciously she's testing her base. She's testing how stable it is, and every single time she tests it you go ahead and show how rocky it is. To her young subconsciousness, you're saying 'I only love conditionally, only when you're being good', but she needs, needs, needs to know you love her when she's being good, bad, pretty, ugly, happy, sad, endearing and disgusting. She needs you to show her that you love her whatever she does, unconditionally.
Many problems like this resolve very quickly the minute we parents realise that our children are in pain and stop berating and start loving and holding (not just physically, but emotionally). It can be enough for the child to sigh with relief that your love is solid, and unbending; that you love her whatever she does.
And if the problem doesn't go away, you have built a good foundation for working together with her to help to find the root of what is causing it.
Before I end this post, I want to just say that you also need to be kind to yourself. You are hurting and scared too, otherwise why would you be so angry? Anger always comes from fear of something, so you also need to deal with that. Be loving to yourself and you are more likely to find the strength to be loving to her as well.