Пример #1
0
def main():
    symbols = ['AAPL', 'MSFT', 'BRK-A']
    # Specifically chosen to include the AAPL split on June 9, 2014.
    for symbol in symbols:
        data = DataReader(
            symbol,
            'yahoo',
            start='2014-03-01',
            end='2014-09-01',
        )
        data.rename(
            columns={
                'Open': 'open',
                'High': 'high',
                'Low': 'low',
                'Close': 'close',
                'Volume': 'volume',
            },
            inplace=True,
        )
        del data['Adj Close']

        dest = join(here, symbol + '.csv')
        print("Writing %s -> %s" % (symbol, dest))
        data.to_csv(dest, index_label='day')
Пример #2
0
def main():
    symbols = ["AAPL", "MSFT", "BRK-A"]
    # Specifically chosen to include the AAPL split on June 9, 2014.
    for symbol in symbols:
        data = DataReader(
            symbol,
            "yahoo",
            start="2014-03-01",
            end="2014-09-01",
        )
        data.rename(
            columns={
                "Open": "open",
                "High": "high",
                "Low": "low",
                "Close": "close",
                "Volume": "volume",
            },
            inplace=True,
        )
        del data["Adj Close"]

        dest = join(here, symbol + ".csv")
        print("Writing %s -> %s" % (symbol, dest))
        data.to_csv(dest, index_label="day")
Пример #3
0
def p03_AAPL(tomtom):
    try:
        DAX = DataReader('AAPL','yahoo',start = '01/01/2000')
        DAX.to_csv(tomtom.get_tmp_name('p03_AAPL.csv'))
    except RemoteDataError:
        print('Error while reading data, revert to stored file in example_data')
        shutil.copy('example_data/p03_AAPL.csv', 'temp')
Пример #4
0
def get_stock(stock, beg, end, write=False, load=False):
    """ return df from yfinance """
    if load == True: return pd.read_csv(f'./{stock}.csv')

    ret_df = DataReader(stock, 'yahoo', beg, end)
    if write: ret_df.to_csv(f'./{stock}.csv')
    return ret_df
def get_outperformance(ticker_list,
                       idx,
                       start=datetime(1993, 1, 1),
                       end=datetime(2019, 7, 9)):
    errors = []
    complete = len(ticker_list)
    done = 0
    for ticker in ticker_list:
        try:
            prices = DataReader(ticker, 'yahoo', start, end)
            yr_ret = list(prices['Close'].pct_change(252).dropna())

            length = len(prices)
            for i in range(length - len(yr_ret)):
                yr_ret.append(nan)

            tmp_idx = idx.loc[prices.index]
            prices['yr_ret'] = yr_ret
            prices['outperformance'] = (
                (prices['yr_ret'] > tmp_idx).astype(int) -
                (prices['yr_ret'] < -tmp_idx).astype(int))
            prices['magnitude'] = abs(prices['yr-ret']) - abs(tmp_idx)
            st = str(min(prices.index))[:-9]
            en = str(max(prices.index))[:-9]
            file = ticker + '_' + st + '_' + en + '.csv'
            prices.to_csv(file)
        except:
            errors.append(ticker)
        done += 1
        print('\r' + '|' + ((u'\u2588') * (int(
            (done + 1) / complete * 100))).ljust(99) +
              '| {0:.2f}%'.format(min((done + 1) / complete * 100, 100)),
              end='')
    return errors
