Skip to content

Commit

Permalink
✏️ Add more documentation to the withdraw method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mxbaylee committed Jul 29, 2023
1 parent 9a1e8bb commit 33eb1f1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/portfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,30 @@ class Portfolio {
/**
* Withdraws an amount from the portfolio.
*
* The naive approach, which works most of the time, would be to sort the
* stonks by the absolute distance between the desired ratio and stonk ratio.
*
* The correct approach would be to split the stonks by less than and greater
* than the desired ratio, then sort them by absolute distance. Each
* iteration, you'd want one from either list based on which brings you closer
* to your desired ratio.
*
* For example if you were targeting a 0.0 ratio and sorted these shares were
* sorted by distance alone, you'd end up with 1.5 in gains by adding the
* first two stonks together. But the first and third stonk get you closer to
* the 0.0 ratio.
*
* ```
* const portfolio = new Portfolio([
* new Stonk({ value: 10, gains: 0.5, shares: 10 }), // 0.05
* new Stonk({ value: 10, gains: 1, shares: 10 }), // 0.1
* new Stonk({ value: 10, gains: -1.5, shares: 10 }), // -0.15
* ])
* const withdraw = portfolio.withdraw(20, 0)
* assert.equal(withdraw.value, 20)
* assert.equal(withdraw.gains, -1)
* ```
*
* @param {number} withdrawAmount The amount to withdraw.
* @param {number} [desiredRatio=0.0]
* The desired ratio of gains to withdraw. Can be `-1.0` to `1.0` for
Expand Down

0 comments on commit 33eb1f1

Please sign in to comment.