59 lines
2.6 KiB
Bash
Executable File
59 lines
2.6 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
|
|
|
|
# 2024-05-27: DEPRECATED - THIS FILE IS GOING TO BE REOMVED:
|
|
# This doesn't seem to be creating smaller responses anymore, therefore this method isn't used anymore anywhere...
|
|
|
|
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
|
|
msg="Querying for Symbol: _${symbol}_ query string: _${queryString}&range=${range}_"
|
|
systemd-cat -t "`basename $0` $1" -p debug <<< ${msg}
|
|
|
|
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}&range=${range}" \
|
|
-H "Authorization: Bearer $(<access_token.dat)" > temp/optionChain.${symbol}.${range}.tmp.$$.json
|
|
|
|
jqQuery=".symbol"
|
|
symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.${range}.tmp.$$.json)
|
|
if [[ "${symbol}" != "${symbolCheck}" ]]; then
|
|
errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.${range}.tmp.$$.json_"
|
|
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
|
|
>&2 echo "${errMsg}"
|
|
exit 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}.json
|
|
pbzip2 -9 -c < temp/optionChain.${symbol}.tmp.$$.json > "${outDir}/${optionChainFileName}.bz2"
|
|
|
|
rm temp/*$$*
|