Пример #6
0
def addCustomColumns(df, market_upd=False):
    start = datetime.date(
        int(df.index.get_level_values('date')[0]) - 10,
        int(df['month'].values[0]), 1)
    end_date_ls = [
        int(d) for d in datetime.date.today().strftime('%Y-%m-%d').split("-")
    ]
    end = datetime.date(end_date_ls[0], end_date_ls[1], end_date_ls[2])
    try:
        url = "https://www.quandl.com/api/v1/datasets/WIKI/{0}.csv?column=4&sort_order=asc&trim_start={1}&trim_end={2}".format(
            df.index.get_level_values('ticker')[0], start, end)
        qr = pd.read_csv(url)
        qr['Date'] = qr['Date'].astype('datetime64[ns]')
        # quotes = DataReader(df.index.get_level_values('ticker')[0], 'yahoo', start, end, pause=1)['Close']
        # quotes = DataReader(df.index.get_level_values('ticker')[0], 'yahoo', start, end, pause=1)['Close']
    except:
        print("Could not read time series data for %s" %
              df.index.get_level_values('ticker')[0])
        exc_type, exc_obj, exc_tb = sys.exc_info()
        app.logger.info(
            "Could not read time series data for {3}: {0}, {1}, {2}".format(
                exc_type, exc_tb.tb_lineno, exc_obj,
                df.index.get_level_values('ticker')[0]))
        raise
    df = addBasicCustomCols(df, qr)
    df = addGrowthCustomCols(df, qr)
    df = addTimelineCustomCols(df, qr)

    pdb.set_trace()
    if market_upd:
        # market = DataReader(".INX", 'google', start, end, pause=1)['Close']
        market = DataReader('^GSPC', 'yahoo', start, end, pause=1)['Close']
        market.to_csv(
            '/home/ubuntu/workspace/finance/app/static/docs/market.csv')
        quotes = pd.DataFrame(quotes)
        market.columns = ['Date', 'market']
        market.set_index('Date')
        quotes['market'] = market
    else:
        market = pd.read_csv(
            '/home/ubuntu/workspace/finance/app/static/docs/market.csv')
        market.columns = ['Date', 'market']
        market['Date'] = market['Date'].apply(pd.to_datetime)
        market = market.set_index('Date')
        qr = pd.merge(qr.set_index('Date'),
                      market,
                      left_index=True,
                      right_index=True)
    df = calcBetas(df, qr)
    '''
    Still need to do:
    'enterpriseToEbitda'
    'ebitdaMargins'
    'ebitda',
    'shortRatio'
    '''
    return df
def load_data(*func_codes):
    datas = []
    for code in func_codes:
        if os.path.exists(f"{code}.csv"):
            data = pd.read_csv(f"{code}.csv")
        else:
            data = DataReader(f"{code}.SZ", "yahoo", start, end)
            data.to_csv(f'{code}.csv')
        datas.append(data)

    return datas
Пример #8
0
def load_data():
    if os.path.exists('000001.csv'):
        data_ss = pd.read_csv('000001.csv')
        data_kdxf = pd.read_csv('002230.csv')
    else:
        # 上证综指
        data_ss = DataReader("000001.SS", "yahoo", start, end)
        # 002230 科大讯飞 深证
        data_kdxf = DataReader("002230.SZ", "yahoo", start, end)
        data_ss.to_csv('000001.csv')
        data_kdxf.to_csv('002230.csv')
    return data_ss, data_kdxf
Пример #9
0
 def getcsv(self):
     '''
     pandas DataReaderを利用してYahooFinanceから過去一年ぶんの株価データを抽出、
     csvファイル化する関数
     '''
     if os.path.exists(self.basedir):
         end = datetime.now()
         start = datetime(end.year - 1, end.month, end.day)
         for index in INDEX_LIST:
             filename = self.basedir + index + ".csv"
             price = DataReader(index, 'yahoo', start, end)
             price.to_csv(filename)
Пример #10
0
def load_data():
    if os.path.exists('000001.csv'):
        data_ss = pd.read_csv('000001.csv')
        data_tlz = pd.read_csv('300005.csv')
    else:
        # 上证综指
        data_ss = DataReader("000001.SS", "yahoo", start, end)
        # 300005 探路者股票 深证
        data_tlz = DataReader("300005.SZ", "yahoo", start, end)
        data_ss.to_csv('000001.csv')
        data_tlz.to_csv('300005.csv')
    return data_ss, data_tlz
