-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_assets.py
57 lines (43 loc) · 1.89 KB
/
load_assets.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
import threading
import csv
import urllib2
import StringIO
import asset
def convert_asset_raw_data_to_list(asset_raw_data):
# Convert asset_raw_data to CSV format
asset_raw_data_file = StringIO.StringIO(asset_raw_data)
asset_data_reader = csv.reader(asset_raw_data_file, delimiter=',')
# Convert CSV format to list
asset_data_list = []
for asset_data in asset_data_reader:
asset_data_list.append(asset_data)
return asset_data_list
def convert_asset_data_list_to_asset_class(asset_data_list, asset_name):
asset_class = asset.Asset(asset_name)
for asset_data in asset_data_list:
date = asset_data[0]
adjusted_closing_price = asset_data[6]
# Skip the headers
if not(date == 'Date'):
asset_class.add_day(date, adjusted_closing_price)
return asset_class
# Download ETF data and build class for asset_name
# Then store in global assets at given index
def load_asset(asset_name, index, assets):
# URL for dividend downloading: http://real-chart.finance.yahoo.com/table.csv?s=SHY&g=v&ignore=.csv
asset_raw_data = urllib2.urlopen('http://real-chart.finance.yahoo.com/table.csv?s=' + asset_name + '&ignore=.csv')
asset_data_list = convert_asset_raw_data_to_list(asset_raw_data.read())
asset_class = convert_asset_data_list_to_asset_class(asset_data_list, asset_name)
assets[index] = asset_class
# Uses multi-threading to parallelize downloads and asset class processing
def load_assets(asset_names):
assets = [None] * len(asset_names)
thread_list = []
for index, asset_name in enumerate(asset_names):
thread = threading.Thread(target=load_asset, args=(asset_name, index, assets))
thread_list.append(thread)
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
return assets