Algorithmic Margin Management