Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Created July 31, 2023 05:39
Show Gist options
  • Save tangoabcdelta/b0571d54b5e6ca23f2c820c48ea49c22 to your computer and use it in GitHub Desktop.
Save tangoabcdelta/b0571d54b5e6ca23f2c820c48ea49c22 to your computer and use it in GitHub Desktop.
Calculate how many more shares to buy to recoup losses
function calculateAdditionalStocks(initialStocks, initialPrice, currentPrice, targetPrice) {
const initialInvestment = initialStocks * initialPrice;
const currentHoldingValue = initialStocks * currentPrice;
const loss = initialInvestment - currentHoldingValue;
const additionalInvestmentNeeded = initialInvestment - targetPrice * initialStocks;
const additionalStocks = Math.ceil(additionalInvestmentNeeded / currentPrice);
return additionalStocks;
}
// Example usage:
const initialStocks = 250;
const initialPrice = 100;
const currentPrice = 93;
const targetPrice = 100;
const additionalStocksNeeded = calculateAdditionalStocks(initialStocks, initialPrice, currentPrice, targetPrice);
console.log(`To recoup your losses when the stocks reach ${targetPrice} INR, you need to buy approximately ${additionalStocksNeeded} more IOCL stocks at the current price of ${currentPrice} INR each.`);

Question

I bought IOCL Corp stocks worth 100 each Now they're worth 93 each How many more should I buy at 92 to recoup the losses when the stocks reach 100

You can use the concept of averaging down.

Let's break down the current situation:

You initially bought 250 IOCL stocks at 100 each. The current price of each IOCL stock is 93. Now, to recoup the losses, you need to bring down your average cost per share to 100. To achieve this, you can calculate the total value of your current holding and the additional investment required to reach the break-even point.

Initial Investment (250 stocks at 100 each): Total Initial Investment = 250 stocks * 100 INR per stock = 25,000 INR

Current Holding (250 stocks at 93 each): Total Current Holding Value = 250 stocks * 93 INR per stock = 23,250 INR

Loss:
Loss = Total Initial Investment - Total Current Holding Value
Loss = 25,000 INR - 23,250 INR = 1,750 INR

To recoup the 1,750 INR loss, you will need to invest an additional amount at 92 INR per stock. Let's call the number of additional stocks you need to buy "X."

Additional Investment (X stocks at 92 each): Total Additional Investment = X stocks * 92 INR per stock = 92X INR

To break even, the total value of your holdings after the additional investment should be equal to your initial investment:

Total Current Holding Value + Total Additional Investment = Total Initial Investment

(23,250 INR) + (92X INR) = 25,000 INR

Now, solve for X:

92X INR = 25,000 INR - 23,250 INR
92X INR = 1,750 INR
X = 1,750 INR / 92 INR
X ≈ 19.02

So, you would need to buy approximately 19 more IOCL stocks at 92 INR each to recoup your losses when the stocks reach 100 INR.

Run

You can copy and paste the code into an HTML file and open it in a browser to see the output in the console. Adjust the values of initialStocks, initialPrice, currentPrice, and targetPrice as needed to calculate the number of additional stocks required based on your specific scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment