Exemplo n.º 1
0
def writeCodesToCSV():
    nse = Nse()
    Index_Name = nse.get_index_list() # this return only name
    all_stock_codes = list(nse.get_stock_codes(cached=True).keys())
    all_stock_name = list(nse.get_stock_codes(cached=True).values())

    codes = [('SYMBOL', all_stock_codes),
             ('NAME', all_stock_name)]

    symbolDF = pd.DataFrame.from_items(codes)
    symbolDF['INDFLAG'] = 0
    symbolDF['FO_FLAG'] = 0

    symbolDF.to_csv(p.symbollist)
Exemplo n.º 2
0
class NSEDataFetch(Nse):
    def __init__(self):
        self.nse = Nse()
        self.equity_stock_watch_all = "https://www.nse-india.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json"

    def get_active_monthly(self):
        pprint(self.nse.get_active_monthly())

    def get_quote(self, symbol):
        pprint(self.nse.get_quote(symbol))

    def download_bhavcopy(self, date_string):
        arr = []
        response = self.nse.download_bhavcopy(date_string)
        s = io.StringIO(response.decode('utf-8'))
        headers = s.readlines(1)[0].split(',')
        filename = "BavCopy{}.xlsx".format(datetime.now())
        for line in s:
            fields = map(lambda s: s.strip(), line.split(','))
            columns = list(fields)
            #bad coding here, need to remove the hardcoded values,
            #need to automate this hardcoded value
            arr.append({
                headers[0]: columns[0],
                headers[1]: columns[1],
                headers[2]: columns[2],
                headers[3]: columns[3],
                headers[4]: columns[4],
                headers[5]: columns[5],
                headers[6]: columns[6],
                headers[7]: columns[7],
                headers[8]: columns[8],
                headers[9]: columns[9],
                headers[10]: columns[10],
                headers[11]: columns[11],
                headers[12]: columns[12],
                headers[13]: columns[13]
            })
        pd.read_json(json.dumps(arr)).to_excel(filename)

    def get_equity_stock_watch_live(self):
        response = self.nse._get_json_response_from_url(
            self.equity_stock_watch_all, True)
        filename = "EquityMatketWatch{}.xls".format(datetime.now())
        df = pd.read_json(response)
        df.to_excel(filename)

    def get_stock_codes(self):
        self.nse.get_stock_codes()
Exemplo n.º 3
0
    def download_stock_data_method(**kwargs):
        nse = Nse()

        stock_names_dict = nse.get_stock_codes()
        stock_names_list = list(stock_names_dict.keys())[1:4]

        stocks = []

        stocks = [
            download_price_history_for_stock(symbol=stock)
            for stock in stock_names_list
        ]

        failed_stocks = [i for i in stocks if i]

        logging.info("Failed Stocks : ")
        logging.info(failed_stocks)

        failed_again = []

        if len(failed_stocks) > 0:
            failed_again = [
                download_price_history_for_stock(symbol=stock)
                for stock in failed_stocks
            ]

        if len(failed_again) > 0:
            logging.info("Failed STOCKS after multiple tries : ")
            logging.info(failed_again)
class NSE:
    def __init__(self):
        self.NSE = Nse()

    def getNSECompanyNames(self):
        NSECompanyCodes = self.NSE.get_stock_codes(cached=False)
        NSECompanyList = list(NSECompanyCodes.keys())[1:]
        return NSECompanyList
Exemplo n.º 5
0
def stonkanalysis(start_date_year=2020,
                  start_date_month=1,
                  start_date_day=1,
                  no_of_days=10,
                  no_check_companies=1):
    '''
    '''
    # Initiate nse connection and get stock names
    nsel = Nse()
    stock_list = nsel.get_stock_codes()
    # Get the stock list
    stock_list = {v: k for k, v in stock_list.items()}
    no_of_comapies = len(stock_list)
    iterator = iter(stock_list.items())
    break_cons = 0
    company_list = []
    company_code = []
    # no of days should account for the holidays in between
    start_date = date(int(start_date_year), int(start_date_month),
                      int(start_date_day))
    end_date = start_date + timedelta(days=int(no_of_days))
    print(start_date, end_date)

    for i in iterator:
        if break_cons >= 1:
            company_list.append(i[0])
            company_code.append(i[1])
        break_cons += 1
        if break_cons == no_check_companies:
            break

    percentage_calc = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    stock_data = list(company_list)
    perc_matrix = []
    summary_dic = {}
    volume_dict = {}
    mcap_dict = {}
    for i in range(len(stock_data)):
        data_frame = nse.get_history(symbol=company_list[i],
                                     start=start_date,
                                     end=end_date)
        #print(data_frame)
        volume_array = np.array(data_frame['Volume'])
        #print(volume_array)
        data_frame.drop(['VWAP', 'Series'], axis=1, inplace=True)
        if i == 0:
            no_of_days = len(np.array(data_frame['Close']))
            perc_matrix = np.zeros(
                (int(no_of_days) - 1, len(percentage_calc), len(stock_data)))
        perc_matrix[:, :,
                    i] = calculate_perc_matrix(data_frame, int(no_of_days),
                                               percentage_calc)
        stock_dict = {company_list[i]: perc_matrix.tolist()}
        summary_dic.update(stock_dict)
        print(summary_dic)
    return summary_dic
Exemplo n.º 6
0
def index(request):
    nse = Nse()
    all_stock_codes = nse.get_stock_codes()
    stock_list = all_stock_codes.items()
    #  row = nse_m_stocklist()
    #  for stock in all_stock_codes.items():
    #      row.stock_code = stock[0]
    #     row.stock_desc = stock[1]
    #      row.save()
    #   stock_list = nse_m_stocklist.objects.order_by('-stock_code')
    return render(request, 'nse/index.html', {'table': stock_list})
Exemplo n.º 7
0
class NseInitializer:
    def __init__(self):
        self.nse = Nse()

    def get_stock_codes(self):
        all_stock_codes = self.nse.get_stock_codes()
        return all_stock_codes

    def get_quote(self,symbol):
        quote = self.nse.get_quote(symbol)
        return quote
Exemplo n.º 8
0
def getstockList():
    nse = Nse()
    all_stock_code = nse.get_stock_codes(cached=True)
    all_stock_codes = pd.DataFrame(list(all_stock_code.items()),
                                   columns=["SYMBOL", "NAME OF COMPANY"])
    all_stock_codes.drop(all_stock_codes.index[0], inplace=True)
    Symbols = all_stock_codes['SYMBOL'].tolist()
    isvalid = list(map(lambda x: nse.is_valid_code(x), Symbols))
    all_stock_codes['isactve'] = isvalid
    all_stock_codes['FUT'] = False
    all_stock_codes['IDX'] = False
    return all_stock_codes
Exemplo n.º 9
0
def additem(request, stockcode=None):
    if request.method == "POST":
        # This is form class
        form = nse_f_stocklist(request.POST)
        form.valid = True
        stockprice = 0.
        if form.is_valid():
            post = form.save(commit=False)
            post.stock_type = 'SH'
            #post.user = '******'
            #post.stamp = datetime.datetime.now()
            post.save()
            updatesummary(post, 'buy')
            # Add data to summary table
        #  try:
        #      summary = nse_m_summarysheet.objects.get(stock_code = post.stock_code)
        #  except:
        #      summary = nse_m_summarysheet()
        ##  summary = nse_m_summarysheet.objects.filter(stock_type = 'SH')
        ##  summary_data = summary.filter(stock_code = post.stock_code)
        #  if not summary.id:
        #      summary_data = nse_m_summarysheet()
        #      summary_data.stock_code = post.stock_code
        #      summary_data.totbuyqty =  float(post.buyqty)
        #      summary_data.totbuyvalue =  float(post.buyqty) * float(post.buyprice)
        #      summary_data.avgbuyprice = float(post.buyprice)
        #      summary_data.stock_type = 'SH'
        #    #  summary_data.currprice = post.currprice
        #    #  summary_data.currvalue = float(post.currprice) * float(post.buyqty)
        #      summary_data.save()
        #  else:
        #      summary_data = nse_m_summarysheet(summary.id)
        #      summary_data.stock_code = post.stock_code
        #      summary_data.totbuyqty = float(summary.totbuyqty) + float(post.buyqty)
        #      summary_data.totbuyvalue = float(summary.totbuyvalue) + (float(post.buyqty) * float(post.buyprice))
        #      summary_data.avgbuyprice = float(summary.totbuyvalue) / float(summary.totbuyqty)
        #      summary_data.stock_type = 'SH'
        # #     summary_data.currprice = post.currprice
        # #     summary_data.currvalue = float(post.currprice) * float(summary_data.totbuyqty)
        #      summary_data.save()
        return redirect('Stock_Summary')
    else:
        form = nse_f_stocklist()
        nse = Nse()
        all_stock_codes = nse.get_stock_codes()
        stock_list = all_stock_codes.items()
        return render(request, 'nse/additem1.html', {
            'form': form,
            'stock_list': stock_list
        })
