-
Notifications
You must be signed in to change notification settings - Fork 14
/
stock trading recursion.py
123 lines (73 loc) · 2.51 KB
/
stock trading recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# coding: utf-8
# In[1]:
#a simple day trading game
#day trader is only allowed to make at maximum two trades
#the strategy is long only
#lets find out the maximum profit
#more details can be found in the following link
# https://www.geeksforgeeks.org/maximum-profit-by-buying-and-selling-a-share-at-most-twice/
#an alternative version in dynamic programming exists
#its done by using a different approach
#strongly recommend you to take a look
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/stock%20trading%20dynamic%20programming.py
# In[2]:
#compute the maximum profit for long only strategy
def compute_profit(prices):
#initialize maximum price with the close price
max_price=prices[-1]
#initialize minimum price with the open price
min_price=prices[0]
#initialize indices
left=0
right=len(prices)-1
minind=0
maxind=len(prices)-1
#we have two indices moving at the same time
#one from left to find the minimum value
#the other from right to find the maximum value
#we are not looking for global minimum
#we only want local minimum before the global maximum
while left<maxind and right>minind:
#when a larger value is found
#update accordingly
if prices[right]>max_price:
max_price=prices[right]
maxind=right
#the same applies to a smaller value
if prices[left]<min_price:
min_price=prices[left]
minind=left
left+=1
right-=1
#maximum profit
profit=max_price-min_price
#when we lose money
#abort the trade
if profit<0:
profit=0
return profit
# In[3]:
#there are two scenarios to maximize the profit
#one trade or two trades
#since we can execute two transactions
#we split the array at an iterating index i into half
#we find out the maximum profit we can obtain from both halves
def stock_trading(prices,ind):
#prevent index error
if ind+1==len(prices):
return 0
#split
upper=prices[0:ind]
lower=prices[ind:]
#compute profit
profit=compute_profit(lower)+compute_profit(upper)
#compare recursively
return max(profit,stock_trading(prices,ind+1))
# In[4]:
stock_trading([10,22,5,75,65,80],1)
# In[5]:
stock_trading([2,30,15,10,8,25,80],1)
# In[6]:
stock_trading([100,30,15,10,8,25,80],1)
# In[7]:
stock_trading([90,70,35,11,5],1)