Пример #11
0
def _load_raw_yahoo_data(indexes=None, stocks=None, start=None, end=None):
    """Load closing prices from yahoo finance.

    :Optional:
        indexes : dict (Default: {'SPX': '^GSPC'})
            Financial indexes to load.
        stocks : list (Default: ['AAPL', 'GE', 'IBM', 'MSFT',
                                 'XOM', 'AA', 'JNJ', 'PEP', 'KO'])
            Stock closing prices to load.
        start : datetime (Default: datetime(1993, 1, 1, 0, 0, 0, 0, pytz.utc))
            Retrieve prices from start date on.
        end : datetime (Default: datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc))
            Retrieve prices until end date.

    :Note:
        This is based on code presented in a talk by Wes McKinney:
        http://wesmckinney.com/files/20111017/notebook_output.pdf
    """

    assert indexes is not None or stocks is not None, """
must specify stocks or indexes"""

    if start is None:
        start = pd.datetime(1990, 1, 1, 0, 0, 0, 0, pytz.utc)

    if start is not None and end is not None:
        assert start < end, "start date is later than end date."

    data = OrderedDict()

    if stocks is not None:
        for stock in stocks:
            print(stock)
            stock_pathsafe = stock.replace(os.path.sep, '--')
            cache_filename = "{stock}-{start}-{end}.csv".format(
                stock=stock_pathsafe,
                start=start,
                end=end).replace(':', '-')
            cache_filepath = get_cache_filepath(cache_filename)
            if os.path.exists(cache_filepath):
                stkd = pd.DataFrame.from_csv(cache_filepath)
            else:
                stkd = DataReader(stock, 'yahoo', start, end).sort_index()
                stkd.to_csv(cache_filepath)
            data[stock] = stkd

    if indexes is not None:
        for name, ticker in iteritems(indexes):
            print(name)
            stkd = DataReader(ticker, 'yahoo', start, end).sort_index()
            data[name] = stkd

    return data
Пример #12
0
def stocks_yahoo():
    if not os.path.exists('stocks_dfs'):
        os.makedirs('stocks_dfs')
    
    start = datetime(2017,12,1)
    end = datetime(2020,12,1)
    
    for ticker in tickers:
        print(ticker)
        if not os.path.exists('stocks_dfs/{}.csv'.format(ticker)) and ticker not in problems:
            df = DataReader(ticker,'yahoo',start,end)
            df.to_csv('stocks_dfs/{}.csv'.format(ticker))
Пример #13
0
def _load_raw_yahoo_data(indexes=None, stocks=None, start=None, end=None):
    """Load closing prices from yahoo finance.

    :Optional:
        indexes : dict (Default: {'SPX': 'SPY'})
            Financial indexes to load.
        stocks : list (Default: ['AAPL', 'GE', 'IBM', 'MSFT',
                                 'XOM', 'AA', 'JNJ', 'PEP', 'KO'])
            Stock closing prices to load.
        start : datetime (Default: datetime(1993, 1, 1, 0, 0, 0, 0, pytz.utc))
            Retrieve prices from start date on.
        end : datetime (Default: datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc))
            Retrieve prices until end date.

    :Note:
        This is based on code presented in a talk by Wes McKinney:
        http://wesmckinney.com/files/20111017/notebook_output.pdf
    """
    assert indexes is not None or stocks is not None, """
must specify stocks or indexes"""

    if start is None:
        start = pd.datetime(1990, 1, 1, 0, 0, 0, 0, pytz.utc)

    if start is not None and end is not None:
        assert start < end, "start date is later than end date."

    data = OrderedDict()

    if stocks is not None:
        for stock in stocks:
            logger.info('Loading stock: {}'.format(stock))
            stock_pathsafe = stock.replace(os.path.sep, '--')
            cache_filename = "{stock}-{start}-{end}.csv".format(
                stock=stock_pathsafe,
                start=start,
                end=end).replace(':', '-')
            cache_filepath = get_cache_filepath(cache_filename)
            if os.path.exists(cache_filepath):
                stkd = pd.DataFrame.from_csv(cache_filepath)
            else:
                stkd = DataReader(stock, 'yahoo', start, end).sort_index()
                stkd.to_csv(cache_filepath)
            data[stock] = stkd

    if indexes is not None:
        for name, ticker in iteritems(indexes):
            logger.info('Loading index: {} ({})'.format(name, ticker))
            stkd = DataReader(ticker, 'yahoo', start, end).sort_index()
            data[name] = stkd

    return data
Пример #14
0
def HistoricalData(symbol: str,
                   start: str,
                   end=datetime.today().strftime("%Y-%m-%d")):
    with open("./apikey", "r") as f:
        key = f.readline()
    data = DataReader(symbol, "av-daily-adjusted", start, end, api_key=key)
    # adjusted close price and dates
    data.to_csv("./data/{}.csv".format(symbol))
    price = list(data["adjusted close"])  # adjusted close price
    with open("./data/{}.csv".format(symbol), "r") as f:
        dates = [
            line[:10] for line in f.readlines()[1:]
        ]  # get date of each price (ignore first line; column description)
    return {"price": price, "dates": dates}
