-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEC 23 DYNAMIC AND READ DATA FROM TXT.py
76 lines (66 loc) · 2.12 KB
/
LEC 23 DYNAMIC AND READ DATA FROM TXT.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
import pylab
import random
class Stock(object):
def __init__(self, price, distribution):
self.price = price
self.history = [price]
self.distribution = distribution
self.last_change = 0
def set_price(self, price):
self.price = price
self.history.append(price)
def get_price(self):
return self.price
def make_move(self, market_bias, momentum=False):
old_price = self.price
base_move = self.distribution() + market_bias
self.price *= 1 + base_move
if momentum:
self.price += self.last_change * random.gauss(.5, .5)
if self.price < .01:
self.price = 0
self.history.append(self.price)
self.last_change = old_price - self.price
def show_history(self, figure_number):
pylab.figure(figure_number)
pylab.plot(self.history)
pylab.title('Closing Price, Test ' + str(figure_number))
pylab.xlabel('DAY')
pylab.ylabel('PRICE')
def get_dat(file_name):
data = open(file_name, 'r')
res = []
for line in data:
res.append(line.split(','))
return res
def test():
def run_simulation(stocks, fig):
mean = 0
for stock in stocks:
for day in range(num_days):
stock.make_move(market_bias, momentum)
stock.show_history(fig)
mean += stock.get_price()
mean = mean / float(num_stocks)
pylab.axhline(mean)
data = get_dat('deviated data.txt')
counter = 0
for A in data:
num_days = int(A[0])
market_bias = float(A[2])
momentum = bool(int(A[3]))
num_stocks = int(A[1])
stokes1 = []
stokes2 = []
for i in range(num_stocks):
volatility = random.uniform(0, .20)
d1 = lambda: random.uniform(-volatility, volatility)
d2 = lambda: random.gauss(0, volatility / 2)
stokes1.append(Stock(100, d1))
stokes2.append(Stock(100, d2))
counter += 1
run_simulation(stokes1, counter)
counter += 1
run_simulation(stokes2, counter)
test()
pylab.show()