Completed
Push — master ( e4fe44...6106cb )
by Eddie
01:23
created

zipline.data.BcolzMinuteBarReader._get_ctable()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 8
rs 9.4286
1
# Copyright 2015 Quantopian, Inc.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
import bcolz
15
import json
16
import os
17
import pandas as pd
18
19
MINUTES_PER_DAY = 390
20
21
METADATA_FILENAME = 'metadata.json'
22
23
24
class BcolzMinuteBarReader(object):
25
26
    def __init__(self, rootdir, sid_path_func=None):
27
        self.rootdir = rootdir
28
29
        metadata = self._get_metadata()
30
31
        self.first_trading_day = pd.Timestamp(
32
            metadata['first_trading_day'], tz='UTC')
33
34
        self._sid_path_func = sid_path_func
35
36
        self._carrays = {
37
            'open': {},
38
            'high': {},
39
            'low': {},
40
            'close': {},
41
            'volume': {},
42
            'sid': {},
43
            'dt': {},
44
        }
45
46
    def _get_metadata(self):
47
        with open(os.path.join(self.rootdir, METADATA_FILENAME)) as fp:
48
            return json.load(fp)
49
50
    def _get_ctable(self, asset):
51
        sid = int(asset)
52
        if self._sid_path_func is not None:
53
            path = self._sid_path_func(self.rootdir, sid)
54
        else:
55
            path = "{0}/{1}.bcolz".format(self.rootdir, sid)
56
57
        return bcolz.open(path, mode='r')
58
59
    def _find_position_of_minute(self, minute_dt):
60
        """
61
        Internal method that returns the position of the given minute in the
62
        list of every trading minute since market open of the first trading
63
        day.
64
65
        IMPORTANT: This method assumes every day is 390 minutes long, even
66
        early closes.  Our minute bcolz files are generated like this to
67
        support fast lookup.
68
69
        ex. this method would return 2 for 1/2/2002 9:32 AM Eastern, if
70
        1/2/2002 is the first trading day of the dataset.
71
72
        Parameters
73
        ----------
74
        minute_dt: pd.Timestamp
75
            The minute whose position should be calculated.
76
77
        Returns
78
        -------
79
        The position of the given minute in the list of all trading minutes
80
        since market open on the first trading day.
81
        """
82
        NotImplementedError
83
84
    def _open_minute_file(self, field, asset):
85
        sid_str = str(int(asset))
86
87
        try:
88
            carray = self._carrays[field][sid_str]
89
        except KeyError:
90
            carray = self._carrays[field][sid_str] = \
91
                self._get_ctable(asset)[field]
92
93
        return carray
94