How Many Shares to Buy to Achieve a Target Average Price?

  • MHB
  • Thread starter Grimmet
  • Start date
In summary, the conversation discusses using a python script to determine the number of shares one should buy at a new price to reach a target price while also owning a certain number of shares at a different price. The script uses a brute force looping method to find an answer, but there is a more elegant way to solve the problem using a weighted average. The formula for the weighted average is given as Shares = ownedShares*targetPrice - ownedShares*boughtPrice / buyPrice - targetPrice."
  • #1
Grimmet
2
0
Writing a small python script...
If one owns 250000 shares at 0.005
and buys shares at 0.03.
How many shares would one buy for the average price to equal 0.02.

My attempt so far as used brute force looping to get close to a figure.
For example, the script gives the answer 375000 shares.
... I'm sure there's a more elegant way.

exitPrg = "y"

def LowBracket(buyP, targetP, currentA, ownedS, lim):
nX = 0
while nX <= limit:
nm = round(((nX * buyP)+currentA)/(nX+ownedS),5)
if nm == targetPrice:
break
nX += 1
return nX

def HighBracket(buyP, targetP, currentA, ownedS, lim):
nY = lim
while nY > 0:
nn = round(((nY * buyP)+currentA)/(nY+ownedS),5)
if nn == targetP:
break
nY -= 1
return nY

while ( exitPrg == "y" or exitPrg == "Y"):
ownedShares = input("\nShares owned: ")
boughtPrice = input("Price bought: ")
buyPrice = input("New price: ")
targetPrice = input("Target price: ")
limit = input("Share limit: ")

ownedShares = int(ownedShares)
boughtPrice = float(boughtPrice)
buyPrice = float(buyPrice)
targetPrice = float(targetPrice)
limit = int(limit)

currentAmount = ownedShares * boughtPrice

numberA = LowBracket(buyPrice, targetPrice, currentAmount, ownedShares, limit)

numberB = HighBracket(buyPrice, targetPrice, currentAmount, ownedShares, limit)

print("\nNumber of shares: %i\n" % ((numberB+numberA)/2))

exitPrg = input("Continue? (y/n) ")

Thanks.
 
Mathematics news on Phys.org
  • #2
You could use what's called a weighted average to determine the answer:

\(\displaystyle \frac{250000\cdot0.005+X\cdot0.03}{250000+X}=0.02\)

Simplify:

\(\displaystyle \frac{1250+0.03X}{250000+X}=0.02\)

Multiply through by $250000+X$:

\(\displaystyle 1250+0.03X=5000+0.02X\)

Rearrange:

\(\displaystyle 0.01X=3750\)

Multiply through by 100:

\(\displaystyle X=375000\)
 
  • #3
Therefore:
Shares = ownedShares*targetPrice - ownedShares*boughtPrice / buyPrice - targetPrice

Thanks for the help.

Grimmet
 
Last edited:

FAQ: How Many Shares to Buy to Achieve a Target Average Price?

1. How do I calculate the number of shares?

To calculate the number of shares, you need to divide the total value of the company's stock by the current stock price. This will give you the total number of shares outstanding.

2. What is the total value of the company's stock?

The total value of the company's stock is also known as its market capitalization. It is calculated by multiplying the number of outstanding shares by the current stock price.

3. Where can I find the current stock price?

The current stock price can be found on various financial websites, such as Yahoo Finance or Google Finance. It can also be found on the company's investor relations website or through a stock trading platform.

4. What is the importance of calculating the number of shares?

Calculating the number of shares is important because it gives you an idea of the size and value of a company. It is also useful for determining ownership percentage and making investment decisions.

5. Can the number of shares change over time?

Yes, the number of shares can change over time. Companies can issue new shares through offerings or stock splits, and existing shareholders can also buy or sell their shares on the stock market, leading to changes in the total number of outstanding shares.

Back
Top