Пример #1
0
def download_ohlc(tickers, start, end):
    #ohlc =  pd.DataFrame(columns=tickers)
    ohlc = {}
    for Symbol in tickers.iteritems():
        print ('Downloading data from Yahoo for %s Symbol', Symbol)
        data = DataReader(Symbol[1], 'yahoo', start, end)
        for item in ['Open', 'High', 'Low']:
            data[item] = data[item] * data['Adj Close'] / data['Close']
        data.rename(columns={'Open': 'open', 'High': 'high', 'Low': 'low','Adj Close': 'close', 'Volume': 'volume', 'Close':'Close'},inplace=True)
        data.drop(['Close'], axis=1,inplace=True)
        ohlc[Symbol[1]] = data
    print ('Finished downloading data')
    return ohlc
Пример #2
0
def download_ohlc(sector_tickers, start, end):
    sector_ohlc = {}
    for sector, tickers in sector_tickers.iteritems():
        print 'Downloading data from Yahoo for %s sector' % sector
        data = DataReader(tickers, 'yahoo', start, end)
        for item in ['Open', 'High', 'Low']:
            data[item] = data[item] * data['Adj Close'] / data['Close']
        data.rename(items={'Open': 'open', 'High': 'high', 'Low': 'low',
                           'Adj Close': 'close', 'Volume': 'volume'},
                    inplace=True)
        data.drop(['Close'], inplace=True)
        sector_ohlc[sector] = data
    print 'Finished downloading data'
    return sector_ohlc
Пример #3
0
class Equity(object):
    """
    An Equity object provides an interface to Share Price Data for a specified Ticker Code.

    It uses pandas (DataReader and DataFrame) and SQLite as a local data store.

    Use importData() to update the SQLite data from Yahoo (adjusting as necessary) and use dataFrame() to get at the
    adjusted data.
    """

    def __init__(self, tickerCode):
        """
        Constructor.

        There isn't much to see here. It just makes a note of the Ticker Code.

        :param tickerCode: Ticker Code.
        """

        Logger.log(logging.DEBUG, "Log Object Creation", {"scope":__name__, "arguments":" ".join({tickerCode})})

        self._tickerCode = tickerCode


    def importData(self):
        """
        Import (New) Data from Yahoo.
        """

        start = self._getLatestDate()
        end = self._getTodaysDate()

        Logger.log(logging.INFO, "Loading Data", {"scope":__name__, "tickerCode":self._tickerCode, "start":str(start), "end":str(end)})
        self._data = DataReader(self._tickerCode, "yahoo", start, end)

        self._data['Code'] = self._tickerCode

        for item in ['Open', 'High', 'Low']:
            self._data[item] = self._data[item] * self._data['Adj Close'] / self._data['Close']

        self._data.drop('Close', axis=1, inplace=True)
        self._data.rename(columns={'Adj Close':'Close'}, inplace=True)
        self._data['Volume'] = self._data['Volume'].astype(float)

        connection = sqlite3.connect(pyswing.database.pySwingDatabase)

        query = "insert or replace into Equities (Date, Open, High, Low, Volume, Close, Code) values (?,?,?,?,?,?,?)"
        connection.executemany(query, self._data.to_records(index=True))
        connection.commit()

        connection.close()

    def dataFrame(self):

        connection = sqlite3.connect(pyswing.database.pySwingDatabase)
        query = "select * from Equities where Code = '%s'" % (self._tickerCode)
        equityData = read_sql_query(query, connection, 'Date')
        connection.close()

        return equityData


    def _getLatestDate(self):

        connection = sqlite3.connect(pyswing.database.pySwingDatabase)

        query = "select max(Date) from Equities where Code = '%s'" % (self._tickerCode)

        cursor = connection.cursor()

        cursor.execute(query)
        dateString = cursor.fetchone()[0]

        connection.close()

        date = pyswing.constants.pySwingStartDate
        if dateString:
            date = datetime.datetime.strptime(dateString, "%Y-%m-%d %H:%M:%S")

        return date

    def _getTodaysDate(self):

        return datetime.datetime.now()