Пример #15
0
def load_data():
    if os.path.exists('000001.csv'):
        data_ss = pd.read_csv('000001.csv')
    else:
        # 上证综指
        data_ss = DataReader("000001.SS", "yahoo", start, end)
        data_ss.to_csv('000001.csv')

    if os.path.exists('000002.csv'):
        data_tlz = pd.read_csv('000002.csv')
    else:
        # A股指数
        data_tlz = DataReader("000002.SS", "yahoo", start, end)
        data_tlz.to_csv('000002.csv')

    return data_ss, data_tlz
Пример #16
0
def download_stock(stock_id="", start_date="", period=1, log=True):
    """ stock_id --> ex: AAPL (str)
        start_date --> ex: 2020-01-19 (str) """
    if (stock_id == "") & (start_date == ""):
        stock_id = input("Enter stock ID (ex: AAPL) = ")
        start_date = input("Enter start date (ex: 2020-01-19) = ")
    else:
        pass
    stock = DataReader(stock_id, "yahoo", start_date)
    csv = "../Database/" + stock_id + ".csv"
    stock.to_csv(csv)

    uploaded = []
    data = []
    file = open(csv, "r")
    info = file.readlines()
    for x in info:
        data.append(x)
        data[len(data) - 1] = data[len(data) - 1].replace("-", ",")
    del data[
        0]  # delete the line: "Date,Open,High,Low,Close,Adj Close,Volume" on the .csv file

    raw = []
    count = 0
    print("\nReading stock data from [", csv, "]")
    if log == True:
        loop = tqdm.tqdm(total=int(len(data) / period),
                         position=0,
                         leave=False)
    for i in range(0, len(data), period):
        line = data[i].split(",")
        raw.append(float(line[CLOSE]))
        count += 1
        if log == True:
            loop.set_description('Reading stock data...'.format(len(data)))
            loop.update(1)
            time.sleep(0.001)
        else:
            pass
    print("\n\nUploaded stock data successfully!")

    return raw, stock_id
Пример #17
0
def load_data():
    """
    加载数据集

    :return:
    """
    data_info_dict = {}
    for stock_code in spec_stock_code_list:
        if not os.path.exists(f'{stock_code.split(".")[0]}.csv'):
            # 获取列表中的股票信息
            data = DataReader(stock_code,
                              'yahoo',
                              start=start_date,
                              end=end_date)
            # 保存为本地的csv文件
            data.to_csv(f'{stock_code.split(".")[0]}.csv')

        data_df = pd.read_csv(f'{stock_code.split(".")[0]}.csv')
        data_info_dict[stock_code.split('.')[0]] = data_df
    return data_info_dict
Пример #18
0
end = date(2017, 11, 06)

# DataReader is a function to import, there are different sources available to import data
# such as ggogle fin, yahoo fin,fred, Oanda(for exchange rates)

# for eg Importing FB data from goolge
stockFb = DataReader('fb', 'google', start, end)
type(stockFb)
# DataReader returns a pandas data frame object

stockFb.head()
stockFb.info()

# from yahoo
stockApl = DataReader('AAPL', 'yahoo', start, end)
stockApl.head()
stockApl.info()

#plotting
stockApl['Close'].plot(title='APPLE')
plt.show()

#sp500 from fred up to now
sp500 = DataReader('SP500', 'fred', start)
#note sys date is deafult for end argument
sp500.tail()
sp500.plot(title='SP500')

#saving locally
sp500.to_csv('SP500')
Пример #19
0
def p03_AAPL(location):
    dax = DataReader('AAPL', 'yahoo', start='01/01/2000')
    dax.to_csv(work_directory_path(location, 'p03_AAPL.csv'))
    return dax
Пример #20
0
def p03_DAX(location):
    dax = DataReader('^GDAXI', 'yahoo', start='01/01/2000')
    dax.to_csv(work_directory_path(location, 'p03_DAX.csv'))
    return dax
Пример #21
0
def p03_AAPL(location):
    dax = DataReader('AAPL', 'yahoo', start='01/01/2000')
    dax.to_csv(work_directory_path(location, 'p03_AAPL.csv'))
    return dax
Пример #22
0
# -*- coding: utf-8 -*-
"""
@author: [email protected]
@site: e-smartdata.org
"""

import pandas as pd
from pandas_datareader.data import DataReader
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# %%
amazon = DataReader('AMZN', 'stooq')
amazon.to_csv('data.csv')

