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

  • Thread starter Thread starter Grimmet
  • Start date Start date
AI Thread Summary
The discussion focuses on calculating the number of shares needed to achieve a target average price using a Python script. A user owns 250,000 shares at $0.005 and wants to buy additional shares at $0.03 to reach an average price of $0.02. The initial brute force method suggested yields a result of 375,000 shares. A more efficient approach using a weighted average formula is also presented, confirming the calculation of 375,000 shares needed. The conversation emphasizes the importance of simplifying the formula for clarity and efficiency in calculations.
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:
Seemingly by some mathematical coincidence, a hexagon of sides 2,2,7,7, 11, and 11 can be inscribed in a circle of radius 7. The other day I saw a math problem on line, which they said came from a Polish Olympiad, where you compute the length x of the 3rd side which is the same as the radius, so that the sides of length 2,x, and 11 are inscribed on the arc of a semi-circle. The law of cosines applied twice gives the answer for x of exactly 7, but the arithmetic is so complex that the...
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...
Back
Top