Exemplo n.º 10
0
class Command(BaseCommand):
    def __init__(self):
        self.company_details_repo = CompanyDetailsRepo()
        self.company_repo = CompanyRepo()
        self.nse = Nse()
        self.stockapi = NSE()

    def run(self):
        nse_list = self.nse.get_stock_codes()

        for code, title in nse_list.iteritems():
            company = self.company_repo.filter(stockExchangeCode='NSE',
                                               code=code).first()
            if company:
                if company.weight < 1:
                    print "saving: ", code, title
                    company.weight += 1
                    company.save()
            else:
                print "creating: ", code, title
                try:
                    details = self.company_details_repo.create(**{})
                    c = self.company_repo.create(details=details,
                                                 **self._extract_company_data(
                                                     title, code))
                    c.details = details
                    c.save()
                    c = self.stockapi.latest(c)
                except:
                    print "error for " + title

    def _extract_company_data(self, name, code):
        return {
            "name": name,
            "code": code,
            #"quandlCode": data['dataset_code'],
            "description": name,
            "stockExchangeCode": 'NSE',
            "type": 'Time Series',
            "frequency": 'daily',
            "refreshedAt": helper.now(),
            #"oldestAvailableDate": helper.now(),
            "newAvailableDate": helper.now(),
            "history": [],
            "historyCount": 0,
            "weight": 1
        }
Exemplo n.º 11
0
def price(code):
	#print("got request")
	#print(code)
	nse = Nse()
	stock_code = nse.get_stock_codes()
	#stocks = ['20MICRONS', '3IINFOTECH','3MINDIA','8KMILES','A2ZINFRA','AARTIDRUGS','AARTIIND']
	dic = {}
	#for code in stocks:
	out = nse.get_quote(code)
	dic[code] = out['lastPrice']
	print(dic)
	with open("sample.json", "w") as outfile: 
	    json.dump(dic, outfile)
	with open('sample.json') as data_file:
		data = json.load(data_file)
	#print(data)
	return data
class nseClass:
    def __init__(self):
        from nsetools import Nse
        self.nse = Nse()

    def createStockCodeNameCSV(self):
        '''
            Creates a csv with 3 columns
            sl no, stock code name with stock name
        '''
        self.all_stocks = self.nse.get_stock_codes()
        import csv
        with open('nse_code_name.csv', 'w', newline='') as file:
            self.writer = csv.writer(file)
            self.writer.writerow(["SN", "Code", "Name"])
            for i, j in enumerate(self.all_stocks):
                if (i > 0):
                    self.writer.writerow([i, j, self.all_stocks[j]])
Exemplo n.º 13
0
 def do(self):
     CommonCronJob.logger.info("Going to run stock index updater cron")
     nse = Nse()
     try:
         with transaction.atomic():
             all_stock_codes = nse.get_stock_codes()
             all_stock_codes.pop('SYMBOL', None)
             models_to_create = []
             for code, name in all_stock_codes.items():
                 models_to_create.append(Stock(code=code, name=name, updated_on=date.today()))
             Stock.objects.all().delete()
             Stock.objects.bulk_create(models_to_create)
             CommonCronJob.logger.info("Completed stock updater cron")
     except URLError:
         CommonCronJob.logger.error("Unable to connect to nse")
     except DatabaseError:
         CommonCronJob.logger.error("Database error")
     except Exception as e:
         CommonCronJob.logger.error("Error happened")
Exemplo n.º 14
0
 def __init__(self, Threads):
     from nsetools import Nse
     Nse = Nse()
     import requests
     import threading
     import json
     self.threading = threading
     # create a new variable for headers
     self.headers = {
         "user-agent":
         "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/80.0.3987.87 Chrome/80.0.3987.87 Safari/537.36"
     }
     # create a new array for stocks
     self.stocksArray = Nse.get_stock_codes()
     # create a new TCP Connection
     self.json = json
     # init an variable for number of threads
     self.threads = Threads
     self.s = requests.Session()
     print("initiated")
Exemplo n.º 15
0
def dayopen():
    nse = Nse()
    print(nse)

    companies_list = nse.get_stock_codes(cached=False)
    my_dict = companies_list
    w = csv.writer(open("stocklist.csv", "w", newline=''))
    for key, val in my_dict.items():
        w.writerow([key, val])

    stock_list = csv.reader(open('stocklist.csv'))

    next(stock_list)

    #creates new data frame
    final = pd.DataFrame()

    # dates configuration
    end_day = date.today()
    start_day = end_day - timedelta(365)

    for company in stock_list:
        try:

            symbol, name = company

            df1 = nse.get_quote(symbol.format(symbol), as_json=True)
            # df10 = nse.get_history(symbol.format(symbol), start=start_day, end=end_day,as_json=True)
            # datax = pd_json.loads(df10)
            # df11 = pd.json_normalize(datax)
            # df12 = pd.DataFrame(df11)
            data = pd_json.loads(df1)  # load
            df = pd.json_normalize(data)  # normalise
            df2 = pd.DataFrame(df)
        except:
            continue
        selected = df2.iloc[0:, [1, 6, 65, 11, 20, 67]]
        final = pd.concat([final, selected])
        final = final.reset_index(drop=True)
        print(final)
        final.to_csv('dayopendata.csv', index=None, header=True)
Exemplo n.º 16
0
def addwatchlistitem(request):
    if request.method == "POST":
        # This is form class
        form = nse_f_addwatchlititem(request.POST)
        form.valid = True
        if form.is_valid():
            post = form.save(commit=False)
            post.stock_type = 'WL'
            #post.user = '******'
            #post.stamp = datetime.datetime.now()
            post.save()
        return redirect('Watch_List')
    else:
        form = nse_f_addwatchlititem()
        nse = Nse()
        all_stock_codes = nse.get_stock_codes()
        stock_list = all_stock_codes.items()
        return render(request, 'nse/addwatchlistitem.html', {
            'form': form,
            'stock_list': stock_list
        })
Exemplo n.º 17
0
class get_all_stock_codes:

    """
        This class will retrive all Stock codes from NSE website
    """

    def __init__(self):
        self.all_stock_codes = []
        self.nse = Nse()
        fileConfig('../properties/logging_config.ini')
        self.log = logging.getLogger()
        self.log.debug('Logger intiated ')

    def nsetools_get_codes(self, out_stock_file):
        """ This method retrivies all stock codes and write to CSV files
            Method uses NSETools to retrieve data from NSE website
        """
        self.log.info("Retriving stock Codes from NSE site .. ")
        self.all_stock_codes = self.nse.get_stock_codes()
        df2 = pd.DataFrame.from_dict(self.all_stock_codes, orient='index')
        self.log.info("Total Stock Codes received :" + str(df2.shape[0]))
        df2.to_csv(out_stock_file)
