Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cleanup #64

Merged
merged 2 commits into from
Dec 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 0 additions & 247 deletions tests/test_nse.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,237 +153,11 @@ def test_stock_df(self):
assert df['DATE'].iloc[0] == np.datetime64("2002-01-15")
assert df['DATE'].iloc[-1] == np.datetime64("2001-01-15")
assert df['OPEN'].iloc[0] == 220

"""
def test__stock_futures(self):
from_date = date(2020, 7, 1)
to_date = date(2020, 7, 30)
expiry_date = to_date
j = h._stock_futures("SBIN", from_date, to_date, expiry_date)
assert j[0]["FH_TIMESTAMP"] == "30-Jul-2020"
assert j[-1]["FH_TIMESTAMP"] == "01-Jul-2020"
for k, v in j[0].items():
print("{}\t{}".format(k, v))
print(len(j[0]))
assert False

def test_stock_futures_raw(self):
from_date = date(2020, 6, 1)
to_date = date(2020, 7, 30)
expiry_date = to_date
j = h.stock_futures_raw("SBIN", from_date, to_date, expiry_date)
assert j[0]["FH_TIMESTAMP"] == "30-Jul-2020"
assert j[-1]["FH_TIMESTAMP"] == "01-Jun-2020"

app_name = nse.APP_NAME + '-stock-fut'
files = os.listdir(user_cache_dir(app_name, app_name))
assert len(files) == 2
"""

class TestDerivatives(TestCase):
def setUp(self):
setup_test(self)

# def test__stock_futures(self):
# """ Test stock futures at _derivative level ie without _pool"""
# from_date = date(2020, 6, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "FUTSTK"
# j = h._derivatives("SBIN", from_date, to_date, expiry_date, instrument_type=instrument)
# assert j[0]['FH_TIMESTAMP'] == to_date.strftime("%d-%b-%Y")
# assert j[-1]['FH_TIMESTAMP'] == from_date.strftime("%d-%b-%Y")
# assert j[0]['FH_INSTRUMENT'] == instrument
# assert j[0]['FH_LAST_TRADED_PRICE'] == '185.95'

# def test__stock_options(self):
# from_date = date(2020, 7, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "OPTSTK"
# strike_price = 190
# j = h._derivatives("SBIN", from_date, to_date, expiry_date, instrument_type=instrument,
# strike_price=strike_price, option_type="CE")
# """ Warning - NSE's website not giving data for last two days, this function cannot be tested right now """
# # assert j[0]['FH_TIMESTAMP'] == to_date.strftime("%d-%b-%Y")
# assert j[-1]['FH_TIMESTAMP'] == from_date.strftime("%d-%b-%Y")
# assert j[0]['FH_INSTRUMENT'] == instrument
# assert j[0]['FH_LAST_TRADED_PRICE'] == '2.60'
# assert j[0]['FH_OPTION_TYPE'] == "CE"
# warnings.warn("Test is work in progress because NSE's new website does not provide correct Derivatives data")

# def test_index_futures(self):
# """ Test stock futures at _derivative level ie without _pool"""
# from_date = date(2020, 7, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "FUTIDX"
# j = h._derivatives("NIFTY", from_date, to_date, expiry_date, instrument_type=instrument)
# assert j[0]['FH_TIMESTAMP'] == to_date.strftime("%d-%b-%Y")
# assert j[-1]['FH_TIMESTAMP'] == from_date.strftime("%d-%b-%Y")
# assert j[0]['FH_INSTRUMENT'] == instrument
# assert j[0]['FH_LAST_TRADED_PRICE'] == '11101.35'


# def test__index_options(self):
# from_date = date(2020, 7, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "OPTIDX"
# strike_price = 10500
# j = h._derivatives("NIFTY", from_date, to_date, expiry_date, instrument_type=instrument,
# strike_price=strike_price, option_type="CE")
# assert j[0]['FH_TIMESTAMP'] == to_date.strftime("%d-%b-%Y")
# assert j[-1]['FH_TIMESTAMP'] == from_date.strftime("%d-%b-%Y")
# assert j[0]['FH_INSTRUMENT'] == instrument
# assert j[0]['FH_LAST_TRADED_PRICE'] == '603.35'
# assert j[0]['FH_OPTION_TYPE'] == "CE"

