def _update_bhavcopy(strdate, stocks_dict, fname=None): """update bhavcopy Database date in DD-MM-YYYY format.""" fname = fname or _DEF_SQLIITE_FNAME with sqlite3.connect(fname) as con: cur = con.cursor() fdate = utils.get_ts_for_datestr(strdate, _date_fmt) # just to be safe global _del_for_date_stmt delete_stmt_final = _del_for_date_stmt % {'date': fdate} cur.execute(delete_stmt_final) for key, val in stocks_dict.iteritems(): insert_stmt_final = _nsedata_insert_stmt % { 'sym': key, 'date': fdate, 'o': val.open, 'h': val.high, 'l': val.low, 'c': val.close, 'v': val.volume, 'd': val.deliv } print "Executing:", insert_stmt_final cur.execute(insert_stmt_final) con.commit()
def _update_bhavcopy(strdate, stocks_dict, fname=None): """update bhavcopy Database date in DD-MM-YYYY format.""" fname = fname or _DEF_SQLIITE_FNAME with sqlite3.connect(fname) as con: cur = con.cursor() fdate = utils.get_ts_for_datestr(strdate, _date_fmt) # just to be safe global _del_for_date_stmt delete_stmt_final = _del_for_date_stmt % { 'date': fdate} cur.execute(delete_stmt_final) for key, val in stocks_dict.iteritems(): insert_stmt_final = _nsedata_insert_stmt % { 'sym' : key, 'date': fdate, 'o' : val.open, 'h': val.high, 'l' : val.low, 'c': val.close, 'v' : val.volume, 'd': val.deliv } print "Executing:", insert_stmt_final cur.execute(insert_stmt_final) con.commit()
bse_isins = bse_stocks_dict.keys() common_isins = set(nse_isins) & set(bse_isins) only_bse_isins = set(bse_isins) - common_isins only_nse_isins = set(nse_isins) - common_isins print common_isins & only_bse_isins print common_isins & only_nse_isins con = sqlite3.connect(_DEF_SQLITE_FNAME) cur = con.cursor() for isin in common_isins: nstock = nse_stocks_dict[isin] bstock = bse_stocks_dict[isin] ndate = get_ts_for_datestr(nstock.listing_date,'%d-%b-%Y') insert_stmt_final = _all_scrips_common_ins_stmt % \ { 'isin': nstock.isin, 'name': nstock.name, 'npresent' : 1, 'nsymbol': nstock.symbol, 'ndate': ndate, 'bpresent' : 1, 'bsymbol': bstock.symbol, 'bid': bstock.bseid, 'bgroup': bstock.group, 'bdate': ndate } print insert_stmt_final cur.execute(insert_stmt_final) for isin in only_bse_isins: bstock = bse_stocks_dict[isin] insert_stmt_final = _all_scrips_bseonly_ins_stmt % \ { 'isin': bstock.isin, 'name': bstock.name,
def get_bhavcopy(date='01-01-2002'): """Downloads a bhavcopy for a given date and returns a dictionary of rows where each row stands for a traded scrip. The scripname is key. If the bhavcopy for a given day is already downloaded, returns None. date is in DD-MM-YYYY format""" global _date_fmt if isinstance(date, str): d2 = dt.strptime(date, _date_fmt) strdate = date elif isinstance(date, dt): d2 = date strdate = dt.strftime(date, _date_fmt) else: return None yr = d2.strftime('%Y') mon = d2.strftime('%b').upper() mm = d2.strftime('%0m') dd = d2.strftime('%0d') print yr, mm, dd fdate = utils.get_ts_for_datestr(strdate, _date_fmt) if _bhavcopy_downloaded(fdate): ## already downloaded return None global _bhav_url_base global _deliv_url_base bhav_url = _bhav_url_base % ({'year':yr, 'mon':mon, 'dd':dd}) deliv_url = _deliv_url_base % ({'year':yr, 'mm':mm, 'dd':dd}) x = requests.get(bhav_url) y = requests.get(deliv_url) stocks_dict = {} _update_dload_success(fdate, x.ok, y.ok) if x.ok and y.ok: z = ZipFile(bio(x.content)) for name in z.namelist(): csv_name = name delivery = bio(y.text) with z.open(csv_name) as bhav: i = 0 for line in bhav: if i == 0: i += 1 continue l = line.split(',') sym, o, h, l, c, v, d = l[0], l[2], l[3], l[4], \ l[5], l[8], l[8] stocks_dict[sym] = [float(o), float(h), float(l), float(c), int(v), int(d)] i = 0 for line in delivery: if not line.startswith('20'): i += 1 continue l = line.split(',') if (len(l)) == 4: sym, d = l[1].strip(), l[3].strip() elif len(l) == 7: sym, d = l[2].strip(), l[5].strip() stocks_dict[sym][-1] = int(d) i += 1 for sym in stocks_dict.keys(): stocks_dict[sym] = ScripOHLCVD(*stocks_dict[sym]) return stocks_dict
def get_bhavcopy(date='01-01-2002'): """Downloads a bhavcopy for a given date and returns a dictionary of rows where each row stands for a traded scrip. The scripname is key. If the bhavcopy for a given day is already downloaded, returns None. date is in DD-MM-YYYY format""" global _date_fmt if isinstance(date, str): d2 = dt.strptime(date, _date_fmt) strdate = date elif isinstance(date, dt): d2 = date strdate = dt.strftime(date, _date_fmt) else: return None yr = d2.strftime('%Y') mon = d2.strftime('%b').upper() mm = d2.strftime('%0m') dd = d2.strftime('%0d') print yr, mm, dd fdate = utils.get_ts_for_datestr(strdate, _date_fmt) if _bhavcopy_downloaded(fdate): ## already downloaded return None global _bhav_url_base global _deliv_url_base bhav_url = _bhav_url_base % ({'year': yr, 'mon': mon, 'dd': dd}) deliv_url = _deliv_url_base % ({'year': yr, 'mm': mm, 'dd': dd}) x = requests.get(bhav_url) y = requests.get(deliv_url) stocks_dict = {} _update_dload_success(fdate, x.ok, y.ok) if x.ok and y.ok: z = ZipFile(bio(x.content)) for name in z.namelist(): csv_name = name delivery = bio(y.text) with z.open(csv_name) as bhav: i = 0 for line in bhav: if i == 0: i += 1 continue l = line.split(',') sym, o, h, l, c, v, d = l[0], l[2], l[3], l[4], \ l[5], l[8], l[8] stocks_dict[sym] = [ float(o), float(h), float(l), float(c), int(v), int(d) ] i = 0 for line in delivery: if not line.startswith('20'): i += 1 continue l = line.split(',') if (len(l)) == 4: sym, d = l[1].strip(), l[3].strip() elif len(l) == 7: sym, d = l[2].strip(), l[5].strip() stocks_dict[sym][-1] = int(d) i += 1 for sym in stocks_dict.keys(): stocks_dict[sym] = ScripOHLCVD(*stocks_dict[sym]) return stocks_dict
bse_isins = bse_stocks_dict.keys() common_isins = set(nse_isins) & set(bse_isins) only_bse_isins = set(bse_isins) - common_isins only_nse_isins = set(nse_isins) - common_isins print common_isins & only_bse_isins print common_isins & only_nse_isins con = sqlite3.connect(_DEF_SQLITE_FNAME) cur = con.cursor() for isin in common_isins: nstock = nse_stocks_dict[isin] bstock = bse_stocks_dict[isin] ndate = get_ts_for_datestr(nstock.listing_date, '%d-%b-%Y') insert_stmt_final = _all_scrips_common_ins_stmt % \ { 'isin': nstock.isin, 'name': nstock.name, 'npresent' : 1, 'nsymbol': nstock.symbol, 'ndate': ndate, 'bpresent' : 1, 'bsymbol': bstock.symbol, 'bid': bstock.bseid, 'bgroup': bstock.group, 'bdate': ndate } print insert_stmt_final cur.execute(insert_stmt_final) for isin in only_bse_isins: bstock = bse_stocks_dict[isin] insert_stmt_final = _all_scrips_bseonly_ins_stmt % \ { 'isin': bstock.isin, 'name': bstock.name,