Exemplo n.º 18
0
class ManagerLib(object):

    _instance = None

    def __new__(self):
        if not self._instance:
            self._instance = super(ManagerLib, self).__new__(self)
            self.nse = Nse()
            self.userList = []
        return self._instance

    #def __init__(self):
    #self.nse = Nse()
    #self.userList = []

    def GetLiveData(self, key, id, stock):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = self.nse.get_quote(str(stock))
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def CheckStockCode(self, key, id, stock):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = self.nse.is_valid_code(str(stock))
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def GetListOfStocks(self, key, id):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = self.nse.get_stock_codes()
        return json.dumps(ret,
                          ensure_ascii=False,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def AddUser(self, key, firebase_url, firebase_pass):
        firebaseauth = {
            'FIREBASE_URL': firebase_url,
            'FIREBASE_PWD': firebase_pass
        }
        userdetailobj = UserDetails(key, firebaseauth)
        self.userList.append(userdetailobj)
        ret = {
            'message': 'User added successfully.',
            'id': userdetailobj.userid
        }
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def GetUsers(self):
        return json.dumps(self.userList,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def GetUser(self, key, id):
        userobj = list(x for x in self.userList if x.userid == id)
        if userobj:
            if userobj[0].userkey == key:
                return userobj[0]
            else:
                return {'error': 'Id-key mismatch'}
        else:
            return {'error': 'Id not found'}

    def GetUserDetails(self, key, id):
        ret = self.GetUser(key, id)
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def AddConditionFieldList(self, key, id, indice, attrnames, attrtypes,
                              attrvalues, conjunctions, operations):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = ret.SetConditionList(indice, attrnames, attrtypes,
                                       attrvalues, conjunctions, operations)
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def AddSubscibeFieldList(self, key, id, conditionid, subscribeFieldlist,
                             triggermessage):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            conditionobjlist = list(condition
                                    for condition in ret.conditionList
                                    if condition.id == conditionid)
            if conditionobjlist:
                if not any(subscription
                           for subscription in ret.subscribeFieldList
                           if subscription.id == conditionid):
                    ret = ret.SetSubscibeFieldList(conditionobjlist[0].id,
                                                   conditionobjlist[0].indice,
                                                   subscribeFieldlist,
                                                   triggermessage)

                else:
                    ret = {
                        'error':
                        'Subscription for the condition already present.Remove and then try.'
                    }
            else:
                ret = {'error': 'Condition id not found.'}

        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def RemoveFromConditionList(self, key, id, conditionid):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = ret.RemoveFromConditionList(conditionid)
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def RemoveFromSubscriptionList(self, key, id, subscriptionid):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = ret.RemoveFromSubscriptionList(subscriptionid)
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def GetConditionList(self, key, userid):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = ret.conditionList
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def GetSubscriptionList(self, key, userid):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            ret = ret.subscribeFieldList
        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def RemoveUser(self, key, id):
        ret = self.GetUser(key, id)
        if isinstance(ret, UserDetails):
            self.userList.remove(ret)
            ret = {'message': 'User removed successfully'}

        return json.dumps(ret,
                          default=lambda o: o.__dict__,
                          sort_keys=True,
                          indent=4)

    def GetADByStockCode(self, stocklist):
        AllAD = self.nse.get_advances_declines()
        ret = list(x for x in AllAD if x['indice'] in stocklist)
        return ret

    def GetAllADStockCode(self):
        AllAD = self.nse.get_advances_declines()
        return list(x['indice'] for x in AllAD)

    def CheckAttrNameOfStock(self, stock, attrname):
        ret = GetAllDetailsOfStock(self, stock)
        if attrname in ret:
            return True
        else:
            return False

    def CheckAttrTypeOfStock(self, stock, attrname, attrtype):
        ret = GetAllDetailsOfStock(self, stock)
        if attrtype == 'date':
            try:
                parse(ret[attrname])
                return True
            except:
                return False

        elif attrtype == 'number':
            try:
                float(ret[attrname])
                return True
            except:
                return False
        else:
            return True

    def CheckOperation(self, attrtype, operation):
        if attrtype == 'string' and operation != 'equals':
            return False
        else:
            return True

    def SetStockList(self, stocklist):
        self.stocklist = stocklist

    def GetStockList(self):
        return self.stocklist

    def AddStock(self, stock):
        self.stocklist.append(stock)

    def RemoveStock(self, stock):
        if len(self.stocklist) > 0 and stock in self.stocklist:
            self.stocklist.remove(stock)
Exemplo n.º 19
0
from nsetools import Nse
from scripts import fetch
from scripts import DB
import json
import tqdm
db = DB.MongoDB()
nse = Nse()
all_codes = nse.get_stock_codes()
for company_codes in tqdm.tqdm(list(all_codes.keys())[304 + 1052:]):
    _temp = {}
    try:
        det = fetch.company_info(company_codes + ".NS")
        if len(det) > 5:
            _temp["ticker"] = company_codes + ".NS"
            _temp["sector"] = det["sector"]
            _temp["name"] = all_codes[company_codes]
            _temp["previousClose"] = det["previousClose"]
            db.insert(_temp)
    except Exception:
        pass
Exemplo n.º 20
0
class TestCoreAPIs(unittest.TestCase):
    def setUp(self):
        self.nse = Nse()

    def test_string_representation(self):
        self.assertEqual(str(self.nse) ,
                         "Driver Class for National Stock Exchange (NSE)")

    def test_instantiate_abs_class(self):
        class Exchange(AbstractBaseExchange): pass
        with self.assertRaises(TypeError):
            exc = Exchange()

    def test_nse_headers(self):
        ret = self.nse.nse_headers()
        self.assertIsInstance(ret, dict)

    def test_nse_opener(self):
        ''' should not raise any exception '''
        opener = self.nse.nse_opener()

    def test_build_url_for_quote(self):
        test_code = 'infy'
        url = self.nse.build_url_for_quote(test_code)
        # 'test_code' should be present in the url
        self.assertIsNotNone(re.search(test_code, url))

    def test_negative_build_url_for_quote(self):
            negative_codes = [1, None]
            with self.assertRaises(Exception):
                for test_code in negative_codes:
                    url = self.nse.build_url_for_quote(test_code)

    def test_response_cleaner(self):
        test_dict = {
            'a': '10',
            'b': '10.0',
            'c': '1,000.10',
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': u'10',
            'k': u'10.0',
            'l': u'1,000.10'
        }

        expected_dict = {
            'a': 10,
            'b': 10.0,
            'c': 1000.10,
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': 10,
            'k': 10.0,
            'l': 1000.10
        }
        ret_dict = self.nse.clean_server_response(test_dict)
        self.assertDictEqual(ret_dict, expected_dict)

    def test_get_stock_codes(self):
        sc = self.nse.get_stock_codes()
        self.assertIsNotNone(sc)
        self.assertIsInstance(sc, dict)
        # test the json format return
        sc_json = self.nse.get_stock_codes(as_json=True)
        self.assertIsInstance(sc_json, str)
        # reconstruct the dict from json and compare
        self.assertItemsEqual(sc, json.loads(sc_json))

# TODO: use mock and create one test where response contains a blank line
# TODO: use mock and create one test where response doesnt contain a csv
# TODO: use mock and create one test where return is null
# TODO: test the cache feature

    def test_negative_get_quote(self):
        wrong_code = 'inf'
        self.assertIsNone(self.nse.get_quote(wrong_code))

    def test_get_quote(self):
        code = 'infy'
        resp = self.nse.get_quote(code)
        self.assertIsInstance(resp, dict)
        # test json response
        json_resp = self.nse.get_quote(code, as_json=True)
        self.assertIsInstance(json_resp, str)
        # reconstruct the original dict from json
        # this test may raise false alarms in case the
        # the price changed in that very moment.
        self.assertDictEqual(resp, json.loads(json_resp))

    def test_is_valid_code(self):
        code = 'infy'
        self.assertTrue(self.nse.is_valid_code(code))

    def test_negative_is_valid_code(self):
        wrong_code = 'in'
        self.assertFalse(self.nse.is_valid_code(wrong_code))

    def test_get_top_gainers(self):
        res = self.nse.get_top_gainers()
        self.assertIsInstance(res, list)
        # test json response
        res = self.nse.get_top_gainers(as_json=True)
        self.assertIsInstance(res, str)

    def test_get_top_losers(self):
        res = self.nse.get_top_losers()
        self.assertIsInstance(res, list)

    def test_render_response(self):
        d = {'fname':'vivek', 'lname':'jha'}
        resp_dict = self.nse.render_response(d)
        resp_json = self.nse.render_response(d, as_json=True)
        # in case of dict, response should be a python dict
        self.assertIsInstance(resp_dict, dict)
        # in case of json, response should be a json string
        self.assertIsInstance(resp_json, str)
        # and on reconstruction it should become same as original dict
        self.assertDictEqual(d, json.loads(resp_json))

    def test_advances_declines(self):
        resp = self.nse.get_advances_declines()
        # it should be a list of dictionaries
        self.assertIsInstance(resp, list)
        # get the json version
        resp_json = self.nse.get_advances_declines(as_json=True)
        self.assertIsInstance(resp_json, str)
        # load the json response and it should have same number of
        # elements as in case of first response
        self.assertEqual(len(resp), len(json.loads(resp_json)))

    def test_is_valid_index(self):
        code = 'CNX NIFTY'
        self.assertTrue(self.nse.is_valid_index(code))
        # test with invalid string
        code = 'some junk stuff'
        self.assertFalse(self.nse.is_valid_index(code))
        # test with lower case
        code = 'cnx nifty'
        self.assertTrue(self.nse.is_valid_index(code))

    def test_get_index_quote(self):
        code = 'CNX NIFTY'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)
        # with json response
        self.assertIsInstance(self.nse.get_index_quote(code, as_json=True),
                              str)
        # with wrong code
        code = 'wrong code'
        self.assertIsNone(self.nse.get_index_quote(code))

        # with lower case code
        code = 'cnx nifty'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)

    def test_get_index_list(self):
        index_list = self.nse.get_index_list()
        index_list_json = self.nse.get_index_list(as_json=True)
        self.assertIsInstance(index_list, list)
        # test json response type
        self.assertIsInstance(index_list_json, str)
        # reconstruct list from json and match
        self.assertListEqual(index_list, json.loads(index_list_json))
