forked from MarkDunne/final-year-project
/
DistributionCalc.py
76 lines (58 loc) · 2.05 KB
/
DistributionCalc.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 csv
import pytz
import itertools
import numpy as np
import random
import TickerList
from datetime import datetime
from zipline.utils.factory import load_bars_from_yahoo
TickerList.tickers = ['AMD', 'CERN', 'COST', 'DELL', 'GPS', 'INTC', 'MMM']
def genWeights(size):
while True:
#create weights so that sum weights = 1
randomWeights = np.array([[random.choice([0.0, 1.0])] for i in xrange(size)])
#scaledweights = randomWeights * (1 / np.sum(randomWeights))
yield randomWeights
if __name__ == '__main__':
csvFileName = raw_input("CSV File Name: ")
start = datetime(2010, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc)
data = load_bars_from_yahoo(stocks=TickerList.tickers, indexes={}, start=start, end=end)
#use last known price for missing data
data = data.fillna(method='ffill')
#fill data at front of series
data = data.fillna(method='bfill')
#drop any other data
data = data.dropna(how='any')
#now shape of data is finalised
num_tickers = data.shape[0]
print 'Number of stocks', num_tickers
#init csv file
csvFile = open(csvFileName, 'wb')
csvWriter = csv.writer(csvFile, delimiter=',')
csvHeader = np.append(data.keys().values, ['returns', 'sharpe'])
csvWriter.writerow(csvHeader)
#create prices matrix
#shape -> m x n, where m = number of stocks, n = sample size
#prices[0] -> price list for first stock
prices = np.array(map(lambda x: data[x]['price'].values, data.items))
num_rows = prices.shape[0]
weightsGenerator = genWeights(num_tickers)
i = 0
while True:
i += 1
print "testing strategy", i
weightsVec = weightsGenerator.next()
weightedPrices = prices * weightsVec
weightedSum = np.sum(weightedPrices, axis=0)
normPrices = weightedSum / weightedSum[0]
stddev = np.std(normPrices)
#return
asset_return = normPrices[-1] - 1
#sharpe ratio
risk_free_return = 0
sharpe = np.sqrt(num_rows) * (asset_return - risk_free_return) / stddev
#write to csv
row = np.append(np.squeeze(weightsVec), [asset_return, sharpe])
csvWriter.writerow(row)
csvFile.close()