What CraigW said, except I use silicone self amalgamating tape as it’s much grippier. And replace the O-ring with a zip tie bent around the hooks (as long as it’s the only bike the light needs to be on and you’re not planning on removing it every day).
I’ve had that light before, hate to say it, but it’s awful, it’s the only chinese light I’ve actually thrown away. I’ve still got my original P7 as a backup in preference to that one.
A bazillion claimed lumens, at best 1500 actual lumens (which TBH is plenty), but the body is really tall and heavy so bounces around a lot as you found. And the battery was complete garbage (this may vary depending on te supplier, but it was the one that killed my faith in Chinese supplied batteries).
My current bar light is a BT70 which is actually about the same size but a lot brighter. The amalgamating tape stops it wobbling too much (it still bounces a bit, but not so much that the light goes SKY / WHEEL / SKY / WHEEL / SKY / WHEEL / SKY / WHEEL .