Exemplo n.º 21
0
class TestCoreAPIs(unittest.TestCase):
    def setUp(self):
        self.nse = Nse()

    def test_string_representation(self):
        self.assertEqual(str(self.nse),
                         "Driver Class for National Stock Exchange (NSE)")

    def test_nse_headers(self):
        ret = self.nse.nse_headers()
        self.assertIsInstance(ret, dict)

    def test_build_url_for_quote(self):
        test_code = 'infy'
        url = self.nse.build_url_for_quote(test_code)
        # 'test_code' should be present in the url
        self.assertIsNotNone(re.search(test_code, url))

    def test_negative_build_url_for_quote(self):
        negative_codes = [1, None]
        with self.assertRaises(Exception):
            for test_code in negative_codes:
                url = self.nse.build_url_for_quote(test_code)

    def test_get_peer_companies(self):
        code = 'infy'
        response = self.nse.get_peer_companies(code)
        self.assertIsInstance(response, pd.DataFrame)

        # This one was causing a weird error. as the offset was different
        code = '63moons'
        response = self.nse.get_peer_companies(code)
        self.assertIsInstance(response, pd.DataFrame)

    def test_market_status(self):
        result = market_status()
        self.assertIsInstance(result, bool)

    def test_response_cleaner(self):
        test_dict = {
            'a': '10',
            'b': '10.0',
            'c': '-1,000.10',
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': u'10',
            'k': u'10.0',
            'l': u'1,000.10'
        }

        expected_dict = {
            'a': 10,
            'b': 10.0,
            'c': -1000.10,
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': 10,
            'k': 10.0,
            'l': 1000.10
        }
        ret_dict = self.nse.clean_server_response(test_dict)
        self.assertDictEqual(ret_dict, expected_dict)

    def test_get_stock_codes(self):
        sc = self.nse.get_stock_codes()
        self.assertIsNotNone(sc)
        self.assertIsInstance(sc, pd.DataFrame)
        if sc.empty:
            self.fail()


# TODO: use mock and create one test where response contains a blank line
# TODO: use mock and create one test where response doesnt contain a csv
# TODO: use mock and create one test where return is null
# TODO: test the cache feature

    def test_negative_get_quote(self):
        wrong_code = 'inf'
        self.assertEqual(self.nse.get_quote(wrong_code), None)

    def test_get_quote(self):
        resp = self.nse.get_quote('infy', '20Microns', 'abb')
        self.assertEqual(len(resp), 3)
        self.assertIsInstance(resp, pd.DataFrame)
        # test json response
        json_resp = self.nse.get_quote('infy',
                                       '20Microns',
                                       'abb',
                                       as_json=True)
        self.assertEqual(len(json_resp), 3)
        self.assertIsInstance(json_resp[0], str)

    def test_is_valid_code(self):
        code = 'infy'
        self.assertTrue(self.nse.is_valid_code(code))

    def test_negative_is_valid_code(self):
        wrong_code = 'in'
        self.assertFalse(self.nse.is_valid_code(wrong_code))

    def test_get_top(self):
        # as_json = False
        # This test will remove the need to test the individual parts
        result = self.nse.get_top('gainers', 'losers', 'volume', 'active',
                                  'advances decline', 'index list', 'JUNK')
        # Gainers/Losers/Volume/Advances Decline is supposed to return a list of dictionaries
        # Index list is supposed to return a list of strings
        # JUNK Should not return anything
        temp = []
        for item in result:
            temp.append(item)

        self.assertEqual(6, len(temp))
        gainer, loser, volume, active, adv_decline, index_list = temp[0], temp[
            1], temp[2], temp[3], temp[4], temp[5]

        # Test these individually
        self.assertIsInstance(gainer, pd.DataFrame)

        self.assertIsInstance(loser, pd.DataFrame)

        self.assertIsInstance(volume, pd.DataFrame)

        self.assertIsInstance(adv_decline, pd.DataFrame)

        self.assertIsInstance(index_list, list)

        # Now as_json = True
        result = self.nse.get_top('gainers',
                                  'losers',
                                  'volume',
                                  'active',
                                  'advances decline',
                                  'index list',
                                  as_json=True)

        temp = []
        for item in result:
            temp.append(item)

        self.assertEqual(6, len(temp))
        gainer, loser, volume, active, adv_decline_json, index_list_json = temp[
            0], temp[1], temp[2], temp[3], temp[4], temp[5]

        self.assertIsInstance(gainer, str)
        self.assertIsInstance(loser, str)
        self.assertIsInstance(volume, str)
        self.assertIsInstance(active, str)

        self.assertIsInstance(adv_decline_json, str)
        self.assertEqual(len(adv_decline), len(json.loads(adv_decline_json)))

        self.assertIsInstance(index_list_json, str)
        self.assertListEqual(index_list, json.loads(index_list_json))

    def test_render_response(self):
        d = {'fname': 'Arkoprabho', 'lname': 'Chakraborti'}
        resp_dict = self.nse.render_response(d)
        resp_json = self.nse.render_response(d, as_json=True)
        # in case of dict, response should be a python dict
        self.assertIsInstance(resp_dict, dict)
        # in case of json, response should be a json string
        self.assertIsInstance(resp_json, str)
        # and on reconstruction it should become same as original dict
        self.assertDictEqual(d, json.loads(resp_json))

    def test_is_valid_index(self):
        code = 'NIFTY BANK'
        self.assertTrue(self.nse.is_valid_index(code))
        # test with invalid string
        code = 'some junk stuff'
        self.assertFalse(self.nse.is_valid_index(code))
        # test with lower case
        code = 'nifty bank'
        self.assertTrue(self.nse.is_valid_index(code))

    def test_get_index_quote(self):
        code = 'NIFTY BANK'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)
        # with json response
        self.assertIsInstance(self.nse.get_index_quote(code, as_json=True),
                              str)
        # with wrong code
        code = 'wrong code'
        self.assertIsNone(self.nse.get_index_quote(code))

        # with lower case code
        code = 'nifty bank'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)

    def test_jsadptor(self):
        buffer = 'abc:true, def:false, ghi:NaN, jkl:none'
        expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None'
        ret = js_adaptor(buffer)
        self.assertEqual(ret, expected_buffer)

    def test_byte_adaptor(self):
        from io import BytesIO
        buffer = b'nsetools'
        fbuffer = BytesIO(buffer)
        ret_file_buffer = byte_adaptor(fbuffer)
        self.assertIsInstance(ret_file_buffer, six.StringIO)

    def test_save_file(self):
        # Call the function, check if the file has been saved at the required location
        dataframe = self.nse.get_stock_codes()
        save_file(dataframe, 'csv', path=gettempdir(), name='StockCodes')
        path = os.path.join(gettempdir(), 'StockCodes.csv')
        if not os.path.exists(path):
            self.fail()

        save_file(dataframe, 'html', path=gettempdir(), name='StockCodes')
        path = os.path.join(gettempdir(), 'StockCodes.html')
        if not os.path.exists(path):
            self.fail()

        save_file(dataframe, 'json', path=gettempdir(), name='StockCodes')
        path = os.path.join(gettempdir(), 'StockCodes.json')
        if not os.path.exists(path):
            self.fail()

        save_file(dataframe, 'tex', path=gettempdir(), name='StockCodes')
        path = os.path.join(gettempdir(), 'StockCodes.tex')
        if not os.path.exists(path):
            self.fail()
