Here’s a similar macro used by hunters.
/cast [@focus,help][@focustarget,help][@target,help][@targettarget,help][@pet,nodead,exists] Misdirection
It casts MD on the focus, if it exits,
Else on the target it it exists,
Else on the target of the target, if it exists,
Else on the pet.