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
24import datetime
25
26from .. import feed
27from .. import TimeFrame
28from ..utils import date2num
29
30
31class VChartCSVData(feed.CSVDataBase):
32    '''
33    Parses a `VisualChart <http://www.visualchart.com>`_ CSV exported file.
34
35    Specific parameters (or specific meaning):
36
37      - ``dataname``: The filename to parse or a file-like object
38    '''
39
40    vctframes = dict(
41        I=TimeFrame.Minutes,
42        D=TimeFrame.Days,
43        W=TimeFrame.Weeks,
44        M=TimeFrame.Months)
45
46    def _loadline(self, linetokens):
47        itokens = iter(linetokens)
48
49        ticker = next(itokens)  # skip ticker name
50        if not self._name:
51            self._name = ticker
52
53        # day/intraday indication
54        timeframe = next(itokens)
55
56        self._timeframe = self.vctframes[timeframe]
57
58        dttxt = next(itokens)
59        y, m, d = int(dttxt[0:4]), int(dttxt[4:6]), int(dttxt[6:8])
60
61        tmtxt = next(itokens)
62        if timeframe == 'I':
63            # use the provided time
64            hh, mmss = divmod(int(tmtxt), 10000)
65            mm, ss = divmod(mmss, 100)
66        else:
67            # put it at the end of the session parameter
68            hh = self.p.sessionend.hour
69            mm = self.p.sessionend.minute
70            ss = self.p.sessionend.second
71
72        dtnum = date2num(datetime.datetime(y, m, d, hh, mm, ss))
73
74        self.lines.datetime[0] = dtnum
75        self.lines.open[0] = float(next(itokens))
76        self.lines.high[0] = float(next(itokens))
77        self.lines.low[0] = float(next(itokens))
78        self.lines.close[0] = float(next(itokens))
79        self.lines.volume[0] = float(next(itokens))
80        self.lines.openinterest[0] = float(next(itokens))
81
82        return True
83
84
85class VChartCSV(feed.CSVFeedBase):
86    DataCls = VChartCSVData
87