def __request(self, symbol, stat):
     try:
         url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
         return urllib.urlopen(url).read().strip().strip('"')
     except IOError:
         raise ufException(Errors.NETWORK_ERROR, "Can't connect to Yahoo server")
     except BaseException as excep:
         raise ufException(Errors.UNKNOWN_ERROR, "Unknown Error in YahooFinance.__request %s" % excep)
    def readCol(self, colNumber, startRow=0, endRow=-1):
        if abs(colNumber) > self.sheet.ncols:
            raise ufException(Errors.INDEX_RANGE_ERROR,
                              "Excellib.readCol: col number too big: col %s, max %s" % (colNumber, self.sheet.ncols) )
        if max(abs(startRow), abs(endRow)) > self.sheet.nrows:
            raise ufException(Errors.INDEX_RANGE_ERROR,
                              "Excellib.readCol: row number too big: row %s, max %s" % (max(abs(startRow), abs(endRow)), self.sheet.nrows) )
        if -1 == endRow:
            endRow = self.sheet.nrows

        return [self.readCell(i, colNumber) for i in range(startRow, endRow)]
    def readRow(self, rowNumber, startCol=0, endCol=-1):
        if abs(rowNumber) >= self.sheet.nrows:
            raise ufException(Errors.INDEX_RANGE_ERROR,
                              "Excellib.readRow: row number too big: row %s, max %s" % (rowNumber, self.sheet.nrows) )
        if max(abs(startCol), abs(endCol)) > self.sheet.ncols:
            raise ufException(Errors.INDEX_RANGE_ERROR,
                              "Excellib.readRow: col number too big: col %s, max %s" % (max(abs(startCol), abs(endCol)), self.sheet.ncols) )
        if -1 == endCol:
            endCol = self.sheet.ncols

        return [self.readCell(rowNumber, i) for i in range(startCol, endCol)]
    def linearRegression(self):
        if self.__regressioned:
            return

        if not self.__benchmarkValues:
            self.__benchmarkValues = YahooFinance().get_historical_prices(self.__benchmark, self.__dateValues[0].date, self.__dateValues[-1].date)

        tradeSuspended = False
        if 0 in map(lambda x: float(x.adjClose), self.__dateValues):
            tradeSuspended = True

        #filter out date tha't not in both stock and benchmark
        dateSet = set([dateValue.date for dateValue in self.__dateValues]) & set([dateValue.date for dateValue in self.__benchmarkValues])
        self.__dateValues = filter(lambda dateValue: dateValue.date in dateSet, self.__dateValues)
        self.__benchmarkValues = filter(lambda dateValue: dateValue.date in dateSet, self.__benchmarkValues)

        if len(self.__dateValues) <= 1 or tradeSuspended:
            msg = "Not enought dateValues" if len(self.__dateValues) <= 1 else "trade suspended"
            LOG.debug(msg)

            self.__beta = 0
            self.__alpha = 0
            self.__regressioned = True
            return

        try:
            x = [float(self.__benchmarkValues[index + 1].adjClose)/float(self.__benchmarkValues[index].adjClose) for index in range(len(self.__benchmarkValues) - 1)]
            y = [float(self.__dateValues[index + 1].adjClose)/float(self.__dateValues[index].adjClose) for index in range(len(self.__dateValues) - 1)]
            (self.__beta, self.__alpha) = numpy.polyfit(x, y, 1)
            self.__regressioned = True
        except BaseException as excep:
            raise ufException(Errors.UNKNOWN_ERROR, "stockMeasurement.linearRegression got unknown error %s" % excep)
    def plot(self):
        """ plot dataValue """
        try:
            fig = pyplot.figure()

            i = 0
            ax0 = None
            for label, dateValues in self.dateValueDict.items():
                if 0 == i:
                    ax = fig.add_axes(self.rect[i])
                    ax0 = ax
                else:
                    ax = fig.add_axes(self.rect[i], sharex=ax0)
                i += 1
                ax.plot_date(
                    [datetime.strptime(dateValue.date, self.dateFormat) for dateValue in dateValues],
                    [dateValue.value for dateValue in dateValues],
                    fmt="b-",
                )
                ax.set_ylabel(label)
                ax.set_ylim(
                    min([int(dateValue.value) for dateValue in dateValues]) / 1.1,
                    max([int(dateValue.value) for dateValue in dateValues]) * 1.1,
                )
                # ax.set_ylim(0, 1000)

            pyplot.show()

        except ufException as excep:
            raise excep
        except BaseException as excep:
            raise ufException(Errors.UNKNOWN_ERROR, "plotDateValueDict.plot got unknown error %s" % excep)
    def calculateReturn(self, dateValues, *args):
        try:
            return self.strategy(dateValues, *args)

        except ufException as excep:
            raise excep
        except BaseException as excep:
            raise ufException(Errors.UNKNOWN_ERROR, "tradingStrategyFactory.calculateReturn got unknown error %s" % traceback.print_exc())
    def get_historical_prices(self, symbol, start_date, end_date):
        """
        Get historical prices for the given ticker symbol.
        Date format is 'YYYY-MM-DD'

        Returns a nested list.
        """
        try:
            start_date = str(start_date).replace('-', '')
            end_date = str(end_date).replace('-', '')

            url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
                'd=%s&' % str(int(end_date[4:6]) - 1) + \
                'e=%s&' % str(int(end_date[6:8])) + \
                'f=%s&' % str(int(end_date[0:4])) + \
                'g=d&' + \
                'a=%s&' % str(int(start_date[4:6]) - 1) + \
                'b=%s&' % str(int(start_date[6:8])) + \
                'c=%s&' % str(int(start_date[0:4])) + \
                'ignore=.csv'
            days = urllib.urlopen(url).readlines()
            values = [day[:-2].split(',') for day in days]
            # sample values:[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos'], \
            #              ['2009-12-31', '112.77', '112.80', '111.39', '111.44', '90637900', '109.7']...]
            data = []
            for value in values[1:]:
                data.append(StockDailyType(value[0], value[1], value[2], value[3], value[4], value[5], value[6]))

            dateValues = sorted(data, key=itemgetter(0))
            return dateValues

        except IOError:
            raise ufException(Errors.NETWORK_ERROR, "Can't connect to Yahoo server")
        except BaseException as excep:
            raise ufException(Errors.UNKNOWN_ERROR, "Unknown Error in YahooFinance.get_historical_prices %s" % excep)
        #sample output
        #[stockDaylyData(date='2010-01-04, open='112.37', high='113.39', low='111.51', close='113.33', volume='118944600', adjClose='111.6'))...]
    def __buildExl(self, stock, workbook):
        ''' get one stock historical data and store it '''
        try:
            ws = workbook.add_sheet(stock)

            #get data
            yahooFinance = YahooFinance()
            allData = yahooFinance.get_historical_prices(stock, self.__startDate, self.__endDate)
            for col, field in enumerate(['date', 'open', 'high', 'low', 'close', 'volume', 'adjClose']):
                ws.write(0, col, field)

            for row, data in enumerate(allData):
                for col, field in enumerate(['date', 'open', 'high', 'low', 'close', 'volume', 'adjClose']):
                    ws.write(row+1, col, getattr(data, field) )

        except ufException as excp:
            raise excp
        except Exception:
            raise ufException(Errors.UNKNOWN_ERROR, "historicalStorage.__buildExl got unknown error  %s"
                              % traceback.print_exc())
    def plot(self):
        ''' plot '''
        try:
            returns = []
            deviations = []
            portfolio1 = self.labelReturnDeviations[0]
            portfolio2 = self.labelReturnDeviations[1]

            for i in range(100):
                returns.append(portfolio1['return']*i/100 + portfolio2['return']*(100-i)/100)
                deviations.append(pow(portfolio1['deviation']*i/100, 2) + pow(portfolio2['deviation']*(100-i)/100, 2) + 2*(i/100)*((100-i)/100)*portfolio1['cov'])

            pyplot.plot([math.sqrt(deviation) for deviation in deviations], returns,'b-')
            pyplot.ylabel('Returns')
            pyplot.xlabel('Deviations')

        except ufException as excep:
            raise excep
        except BaseException as excep:
            raise ufException(Errors.UNKNOWN_ERROR, "plotPortfolio.plot got unknown error %s" % excep)
    def buildExls(self, stocks, div=1):
        ''' get a list of stock data and store '''
        print "BuildExls %s, div %s" % (stocks, div)

        if div < 1:
            raise ufException(Errors.INDEX_RANGE_ERROR, "div need to be at least 1, %s are given" % div)

        for i in range(div):
            workbook = Workbook()
            stocksToProcess = stocks[i * len(stocks)/div: (i+1) * len(stocks)/div]
            for stock in stocksToProcess:
                try:
                    self.__buildExl(stock, workbook)
                except Exception:
                    print "failed buildExl for stock %s: %s" % (stock, traceback.print_exc())

                #sleep for 2 seconds, or Yahoo server will throw exception
                time.sleep(2)

            fileName = '%s%d.xls' % (self.__outputPrefix, i)
            print "Saved %s to %s" % (stocksToProcess, fileName)
            workbook.save(fileName)
 def readCell(self, rowNumber, colNumber):
     ''' read a cell'''
     try:
         return self.sheet.cell(rowNumber, colNumber).value
     except BaseException as excep:
         raise ufException(Errors.UNKNOWN_ERROR, "Unknown Error in Excellib.readCell %s" % excep)