Jugaad Data - Download historical stock market data

Home

Download bhavcopies

You can download bhavcopies for stocks, indices and futures & Optios using jugaad-data. Below example shows how to download different bhavcopies for 1st Jan 2020 and saves to a directory in csv format.

from datetime import date
from jugaad_data.nse import bhavcopy_save, full_bhavcopy_save, bhavcopy_fo_save, bhavcopy_index_save

bhavcopy_save(date(2020,1,1), "/path/to/directory")
full_bhavcopy_save(date(2020,1,1), "/path/to/directory")
bhavcopy_fo_save(date(2020,1,1), "/path/to/directory")
bhavcopy_index_save(date(2020,1,1), "/path/to/directory")

Note Difference between bhavcopy_save and full_bhavcopy_save is that in full bhavcopy you also get the percentage of volume which was in delivery.

Download historical stock data

from datetime import date
from jugaad_data.nse import stock_csv, stock_df

# Download as pandas dataframe
df = stock_df(symbol="SBIN", from_date=date(2020,1,1),
            to_date=date(2020,1,30), series="EQ")
print(df.head())
        DATE SERIES    OPEN    HIGH     LOW  PREV. CLOSE     LTP   CLOSE  \
0 2020-01-30     EQ  316.75  316.75  305.65       316.45  310.00  310.70   
1 2020-01-29     EQ  317.85  319.70  315.55       315.10  316.95  316.45   
2 2020-01-28     EQ  317.95  320.00  311.05       316.20  316.40  315.10   
3 2020-01-27     EQ  320.90  322.00  315.80       324.05  316.40  316.20   
4 2020-01-24     EQ  323.50  327.30  321.15       323.20  324.30  324.05   

     VWAP  52W H   52W L    VOLUME         VALUE  NO OF TRADES SYMBOL  
0  311.18  373.8  244.35  35802330  1.114102e+10        227687   SBIN  
1  317.75  373.8  244.35  23914114  7.598704e+09        143297   SBIN  
2  316.67  373.8  244.35  26488426  8.388015e+09        173879   SBIN  
3  318.82  373.8  244.35  23309355  7.431558e+09        148768   SBIN  
4  325.15  373.8  244.35  22706879  7.383247e+09        146667   SBIN  


# Download data and save to a csv file
stock_csv(symbol="SBIN", from_date=date(2020,1,1),
            to_date=date(2020,1,30), series="EQ", output="/path/to/file.csv")

Download historical index data

from jugaad_data.nse import index_csv, index_df


# Download as pandas dataframe
df = index_df(symbol="NIFTY 50", from_date=date(2020,1,1),
            to_date=date(2020,1,30))

print(df.head())

  Index Name INDEX_NAME HistoricalDate      OPEN      HIGH       LOW     CLOSE
0   Nifty 50   NIFTY 50     2020-01-30  12147.75  12150.30  12010.60  12035.80
1   Nifty 50   NIFTY 50     2020-01-29  12114.90  12169.60  12103.80  12129.50
2   Nifty 50   NIFTY 50     2020-01-28  12148.10  12163.55  12024.50  12055.80
3   Nifty 50   NIFTY 50     2020-01-27  12197.10  12216.60  12107.00  12119.00
4   Nifty 50   NIFTY 50     2020-01-24  12174.55  12272.15  12149.65  12248.25

# Download as csv file
index_csv(symbol="NIFTY 50", from_date=date(2020,1,1),
            to_date=date(2020,1,30), output="/path/to/file.csv")

Download historical derivatives data

def derivatives_df(symbol, from_date, to_date, expiry_date, instrument_type, option_type. strike_price)
    """
    Downloads the historical data for a given contract in a pandas dataframe
    Arguments-

        symbol - Stock symbol eg.SBIN or NIFTY
        from_date - From date as datetime.date object
        to_date - To date as datetime.date object
        expiry_date - Expiry date as datetime.date object
        instrument_type 1 - Can take values from “FUTSTK”, “FUTIDX”, “OPTSTK”, “OPTIDX”
        option_type - “CE” for call option and “PE” for put option (Required only for instrument type OPTSTK and OPTIDX)
        strike_price - Strike price as float
    """

