Function investedCostToPercent

Convert an absolute dollar amount to a percentage of a cost basis. Use the result as the percent argument to commitPartialProfit / commitPartialLoss.

IMPORTANT: percentToClose in partial closes is applied to the REMAINING cost basis (what is still held after prior partials), not to the total invested amount. To close an exact dollar amount, pass the remaining cost basis from getTotalCostClosed as costBasis — not getPositionInvestedCost.

const remaining = await getTotalCostClosed("BTCUSDT"); // e.g. 300
const percent = investedCostToPercent(150, remaining); // 50
await commitPartialProfit("BTCUSDT", percent);
  • Parameters

    • dollarAmount: number

      Dollar value to close (e.g. 150)

    • costBasis: number

      Remaining cost basis from getTotalCostClosed (e.g. 300)

    Returns number

    Percentage of the remaining position to close (0–100)