# How to optimize the return given the stock price of all time?

There is a list of N stocks.

In M days, the stock price is given. Assume that each stock has only 1 price per day. Assume that these prices are constant.

We start with an amount of money K.

For each day we can buy and sell an arbitrary number of stocks. In the last day, we have to sell all the stocks we have to get back cash.

The question is: 1) How to build a strategy that buys and sells stocks that maximize the cash in the end?

2) Given the constraint that the amount of money we hold at any time cannot be negative, what is the best strategy?

For instance, if we have only 1 stock and the price is:

1 7 3

The best strategy would be spending all the money to buy in day 1, sell everything on day 2 and do nothing on day 3.

Update: my very naive approach (for the case when there is one stock only) is a heuristic one - buying on the first day and then selling when the price is higher than buying price, then buying again when the price is lower than the previous selling price.

• What are your thoughts on the question? What have you tried, and where did you get stuck? – Yuval Filmus Feb 21 at 11:39
• We're happy to help you understand the concepts but just solving exercises for you is unlikely to achieve that. You might find this page helpful in improving your question. – D.W. Feb 21 at 23:31