Search code examples
pythonstockquotesalpha-vantage

how to get multiple stock quote with symbol in python


I want to get NSE stocks only. I m using to get multiple stocks quotes with a symbol in python.

code:

from nsetools import Nse

nse = Nse()

def get_current(stock):
    return nse.get_quote(stock)

for i in stocks:
        data = get_current(i)

This doesn't help. please help


Solution

  • You need to fetch a list of quotes. Something like this perhaps:

    from nsetools import Nse
    nse = Nse()
    stocks = ['INDUSINDBK', 'SHREECEM']
    data = [nse.get_quote(stock) for stock in stocks]
    

    Edit: to just get last price per symbol, you can do:

    symbol2price = {stock:nse.get_quote(stock)['lastPrice'] for stock in stocks}
    

    which would then contain the symbols mapping to the last price, like so:

    >>> symbol2price
    {'INDUSINDBK': 510.25, 'SHREECEM': 21598.0}