Skip to content

Commit e4aa3f1

Browse files
authored
Add files via upload
1 parent ba87ece commit e4aa3f1

4 files changed

+399
-0
lines changed

stock trading dynamic programming.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
#a simple day trading game
3+
#day trader is only allowed to make at maximum two trades
4+
#the strategy is long only
5+
#lets find out the maximum profit
6+
7+
#more details can be found in the following link
8+
# https://www.geeksforgeeks.org/maximum-profit-by-buying-and-selling-a-share-at-most-twice/
9+
10+
#an alternative version in recursion exists
11+
#its done by using a different approach
12+
#strongly recommend you to take a look
13+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/stock%20trading%20recursion.jl
14+
15+
#there are two scenarios to maximize the profit
16+
#one trade or two trades
17+
#first we run a reverse iteration
18+
#to obtain the maximum profit from one trade
19+
#then we run a normal iteration
20+
#to obtain the maximum profit
21+
#from one trade plus the result from reverse iteration
22+
function stock_trading(prices)
23+
24+
#initialize the profit at zero
25+
profit=[0 for _ in 1:length(prices)]
26+
27+
#initialize maximum price with the close price
28+
max_price=prices[end]
29+
30+
#reverse order iteration
31+
for i in length(prices)-1:-1:1
32+
33+
#update the maximum price to compute the maximum profit
34+
if prices[i]>max_price
35+
36+
max_price=prices[i]
37+
38+
end
39+
40+
#two scenarios to get the maximum profit
41+
#either the previous iteration is larger
42+
#or this round of iteration
43+
profit[i]=max(profit[i+1],max_price-prices[i])
44+
45+
end
46+
47+
#initialize minimum price with the open price
48+
min_price=prices[1]
49+
50+
#second round of iteration
51+
for i in 2:length(prices)
52+
53+
#update the minimum price to compute the maximum profit
54+
if prices[i]<min_price
55+
56+
min_price=prices[i]
57+
58+
end
59+
60+
#two scenarios to get the maximum profit
61+
#either the previous iteration is larger
62+
#or this round of iteration plus the result from single transaction
63+
profit[i]=max(profit[i-1],profit[i]+prices[i]-min_price)
64+
65+
end
66+
67+
return profit[end]
68+
69+
end
70+
71+
stock_trading([10,22,5,75,65,80])
72+
73+
stock_trading([2,30,15,10,8,25,80])
74+
75+
stock_trading([100,30,15,10,8,25,80])
76+
77+
stock_trading([90,70,35,11,5])

stock trading dynamic programming.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
7+
#a simple day trading game
8+
#day trader is only allowed to make at maximum two trades
9+
#the strategy is long only
10+
#lets find out the maximum profit
11+
12+
#more details can be found in the following link
13+
# https://www.geeksforgeeks.org/maximum-profit-by-buying-and-selling-a-share-at-most-twice/
14+
15+
#an alternative version in recursion exists
16+
#its done by using a different approach
17+
#strongly recommend you to take a look
18+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/stock%20trading%20recursion.py
19+
20+
21+
# In[2]:
22+
23+
24+
#there are two scenarios to maximize the profit
25+
#one trade or two trades
26+
#first we run a reverse iteration
27+
#to obtain the maximum profit from one trade
28+
#then we run a normal iteration
29+
#to obtain the maximum profit
30+
#from one trade plus the result from reverse iteration
31+
def stock_trading(prices):
32+
33+
#initialize the profit at zero
34+
profit=[0 for _ in range(len(prices))]
35+
36+
#initialize maximum price with the close price
37+
max_price=prices[-1]
38+
39+
#reverse order iteration
40+
for i in range(len(prices)-2,-1,-1):
41+
42+
#update the maximum price to compute the maximum profit
43+
if prices[i]>max_price:
44+
45+
max_price=prices[i]
46+
47+
#two scenarios to get the maximum profit
48+
#either the previous iteration is larger
49+
#or this round of iteration
50+
profit[i]=max(profit[i+1],max_price-prices[i])
51+
52+
#initialize minimum price with the open price
53+
min_price=prices[0]
54+
55+
#second round of iteration
56+
for i in range(1,len(prices)):
57+
58+
#update the minimum price to compute the maximum profit
59+
if prices[i]<min_price:
60+
61+
min_price=prices[i]
62+
63+
#two scenarios to get the maximum profit
64+
#either the previous iteration is larger
65+
#or this round of iteration plus the result from single transaction
66+
profit[i]=max(profit[i-1],profit[i]+prices[i]-min_price)
67+
68+
return profit[-1]
69+
70+
71+
# In[3]:
72+
73+
74+
stock_trading([10,22,5,75,65,80])
75+
76+
77+
# In[4]:
78+
79+
80+
stock_trading([2,30,15,10,8,25,80])
81+
82+
83+
# In[5]:
84+
85+
86+
stock_trading([100,30,15,10,8,25,80])
87+
88+
89+
# In[6]:
90+
91+
92+
stock_trading([90,70,35,11,5])
93+

stock trading recursion.jl

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

0 commit comments

Comments
 (0)