3

I have looked into stock profit maximising algorithms depending on the situation.

Strategies for situations where you only have one stock and can either buy/sell once or multiple times are clear to me. You use greatest difference and maximum sub array respectively.

But what happens when given two stocks and their respective fluctuating prices? You cannot hold both stocks at the same time and selling one and buying another introduces a transaction cost.

Example: Maximise returns given Stocks A and B. The stocks prices fluctuate from period to period. So if given an array, the indices in each array for A and B indicate the price of the stock at a particular time. Considering that you cannot hold both stocks at the same time and buying A and selling B induces a transaction cost, what's the best strategy to use?

Django
  • 343
  • 1
  • 4
  • 23
  • show us what you've tried so far? this smells like a homework/interview problem... – RAZ_Muh_Taz Jan 16 '17 at 20:07
  • I think your teacher is looking for you to say "Dynamic programming". – Adam Jan 16 '17 at 20:13
  • Can you give an example? I'm not very clear on whether or not you have a starting budget and if buying stocks reduces your profit or not initially. – IVlad Jan 16 '17 at 20:38
  • In this example there is no budget. There is just an array of stock prices over a period of time. I am trying to work out if there is a strategy in which I can get the maximum profit by buying and selling using the prices given in an array for each stock. – Django Jan 16 '17 at 20:48
  • Then I think my answer should work. – IVlad Jan 16 '17 at 20:50
  • What exactly is the objective to be maximized? The value you posess at the 'end of time'? – Codor Jan 16 '17 at 20:50
  • @Codor, let's say we have an array of integers for one stock. The indices in the array are separated by 1 hour. So these are the prices at 1,2,3 pm etc. Say we have [3,1,4,5,8]. If we are allowed to buy and sell only once we would buy at 1 and sell at 8 to make max profit. This is a situation where we have 1 stock and only one chance to buy and sell. What about if we have 2 stocks and we can buy and sell as much as we want, except we can't hold both A and B at the same time. – Django Jan 16 '17 at 20:55

1 Answers1

1

Let:

dp[i, j] = maximum worth obtainable at period i if we currently hold 
           stock j

Assume t[i] = transaction cost at period i.

We have:

dp[0, A] = A[0] # buy the best at time 0
dp[0, B] = B[0] # you said only buying A and selling B induces a cost,
                # you didn't say buying B induces a cost. If it does,
                # just subtract t accordingly below
dp[i, A] = max(
             dp[i - 1, B] + A[i] - t[i], # sell B, buy A
             A[i]                        # buy A alone
           )                             # we buy A no matter what

dp[i, B] = max(
             dp[i - 1, A] + B[i],        # sell A, buy B, - t[i] if needed
             B[i]                        # buy B alone
           )

Answer is:

max{dp[i, A], dp[i, B]}
 i
IVlad
  • 43,099
  • 13
  • 111
  • 179
  • In your array `t` of transaction cost, do you assume that switching from stock A to stock B and vice versa causes the same cost? – Codor Jan 16 '17 at 20:58
  • 1
    @Codor I only assume that selling `B` and buying `A` incurs a cost, not vice versa. If the reverse applies as well, it's easy to change the second `dp` update. There was also a slight error in the first update, fixing right after this comment. – IVlad Jan 16 '17 at 21:01
  • I'm not quite sure if this is _exactly_ what is meant in the original question, but the answer seems basically right. – Codor Jan 16 '17 at 21:03
  • I'm not entirely convinced myself. A worked example by the OP would go a long way towards someone coming up with an exact answer. – IVlad Jan 16 '17 at 21:04