Skip to content

shikharka/stocks-app

Repository files navigation

Fetch Stock Quotes

Task

Build an API for fetching current quotes for NSE/BSE exhchange. This node app scrapes quotes from Yahoo Finance using cheerios module.

Prerequisite

nodejs

Quick Start

# Install dependencies
npm install

# Start Server
npm start

Following values can be configured before starting the app

  • port: port on which the app will run.
  • connectionURL: MongoDB server ULR
  • databaseName: Database name for collections to be stored in.
  • maxTry: Maximum number of times to retry fetch quotes if it fails first time.
  • timeout: Timeout for each fetch call.
{
    "port": 8080,
    "connectionURL": "mongodb://127.0.0.1:27017",
    "databaseName": "historicalQuotes",
    "maxTry": 10,
    "timeout": 1200000
}

Description:

This Node Application creates an Express server hosting two APIs. It scrapes data from in.finance.yahoo.com/ using cheerio module.

Other Node modules used:

  • axios: HTTP request
  • mongoose: MongoDB Operations
  • progress: Progress bar (/writeQuotes)
  • winston: Logging. Outputs to ./logs

/getQuote

This API gets Current Day quotes of passed stock symbol and exchange(NSE/BSE).

GET /getQuote?exchange=<exchange>&symbol=<stock-symbol>

Example Request

curl --location --request GET 'http://localhost:8080/getQuote?exchange=NSE&symbol=NTPC'

Example Response

{
  "close": 122.6,
  "prevClose": 123.85,
  "open": 123.4,
  "volume": 334,
  "avgVolume3Months": 7913068,
  "marketCap": 1.214,
  "monthly5Years": 0.31,
  "peRatio": 8.87,
  "epsRatio": 13.82
  }

Server Side Console

/getQuote Output

/writeQuotes

This API loops over equitiesList.json for Stock Symbols and save close quote and volume quote for each stock for current date and give exchange(NSE/BSE) in a MongoDB database. It creates a collection each for "close" and "volume". It can be used by admin users to create historical data set.

POST writeQuotes?exchange=<exchange>

Example Request

curl --location --request POST 'http://localhost:8080/writeQuotes?exchange=NSE'

Example Response

Insert Complete

Server Side Console

/writeQuotes Output

MongoDB Collections

closes(Robo3T Screenshot)

closes Collection

volumes(Robo3T Screenshot)

volumes Collection