1#!/usr/bin/env python
2# -*- coding: utf-8; py-indent-offset:4 -*-
3###############################################################################
4#
5# Copyright (C) 2015, 2016, 2017 Daniel Rodriguez
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20###############################################################################
21from __future__ import (absolute_import, division, print_function,
22                        unicode_literals)
23
24from backtrader import date2num
25import backtrader.feed as feed
26
27
28class BlazeData(feed.DataBase):
29    '''
30    Support for `Blaze <blaze.pydata.org>`_ ``Data`` objects.
31
32    Only numeric indices to columns are supported.
33
34    Note:
35
36      - The ``dataname`` parameter is a blaze ``Data`` object
37
38      - A negative value in any of the parameters for the Data lines
39        indicates it's not present in the DataFrame
40        it is
41    '''
42
43    params = (
44        # datetime must be present
45        ('datetime', 0),
46        # pass -1 for any of the following to indicate absence
47        ('open', 1),
48        ('high', 2),
49        ('low', 3),
50        ('close', 4),
51        ('volume', 5),
52        ('openinterest', 6),
53    )
54
55    datafields = [
56        'datetime', 'open', 'high', 'low', 'close', 'volume', 'openinterest'
57    ]
58
59    def start(self):
60        super(BlazeData, self).start()
61
62        # reset the iterator on each start
63        self._rows = iter(self.p.dataname)
64
65    def _load(self):
66        try:
67            row = next(self._rows)
68        except StopIteration:
69            return False
70
71        # Set the standard datafields - except for datetime
72        for datafield in self.datafields[1:]:
73            # get the column index
74            colidx = getattr(self.params, datafield)
75
76            if colidx < 0:
77                # column not present -- skip
78                continue
79
80            # get the line to be set
81            line = getattr(self.lines, datafield)
82            line[0] = row[colidx]
83
84        # datetime - assumed blaze always serves a native datetime.datetime
85        colidx = getattr(self.params, self.datafields[0])
86        dt = row[colidx]
87        dtnum = date2num(dt)
88
89        # get the line to be set
90        line = getattr(self.lines, self.datafields[0])
91        line[0] = dtnum
92
93        # Done ... return
94        return True
95