In case you want to download it as csv file use below function where output is name of the output csv file you wish to save with complete path.

derivatives_csv(symbol, from_date, to_date, expiry_date, instrument_type, option_type. strike_price, output)

Stock futures

from jugaad_data.nse import derivatives_csv, derivatives_df
df = derivatives_df(symbol="SBIN", from_date=date(2020,1,1), to_date=date(2020,1,30),
            expiry_date=date(2020,1,30), instrument_type="FUTSTK")
print(df.head())
        DATE     EXPIRY    OPEN    HIGH     LOW   CLOSE     LTP  SETTLE PRICE  \
0 2020-01-30 2020-01-30  316.25  316.40  305.80  310.55  309.60        310.70   
1 2020-01-29 2020-01-30  317.55  320.15  315.65  316.60  317.05        316.60   
2 2020-01-28 2020-01-30  317.35  320.40  311.00  315.20  316.40        315.20   
3 2020-01-27 2020-01-30  321.30  322.30  316.05  316.45  316.55        316.45   
4 2020-01-24 2020-01-30  324.50  327.95  322.00  324.55  324.80        324.55   

   TOTAL TRADED QUANTITY  MARKET LOT  PREMIUM VALUE  OPEN INTEREST  \
0               72678000        3000   2.267156e+10      6564000.0   
1               69747000        3000   2.217897e+10     38799000.0   
2               97299000        3000   3.082756e+10     65694000.0   
3               66660000        3000   2.128511e+10     94551000.0   
4               61323000        3000   1.997960e+10    102711000.0   

   CHANGE IN OI SYMBOL  
0   -32235000.0   SBIN  
1   -26895000.0   SBIN  
2   -28857000.0   SBIN  
3    -8160000.0   SBIN  
4    -9417000.0   SBIN  

Stock options

from jugaad_data.nse import derivatives_csv, derivatives_df
df = derivatives_df(symbol="SBIN", from_date=date(2020,1,1), to_date=date(2020,1,30),
            expiry_date=date(2020,1,30), instrument_type="OPTSTK", option_type="CE", strike_price=300)
print(df.head())
        DATE     EXPIRY OPTION TYPE  STRIKE PRICE   OPEN   HIGH    LOW  CLOSE  \
0 2020-01-30 2020-01-30          CE         300.0  13.85  15.15   6.50  10.40   
1 2020-01-29 2020-01-30          CE         300.0  18.45  20.20  16.55  16.55   
2 2020-01-28 2020-01-30          CE         300.0  19.00  20.05  12.30  14.90   
3 2020-01-27 2020-01-30          CE         300.0  20.00  22.25  16.60  16.85   
4 2020-01-24 2020-01-30          CE         300.0  22.70  27.70  22.70  24.90   

     LTP  SETTLE PRICE  TOTAL TRADED QUANTITY  MARKET LOT  PREMIUM VALUE  \
0  10.40          0.00                 102000        3000     31863750.0   
1  16.55         16.55                  48000        3000     15276450.0   
2  16.80         14.90                 186000        3000     58815300.0   
3  16.90         16.85                  90000        3000     28730400.0   
4  25.00         24.90                 219000        3000     71488350.0   

   OPEN INTEREST  CHANGE IN OI SYMBOL  
0       144000.0      -30000.0   SBIN  
1       174000.0       -6000.0   SBIN  
2       180000.0      -75000.0   SBIN  
3       255000.0      -39000.0   SBIN  
4       294000.0      -51000.0   SBIN  

Index futures

from jugaad_data.nse import derivatives_csv, derivatives_df
df = derivatives_df(symbol="NIFTY", from_date=date(2020,1,1), to_date=date(2020,1,30),
            expiry_date=date(2020,1,30), instrument_type="FUTIDX")
print(df.head())
        DATE     EXPIRY      OPEN      HIGH      LOW     CLOSE       LTP  \
0 2020-01-30 2020-01-30  12115.60  12117.90  12006.3  12043.50  12036.80   
1 2020-01-29 2020-01-30  12114.40  12182.45  12095.6  12121.55  12121.00   
2 2020-01-28 2020-01-30  12124.90  12162.45  12038.0  12068.00  12075.00   
3 2020-01-27 2020-01-30  12168.85  12209.60  12117.5  12125.70  12118.95   
4 2020-01-24 2020-01-30  12204.75  12292.70  12157.0  12271.60  12271.20   

   SETTLE PRICE  TOTAL TRADED QUANTITY  MARKET LOT  PREMIUM VALUE  \
0      12035.80                9898650          75   1.192217e+11   
1      12121.55               10762125          75   1.306506e+11   
2      12068.00                9964200          75   1.206328e+11   
3      12125.70                9734250          75   1.184152e+11   
4      12271.60                8184525          75   1.002034e+11   

   OPEN INTEREST  CHANGE IN OI SYMBOL  
0      5467875.0    -2761125.0  NIFTY  
1      8229000.0    -2666025.0  NIFTY  
2     10895025.0    -1474650.0  NIFTY  
3     12369675.0      -24825.0  NIFTY  
4     12394500.0     -203325.0  NIFTY  

Index options

from jugaad_data.nse import derivatives_csv, derivatives_df
df = derivatives_df(symbol="NIFTY", from_date=date(2020,1,1), to_date=date(2020,1,30),
            expiry_date=date(2020,1,30), instrument_type="OPTIDX", option_type="CE", strike_price=12000)
print(df.head())
        DATE     EXPIRY OPTION TYPE  STRIKE PRICE    OPEN    HIGH     LOW  \
0 2020-01-30 2020-01-30          CE       12000.0  141.30  141.30   18.10   
1 2020-01-29 2020-01-30          CE       12000.0  118.30  185.00  105.15   
2 2020-01-28 2020-01-30          CE       12000.0  149.35  176.95   78.35   
3 2020-01-27 2020-01-30          CE       12000.0  192.50  222.55  146.40   
4 2020-01-24 2020-01-30          CE       12000.0  211.95  300.00  185.00   

    CLOSE     LTP  SETTLE PRICE  TOTAL TRADED QUANTITY  MARKET LOT  \
0   44.60   35.45          0.00               44875050          75   
1  126.05  124.00        126.05                5006025          75   
2   96.20  102.00         96.20                6231225          75   
3  152.00  147.00        152.00                1334100          75   
4  280.85  281.95        280.85                1067925          75   

   PREMIUM VALUE  OPEN INTEREST  CHANGE IN OI SYMBOL  
0   5.402420e+11       998775.0      178650.0  NIFTY  
1   6.078953e+10       820125.0     -284625.0  NIFTY  
2   7.548726e+10      1104750.0      219300.0  NIFTY  
3   1.625192e+10       885450.0      102900.0  NIFTY  
4   1.308711e+10       782550.0      -99375.0  NIFTY  

Command line interface

jugaad-data also comes with a powerful commandline interface - jdata. Simply type jdata --help to explore all the available options-

$ jdata --help
Usage: jdata [OPTIONS] COMMAND [ARGS]...

  This is a command line tool to download stock market data to csv files.

Options:
  --help  Show this message and exit.

Commands:
  bhavcopy     Downloads bhavcopy from NSE's website Download today's...
  derivatives  Sample usage- Download stock futures- jdata derivatives -s...
  index        Download historical index data $jdata index --symbol "NIFTY...
  stock        Download historical stock data $jdata stock --symbol STOCK1...

Download bhavcopies

$ jdata bhavcopy --help

Usage: jdata bhavcopy [OPTIONS]

  Downloads bhavcopy from NSE's website

Options:
  -d, --dest DIRECTORY   Destination directory path  [required]
  -f, --from [%Y-%m-%d]  From date
  -t, --to [%Y-%m-%d]    To date
  --fo / --no-fo         Downloads F&O bhavcopy
  --idx / --no-idx       Downloads Index bhavcopy
  --full / --no-full     Full Bhavcopy
  --help                 Show this message and exit.

Download today’s bhavcopy (works only after market hours once bhavcopy is ready)

$ jdata bhavcopy -d /path/to/dir

Download bhavcopy for a specific date

$ jdata bhavcopy -d /path/to/dir -f 2020-01-01

Download all bhavcopies between date range

$ jdata bhavcopy -d /path/to/dir -f 2020-01-01 -t 2020-01-30

Download full bhavcopies with delivery trade quantity

$ jdata bhavcopy -d /path/to/dir -f 2020-01-01 -t 2020-01-30 --full

Download index bhavcopies

$ jdata bhavcopy -d /path/to/dir -f 2020-01-01 -t 2020-01-30 --idx

Download derivatives bhavcopies between date range

$ jdata bhavcopy -d /path/to/dir -f 2020-01-01 -t 2020-01-30 --fo

Download historical stock data

$ jdata stock --help
Usage: jdata stock [OPTIONS]


Options:
  -s, --symbol TEXT  Stock symbol  [required]
  -f, --from TEXT    From date - yyyy-mm-dd  [required]
  -t, --to TEXT      From date - yyyy-mm-dd  [required]
  -S, --series TEXT  Series - EQ, BE etc.  [default: EQ]
  -o, --output TEXT  Full path for output file
  --help             Show this message and exit.

Download historical stock data

$ jdata stock -s SBIN -f 2020-01-01 -t 2020-01-31 -o SBIN-Jan.csv

Download historical index data

$ jdata index --help
Usage: jdata index [OPTIONS]


Options:
  -s, --symbol TEXT  Stock symbol  [required]
  -f, --from TEXT    From date - yyyy-mm-dd  [required]
  -t, --to TEXT      From date - yyyy-mm-dd  [required]
  -o, --output TEXT  Full path for output file
  --help             Show this message and exit.

Download historical index data

$ jdata index -s "NIFTY 50" -f 2020-01-01 -t 2020-01-31 -o SBIN-Jan.csv

Download futures & options data

$ jdata derivatives --help
Usage: jdata derivatives [OPTIONS]

Options:
  -s, --symbol TEXT  Stock/Index symbol  [required]
  -f, --from TEXT    From date - yyyy-mm-dd  [required]
  -t, --to TEXT      To date - yyyy-mm-dd  [required]
  -e, --expiry TEXT  Expiry date - yyyy-mm-dd  [required]
  -i, --instru TEXT  FUTSTK - Stock futures, FUTIDX - Index Futures, OPTSTK -
                     Stock Options, OPTIDX - Index Options  [required]

  -p, --price TEXT   Strike price (Only for OPTSTK and OPTIDX)
  --ce / --pe        --ce for call and --pe for put (Only for OPTSTK and
                     OPTIDX)

  -o, --output TEXT  Full path of output file
  --help             Show this message and exit.

Download stock futures data for SBIN with expiry on 30 Jan 2020

$ jdata derivatives -s SBIN -f 2020-01-01 -t 2020-01-30 -e 2020-01-30 -i FUTSTK -o file_name.csv

Download index futures data for NIFTY with expiry on 30 Jan 2020

$ jdata derivatives -s NIFTY -f 2020-01-01 -t 2020-01-30 -e 2020-01-30 -i FUTIDX -o file_name.csv

Download stock Call options data for SBIN with expiry on 30 Jan 2020 and at strike price of 300

$ jdata derivatives -s SBIN -f 2020-01-01 -t 2020-01-30 -e 2020-01-30 -i OPTSTK -p 330 --ce -o file_name.csv

Download index Put options data for “NIFTY” with expiry on 23 Jan 2020 and at strike price of 11000

$ jdata derivatives -s NIFTY -f 2020-01-01 -t 2020-01-23 -e 2020-01-23 -i OPTIDX -p 11000 --pe -o file_name.csv