This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
initialize.py
96 lines (88 loc) · 2.98 KB
/
initialize.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import csv
import datetime
def universe_from_csv():
"""
Returns instrument universe and associated holdings.
"""
holdings = {
'timestamp':'{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()),
'holdings':[]
}
Instrument_Universe = {
'timestamp': '{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()) ,
'closed':False,
'data':{'type':'root'},
'name':'instrument_universe'
}
with open('Instrument Universe.csv','r') as i_file:
reader = csv.reader(i_file,delimiter=',')
headers = []
universe = [row for row in reader]
i_file.close()
headers = [row for row in universe[0]]
for row in universe[1:]:
asset = {}
for i in range(len(row)):
asset[headers[i]] = row[i]
holdings['holdings'].append(asset)
Instrument_Universe['holdings'] = holdings['holdings']
return Instrument_Universe, holdings
def portfolio_from_csv():
"""
Returns a current portfolio and associated holdings
"""
holdings = {
'timestamp':'{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()),
'holdings':[]
}
with open('portfolio.csv','r') as p_file:
reader = csv.reader(p_file,delimiter=',')
portfolio = [row for row in reader]
p_file.close()
headers = [row for row in portfolio[0]]
my_portfolio = {
"timestamp": '{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()) ,
'closed':False,
'data':{'type':'user_portfolio'},
'name':'my_portfolio'
}
for p in portfolio[1:]:
if float(p[2]) > 0:
asset = {}
asset[headers[0]] = p[0]
asset[headers[1]] = p[1]
asset['quantity'] = float(p[2])
holdings['holdings'].append(asset)
return my_portfolio, holdings
def benchmarks_from_csv():
"""
Returns a set of benchmark portfolios and associated holdings.
"""
benchmark_portfolios = []
with open('benchmarks.csv','r') as b_file:
reader = csv.reader(b_file,delimiter=',')
benchmarks = [row for row in reader]
b_file.close()
headers = [row for row in benchmarks[0]]
#b is for benchmark
for b in range(2,len(headers)): #instrument_id, and name
benchmark = {
"timestamp": '{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()),
'data':{'type':'benchmark'},
'closed':False,
"name":headers[b]
}
holdings = {
'timestamp':'{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()),
'holdings' :[]
}
#a is for asset
for a in benchmarks[1:]:
if float(a[b]) > 0:
asset = {}
asset[headers[0]] = a[0]
asset[headers[1]] = a[1]
asset['quantity'] = float(a[b])
holdings['holdings'].append(asset)
benchmark_portfolios.append([benchmark, holdings])
return benchmark_portfolios