54 lines
2.3 KiB
Bash
Executable File
54 lines
2.3 KiB
Bash
Executable File
#!/bin/bash
|
|
#set -x
|
|
# This is for big chains, that cause the Schwab API to error out (like $XSP)
|
|
# I add the range parameter (ITM/NTM/OTM) so that I get less data per request
|
|
|
|
. helpers.sh
|
|
|
|
symbol=$1
|
|
outDir=data/$(date +%Y-%m-%d)
|
|
queryString="?symbol=${symbol}"
|
|
|
|
mkdir -p "${outDir}"
|
|
mkdir -p temp
|
|
|
|
#Handle additional commandline parameters
|
|
shift
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
queryString="${queryString}&$1=$2"
|
|
shift; shift
|
|
done
|
|
|
|
for range in ITM OTM NTM; do
|
|
log "Querying for Symbol: _${symbol}_ query string: _${queryString}&range=${range}_" debug
|
|
|
|
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}&range=${range}" \
|
|
-H "Authorization: Bearer $(./getNewAccessToken.sh)" > temp/optionChain.${symbol}.${range}.tmp.$$.json
|
|
|
|
jqQuery=".symbol"
|
|
symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.${range}.tmp.$$.json)
|
|
if [[ "${symbol}" != "${symbolCheck}" ]]; then
|
|
throw "ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.${range}.tmp.$$.json_" err 2
|
|
fi
|
|
|
|
optionChainPartFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.${range}.json
|
|
pbzip2 -9 -c < temp/optionChain.${symbol}.${range}.tmp.$$.json > "${outDir}/${optionChainPartFileName}.bz2"
|
|
done
|
|
|
|
###Paste together the parts, the NTM part, I don't think I need, but I will save it just in case..
|
|
# sum up the number of Contracts
|
|
|
|
ITMtempFile=temp/optionChain.${symbol}.ITM.tmp.$$.json
|
|
OTMtempFile=temp/optionChain.${symbol}.OTM.tmp.$$.json
|
|
jq <<< '{ "glued":true, "numberOfContracts":'$(( $(jq '.numberOfContracts' < ${ITMtempFile}) + $(jq '.numberOfContracts' < ${OTMtempFile}) ))' }' > temp/numberOfContracts.$$.tmp
|
|
jq '. | del(.numberOfContracts) | map_values(select(type!="object"))' < ${ITMtempFile} > temp/CommonDetail.$$.tmp
|
|
jq '. | map_values(select(type=="object"))' < ${ITMtempFile} > ${ITMtempFile}.objOnly
|
|
jq '. | map_values(select(type=="object"))' < ${OTMtempFile} > ${OTMtempFile}.objOnly
|
|
jq -s '.[0] * .[1] * .[2] * .[3]' temp/numberOfContracts.$$.tmp temp/CommonDetail.$$.tmp ${ITMtempFile}.objOnly ${OTMtempFile}.objOnly > temp/optionChain.${symbol}.tmp.$$.json
|
|
|
|
optionChainFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.stitched.json
|
|
pbzip2 -9 -c < temp/optionChain.${symbol}.tmp.$$.json > "${outDir}/${optionChainFileName}.bz2"
|
|
|
|
rm temp/*$$*
|