-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW1.py
More file actions
139 lines (114 loc) · 3.85 KB
/
HW1.py
File metadata and controls
139 lines (114 loc) · 3.85 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
class Portfolio():
def __init__(self, cash=0, stock={}, stock_prices={}, mf={}, hist=[]):
self.cash = cash
self.stock = stock
self.stock_prices = stock_prices
self.mf = mf
self.hist = hist
def __repr__(self):
return "Portfolio object. Use print function!"
def __str__(self):
print("Cash:")
print(self.cash)
print("Stock:")
print(self.stock)
print("MutualFund:")
print(self.mf)
return("")
def addCash(self, cash):
self.cash += cash
self.hist.append("Added cash:{}".format(cash))
def withdrawCash(self, cash):
if self.cash < cash:
print("Not enough money!")
else:
self.cash -= cash
self.hist.append("Withdrawn cash:{}".format(cash))
def buyStock(self, n, s): #n= number of shares / s = stock price
if self.cash < n*s.price:
print("Not enough money!")
else:
self.cash -= n*s.price
if s.symbol in self.stock.keys():
self.stock[s.symbol] += n
else:
self.stock[s.symbol] = n
self.stock_prices[s.symbol] = s.price
self.hist.append(
"Bought stock: {} shares of ".format(n)
+ s.symbol
+ " for {} dollars per share.".format(s.price)
)
def sellStock(self, s, n): # s= stock's symbol / n= shares to be sold
if s not in self.stock.keys():
print("Stock not found!")
elif self.stock[s] < n:
print("Not enough shares of this stock!")
else:
selling_price = np.random.uniform(0.5*self.stock_prices[s], 1.5*self.stock_prices[s])
self.stock[s] -= n
if self.stock[s] == 0:
self.stock.pop(s)
self.cash += n*selling_price
self.hist.append(
"Sold stock: {} shares of ".format(n)
+ s
+ " for {} dollars per share.".format(selling_price)
)
def buyMutualFund(self, n, mf):
if self.cash < n:
print("Not enough money!")
else:
if mf.symbol in self.mf.keys():
self.mf[mf.symbol] += n
else:
self.mf[mf.symbol] = n
self.hist.append("Bought MutualFund: {} shares of ".format(n) + mf.symbol)
def sellMutualFund(self, s, n):
if s not in self.mf.keys():
print("MutualFund not found!")
elif self.mf[s] < n:
print("Not enough shares of this mutual fund")
else:
selling_price = np.random.uniform(0.9, 1.2)
self.mf[s] -= n
if self.mf[s] == 0:
self.mf.pop(s)
self.cash += n*selling_price
self.hist.append(
"Sold MutualFund: {} shares of ".format(n)
+ s
+ " for {} dollars per share.".format(selling_price)
)
def history(self):
for item in self.hist:
print(item)
class Stock():
def __init__(self, price=0, symbol=""):
if symbol == "":
print("Symbol Error!")
else:
self.symbol = symbol
self.price = price
class MutualFund():
def __init__(self, symbol=""):
if symbol == "":
print("Symbol Error!")
else:
self.symbol = symbol
if __name__ == "__main__":
portfolio = Portfolio()
portfolio.addCash(300.50)
s = Stock(20, "HFH")
portfolio.buyStock(5, s)
mf1 = MutualFund("BRT")
mf2 = MutualFund("GHT")
portfolio.buyMutualFund(10.3, mf1)
portfolio.buyMutualFund(2, mf2)
print(portfolio)
portfolio.sellMutualFund("BRT", 3)
portfolio.sellStock("HFH", 1)
portfolio.withdrawCash(50)
portfolio.history()
print(portfolio)