Issue #23254:fix gain/loss calculation