Exemplo n.º 22
0
class TestCoreAPIs(unittest.TestCase):
    def setUp(self):
        self.nse = Nse()

    def test_string_representation(self):
        self.assertEqual(str(self.nse) ,
                         "Driver Class for National Stock Exchange (NSE)")

    def test_instantiate_abs_class(self):
        class Exchange(AbstractBaseExchange): pass
        with self.assertRaises(TypeError):
            exc = Exchange()

    def test_nse_headers(self):
        ret = self.nse.nse_headers()
        self.assertIsInstance(ret, dict)

    def test_nse_opener(self):
        ''' should not raise any exception '''
        opener = self.nse.nse_opener()

    def test_build_url_for_quote(self):
        test_code = 'infy'
        url = self.nse.build_url_for_quote(test_code)
        # 'test_code' should be present in the url
        self.assertIsNotNone(re.search(test_code, url))

    def test_negative_build_url_for_quote(self):
            negative_codes = [1, None]
            with self.assertRaises(Exception):
                for test_code in negative_codes:
                    url = self.nse.build_url_for_quote(test_code)

    def test_response_cleaner(self):
        test_dict = {
            'a': '10',
            'b': '10.0',
            'c': '1,000.10',
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': u'10',
            'k': u'10.0',
            'l': u'1,000.10'
        }

        expected_dict = {
            'a': 10,
            'b': 10.0,
            'c': 1000.10,
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': 10,
            'k': 10.0,
            'l': 1000.10
        }
        ret_dict = self.nse.clean_server_response(test_dict)
        self.assertDictEqual(ret_dict, expected_dict)

    def test_get_stock_codes(self):
        sc = self.nse.get_stock_codes()
        self.assertIsNotNone(sc)
        self.assertIsInstance(sc, dict)
        # test the json format return
        sc_json = self.nse.get_stock_codes(as_json=True)
        self.assertIsInstance(sc_json, str)
        # reconstruct the dict from json and compare
        six.assertCountEqual(self, sc, json.loads(sc_json))

# TODO: use mock and create one test where response contains a blank line
# TODO: use mock and create one test where response doesnt contain a csv
# TODO: use mock and create one test where return is null
# TODO: test the cache feature

    def test_negative_get_quote(self):
        wrong_code = 'inf'
        self.assertIsNone(self.nse.get_quote(wrong_code))

    def test_get_quote(self):
        code = 'infy'
        resp = self.nse.get_quote(code)
        self.assertIsInstance(resp, dict)
        # test json response
        json_resp = self.nse.get_quote(code, as_json=True)
        self.assertIsInstance(json_resp, str)
        # reconstruct the original dict from json
        # this test may raise false alarms in case the
        # the price changed in that very moment.
        self.assertDictEqual(resp, json.loads(json_resp))

    def test_is_valid_code(self):
        code = 'infy'
        self.assertTrue(self.nse.is_valid_code(code))

    def test_negative_is_valid_code(self):
        wrong_code = 'in'
        self.assertFalse(self.nse.is_valid_code(wrong_code))

    def test_get_top_gainers(self):
        res = self.nse.get_top_gainers()
        self.assertIsInstance(res, list)
        # test json response
        res = self.nse.get_top_gainers(as_json=True)
        self.assertIsInstance(res, str)

    def test_get_top_losers(self):
        res = self.nse.get_top_losers()
        self.assertIsInstance(res, list)

    def test_render_response(self):
        d = {'fname':'vivek', 'lname':'jha'}
        resp_dict = self.nse.render_response(d)
        resp_json = self.nse.render_response(d, as_json=True)
        # in case of dict, response should be a python dict
        self.assertIsInstance(resp_dict, dict)
        # in case of json, response should be a json string
        self.assertIsInstance(resp_json, str)
        # and on reconstruction it should become same as original dict
        self.assertDictEqual(d, json.loads(resp_json))

    def test_advances_declines(self):
        resp = self.nse.get_advances_declines()
        # it should be a list of dictionaries
        self.assertIsInstance(resp, list)
        # get the json version
        resp_json = self.nse.get_advances_declines(as_json=True)
        self.assertIsInstance(resp_json, str)
        # load the json response and it should have same number of
        # elements as in case of first response
        self.assertEqual(len(resp), len(json.loads(resp_json)))

    def test_is_valid_index(self):
        code = 'CNX NIFTY'
        self.assertTrue(self.nse.is_valid_index(code))
        # test with invalid string
        code = 'some junk stuff'
        self.assertFalse(self.nse.is_valid_index(code))
        # test with lower case
        code = 'cnx nifty'
        self.assertTrue(self.nse.is_valid_index(code))

    def test_get_index_quote(self):
        code = 'CNX NIFTY'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)
        # with json response
        self.assertIsInstance(self.nse.get_index_quote(code, as_json=True),
                              str)
        # with wrong code
        code = 'wrong code'
        self.assertIsNone(self.nse.get_index_quote(code))

        # with lower case code
        code = 'cnx nifty'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)

    def test_get_index_list(self):
        index_list = self.nse.get_index_list()
        index_list_json = self.nse.get_index_list(as_json=True)
        self.assertIsInstance(index_list, list)
        # test json response type
        self.assertIsInstance(index_list_json, str)
        # reconstruct list from json and match
        self.assertListEqual(index_list, json.loads(index_list_json))

    def test_jsadptor(self):
        buffer = 'abc:true, def:false, ghi:NaN, jkl:none'
        expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None'
        ret = js_adaptor(buffer)
        self.assertEqual(ret, expected_buffer)

    def test_byte_adaptor(self):
        if six.PY2:
            from StringIO import StringIO
            buffer = 'nsetools'
            fbuffer = StringIO(buffer)
        else:
            from io import BytesIO
            buffer = b'nsetools'
            fbuffer = BytesIO(buffer)
        ret_file_buffer = byte_adaptor(fbuffer)
        self.assertIsInstance(ret_file_buffer, six.StringIO)
Exemplo n.º 23
0
import nsepy

from nsetools import Nse
nse = Nse()
print(nse)

all_stock_codes = nse.get_stock_codes(cached=True)
print(all_stock_codes)

index_codes = nse.get_index_list(cached=True)
top_gainers = nse.get_top_gainers()
nse.is_valid_code('infy')

