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 datetime import date, datetime, time
25
26from .. import feed
27from ..utils import date2num
28
29
30class BacktraderCSVData(feed.CSVDataBase):
31    '''
32    Parses a self-defined CSV Data used for testing.
33
34    Specific parameters:
35
36      - ``dataname``: The filename to parse or a file-like object
37    '''
38
39    def _loadline(self, linetokens):
40        itoken = iter(linetokens)
41
42        dttxt = next(itoken)  # Format is YYYY-MM-DD - skip char 4 and 7
43        dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))
44
45        if len(linetokens) == 8:
46            tmtxt = next(itoken)  # Format if present HH:MM:SS, skip 3 and 6
47            tm = time(int(tmtxt[0:2]), int(tmtxt[3:5]), int(tmtxt[6:8]))
48        else:
49            tm = self.p.sessionend  # end of the session parameter
50
51        self.lines.datetime[0] = date2num(datetime.combine(dt, tm))
52        self.lines.open[0] = float(next(itoken))
53        self.lines.high[0] = float(next(itoken))
54        self.lines.low[0] = float(next(itoken))
55        self.lines.close[0] = float(next(itoken))
56        self.lines.volume[0] = float(next(itoken))
57        self.lines.openinterest[0] = float(next(itoken))
58
59        return True
60
61
62class BacktraderCSV(feed.CSVFeedBase):
63    DataCls = BacktraderCSVData
64