-
Notifications
You must be signed in to change notification settings - Fork 14
/
stock trading dynamic programming.jl
102 lines (59 loc) · 2.25 KB
/
stock trading dynamic programming.jl
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
# 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 recursion 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%20recursion.jl
# In[2]:
#there are two scenarios to maximize the profit
#one trade or two trades
#first we run a reverse iteration
#to obtain the maximum profit from one trade
#then we run a normal iteration
#to obtain the maximum profit
#from one trade plus the result from reverse iteration
function stock_trading(prices)
#initialize the profit at zero
profit=[0 for _ in 1:length(prices)]
#initialize maximum price with the close price
max_price=prices[end]
#reverse order iteration
for i in length(prices)-1:-1:1
#update the maximum price to compute the maximum profit
if prices[i]>max_price
max_price=prices[i]
end
#two scenarios to get the maximum profit
#either the previous iteration is larger
#or this round of iteration
profit[i]=max(profit[i+1],max_price-prices[i])
end
#initialize minimum price with the open price
min_price=prices[1]
#second round of iteration
for i in 2:length(prices)
#update the minimum price to compute the maximum profit
if prices[i]<min_price
min_price=prices[i]
end
#two scenarios to get the maximum profit
#either the previous iteration is larger
#or this round of iteration plus the result from single transaction
profit[i]=max(profit[i-1],profit[i]+prices[i]-min_price)
end
return profit[end]
end
# In[3]:
stock_trading([10,22,5,75,65,80])
# In[4]:
stock_trading([2,30,15,10,8,25,80])
# In[5]:
stock_trading([100,30,15,10,8,25,80])
# In[6]:
stock_trading([90,70,35,11,5])