print(index_codes)
Exemplo n.º 24
0
def getstonkfocus(start_date_year=2020,
                  start_date_month=1,
                  start_date_day=1,
                  no_of_days=10,
                  no_check_companies=1,
                  interest_period=3,
                  interest_percentage=3):
    '''
    '''
    # Initiate nse connection and get stock names
    nsel = Nse()
    stock_list = nsel.get_stock_codes()
    # Get the stock list
    stock_list = {v: k for k, v in stock_list.items()}
    iterator = iter(stock_list.items())
    break_cons = 0
    company_list = []
    company_code = []
    # no of days should account for the holidays in between
    start_date = date(int(start_date_year), int(start_date_month),
                      int(start_date_day))
    end_date = start_date + timedelta(days=int(no_of_days))
    print(start_date, end_date)

    for i in iterator:
        if break_cons >= 1:
            company_list.append(i[0])
            company_code.append(i[1])
        break_cons += 1
        if break_cons == no_check_companies:
            break
    company_list.append('TATAMOTORS')
    stock_data = list(company_list)
    perc_matrix = []
    summary_dic = {}
    for i in range(len(stock_data)):
        data_frame = nse.get_history(symbol=company_list[i],
                                     start=start_date,
                                     end=end_date)
        volume_array = np.array(data_frame['Volume'])
        print(volume_array)
        close_array = np.array(data_frame['Close'])
        data_frame.drop(['VWAP', 'Series'], axis=1, inplace=True)
        if i == 0:
            no_of_days = len(np.array(data_frame['Close']))
            perc_matrix = np.zeros((1, 1, len(stock_data)))
        perc_matrix[:, :, i] = calculate_perc(data_frame, interest_period,
                                              interest_percentage)
        if (volume_array.size == 0):
            stock_dict = {company_list[i]: [perc_matrix[0, 0, i], 0, -1]}
        else:
            stock_dict = {
                company_list[i]: [
                    perc_matrix[0, 0, i],
                    np.mean(volume_array),
                    np.mean(close_array)
                ]
            }
        summary_dic.update(stock_dict)
        print(summary_dic)

    return summary_dic
Exemplo n.º 25
0
class LoadCashDataFromNseTools:
    def __init__(self):
        self.nseClient  = Nse()
        self.dbClient   = motor.motor_asyncio.AsyncIOMotorClient(config.DB_HOST, config.DB_PORT)
        self.db         = self.dbClient[config.DATABASE]
        self.collection = self.db[config.TEMP_COLLECTION] # <- TBD

    async def loadBhavdataToDB(self):
        entryCount = 0
        updateCount = 0
        skippedCount = 0

        try:
            stockCodes = self.nseClient.get_stock_codes()
            sList = list(stockCodes.keys())

            stockList = []
            for stock in sList:
                stockList.append(stock)

            for stock in stockList[1:]:
                stockData = self.nseClient.get_quote(stock)

                # Convert to bhavData format
                bhavData = {
                    'date'          : parser.parse(str(stockData['secDate']).strip()),  # as a gatetime.date() object
                    'prevClose'     : float(stockData['previousClose'] if (utils.is_number(stockData['previousClose'])) else '0.0'),
                    'openPrice'     : float(stockData['open'] if (utils.is_number(stockData['open'])) else '0.0'),
                    'highPrice'     : float(stockData['dayHigh'] if (utils.is_number(stockData['dayHigh'])) else '0.0'),
                    'lowPrice'      : float(stockData['dayLow'] if (utils.is_number(stockData['dayLow'])) else '0.0'),
                    'lastPrice'     : float(stockData['lastPrice'] if (utils.is_number(stockData['lastPrice'])) else '0.0'),
                    'closePrice'    : float(stockData['closePrice'] if (utils.is_number(stockData['closePrice'])) else '0.0'),
                    'avgPrice'      : float(stockData['averagePrice'] if (utils.is_number(stockData['averagePrice'])) else '0.0'),
                    'ttlTrdQtnty'   : int(stockData['quantityTraded'] if (utils.is_number(stockData['quantityTraded'])) else '0'),
                    'turnoverLacs'  : float(stockData['totalTradedValue'] if (utils.is_number(stockData['totalTradedValue'])) else '0.0'), # TBD
                    #'noOfTrades'   : int(stockData['noOfTrades'] if (utils.is_number(stockData['noOfTrades'])) else '0'),
                    'noOfTrades'    : int('0'),
                    'delivQty'      : int(stockData['deliveryQuantity'] if (utils.is_number(stockData['deliveryQuantity'])) else '0'),
                    'delivPer'      : float(stockData['deliveryToTradedQuantity'] if (utils.is_number(stockData['deliveryToTradedQuantity'])) else '0.0')
                }

                dateList = []
                document = await self.collection.find_one({'symbol': str(stockData['symbol']).strip()})
                if document:
                    for items in document['bhavData']:
                        dateList.append(items['date'])

                    if bhavData['date'] in dateList:
                        # Data for this date is already present in Database
                        skippedCount += 1
                    else:
                        result = await self.collection.update_one({'symbol': str(stockData['symbol']).strip()},
                                                                  {'$push': {'bhavData': bhavData}})
                        updateCount += 1
                else:
                    result = await self.collection.insert_one({'symbol': str(stockData['symbol']), 'bhavData': [bhavData]})
                    entryCount += 1

        except Exception as e:
            print('loadBhavdataToDB () - ERROR : ', e)

        return entryCount, updateCount, skippedCount
