Created
February 9, 2023 11:30
-
-
Save rhnvrm/87d0031d7e3d4a4f0fcda380db121c5a to your computer and use it in GitHub Desktop.
Utility for querying and manipulating Zerodha instruments file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# instq.sh | |
# Utility for querying and manipulating Zerodha instruments file | |
# Usage | |
# ----- | |
# | |
# Requirements: | |
# 1. Install jq | |
# sudo apt-get install jq | |
# | |
# Modes: | |
# search_ts = search the instruments file and retrieve instrument IDs | |
# search for INFY for today | |
# eg: ./instq.sh search_ts INFY | |
# can also be used to search for a specific date | |
# eg: ./instq.sh -d 2023-02-08 search_ts INFY | |
# | |
# ----- | |
# fail on err | |
set -e | |
# constants | |
INSTRUMENTS_URL="https://api.kite.trade/instruments.json" | |
INSTRUMENTS_ARCHIVE_URL="https://kitestatic.zerodha.com/static/instruments/" | |
# check requirements | |
# jq | |
if ! command -v jq &> /dev/null | |
then | |
echo "jq could not be found" | |
exit | |
fi | |
# check if mode name was there | |
if [ "$1" = "" ]; then | |
echo "You must mention the mode name as an argument" | |
echo "available modes:" | |
echo "- search_ts" | |
exit | |
fi | |
# check for flags | |
while getopts 'd:' OPTION; do | |
case "$OPTION" in | |
d) | |
date="$OPTARG" | |
;; | |
esac | |
done | |
# shift the arguments | |
shift $((OPTIND-1)) | |
# variables | |
mode=$1 | |
if [ $mode = "search_ts" ]; then | |
if [ "$2" = "" ]; then | |
echo "no search query provided" | |
echo "eg: ./instq.sh search_ts INFY" | |
exit | |
fi | |
# search for specific ts | |
ts=$2 | |
# set default URL | |
URL=$INSTRUMENTS_URL | |
# check if date is provided, if yes, use the archive URL | |
if [ "$date" != "" ]; then | |
# check if date matches the format 2023-02-08 | |
if [[ ! $date =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then | |
echo "date ($date) must be in the format YYYY-MM-DD" | |
exit | |
fi | |
URL="$INSTRUMENTS_ARCHIVE_URL""instruments.""$date"".nest.json" | |
fi | |
# send the curl and grep the ts | |
curl -s --header 'Accept-Encoding: gzip' -o - $URL | gzip -cdf | grep "\"$ts\"" | jq -r .id | |
else | |
echo "unknown mode: $mode" | |
exit | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment