Exemple #1
0
def test_break_dates():
    from_date = date(2000, 12, 14)
    to_date = date(2005, 1, 20)
    dates = ut.break_dates(from_date, to_date)
    assert from_date == dates[0][0]
    assert to_date == dates[-1][1]
    assert len(dates) == 50

    from_date = date(2019, 1, 1)
    to_date = date(2020, 1, 31)
    dates = ut.break_dates(from_date, to_date)
    assert from_date == dates[0][0]
    assert to_date == dates[-1][1]
    assert len(dates) == 13
Exemple #2
0
def derivatives_csv(symbol, from_date, to_date, expiry_date, instrument_type, strike_price=None, option_type=None, output="", show_progress=False):
    if show_progress:
        h = NSEHistory()
        h.show_progress = show_progress
        date_ranges = ut.break_dates(from_date, to_date)
        params = [(symbol, x[0], x[1], expiry_date, instrument_type, strike_price, option_type) for x in reversed(date_ranges)]
        with click.progressbar(params, label=symbol) as ps:
            chunks = []
            for p in ps:
                r = h.derivatives_raw(*p)
                chunks.append(r)
            raw = list(itertools.chain.from_iterable(chunks))
    else:
        raw = derivatives_raw(symbol, from_date, to_date, expiry_date, instrument_type, strike_price, option_type)
    if not output:
        output = "{}-{}-{}-{}.csv".format(symbol, from_date, to_date, series)
    if "FUT" in instrument_type:
        final_headers = futures_final_headers
        select_headers = futures_select_headers
    if "OPT" in instrument_type:
        final_headers = options_final_headers
        select_headers = options_select_headers
    if raw:
        with open(output, 'w') as fp:
            fp.write(",".join(final_headers) + '\n')
            for row in raw:
                row_select = [str(row[x]) for x in select_headers]
                line = ",".join(row_select) + '\n'
                fp.write(line) 
    return output
Exemple #3
0
def stock_csv(symbol, from_date, to_date, series="EQ", output="", show_progress=True):
    if show_progress:
        h = NSEHistory()
        h.show_progress = show_progress
        date_ranges = ut.break_dates(from_date, to_date)
        params = [(symbol, x[0], x[1], series) for x in reversed(date_ranges)]
        with click.progressbar(params, label=symbol) as ps:
            chunks = []
            for p in ps:
                r = h.stock_raw(*p)
                chunks.append(r)
            raw = list(itertools.chain.from_iterable(chunks))
    else:
        raw = stock_raw(symbol, from_date, to_date, series)

    if not output:
        output = "{}-{}-{}-{}.csv".format(symbol, from_date, to_date, series)
    if raw:
        with open(output, 'w') as fp:
            fp.write(",".join(stock_final_headers) + '\n')
            for row in raw:
                row_select = [str(row[x]) for x in stock_select_headers]
                line = ",".join(row_select) + '\n'
                fp.write(line) 
    return output
Exemple #4
0
def index_csv(symbol, from_date, to_date, output="", show_progress=False):
    if show_progress:
        h = NSEIndexHistory()
        date_ranges = ut.break_dates(from_date, to_date)
        params = [(symbol, x[0], x[1]) for x in reversed(date_ranges)]
        with click.progressbar(params, label=symbol) as ps:
            chunks = []
            for p in ps:
                r = h._index(*p)
                chunks.append(r)
            raw = list(itertools.chain.from_iterable(chunks))
    else:
        raw = index_raw(symbol, from_date, to_date)

    if not output:
        output = "{}-{}-{}.csv".format(symbol, from_date, to_date)

    if raw:
        with open(output, 'w') as fp:
            fieldnames = [
                "INDEX_NAME", "HistoricalDate", "OPEN", "HIGH", "LOW", "CLOSE"
            ]
            writer = csv.DictWriter(fp,
                                    fieldnames=fieldnames,
                                    extrasaction='ignore')
            writer.writeheader()
            writer.writerows(raw)
    return output
Exemple #5
0
 def derivatives_raw(self, symbol, from_date, to_date, expiry_date,
                     instrument_type, strike_price, option_type):
     date_ranges = ut.break_dates(from_date, to_date)
     params = [(symbol, x[0], x[1], expiry_date, instrument_type,
                strike_price, option_type) for x in reversed(date_ranges)]
     chunks = ut.pool(self._derivatives, params, max_workers=self.workers)
     return list(itertools.chain.from_iterable(chunks))
Exemple #6
0
 def stock_raw(self, symbol, from_date, to_date, series="EQ"):
     date_ranges = ut.break_dates(from_date, to_date)
     params = [(symbol, x[0], x[1], series) for x in reversed(date_ranges)]
     chunks = ut.pool(self._stock, params)
         
     return list(itertools.chain.from_iterable(chunks))
Exemple #7
0
 def index_raw(self, symbol, from_date, to_date):
     date_ranges = ut.break_dates(from_date, to_date)
     params = [(symbol, x[0], x[1]) for x in reversed(date_ranges)]
     chunks = ut.pool(self._index, params)
     return list(itertools.chain.from_iterable(chunks))