Exemplo n.º 26
0
class TestCoreAPIs(unittest.TestCase):
    def setUp(self):
        self.nse = Nse()

    def test_string_representation(self):
        self.assertEqual(str(self.nse), "Driver Class for National Stock Exchange (NSE)")

    def test_instantiate_abs_class(self):
        class Exchange(AbstractBaseExchange):
            pass

        with self.assertRaises(TypeError):
            exc = Exchange()

    def test_nse_headers(self):
        ret = self.nse.nse_headers()
        self.assertIsInstance(ret, dict)

    def test_nse_opener(self):
        """ should not raise any exception """
        opener = self.nse.nse_opener()

    def test_build_url_for_quote(self):
        test_code = "infy"
        url = self.nse.build_url_for_quote(test_code)
        # 'test_code' should be present in the url
        self.assertIsNotNone(re.search(test_code, url))

    def test_negative_build_url_for_quote(self):
        negative_codes = [1, None]
        with self.assertRaises(Exception):
            for test_code in negative_codes:
                url = self.nse.build_url_for_quote(test_code)

    def test_response_cleaner(self):
        test_dict = {
            "a": "10",
            "b": "10.0",
            "c": "1,000.10",
            "d": "vsjha18",
            "e": 10,
            "f": 10.0,
            "g": 1000.10,
            "h": True,
            "i": None,
            "j": u"10",
            "k": u"10.0",
            "l": u"1,000.10",
        }

        expected_dict = {
            "a": 10,
            "b": 10.0,
            "c": 1000.10,
            "d": "vsjha18",
            "e": 10,
            "f": 10.0,
            "g": 1000.10,
            "h": True,
            "i": None,
            "j": 10,
            "k": 10.0,
            "l": 1000.10,
        }
        ret_dict = self.nse.clean_server_response(test_dict)
        self.assertDictEqual(ret_dict, expected_dict)

    def test_get_stock_codes(self):
        sc = self.nse.get_stock_codes()
        self.assertIsNotNone(sc)
        self.assertIsInstance(sc, dict)
        # test the json format return
        sc_json = self.nse.get_stock_codes(as_json=True)
        self.assertIsInstance(sc_json, str)
        # reconstruct the dict from json and compare
        six.assertCountEqual(self, sc, json.loads(sc_json))

    # TODO: use mock and create one test where response contains a blank line
    # TODO: use mock and create one test where response doesnt contain a csv
    # TODO: use mock and create one test where return is null
    # TODO: test the cache feature

    def test_negative_get_quote(self):
        wrong_code = "inf"
        self.assertIsNone(self.nse.get_quote(wrong_code))

    def test_get_quote(self):
        code = "infy"
        resp = self.nse.get_quote(code)
        self.assertIsInstance(resp, dict)
        # test json response
        json_resp = self.nse.get_quote(code, as_json=True)
        self.assertIsInstance(json_resp, str)
        # reconstruct the original dict from json
        # this test may raise false alarms in case the
        # the price changed in that very moment.
        self.assertDictEqual(resp, json.loads(json_resp))

    def test_is_valid_code(self):
        code = "infy"
        self.assertTrue(self.nse.is_valid_code(code))

    def test_negative_is_valid_code(self):
        wrong_code = "in"
        self.assertFalse(self.nse.is_valid_code(wrong_code))

    def test_get_top_gainers(self):
        res = self.nse.get_top_gainers()
        self.assertIsInstance(res, list)
        # test json response
        res = self.nse.get_top_gainers(as_json=True)
        self.assertIsInstance(res, str)

    def test_get_top_losers(self):
        res = self.nse.get_top_losers()
        self.assertIsInstance(res, list)

    def test_render_response(self):
        d = {"fname": "vivek", "lname": "jha"}
        resp_dict = self.nse.render_response(d)
        resp_json = self.nse.render_response(d, as_json=True)
        # in case of dict, response should be a python dict
        self.assertIsInstance(resp_dict, dict)
        # in case of json, response should be a json string
        self.assertIsInstance(resp_json, str)
        # and on reconstruction it should become same as original dict
        self.assertDictEqual(d, json.loads(resp_json))

    def test_advances_declines(self):
        resp = self.nse.get_advances_declines()
        # it should be a list of dictionaries
        self.assertIsInstance(resp, list)
        # get the json version
        resp_json = self.nse.get_advances_declines(as_json=True)
        self.assertIsInstance(resp_json, str)
        # load the json response and it should have same number of
        # elements as in case of first response
        self.assertEqual(len(resp), len(json.loads(resp_json)))

    def test_is_valid_index(self):
        code = "CNX NIFTY"
        self.assertTrue(self.nse.is_valid_index(code))
        # test with invalid string
        code = "some junk stuff"
        self.assertFalse(self.nse.is_valid_index(code))
        # test with lower case
        code = "cnx nifty"
        self.assertTrue(self.nse.is_valid_index(code))

    def test_get_index_quote(self):
        code = "CNX NIFTY"
        self.assertIsInstance(self.nse.get_index_quote(code), dict)
        # with json response
        self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str)
        # with wrong code
        code = "wrong code"
        self.assertIsNone(self.nse.get_index_quote(code))

        # with lower case code
        code = "cnx nifty"
        self.assertIsInstance(self.nse.get_index_quote(code), dict)

    def test_get_index_list(self):
        index_list = self.nse.get_index_list()
        index_list_json = self.nse.get_index_list(as_json=True)
        self.assertIsInstance(index_list, list)
        # test json response type
        self.assertIsInstance(index_list_json, str)
        # reconstruct list from json and match
        self.assertListEqual(index_list, json.loads(index_list_json))

    def test_jsadptor(self):
        buffer = "abc:true, def:false, ghi:NaN, jkl:none"
        expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None'
        ret = js_adaptor(buffer)
        self.assertEqual(ret, expected_buffer)

    def test_byte_adaptor(self):
        if six.PY2:
            from StringIO import StringIO

            buffer = "nsetools"
            fbuffer = StringIO(buffer)
        else:
            from io import BytesIO

            buffer = b"nsetools"
            fbuffer = BytesIO(buffer)
        ret_file_buffer = byte_adaptor(fbuffer)
        self.assertIsInstance(ret_file_buffer, six.StringIO)
Exemplo n.º 27
0
"""
Created on Wed Oct 14 12:56:51 2015

@author: itithilien
"""

from nsetools import Nse
import pandas as pd
import time
from urllib2 import build_opener, HTTPCookieProcessor, Request

# test
nse = Nse()
print nse

all_stock_codes = nse.get_stock_codes()
ticklist = all_stock_codes.keys()
ticklist = sorted(ticklist)


while True:
    sata = pd.DataFrame()
    for ticker in ticklist:
        ptime = time.ctime()
        try:
            q = nse.get_quote(ticker)
            qdf = pd.DataFrame(q.items(), columns=["Item", "Value"])
            qdf["symbol"] = ticker
            qdf["timestamp"] = ptime
            # qdf = qdf.sort(['Item'])
            sata = pd.concat([sata, qdf], axis=0, ignore_index=True)
Exemplo n.º 28
0
 def fetch_ticker_list(cls):
     nse = Nse()
     index_codes = nse.get_stock_codes()
     # pprint(index_codes)
     return index_codes
Exemplo n.º 29
0
        if answer is None:
            print ("Google Search")
        else:
       		print (answer) 
        
    elif "exit" in query:
    	break
    elif "Stock" in query or 'stock' in query:
    	#for using stockgogole-api
    	stock = raw_input("Please tell me the exact stock name\n")
    	St = nse.get_quote(stock)
    	try:
    		a = St['averagePrice']
    		b = St['buyPrice1']
    		c = St['sellPrice1']
	    	print ("Average Price:"),a
	    	print ("BuyPrice:"),b
	    	print ('SellPrice:'),c
	except:
		s = raw_input("Please type correct Stock name, Should I tell you about avaiable Stocks?")
		if s=='yes':
			pprint (nse.get_stock_codes())
	   
    else:
    	line = kernel.respond(query)
    	for j in line.split("."):
    		print (j)
            

	
Exemplo n.º 30
0
class TestCoreAPIs(unittest.TestCase):
    def setUp(self):
        self.nse = Nse()

    def test_string_representation(self):
        self.assertEqual(str(self.nse), "Driver Class for National Stock Exchange (NSE)")

    def test_instantiate_abs_class(self):
        class Exchange(AbstractBaseExchange):
            pass
        with self.assertRaises(TypeError):
            exc = Exchange()

    def test_nse_headers(self):
        ret = self.nse.nse_headers()
        self.assertIsInstance(ret, dict)

    def test_nse_opener(self):
        ''' should not raise any exception '''
        opener = self.nse.nse_opener()

    def test_build_url_for_quote(self):
        test_code = 'infy'
        url = self.nse.build_url_for_quote(test_code)
        # 'test_code' should be present in the url
        self.assertIsNotNone(re.search(test_code, url))

    def test_negative_build_url_for_quote(self):
            negative_codes = [1, None]
            with self.assertRaises(Exception):
                for test_code in negative_codes:
                    url = self.nse.build_url_for_quote(test_code)

    def test_response_cleaner(self):
        test_dict = {
            'a': '10',
            'b': '10.0',
            'c': '1,000.10',
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': u'10',
            'k': u'10.0',
            'l': u'1,000.10'
        }

        expected_dict = {
            'a': 10,
            'b': 10.0,
            'c': 1000.10,
            'd': 'vsjha18',
            'e': 10,
            'f': 10.0,
            'g': 1000.10,
            'h': True,
            'i': None,
            'j': 10,
            'k': 10.0,
            'l': 1000.10
        }
        ret_dict = self.nse.clean_server_response(test_dict)
        self.assertDictEqual(ret_dict, expected_dict)

    def test_get_stock_codes(self):
        sc = self.nse.get_stock_codes()
        self.assertIsNotNone(sc)
        self.assertIsInstance(sc, dict)
        # test the json format return
        sc_json = self.nse.get_stock_codes(as_json=True)
        self.assertIsInstance(sc_json, str)
        # reconstruct the dict from json and compare
        six.assertCountEqual(self, sc, json.loads(sc_json))

