Ejemplo n.º 1
0
    def at(self, ticker, current_date):
        # Cache quotes array to avoid re-reading the file when asked for the same ticker twice in a row
        if ticker != self._last_ticker or not self.quotes:
            # parse the historical quotes file for the given ticker
            self.quotes = parse_historical_quotes_file(ticker, self._quotes_dir, self._has_header)
            self._last_ticker = ticker

        # if the requested date is outside the file's date range raise exception
        if not self.quotes or current_date < self.quotes.keys()[-1] or current_date > self.quotes.keys()[0]:
            raise DatesNotAvailableException

        # return the quote on the closest date within the range
        return float(self.quotes[find_closest(self.quotes.keys(), current_date)])
Ejemplo n.º 2
0
def get_price_change(ticker, start_date, end_date):
    with open(HISTORICAL_QUOTES_PATH + ticker.upper() + '.txt', 'r') as fp:
        # build quotes list for the selected stock
        quotes = OrderedDefaultdict(list)
        cp = csv.reader(fp)

        try:
            cp.next()  # skip header line
        except StopIteration:
            raise DatesNotAvailableException

        for l in cp:
            if len(l) < FIELD_QUOTE_PRICE:
                continue
            quotes[datetime.strptime(l[FIELD_QUOTE_DATE], QUOTES_DATE_FORMAT)] = float(l[FIELD_QUOTE_PRICE])

        if not quotes or start_date < quotes.keys()[-1] or end_date > quotes.keys()[0]:
            raise DatesNotAvailableException

        start = quotes[find_closest(quotes.keys(), start_date)]
        end = quotes[find_closest(quotes.keys(), end_date)]

        return start, end, end / start
Ejemplo n.º 3
0
def get_price_change(ticker, start_date, end_date):
    with open(HISTORICAL_QUOTES_PATH + ticker.upper() + ".txt", "r") as fp:
        # build quotes list for the selected stock
        quotes = OrderedDefaultdict(list)
        cp = csv.reader(fp)

        try:
            cp.next()  # skip header line
        except StopIteration:
            raise DatesNotAvailableException

        for l in cp:
            if len(l) < FIELD_QUOTE_PRICE:
                continue
            quotes[datetime.strptime(l[FIELD_QUOTE_DATE], QUOTES_DATE_FORMAT)] = float(l[FIELD_QUOTE_PRICE])

        if not quotes or start_date < quotes.keys()[-1] or end_date > quotes.keys()[0]:
            raise DatesNotAvailableException

        start = quotes[find_closest(quotes.keys(), start_date)]
        end = quotes[find_closest(quotes.keys(), end_date)]

        return start, end, end / start