Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: I need help-code and math

  1. #1
    Senior Member
    Join Date
    Feb 2015
    Location
    USA
    Posts
    126

    Default I need help-code and math

    The problem:

    I own 1000 shares of XYZ Company @$10/share.

    I want to consider cost averaging.

    Currently the shares are at $3/share. I want a new cost average at $5/share. At that price, how many shares would I need to buy?

    This is in Access 2007, I am self taught so basic programming skills I am sure I am missing. Not to mention I stink at math. To get around this problem, I did the following code which works just fine, I just know there has to be a better way.


    CurrNumbShares = Forms!SalesScheduleFrm!StockShares
    CurrInvestment = Forms!SalesScheduleFrm!Text7
    TargetCPS = Forms!SalesScheduleFrm!Text105
    ShareCounter = 0

    Do Until ShareCounter = 100000
    ShareCounter = ShareCounter + 1
    NewNumShares = Forms!SalesScheduleFrm!StockShares + ShareCounter
    NewInvestment = (Forms!SalesScheduleFrm!Text115 * ShareCounter)
    NewCPS = (NewInvestment + CurrInvestment) / NewNumShares
    Forms!SalesScheduleFrm!Text107 = NewInvestment
    Forms!SalesScheduleFrm!Text109 = ShareCounter
    Forms!SalesScheduleFrm!Text113 = NewCPS

    If NewCPS <= TargetCPS Then
    Exit Do
    End If

    Loop

    Any help is appreciated, you will get nothing but a thank you, but it is better than a poke in the eye, if you ask me.

    Thank you. Now where did I put that eye poker? (I lie)

    PS. I only picked 100,000 because I am curious and it reminds me of what a bonehead I am, some times. There are these things called penny stocks.
    Last edited by Eleftherios Karagiannis; 06-04-2015 at 06:01 PM.
    Sometimes, the logical answer defies logic....Stupid accident.

    If my thoughts are math, how can I be wrong?

  2. #2
    Moderator grapes's Avatar
    Join Date
    Nov 2010
    Location
    NC USA
    Posts
    4,021

    Default Re: I need help-code and math

    Quote Originally Posted by Eleftherios Karagiannis View Post
    The problem:

    I own 1000 shares of XYZ Company @$10/share.

    I want to consider cost averaging.

    Currently the shares are at $3/share. I want a new cost average at $5/share. At that price, how many shares would I need to buy?

    This is in Access 2007, I am self taught so basic programming skills I am sure I am missing. Not to mention I stink at math. To get around this problem, I did the following code which works just fine, I just know there has to be a better way.


    CurrNumbShares = Forms!SalesScheduleFrm!StockShares
    CurrInvestment = Forms!SalesScheduleFrm!Text7
    TargetCPS = Forms!SalesScheduleFrm!Text105
    ShareCounter = 0

    Do Until ShareCounter = 100000
    ShareCounter = ShareCounter + 1
    NewNumShares = Forms!SalesScheduleFrm!StockShares + ShareCounter
    NewInvestment = (Forms!SalesScheduleFrm!Text115 * ShareCounter)
    NewCPS = (NewInvestment + CurrInvestment) / NewNumShares
    Forms!SalesScheduleFrm!Text107 = NewInvestment
    Forms!SalesScheduleFrm!Text109 = ShareCounter
    Forms!SalesScheduleFrm!Text113 = NewCPS

    If NewCPS <= TargetCPS Then
    Exit Do
    End If

    Loop

    Any help is appreciated, you will get nothing but a thank you, but it is better than a poke in the eye, if you ask me.

    Thank you. Now where did I put that eye poker? (I lie)

    PS. I only picked 100,000 because I am curious and it reminds me of what a bonehead I am, some times. There are these things called penny stocks.
    OK, you set CurrNumbShares, but you don't use it later. Instead, you re-retrieve it from the form.

    Here's the way to solve the problem algebraically: assume you have a ShareCounter that works! Usually, in algebra, we call that x, but we can call it anything we want. Let's call it SharesToBuy.

    The restriction on SharesToBuy comes from wanting the following to be true, correct?

    TargetCPS = (NewInvestment + CurrInvestment) / NewNumShares

    We also know that

    NewNumShares = Forms!SalesScheduleFrm!StockShares + SharesToBuy
    NewInvestment = (Forms!SalesScheduleFrm!Text115 * SharesToBuy)

    even though we don't yet know what SharesToBuy is (it's the x that algebra is always looking for!) We can combine the three equations into a single equation where we know everything, except SharesToBuy, because algebra can solve that equation for SharesToBuy:

    TargetCPS = ((Forms!SalesScheduleFrm!Text115 * SharesToBuy) + CurrInvestment) / (Forms!SalesScheduleFrm!StockShares + SharesToBuy)

    Solving that equation algrbraically for SharesToBuy, we get (which can replace your program, except where you set the values CurrInvestment and TargetCPS, or you could include them directly):

    SharesToBuy = (CurrInvestment - TargetCPS * Forms!SalesScheduleFrm!StockShares) / (TargetCPS - Forms!SalesScheduleFrm!Text115)

    SharesToBuy = ($3000 - $5 * 1000) / ($5 - $10)

    SharesToBuy = 400

  3. #3
    Senior Member
    Join Date
    Feb 2015
    Location
    USA
    Posts
    126

    Default Re: I need help-code and math

    Awesome, thank you much!!


    But I still owe you a poke in the eye.
    Sometimes, the logical answer defies logic....Stupid accident.

    If my thoughts are math, how can I be wrong?

  4. #4
    Moderator grapes's Avatar
    Join Date
    Nov 2010
    Location
    NC USA
    Posts
    4,021

    Default Re: I need help-code and math

    Instead of poking you in the eye, I will just use your example in class of how someone with decent programming skills can solve a problem with a sledge hammer, that only takes a jeweler's tack.

    The principles of algebra are simple, but breath-taking in their utility in application

  5. #5
    Senior Member
    Join Date
    Feb 2015
    Location
    USA
    Posts
    126

    Default Re: I need help-code and math

    I have no doubt algebra is awesome and hard to argue against as facts, I can't, they were the simplest ones of course, way back when, I really wish I knew how to do algebra.

    You have given me a start.

    But your eye is still mine, no smiley face this time.

    Late edit: You can use my code in your class, maybe it can teach them something else, I used logic to solve that problem. It's all I got, what little it is. First, maybe ask them to solve it without algebra?
    Last edited by Eleftherios Karagiannis; 06-05-2015 at 01:56 AM.
    Sometimes, the logical answer defies logic....Stupid accident.

    If my thoughts are math, how can I be wrong?

  6. #6
    Moderator grapes's Avatar
    Join Date
    Nov 2010
    Location
    NC USA
    Posts
    4,021

    Default Re: I need help-code and math

    Quote Originally Posted by Eleftherios Karagiannis View Post
    I really wish I knew how to do algebra.

    You have given me a start.

    But your eye is still mine, no smiley face this time.

    Late edit: You can use my code in your class, maybe it can teach them something else, I used logic to solve that problem. It's all I got, what little it is. First, maybe ask them to solve it without algebra?
    Well, you did all the hard part, so that should impress them.

    That's one of the approaches to teaching algebra, to make the students do it the "hard way" so that they appreciate the advanced techniques. Still, they sometimes lose sight of the fact that the advanced techniques are based upon the simple steps we took above.

    You had a simple equation, but the variable that you were interested in (SharesToBuy) appears in the equation twice:
    TargetCPS = ((Forms!SalesScheduleFrm!Text115 * SharesToBuy) + CurrInvestment) / (Forms!SalesScheduleFrm!StockShares + SharesToBuy)
    One of the principles of algebra is that you can perform the same operation on both sides of the equation, and the result will still be an equation. With certain reservations. We just got to manipulate it so that we only have the SharesToBuy by itself on one side of the equation, and we'll be done. So we multiply both sides of the equation by (Forms!SalesScheduleFrm!StockShares + SharesToBuy)
    (Forms!SalesScheduleFrm!StockShares + SharesToBuy) * TargetCPS = ((Forms!SalesScheduleFrm!Text115 * SharesToBuy) + CurrInvestment) / (Forms!SalesScheduleFrm!StockShares + SharesToBuy) * (Forms!SalesScheduleFrm!StockShares + SharesToBuy)
    On the right hand side, the division by (Forms!SalesScheduleFrm!StockShares + SharesToBuy), and then the multiplication by (Forms!SalesScheduleFrm!StockShares + SharesToBuy), cancel each other out. That's why we chose to multiply both sides by that factor, because it gets rid of the fraction with the SharesToBuy in the denominator. So, we're left with:
    (Forms!SalesScheduleFrm!StockShares + SharesToBuy) * TargetCPS = ((Forms!SalesScheduleFrm!Text115 * SharesToBuy) + CurrInvestment)
    Now we multiply each part of (Forms!SalesScheduleFrm!StockShares + SharesToBuy) by the TargetCPS, to get rid of the parentheses:
    Forms!SalesScheduleFrm!StockShares * TargetCPS + SharesToBuy * TargetCPS = Forms!SalesScheduleFrm!Text115 * SharesToBuy + CurrInvestment
    Now we subtract Forms!SalesScheduleFrm!StockShares * TargetCPS and Forms!SalesScheduleFrm!Text115 * SharesToBuy from both sides:
    Forms!SalesScheduleFrm!StockShares * TargetCPS + SharesToBuy * TargetCPS - Forms!SalesScheduleFrm!StockShares * TargetCPS = Forms!SalesScheduleFrm!Text115 * SharesToBuy + CurrInvestment - Forms!SalesScheduleFrm!StockShares * TargetCPS

    SharesToBuy * TargetCPS = Forms!SalesScheduleFrm!Text115 * SharesToBuy + CurrInvestment - Forms!SalesScheduleFrm!StockShares * TargetCPS

    SharesToBuy * TargetCPS - Forms!SalesScheduleFrm!Text115 * SharesToBuy = Forms!SalesScheduleFrm!Text115 * SharesToBuy + CurrInvestment - Forms!SalesScheduleFrm!StockShares * TargetCPS - Forms!SalesScheduleFrm!Text115 * SharesToBuy

    SharesToBuy * TargetCPS - Forms!SalesScheduleFrm!Text115 * SharesToBuy = CurrInvestment - Forms!SalesScheduleFrm!StockShares * TargetCPS
    The left hand side is just (SharesToBuy) times (TargetCPS - Forms!SalesScheduleFrm!Text115) :
    SharesToBuy * (TargetCPS - Forms!SalesScheduleFrm!Text115) = (CurrInvestment - Forms!SalesScheduleFrm!StockShares * TargetCPS)
    So, if we divide both sides by (TargetCPS - Forms!SalesScheduleFrm!Text115), we'll be left with SharesToBuy by itself on the left hand side, which was our goal all along:
    SharesToBuy * (TargetCPS - Forms!SalesScheduleFrm!Text115) / (TargetCPS - Forms!SalesScheduleFrm!Text115) = (CurrInvestment - Forms!SalesScheduleFrm!StockShares * TargetCPS)/ (TargetCPS - Forms!SalesScheduleFrm!Text115)

    SharesToBuy = (CurrInvestment - Forms!SalesScheduleFrm!StockShares * TargetCPS) / (TargetCPS - Forms!SalesScheduleFrm!Text115)

  7. #7
    Senior Member
    Join Date
    Feb 2015
    Location
    USA
    Posts
    126

    Default Re: I need help-code and math

    If you wanted your eye that bad, all you had to do is say please.

    Iím not sure how to thank you enough, other than learning this and making myself better. Thank you much is not enough, in my opinion.
    Sometimes, the logical answer defies logic....Stupid accident.

    If my thoughts are math, how can I be wrong?

  8. #8
    Moderator grapes's Avatar
    Join Date
    Nov 2010
    Location
    NC USA
    Posts
    4,021

    Default Re: I need help-code and math

    Quote Originally Posted by Eleftherios Karagiannis View Post

    Iím not sure how to thank you enough, other than learning this and making myself better.
    That would be thanks enough, actually!

    Here's another algebraic trick:

    Imagine you're looking for a number such that multiplying it by 3, then subtracting 5 from that, then squaring that, the result equals 9.

    If we call the number that we're looking for "x" then that sentence can be translated into this algebraic formula:



    Do you see that? (I'm not done with the "trick")

  9. #9
    Senior Member
    Join Date
    Feb 2015
    Location
    USA
    Posts
    126

    Default Re: I need help-code and math

    Are you trying to convince me that .999... =1?

    I didn't understand anything up there, but I remember reading many arguments about 3's and other numbers.

    I will try that trick, at least do the best I can.
    Last edited by Eleftherios Karagiannis; 06-05-2015 at 07:40 PM.
    Sometimes, the logical answer defies logic....Stupid accident.

    If my thoughts are math, how can I be wrong?

  10. #10
    Moderator grapes's Avatar
    Join Date
    Nov 2010
    Location
    NC USA
    Posts
    4,021

    Default Re: I need help-code and math

    Nah, nah, nah, this is just algebra, for you
    Quote Originally Posted by grapes View Post
    After translating that text into the above symbols, we can attack it. Take the square root of both sides. The square root of the left hand side is easy, the square root is the inverse of the squaring, so all we're left with is (3x-5), with two possibilities, that it could be the positive or the negative square root. Same for the right hand side, the square root of nine is three of course, but it could be negative or positive. However, all those different combinations just boil down to two possibilities, either 3x-5 equals 3, or 3x-5 equals the opposite of 3:
    OK?

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •