3
import time
import argparse
import pandas as pd
from nsetools import Nse

nse = Nse()
t = time.time()
FILE_LOCATION = ''  # csv file, blank because path not relevant to others
df = pd.read_csv(FILE_LOCATION)

CSV File contents:

Instrument,Qty,Avg buy price
APLAPOLLO,3,949.95
AVANTIFEED,6,554.55
BALAMINES,9,337.72
BALMLAWRIE,4,258.5
BANCOINDIA,15,217
DCMSHRIRAM,12,261.4
GHCL,12,267.2
GIPCL,27,101.95
JAMNAAUTO,15,182.1
JBCHEPHARM,15,344.85
KEI,24,143.95
KPRMILL,6,569.65
KRBL,9,312
MPHASIS,6,533.95
SHEMAROO,2,413.25

Code:

# using argparse to provide options for obtaining closePrice or buyPrice1
# of stocks
parser = argparse.ArgumentParser(description='Stock Quote fetcher')
parser.add_argument('-r', '--realtime', help='Obtain realtime stock\
                    quotes', action='store_true')
args = parser.parse_args()


def get_closing(stock):
    """Function to obtain closePrice or buyPrice1 of stocks"""
    if args.realtime:
        return nse.get_quote(stock)['buyPrice1']
    else:
        return nse.get_quote(stock)['closePrice']


# calculating current value of investment
current_value = sum(get_closing(row[0]) * row[1] for index, row in
                    df.iterrows())

print(current_value)
print("Completed in ", time.time() - t)

Currently the stock prices are obtained sequentially using the generator expression. Doing this takes 18-25 seconds to calculate the current value of investment. Is there any way to obtain these prices all together in parallel and calculate current value of investment?

Dana
  • 437
  • 1
  • 7
  • 12
  • problem can be `nse` which need some time to get data. Maybe it can get many stocks in one request. – furas Jan 17 '17 at 10:22
  • There is no functionality provided by nsetools to obtain prices of multiple stocks in one request. – Dana Jan 17 '17 at 10:32
  • Parallelize the process, don't know how many connections could you open with nse. but you can fork into N process who ask only one sock and save it into the csv as additional column, wait for all to finish, and then do the sum. – Mquinteiro Jan 17 '17 at 12:16
  • Since these are file transfers over the internet (I assume) they are I/O bound and multiple threads work fine to transfer the files. – Marichyasana Jan 17 '17 at 12:27

1 Answers1

1

I have forked the repository here, where I have been working on moving the nsetools API to a DataFrame-based approach and using threads to reduce the time it takes to fetch multiple quotes.

The latest release as of today v1.1.0 currently has all these features implemented.

However, python2 compatibility is not guaranteed. If you wish to request for any more features, be sure to create a new issue. I will work on it and keep updating the repo.

Arko Chakraborti
  • 403
  • 5
  • 19
  • I tried visiting the link, github says page not found – Dana Apr 10 '18 at 06:17
  • Thank you for pointing out. I have update the link. Kindly check – Arko Chakraborti Apr 11 '18 at 01:15
  • The link for detailed documentation throws 404 not found error – Dana Apr 11 '18 at 01:51
  • Yes.I am aware of it. There is an open issue for it too. I haven't been able to work on generating the documentation. It would be of great help if one can start a PR with the proper documentation. The code is commented properly with docstrings, so documentation would be just a matter of invoking proper commands with sphinx (or any alternate) – Arko Chakraborti Apr 11 '18 at 01:57