# def test_errors(self):
# from_date = date(2020, 7, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "OPTIDX"
# strike_price = 10500
# with pytest.raises(Exception):
# h._derivatives("NIFTY", from_date, to_date, expiry_date, instrument_type=instrument)
# h._derivatives("NIFTY", from_date, to_date, expiry_date, instrument_type=instrument, strike_price=33)
# h._derivatives("NIFTY", from_date, to_date, expiry_date, instrument_type=instrument, option_type="CE")

# def test_derivative_raw(self):
# from_date = date(2020, 6, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "OPTIDX"
# strike_price = 10500
# j = h.derivatives_raw("NIFTY", from_date, to_date, expiry_date, instrument_type=instrument,
# strike_price=strike_price, option_type="CE")
# assert j[0]['FH_TIMESTAMP'] == to_date.strftime("%d-%b-%Y")
# assert j[-1]['FH_TIMESTAMP'] == from_date.strftime("%d-%b-%Y")
# assert j[0]['FH_INSTRUMENT'] == instrument
# assert j[0]['FH_LAST_TRADED_PRICE'] == '603.35'
# assert j[0]['FH_OPTION_TYPE'] == "CE"
# app_name = nse.APP_NAME + '-derivatives'
# files = os.listdir(user_cache_dir(app_name, app_name))
# assert len(files) == 2
# assert '2020-07-30-2020-07-01-OPTIDX-CE-10500-NIFTY-2020-07-30' in files
# assert '2020-07-30-2020-06-01-OPTIDX-CE-10500-NIFTY-2020-06-30' in files

# def test_futures_csv(self):
# symbol = "NIFTY"
# from_date = date(2020, 6, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "FUTIDX"
# j = nse.derivatives_csv(symbol , from_date, to_date, expiry_date, instrument_type=instrument, output="/tmp/x.csv")
# with open(j) as fp:
# r = list(csv.reader(fp))
# assert r[0][0] == "DATE"
# assert r[1][0] == to_date.strftime("%d-%b-%Y")
# assert r[-1][0] == from_date.strftime("%d-%b-%Y")
# assert r[-1][2] == "9626.85"
# assert r[1][2] == "11253.65"

# symbol = "SBIN"
# instrument = "FUTSTK"
# j = nse.derivatives_csv(symbol , from_date, to_date, expiry_date, instrument_type=instrument, output="/tmp/x.csv")
# with open(j) as fp:
# r = list(csv.reader(fp))
# assert r[0][0] == "DATE"
# assert r[1][0] == to_date.strftime("%d-%b-%Y")
# assert r[-1][0] == from_date.strftime("%d-%b-%Y")
# assert r[-1][2] == "162.65"
# assert r[1][2] == "192.85"

# def test_options_csv(self):
# symbol = "NIFTY"
# from_date = date(2020, 6, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "OPTIDX"
# strike_price = 10000
# option_type = "CE"
# j = nse.derivatives_csv(symbol , from_date, to_date, expiry_date, instrument_type=instrument,
# option_type=option_type, strike_price=strike_price, output="/tmp/x.csv")
# with open(j) as fp:
# r = list(csv.reader(fp))
# assert r[0][0] == "DATE"
# #assert r[1][0] == to_date.strftime("%d-%b-%Y")
# assert r[-1][0] == from_date.strftime("%d-%b-%Y")
# assert r[-1][1] == expiry_date.strftime("%d-%b-%Y")
# assert r[-1][2] == "CE"
# assert r[-1][3] == "10000.00"
# assert r[-1][4] == "219.90"
# #assert r[1][4] == "469.05"

# symbol = "SBIN"
# instrument = "OPTSTK"
# strike_price = 190
# option_type = "CE"
# j = nse.derivatives_csv(symbol , from_date, to_date, expiry_date, instrument_type=instrument,
# option_type=option_type, strike_price=strike_price, output="/tmp/x.csv")
# with open(j) as fp:
# r = list(csv.reader(fp))
# assert r[0][0] == "DATE"
# #assert r[1][0] == to_date.strftime("%d-%b-%Y")
# assert r[-1][0] == from_date.strftime("%d-%b-%Y")
# assert r[-1][1] == expiry_date.strftime("%d-%b-%Y")
# assert r[-1][2] == "CE"
# assert r[-1][3] == "190.00"
# assert r[-1][4] == "6.05"
# #assert r[1][4] == "469.05"
# warnings.warn("Test is work in progress because NSE's new website does not provide correct Derivatives data")