# TODO: use mock and create one test where response contains a blank line
# TODO: use mock and create one test where response doesnt contain a csv
# TODO: use mock and create one test where return is null
# TODO: test the cache feature

    def test_negative_get_quote(self):
        wrong_code = 'inf'
        self.assertIsNone(self.nse.get_quote(wrong_code))

    def test_get_quote(self):
        code = 'infy'
        resp = self.nse.get_quote(code)
        self.assertIsInstance(resp, dict)
        # test json response
        json_resp = self.nse.get_quote(code, as_json=True)
        self.assertIsInstance(json_resp, str)
        # reconstruct the original dict from json
        # this test may raise false alarms in case the
        # the price changed in that very moment.
        self.assertDictEqual(resp, json.loads(json_resp))

    def test_is_valid_code(self):
        code = 'infy'
        self.assertTrue(self.nse.is_valid_code(code))

    def test_negative_is_valid_code(self):
        wrong_code = 'in'
        self.assertFalse(self.nse.is_valid_code(wrong_code))

    def test_get_top_gainers(self):
        res = self.nse.get_top_gainers()
        self.assertIsInstance(res, list)
        # test json response
        res = self.nse.get_top_gainers(as_json=True)
        self.assertIsInstance(res, str)

    def test_get_top_losers(self):
        res = self.nse.get_top_losers()
        self.assertIsInstance(res, list)

    def test_render_response(self):
        d = {'fname':'vivek', 'lname':'jha'}
        resp_dict = self.nse.render_response(d)
        resp_json = self.nse.render_response(d, as_json=True)
        # in case of dict, response should be a python dict
        self.assertIsInstance(resp_dict, dict)
        # in case of json, response should be a json string
        self.assertIsInstance(resp_json, str)
        # and on reconstruction it should become same as original dict
        self.assertDictEqual(d, json.loads(resp_json))

    def test_advances_declines(self):
        resp = self.nse.get_advances_declines()
        # it should be a list of dictionaries
        self.assertIsInstance(resp, list)
        # get the json version
        resp_json = self.nse.get_advances_declines(as_json=True)
        self.assertIsInstance(resp_json, str)
        # load the json response and it should have same number of
        # elements as in case of first response
        self.assertEqual(len(resp), len(json.loads(resp_json)))

    def test_is_valid_index(self):
        code = 'NIFTY BANK'
        self.assertTrue(self.nse.is_valid_index(code))
        # test with invalid string
        code = 'some junk stuff'
        self.assertFalse(self.nse.is_valid_index(code))
        # test with lower case
        code = 'nifty bank'
        self.assertTrue(self.nse.is_valid_index(code))

    def test_get_index_quote(self):
        code = 'NIFTY BANK'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)
        # with json response
        self.assertIsInstance(self.nse.get_index_quote(code, as_json=True),
                              str)
        # with wrong code
        code = 'wrong code'
        self.assertIsNone(self.nse.get_index_quote(code))

        # with lower case code
        code = 'nifty bank'
        self.assertIsInstance(self.nse.get_index_quote(code), dict)

    def test_get_index_list(self):
        index_list = self.nse.get_index_list()
        index_list_json = self.nse.get_index_list(as_json=True)
        self.assertIsInstance(index_list, list)
        # test json response type
        self.assertIsInstance(index_list_json, str)
        # reconstruct list from json and match
        self.assertListEqual(index_list, json.loads(index_list_json))

    def test_jsadptor(self):
        buffer = 'abc:true, def:false, ghi:NaN, jkl:none'
        expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None'
        ret = js_adaptor(buffer)
        self.assertEqual(ret, expected_buffer)

    def test_byte_adaptor(self):
        if six.PY2:
            from StringIO import StringIO
            buffer = 'nsetools'
            fbuffer = StringIO(buffer)
        else:
            from io import BytesIO
            buffer = b'nsetools'
            fbuffer = BytesIO(buffer)
        ret_file_buffer = byte_adaptor(fbuffer)
        self.assertIsInstance(ret_file_buffer, six.StringIO)

    def test_nse_lot_sizes(self):
        data = self.nse.get_fno_lot_sizes()
        self.assertIsInstance(data, dict)

    def test_6th_Dec_1994(self):
        data = self.nse.download_bhavcopy('1994-12-06')
        self.assertIsInstance(self, data, bytes)

    def test_top_fno_gainers_losers(self):
        fno_gainer = self.nse.get_top_fno_gainers()
        self.assertIsInstance(fno_gainer, list)
        fno_gainer_json = self.nse.get_top_fno_gainers(as_json=True)
        self.assertIsInstance(fno_gainer_json, str)
        fno_loser = self.nse.get_top_fno_losers()
        self.assertIsInstance(fno_loser, list)
        fno_loser_json = self.nse.get_top_fno_losers(as_json=True)
        self.assertIsInstance(fno_loser_json, str)

    def test_statistics(self):
        active = self.nse.get_active_monthly()
        self.assertIsInstance(active, list)
        active_json = self.nse.get_active_monthly(as_json=True)
        self.assertIsInstance(active_json, str)
        yr_high = self.nse.get_year_high()
        self.assertIsInstance(yr_high, list)
        yr_high_json = self.nse.get_year_high(as_json=True)
        self.assertIsInstance(yr_high_json, str)
        yr_low = self.nse.get_year_low()
        self.assertIsInstance(yr_low, list)
        yr_low_json = self.nse.get_year_low(as_json=True)
        self.assertIsInstance(yr_low_json, str)
        preopen = self.nse.get_preopen_nifty()
        self.assertIsInstance(preopen, list)
        preopen_json = self.nse.get_preopen_nifty(as_json=True)
        self.assertIsInstance(preopen_json, str)
        preopen_nb = self.nse.get_preopen_niftybank()
        self.assertIsInstance(preopen_nb, list)
        preopen_nb_json = self.nse.get_preopen_niftybank(as_json=True)
        self.assertIsInstance(preopen_nb_json, str)
        preopen_fno = self.nse.get_preopen_fno()
        self.assertIsInstance(preopen_fno, list)
        preopen_fno_json = self.nse.get_preopen_fno(as_json=True)
        self.assertIsInstance(preopen_fno_json, str)
Exemplo n.º 31
0
 def getStockSymbols(all):
     nse = Nse()
     all_stock_codes = nse.get_stock_codes()
     print(all_stock_codes)
Exemplo n.º 32
0
from nsetools import Nse
from pprint import pprint
import pandas as pd
from tqdm import tqdm

nse = Nse()

stocks = list(nse.get_stock_codes().keys())

unwanted_stocks = [
    'SYMBOL', '3PLAND', 'ANKITMETAL', 'BURNPUR', 'CHROMATIC', 'CREATIVEYE',
    'DCMFINSERV', 'EASTSILK', 'EUROMULTI', 'EUROTEXIND', 'EXCEL', 'GAYAHWS',
    'GISOLUTION', 'GLOBOFFS', 'HBSL', 'INTEGRA', 'KALYANI', 'KAUSHALYA',
    'MOHOTAIND', 'MOHITIND', 'MUKANDENGG', 'NAGREEKCAP', 'NAGREEKCAP',
    'NATNLSTEEL', 'NIRAJISPAT', 'NORBTEAEXP', 'NTL', 'OMKARCHEM', 'PAEL',
    'PRADIP', 'PREMIER', 'RADAAN', 'SABEVENTS', 'SHYAMTEL', 'SOMATEX',
    'TECHIN', 'TFL', 'TGBHOTELS', 'VIVIDHA', 'YESBANK', 'ZICOM'
]

stocks = [stock for stock in stocks if stock not in unwanted_stocks]

price = []
company = []
symbol = []

for stock in tqdm(stocks[:10]):
    q = nse.get_quote(stock)
    if (q['deliveryToTradedQuantity'] is not None):
        if ((q['deliveryToTradedQuantity'] > 90)):
            company.append(q['companyName'])
            price.append(q['lastPrice'])
Exemplo n.º 33
0
#imports
from nsetools import Nse
from datetime import datetime
import json
from pprint import pprint

#initializations
nse = Nse()
i = 0 

all_stock_codes = nse.get_stock_codes() #1626

with open('files/stock_data_min.json') as f :
    min_stock_data = json.load(f)
    # print(json.dumps(min_stock_data, indent=4, sort_keys=True))

# TODO: make this multithreaded
for stock_code in all_stock_codes : 
    try:
        stock_data = nse.get_quote(stock_code)
    except Exception as e:
        print(str(e), " for ", stock_code)
        continue
    today_date = datetime.today().strftime('%Y-%m-%d')

    data_for_today = {}
    data_for_today[today_date] = {
        "open": stock_data["open"],
        "high": stock_data["dayHigh"],
        "low" : stock_data["dayLow"],
        "close": stock_data["closePrice"]
Exemplo n.º 34
0
from nsetools import Nse

nse = Nse()

allsocks = nse.get_stock_codes()

stock_item = nse.get_quote('infy', as_json=False)

print(type(stock_item))