There are two types of brake anchor (one is plum-shaped, the other is barrel-shaped or cylindrical). If you have the wrong one at the brake end, it won't fit properly and will slide around.
To solve this, some brake cable vendors sell their cables with both types of end anchors attached. You just use the end that you need and cut off (with a proper brake line cutter, not a pair of pliers) the end you don't need:
!enter image description here
Be sure to put a ferrule on the cut end of the cable because it will unravel. DIY options include shrink wrap, a cork, an electrical crimp connector, tape (not personally recommended), string, wire, hot glue, etc.