Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON Help Needed Many investment management companies are switching from manual stock trading done by humans to automatic stock trading by computers. I need help

PYTHON Help Needed

Many investment management companies are switching from manual stock trading done by humans to automatic stock trading by computers. I need help writing an automatic stock trader function that determines whether to buy, sell, or do nothing (hold) for a particular account. It should follow the old saying "Buy low, sell high!"

This function takes4 input parameters in this order:

  1. current_shares- current number of shares of this stock in the account (int).
  2. purchase_price -price (per share) paid for current stock in the account (float).
  3. market_price - current market price (per share) of stock in the account (float).
  4. available_funds- maximum amount the client is willing to spend on a stock purchase (float).

Any transaction (buy or sell) costs $10. This $10 must be paid out of the available_funds for a purchase, or out of the proceeds of a stock sale. Be sure to account for this fee in your profit calculations.

A purchase would be considered profitable when the current market price is lower than the purchase price, and the available funds will allow us to buy enough shares so that the difference in value will cover the $10 transaction fee. In this case the function should return the string "Buy # shares" where # is an integer representing the number of shares to purchase.

A sale would be considered profitable when the current market price is higher than the purchase price, and the value gained by selling the shares will cover the $10 transaction fee. In this case the function should return the string "Sell # shares" where # is an integer representing the number of shares to sell.

If neither a buy nor a sell would be profitable, then the function should return the string "Hold shares."

Here are some test cases that my function should satisfy:

Rationale for test cases:

Test 1

Even though the current market price is very low (compared to the purchase price), after paying the $10 transaction fee, we would not have any funds left to buy shares; so we can only hold.

Test 2

After paying the $10 transaction fee, there are enough funds remaining to buy 11 shares. At a purchase_price vs. market_price difference of $1 per share, our 11 shares represent a value gain of $11 dollars, which is $1 more than the $10 transaction fee - so we come out $1 ahead.

Test 3

After paying the $10 transaction fee, there are enough funds remaining to buy 2 shares. At a purchase_price vs. market_price difference of $11 per share, our 2 shares represent a value gain of $22 dollars, which is $12 more than the $10 transaction fee - so we come out $12 ahead.

Test 4

Selling our 1 share for $11 will leave us with just $1 after we pay the $10 transaction fee. That is the same as what we paid for it, and we won't make any profit - so we should hold.

Test 5

With a market_price vs. purchase_price vs. difference of $2 per share, we stand to make $20 from the sale of our 10 shares. This is $10 more than the price of the transaction fee, so we will come out $10 ahead - therefore we should sell all 10 shares.

Test 6

Our 1 share is worth $11 more than we paid for it at the current market price. The $11 dollars obtained by selling that share now will still leave us with a profit of $1 after paying the $10 transaction fee. Profit is profit, so we should sell.

Things to think about:

  1. Look for opportunities to use variables as a way to name things, and to break larger more complex expressions down into simpler expressions.
  2. Spend some time choosing your names carefully. Names should be descriptive.
  3. Try to design and write your code a few lines at a time. Design and write a few lines, then run some tests to see if these lines are doing what you want them to do. If they are not, then analyze and correct them before you move on to the next few lines of code.

Please note the code for my assignment is in 2 parts. In vocareum use the dropdown menu on top to switch between parts.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

=+strategic window and to minimize the risks posed by the threats.

Answered: 1 week ago