I haven't programmed it, but the formula looks almost correct. I believe it's Margin * OptimalF(Vince) / Max Loss.