# %%
amazon['Close'].plot()
amazon['Close'].plot(alpha=0.5)

# %%
amazon['Close'].plot(alpha=0.7)
amazon.resample('BQ').mean()['Close'].\
       plot(color='green', style='--', alpha=0.7)

plt.legend(['price', 'quarter average'])

# %% shifting
fig, ax = plt.subplots(3, sharex=True)

amazon['Close'].plot(ax=ax[0])
Пример #23
0
def p03_DAX(tomtom):
    DAX = DataReader('^GDAXI','yahoo',start = '01/01/2000')
    DAX.to_csv(tomtom.get_tmp_name('p03_DAX.csv'))
Пример #24
0
def p03_DAX(tomtom):
    DAX = DataReader('^GDAXI', 'yahoo', start='01/01/2000')
    DAX.to_csv(tomtom.get_tmp_name('p03_DAX.csv'))
Пример #25
0
class Prices(object):
    """
    Prices gets local or remote data, clean it and gets some statistic from the data.
    """
    def __init__(self):
        self.__loaded = False
        self.data = None
        self._volatility = None
        self.source = "Yahoo"
        #self.roll_volatility = None

    def download(self,
                 code,
                 start,
                 end,
                 downloadPath=None,
                 source='morningstar'):
        self.source = source
        Protocol.sendStatus("Data source", source)
        if source == "Yahoo":
            result = self.downloadYahoo(code, start, end, downloadPath)
            if not result:
                return self.download(code, start, end, downloadPath)
            else:
                return result
        else:
            start = dt.datetime.fromtimestamp(
                int(start)) if type(start) == str else start
            if start != None:
                self.data = DataReader(code,
                                       data_source=source,
                                       start=start,
                                       end=dt.datetime.today())
            else:
                self.data = DataReader(code,
                                       data_source=source,
                                       end=dt.datetime.today())
            self.__loaded = True
            if downloadPath != None:
                filepath = os.path.join(downloadPath,
                                        code + str(int(end)) + '.csv')
                self.data.to_csv(filepath)
                return filepath
            return None

    def downloadYahoo(self, code, start, end, downloadPath):
        """
        download take arguments and download from Yahoo! the data, save
        them in the given path and returns the csv route.
        """
        cookier = urllib.request.HTTPCookieProcessor()
        opener = urllib.request.build_opener(cookier)
        urllib.request.install_opener(opener)

        # cookie and corresponding crumb
        _cookie = None
        _crumb = None

        # headers to fake a user agent
        _headers = {
            'User-Agent':
            'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'
        }

        # perform a Yahoo financial lookup on SP500
        req = urllib.request.Request('https://finance.yahoo.com/quote/^GSPC',
                                     headers=_headers)
        f = urllib.request.urlopen(req)
        alines = f.read().decode('utf-8')

        # extract the crumb from the response
        cs = alines.find('CrumbStore')
        cr = alines.find('crumb', cs + 10)
        cl = alines.find(':', cr + 5)
        q1 = alines.find('"', cl + 1)
        q2 = alines.find('"', q1 + 1)
        _crumb = alines[q1 + 1:q2]

        # extract the cookie from cookiejar

        for c in cookier.cookiejar:
            if c.domain != '.yahoo.com':
                continue
            if c.name != 'B':
                continue
            _cookie = c.value

        # print the cookie and crumb
        #print('Cookie:', _cookie)
        #print('Crumb:', _crumb)
        # prepare the parameters and the URL

        param = dict()
        param['period1'] = int(start)
        param['period2'] = int(end)
        param['interval'] = '1d'
        param['events'] = 'history'
        param['crumb'] = _crumb
        params = urllib.parse.urlencode(param)
        url = 'https://query1.finance.yahoo.com/v7/finance/download/{}?{}'.format(
            code, params)
        req = urllib.request.Request(url, headers=_headers)

        # perform the query
        # there is no need to enter the cookie here, as it is automatically handled by opener
        try:
            f = urllib.request.urlopen(req)
        except urllib.error.URLError as e:
            if hasattr(e, 'code'):
                Protocol.sendError("Downloading from Yahoo failded", e.code)
            if hasattr(e, 'reason'):
                Protocol.sendError("Downloading from Yahoo failded", e.reason)
            return False
        except urllib.error.HTTPError as e:
            if hasattr(e, 'code'):
                Protocol.sendError("Downloading from Yahoo failded", e.code)
            if hasattr(e, 'reason'):
                Protocol.sendError("Downloading from Yahoo failded", e.reason)
            return False

        #urlopen(req)
        alines = f.read().decode('utf-8')

        #print(alines)
        holder = alines.split('\n')

        # generates csv filename
        filename = downloadPath + code + str(int(end)) + '.csv'

        # creates csv
        with open(filename, 'w') as csvfile:
            filewriter = csv.writer(csvfile,
                                    delimiter=',',
                                    quotechar='|',
                                    quoting=csv.QUOTE_MINIMAL)
            for line in holder:
                b = line.split(',')
                filewriter.writerow(b)
        return filename

    def load(self, csv_filepath):
        """
        load reads a csv file
        """
        try:
            self.data = pd.read_csv(csv_filepath)
            self.source = "Yahoo"
            self.__loaded = True
        except Exception:
            if csv_filepath == "":
                Protocol.sendError("csv path empty")
            else:
                Protocol.sendError("csv not found", csv_filepath)

    def isValidData(self):
        """
        isValidData verifies data integrity in csv, if valid use them for calculations.
        """
        if self.__loaded:
            #TODO check data
            return True
        else:
            return False

    def isLoaded(self):
        """
        isLoaded verifies if there are loaded data
        """
        return self.__loaded

    def cleanData(self):
        """ 
        cleanData removes incongruencies in data
        """
        #TODO
        pass

    def getLastPrice(self):
        """
        getLastPrice return the last closing price of the loaded data, otherwise return None
        """
        if self.isLoaded and self.isValidData():
            return self.data["Close"].iloc[-1]

        return None

    def getPlot(self):
        """
        getPlot plots loaded data, saves image in predefines path
        """
        if not self.isLoaded:
            return

        data = self.data

        plt.figure(figsize=(20, 10))

        # close
        plt.plot(data["Date"], data["Close"], "g", linewidth=5)
        # high and low
        plt.plot(data["Date"],
                 data["High"],
                 "r--",
                 data["Low"],
                 "b--",
                 linewidth=2)

        # format axes #TODO
        #ax.yaxis.set_major_formatter(mticker.FormatStrFormatter('$%1.2f'))
        #ax.xaxis.set_major_formatter(mdates.DateFormatter('%d'))
        plt.gcf().autofmt_xdate()
        plt.grid()
        plt.ylabel('Valor $', fontsize=24)
        plt.legend(["Close", "High", "Low"], prop={'size': 24})

        plt.tick_params(axis='both', which='major', labelsize=25)

        filename = TEMP_DATA_PATH + "plot_" + datetime.datetime.now().strftime(
            "%y%m%d_%H%M%S") + ".png"
        plt.savefig(filename)
        return filename

    def getPlotDash(self):
        if self.source.lower() == "yahoo":

            return {'data': [{'x': self.data.Date, 'y': self.data.Close}]}
        elif self.source == "morningstar":
            return {
                'data': [{
                    'x': self.data.index.get_level_values(1),
                    'y': self.data.Close
                }]
            }

    def getVolatility(self):
        """
        getVolatility evaluate data and retrives its volatility

        This method was taken from 'A Review of Volatility and Option Pricing' by Sovan Mitra, Section 3.2

        We can calculate the historical volatility (or sigma) of prices X using this

        sigma = sqrt(Vx)/sqrt(dt)

        where
        Vx is the sample variance 
            Vx = 1/(n-1) * SUM((Xi - MEAN(X))**2)

        Xi is calculated in all steps
            Xi = ln(X(t_{i})/ X(t_{i-1}))

        dt is the interval between to samples, we use one day
            dt = 1 day

        X(t_{i}) is a sample price.
        """
        if self._volatility == None:
            X = self.data["Close"]
            self._volatility = np.diff(np.log(X)).std()
        return self._volatility
