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

  • Thread starter Thread starter Grimmet
  • Start date Start date
Grimmet
Messages
2
Reaction score
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
You could use what's called a weighted average to determine the answer:

$$\frac{250000\cdot0.005+X\cdot0.03}{250000+X}=0.02$$

Simplify:

$$\frac{1250+0.03X}{250000+X}=0.02$$

Multiply through by $250000+X$:

$$1250+0.03X=5000+0.02X$$

Rearrange:

$$0.01X=3750$$

Multiply through by 100:

$$X=375000$$
 
Therefore:
Shares = ownedShares*targetPrice - ownedShares*boughtPrice / buyPrice - targetPrice

Thanks for the help.

Grimmet
 
Last edited:
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Fermat's Last Theorem has long been one of the most famous mathematical problems, and is now one of the most famous theorems. It simply states that the equation $$ a^n+b^n=c^n $$ has no solutions with positive integers if ##n>2.## It was named after Pierre de Fermat (1607-1665). The problem itself stems from the book Arithmetica by Diophantus of Alexandria. It gained popularity because Fermat noted in his copy "Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et...
I'm interested to know whether the equation $$1 = 2 - \frac{1}{2 - \frac{1}{2 - \cdots}}$$ is true or not. It can be shown easily that if the continued fraction converges, it cannot converge to anything else than 1. It seems that if the continued fraction converges, the convergence is very slow. The apparent slowness of the convergence makes it difficult to estimate the presence of true convergence numerically. At the moment I don't know whether this converges or not.
Back
Top