1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Yahoo! Finance market data downloader (+fix for Pandas Datareader)
5# https://github.com/ranaroussi/yfinance
6#
7# Copyright 2017-2019 Ran Aroussi
8#
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13#     http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20#
21
22from __future__ import print_function
23
24from . import Ticker, multi
25# from collections import namedtuple as _namedtuple
26
27
28class Tickers():
29
30    def __repr__(self):
31        return 'yfinance.Tickers object <%s>' % ",".join(self.symbols)
32
33    def __init__(self, tickers):
34        tickers = tickers if isinstance(
35            tickers, list) else tickers.replace(',', ' ').split()
36        self.symbols = [ticker.upper() for ticker in tickers]
37        ticker_objects = {}
38
39        for ticker in self.symbols:
40            ticker_objects[ticker] = Ticker(ticker)
41
42        self.tickers = ticker_objects
43        # self.tickers = _namedtuple(
44        #     "Tickers", ticker_objects.keys(), rename=True
45        # )(*ticker_objects.values())
46
47    def history(self, period="1mo", interval="1d",
48                start=None, end=None, prepost=False,
49                actions=True, auto_adjust=True, proxy=None,
50                threads=True, group_by='column', progress=True,
51                timeout=None, **kwargs):
52
53        return self.download(
54                period, interval,
55                start, end, prepost,
56                actions, auto_adjust, proxy,
57                threads, group_by, progress,
58                timeout, **kwargs)
59
60    def download(self, period="1mo", interval="1d",
61                 start=None, end=None, prepost=False,
62                 actions=True, auto_adjust=True, proxy=None,
63                 threads=True, group_by='column', progress=True,
64                 timeout=None, **kwargs):
65
66        data = multi.download(self.symbols,
67                              start=start, end=end,
68                              actions=actions,
69                              auto_adjust=auto_adjust,
70                              period=period,
71                              interval=interval,
72                              prepost=prepost,
73                              proxy=proxy,
74                              group_by='ticker',
75                              threads=threads,
76                              progress=progress,
77                              timeout=timeout,
78                              **kwargs)
79
80        for symbol in self.symbols:
81            self.tickers.get(symbol, {})._history = data[symbol]
82
83        if group_by == 'column':
84            data.columns = data.columns.swaplevel(0, 1)
85            data.sort_index(level=0, axis=1, inplace=True)
86
87        return data
88