Пример #26
0
def p03_DAX(location):
    dax = DataReader('^GDAXI', 'yahoo', start='01/01/2000')
    dax.to_csv(work_directory_path(location, 'p03_DAX.csv'))
    return dax
Пример #27
0
def p03_AAPL(tomtom):
    DAX = DataReader('AAPL','yahoo',start = '01/01/2000')
    DAX.to_csv(tomtom.get_tmp_name('p03_AAPL.csv'))
Пример #28
0
#%% Importing modules
from pandas_datareader.data import DataReader
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

#%% Import data to dataframe
raw_uber = DataReader('UBER', 'stooq')

#%% Export imported data to csv
raw_uber.to_csv('./source_data/Uber.csv')

#%% Plot close Uber values
raw_uber['Close'].plot()

#%% Create new sample with monthly periods and calculated mean
resampled_uber = raw_uber.resample('BM').mean()

#%% Plot resampled and raw close data on one plot
raw_uber['Close'].plot()
resampled_uber['Close'].plot(style='--', color='green')

#%% Plot raw close values with shift on one plot
fig, ax = plt.subplots(3, sharex=True)
raw_uber['Close'].plot(ax=ax[0])
raw_uber['Close'].shift(100).plot(ax=ax[1])
raw_uber['Close'].shift(-100).plot(ax=ax[2])

