Calculating Profit

Given the sensitive nature of this article, we will simply produce the (pseudo) code below for profit in it's entirety:

if size_usd == 0 {
    return Ok(0);
}

// entry_value is either position's IV, RV, or RV-IV depending on product type
let mut modded_entry_value = entry_value;
if entry_value < 0 {
    // If starting difference is -0.1, and we're at 0.2, then the difference is 0.3
    // and we're doing 0.3 / -0.1 * size_usd, does that really make sense? 
    // No. Need the - to be gone.
    // Similarly, for a short, we need the - to be gone. So cut the -.
    // But we want starting difference to keep it's sign for the differences calculation.
    modded_entry_value = entry_value*-1;
}

let differences = current_value - entry_value;
let mut profit = differences * size_usd / modded_entry_value;


if side == Side::Short {
    profit = profit * -1;
}

return Ok(profit)

As you can see, we multiply the size of the entire Position (loan and collateral) by the fractional change of the entry value to get the profit or loss.

Last updated