Set desired yaw angle in degrees for loiter mode

When switching from guided mode to loiter mode with a Lua script, the copter suddenly turns north (0 degrees yaw). That looks quite unstable. Is it possible to maintain the same heading the copter had before when flying in guided mode after switching to loiter mode?

In guided mode I am using the function vehicle:set_target_angle_and_climbrate(0, -5, 74, 0, false, 0) to fly forward and then vehicle:set_target_angle_and_climbrate(0, 0, 74, 0, false, 0) to slow down.
What I have found out so far is that the aggressiveness of the turn decreases with the groundspeed the copter had before switching to loiter. With groundspeeds above 2 m/s the copter does not even make the full turn to 0 degrees heading.
But I would like to have the groundspeed as low as possible before switching to loiter, ideally the copter is already hovering.