def scrape(self,symbol,start_date,end_date): if ohlc.query.filter(ohlc.symbol==symbol).count() == 0: link = self.first_part + symbol + self.full +self.last_part else: link = self.first_part + symbol + self.last_part r = requests.get(link) av_stock_json = r.json() s=stock.query.filter_by(symbol=symbol).first() try: # print datetime.datetime(int(i[0:4]),int(i[5:7]),int(i[8:10])) for i in av_stock_json['Time Series (Daily)']: o = ohlc(oCode=symbol+str(i) , symbol = symbol , Date=datetime.datetime(int(i[0:4]),int(i[5:7]),int(i[8:10])) , Open = av_stock_json['Time Series (Daily)'][i]['1. open'] , High = av_stock_json['Time Series (Daily)'][i]['2. high'] , Low = av_stock_json['Time Series (Daily)'][i]['3. low'] , Close = av_stock_json['Time Series (Daily)'][i]['4. close'] , Volume =av_stock_json['Time Series (Daily)'][i]['6. volume'] , AdjClose = av_stock_json['Time Series (Daily)'][i]['5. adjusted close'] , stock_symbol = s) db.session.add(o) db.session.commit() except Exception,e: db.session.rollback() print str(e) s.exists_av = False db.session.add(s) db.session.commit() return False
def scrape_date_range(self, symbol, start_date, end_date): link = self.first_part + symbol + self.mid_part + start_date + self.mid_part2 + end_date + self.last_part r = requests.get(link) quandl_stock_json = r.json() s = stock.query.filter_by(symbol=symbol).first() try: data = quandl_stock_json['dataset_data']['data'] for i in range(0, len(data)): o = ohlc(oCode=symbol + str(data[i][0]), symbol=symbol, Date=datetime.datetime(int(data[i][0][0:4]), int(data[i][0][5:7]), int(data[i][0][8:10])), Open=data[i][1], High=data[i][2], Low=data[i][3], Close=data[i][4], Volume=data[i][5], AdjClose=data[i][11], stock_symbol=s) db.session.add(o) try: db.session.commit() s.exists_quandl = True except Exception, e: db.session.rollback() print str(e) pass except Exception, e: print str(e) s.exists_quandl = False print("stock doesn't exist with quandl") return False
) av_stock_json = r.json() symbol = av_stock_json['Meta Data']['2. Symbol'] d = datetime.datetime(int(2017), int(11), int(01)) s = stock(symbol=symbol, maxDate=d, minDate=d) db.session.add(s) db.session.commit() for i in av_stock_json['Time Series (Daily)']: print i o = ohlc( oCode=symbol + str(i), symbol=symbol, Date=datetime.datetime(int(i[0:4]), int(i[5:7]), int(i[8:10])), Open=av_stock_json['Time Series (Daily)'][i]['1. open'], High=av_stock_json['Time Series (Daily)'][i]['2. high'], Low=av_stock_json['Time Series (Daily)'][i]['3. low'], Close=av_stock_json['Time Series (Daily)'][i]['4. close'], Volume=av_stock_json['Time Series (Daily)'][i]['6. volume'], AdjClose=av_stock_json['Time Series (Daily)'][i]['5. adjusted close'], stock_symbol=s) db.session.add(o) db.session.commit() print av_stock_json['Time Series (Daily)'][i]['1. open'] print av_stock_json['Time Series (Daily)'][i]['2. high'] print av_stock_json['Time Series (Daily)'][i]['3. low'] print av_stock_json['Time Series (Daily)'][i]['4. close'] print av_stock_json['Time Series (Daily)'][i]['5. adjusted close'] print av_stock_json['Time Series (Daily)'][i]['6. volume']