Files
OptionRecorderSchwab/recordOptionChainSingleSymbolSpecialRun.sh

44 lines
2.1 KiB
Bash
Raw Normal View History

2024-05-09 15:29:45 -05:00
#!/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
#
symbol=$1
outDir=data/$(date +%Y-%m-%d)
mkdir -p "${outDir}"
mkdir -p temp
for range in ITM OTM NTM; do
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains?symbol=${symbol}&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}
2024-05-09 15:29:45 -05:00
>&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/*$$*