# def test_futures_df(self):
# symbol = "NIFTY"
# from_date = date(2020, 6, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "FUTIDX"
# j = nse.derivatives_df(symbol , from_date, to_date, expiry_date, instrument_type=instrument)
# assert j.columns[0] == "DATE"
# assert j["DATE"].iloc[0] == to_date
# assert j["DATE"].iloc[-1] == from_date
# assert j["OPEN"].iloc[-1] == 9626.85
# assert j["OPEN"].iloc[0] == 11253.65
# symbol = "SBIN"
# instrument = "FUTSTK"
# j = nse.derivatives_df(symbol , from_date, to_date, expiry_date, instrument_type=instrument)
# assert j.columns[0] == "DATE"
# assert j["DATE"].iloc[0] == to_date
# assert j["DATE"].iloc[-1] == from_date
# assert j["OPEN"].iloc[-1] == 162.65
# assert j["OPEN"].iloc[0] == 192.85

# def test_options_df(self):
# symbol = "NIFTY"
# from_date = date(2020, 6, 1)
# to_date = date(2020, 7, 30)
# expiry_date = to_date
# instrument = "OPTIDX"
# strike_price = 10000.0
# option_type = "CE"
# j = nse.derivatives_df(symbol , from_date, to_date, expiry_date, instrument_type=instrument,
# strike_price=strike_price, option_type=option_type)
# assert j.columns[0] == "DATE"
# # assert j["DATE"].iloc[0] == to_date
# assert j["DATE"].iloc[-1] == from_date
# assert j["OPEN"].iloc[-1] == 219.90
# #assert j["OPEN"].iloc[0] == 1250.8

# symbol = "SBIN"
# instrument = "OPTSTK"
# strike_price = 190.0
# option_type = "PE"
# j = nse.derivatives_df(symbol , from_date, to_date, expiry_date, instrument_type=instrument,
# strike_price=strike_price, option_type=option_type)
# print(j.iloc[0])
# assert j.columns[0] == "DATE"
# assert j["DATE"].iloc[0] == to_date
# assert j["DATE"].iloc[-1] == from_date
# assert j["LTP"].iloc[-1] == 32.95
# assert j["OPEN"].iloc[0] == 0.75


class TestIndexHistory(TestCase):
def setUp(self):
Expand All @@ -397,18 +171,6 @@ def test__post(self):
r = h._post_json("mypath", params=params)
assert json.loads(r.json()['data']) == params

# def test_index(self):
# h = nse.NSEIndexHistory()
# symbol = "NIFTY 50"
# from_date = date(2020, 6, 1)
# to_date = date(2020, 7, 30)
# d = h._index(symbol, from_date, to_date)
# assert d[0]['Index Name'] == 'Nifty 50'
# assert d[0]['HistoricalDate'] == '30 Jul 2020'
# assert d[-1]['HistoricalDate'] == '01 Jun 2020'
# app_name = nse.APP_NAME + '-index'
# files = os.listdir(user_cache_dir(app_name, app_name))
# assert len(files) == 1

def test_index_raw(self):
symbol = "NIFTY 50"
Expand All @@ -432,15 +194,6 @@ def test_index_csv(self):
rows = [x.split(',') for x in text.split('\n')]
assert rows[1][2] == raw[0]['OPEN']

# def test_index_df(self):
# from_date = date(2001,3,15)
# to_date = date(2001,6,15)
# raw = nse.index_raw("NIFTY 50", from_date, to_date)
# df = nse.index_df("NIFTY 50", from_date, to_date)
# print(df.head())
# assert float(raw[0]['OPEN']) == df.iloc[0]['OPEN']
# assert float(raw[0]['CLOSE']) == df.iloc[0]['CLOSE']
# assert df.iloc[0]['HistoricalDate'] == date(2001,6,15)

def test_expiry_dates():
dt = date(2020,1,1)
Expand Down