Example #1
0
 def rank(self, stocks):
   """Ranks a collection of stocks given an end date and a start date
      based on a certain objective function.
     
     Args:
       stocks: list of ticker symbols
     Returns:
       an array of ordered stocks according to rank with some information
       as to why they were ranked a certain way.
   """
   def compareF(item1, item2):
     return int((item1['info']['total_return'] * 100) - (item2['info']['total_return'] * 100))
     
   stockInfos = []
   for stock in stocks:
     tbl = YahooStockTable.make_data_table(stock, start_date, end_date)
     #print tbl.table
     try:
       infoTuple = self.getInformation(tbl)
     except Exception:
       #print tbl.table
       continue
     sInfo = {}
     sInfo['info'] = infoTuple
     sInfo['stock_name'] = stock       
     stockInfos.append(sInfo)
   stockInfos.sort(compareF)
   return stockInfos
Example #2
0
 def main_fundamentals(stocks, start_year):
   start_date = DateUtils.getDateStr(start_year, 1, 2)
   end_date = DateUtils.getDateStr(start_year, 1, 3)
   for stock in stocks:
     dt = YahooStockTable.make_data_table(stock, start_date, end_date)
     print dt.header
     print dt.table
   return ''
Example #3
0
 def get_return(self, stock):
   """Gets the return on a particular stock for the particular
      start_date and end_date.
       
      If one had a 5% gain, this will return 1.05
      If one had a 10% loss, this will return 0.9
   """
   ytable = YahooStockTable.make_data_table(stock, self.start_date, self.end_date)
   closing_values = ytable.getAllClosing()
   return StockUtils.getTotalReturn(closing_values)
Example #4
0
  def makeMonthlyTables(ticker_symbol, year):
     """Provide monthly (12 months) for a particular year for a particular
        ticker symbol.

     Ex: GOOG, 2012 -> tables for Jan 2012, Feb 2012, ..., Dec 2012     
     """
     months = [month for month in range(1, 13)]
     monthly_tables = []
     for month in months:
       # Assume we go from the 1st of the month, and then 30 days later.
       start_date = DateUtils.getDateStr(year, month, 1)
       end_date = DateUtils.getEndDate(start_date, 30)
       tbl = YahooStockTable.make_data_table(ticker_symbol, start_date, end_date)
       monthly_tables.append(tbl)
     return monthly_tables  
Example #5
0
  def get_correlation(self, stocks):
    """Returns a correlation matrix defined by numpy.corrcoef.
      Basically if you pass in 3 stocks (0, 1, 2)
      Then corr[0][2] or corr[2][0] will tell the correlation of 0 and 2.
 
     Correlation is: Var(A * B) / Stdev(A) * Stdev(B)
    """
    lists_of_changes = []
    for stock in stocks:
      tbl = YahooStockTable.make_data_table(stock, self.start_date, self.end_date)
      closing = tbl.getAllClosing()
      pct_changes = StockUtils.getPercentChanges(closing)
      lists_of_changes.append(pct_changes)

    for changes in lists_of_changes:
      logging.info(self.fformat(changes))
    corr = numpy.corrcoef(lists_of_changes)
    return corr # correlation matrix
Example #6
0
 def get_rebal_values(self, stock, freq_days):
   """What are my closing values on rebalancing days.
   """
   ytable = YahooStockTable.make_data_table(stock, self.start_date, self.end_date)
   closing_values = ytable.getAllClosing()
   total_days = len(closing_values)
   print 'Total num days: %d'  % total_days 
   # Construct closing values after each end of frequency days.
   num_realloc = total_days / freq_days
   # Construct closing_values for only the days you start investing and when you reallocate
   # your money.
   realloc_day = 0
   rebal_values = []
   for ind in range(0, num_realloc + 1):
     # To ensure we stay within bounds of days.
     day = min(realloc_day, total_days-1)
     print 'Reallocating at day: ' + str(day)
     rebal_values.append(closing_values[day])
     realloc_day += freq_days
   return rebal_values
Example #7
0
 def makeYearlyTable(ticker_symbol, year):
   """Provide year data."""
   start_date = DateUtils.getDateStr(year, 1, 1)
   end_date = DateUtils.getDateStr(year, 12, 30)
   tbl = YahooStockTable.make_data_table(ticker_symbol, start_date, end_date)    
   return tbl