def getTimewindowStockPrice(ticker): timewindowStockPrice = {}#key: year; value: weekly price stockDF = DataReader(ticker, "google", "2009-01-01", datetime.today().date()) # print stockDF for idx, row in stockDF.iterrows(): # print row[0], row['Close'] # print datetime.fromtimestamp(idx) # print str(idx) dt = dateutil.parser.parse(str(idx)).date() year = dt.isocalendar()[0] week = dt.isocalendar()[1] price = row['Close'] print year, week, price if year not in timewindowStockPrice: timewindowStockPrice[year] = {} if week not in timewindowStockPrice[year]: timewindowStockPrice[year][week] = [] # print row['Close'] timewindowStockPrice[year][week].append(price) #normalized weekly price for year in timewindowStockPrice.keys(): for week in timewindowStockPrice[year].keys(): timewindowStockPrice[year][week] = scipy.mean(timewindowStockPrice[year][week]) # for year in timewindowStockPrice.keys(): # print timewindowStockPrice[year] return timewindowStockPrice
def refreshYahooDatabase(db): rs = yahoo.yahooRecordset(db) ts = timeseries.timeseriesRecordset(db) # step 1: get the yahoo index end date idx = rs.select('*') for i in idx: try: r = ts.getLastDate(i['id']) start = r[0]['max_date'] data = pd.DataFrame() if start != None: start = start + dt.timedelta(days=1) data = DataReader( str(i['key']), 'yahoo', start) else: data = DataReader( str(i['key']), 'yahoo') # insert new data add = [] for index, row in data.iterrows(): add.append((i['id'], index, row[i['field']])) ts.insert(add) except Exception as e: print('error processing index ' + str(i['key'] \ + '/' + str(i['field']) + '\n'))
class Indicator1(): def __init__(self, indicator, stock_name, start_date, end_date, buy_value, sell_value): if not isinstance(stock_name, str): raise TypeError("Sorry. 'Stock Name' must be string") self.stock = DataReader(stock_name, "yahoo", start_date, end_date) self.stock['returnValue'] = 'NaN' self.stock['returnValue'] = 'NaN' high_array = [] for index, row in self.stock.iterrows(): high_array.append(row['High']) high_nparray = numpy.asarray(high_array) low_array = [] for index, row in self.stock.iterrows(): low_array.append(row['Low']) low_nparray = numpy.asarray(low_array) close_array = [] for index, row in self.stock.iterrows(): close_array.append(row['Close']) close_nparray = numpy.asarray(close_array) real = getattr(talib, indicator)(high_nparray, low_nparray, close_nparray, timeperiod=14) self.stock['indicator'] = real self.stock['indicator_trigger'] = "NaN" count = 0 flag = False for index, row in self.stock.iterrows(): if count < len(self.stock.index): if float(row['indicator']) < buy_value and (not flag): self.stock['indicator_trigger'][count] = "Buy" flag = True elif float(row['indicator']) > sell_value and flag: self.stock['indicator_trigger'][count] = "Sell" flag = False count += 1