ax[0].legend(['Input'])
ax[1].legend(['Shift by 100 days'])
ax[2].legend(['Shift by -100 days'])
Пример #29
0
def p03_AAPL(tomtom):
    DAX = DataReader('AAPL', 'yahoo', start='01/01/2000')
    DAX.to_csv(tomtom.get_tmp_name('p03_AAPL.csv'))
Пример #30
0
def downloadData(string):
    """
    INPUT
        @string: Source of data - Quandl or Yahoo
    OUTPUT
        The master security sheet is opened and data for the mentioned securties
        are downloaded from the mentioned source.
        The columns in the master sheet are:
        Country, Ticker, Staging and Dest
        
        For Quandl the dataFolder is D:\Personal\Projects\SecData\Quandl
        For Yahoo Finance: D:\Personal\Projects\SecData\Yahoo_Finance
    """

    ### The input must be string
    if not isinstance(string, str):
        print("The input type is not expected.")

    ### Read in the master sheet
    masterFolder = "D:\\Trading\\Rawdata\\"
    ### My_Watch is the name of the sheet I want to read
    master = pd.read_excel(masterFolder + 'Master_Sheet.xlsx', 'My_Watch')

    ### This is where the securities are stored
    dataFolder = "D:\\Trading\\Rawdata\\Securities\\"

    ### The input must be Quandl or Yahoo
    if string == 'Quandl':
        with open("D:\\Trading\\myQuandlAuthToken.txt", 'r') as authToken:
            auth = authToken.readline()

        ### i is the row count of the DataFrame master
        ### ticker is the name of the ticker
        ### Country is which country the ticker is registered
        for i, name, country, filename in zip(range(master.shape[0]), \
        master['Ticker'], master['Country'], master['Staging']):

            if country == 'USA':
                ticker = 'EOD/' + name
            else:
                ticker = 'XNSE/' + name

            ### Declare the path where it is to be stored
            ### Example: D:\Personal\Projects\SecData\Quandl\India
            path = dataFolder + country + "\\" + string + "\\"

            ### Set the date since when the data is to be downloaded
            lastDate = checkDate(path + filename)

            ### If the file does not exist then we want to download data
            ### for the last 5 years
            if lastDate == 'Null':
                end = datetime.date.today()
                lastDate = end.replace(year=end.year - 5)

            ### Download the data
            temp = quandl.get(ticker, authtoken=auth, start_date=lastDate)
            ### Save it to the Staging file
            temp.to_csv(path + filename + ".csv")

    elif string == 'Yahoo':
        for i, name, country, filename in zip(range(master.shape[0]), \
        master['Ticker'], master['Country'], master['Staging']):

            ### The Indian stocks in Yahoo are listed with the suffix .NS
            if country == 'India':
                ticker = name + ".NS"
            else:
                ticker = name

            ### Declare the path where it is to be stored
            ### Example: D:\Personal\Projects\SecData\Quandl\India
            path = dataFolder + country + "\\" + string + "\\"

            ### Set the date since when the data is to be downloaded
            lastDate = checkDate(path + filename)

            ### If the file does not exist then we want to download data
            ### for the last 5 years
            if lastDate == 'Null':
                end = datetime.date.today()
                lastDate = end.replace(year=end.year - 5)

            ### Download the data
            temp = DataReader(ticker, 'yahoo', start=lastDate)

            ### Save the data to the staging file
            temp.to_csv(path + filename + ".csv")

    else:
        print("We can download data from the following only:")
        print("1. Quandl")
        print("2. Yahoo")