Files
OptionRecorderSchwab/recordOptionChainSingleSymbolSpecialRun.sh

54 lines
2.3 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
2024-08-27 12:16:01 -05:00
. helpers.sh
2024-05-09 15:29:45 -05:00
symbol=$1
outDir=data/$(date +%Y-%m-%d)
queryString="?symbol=${symbol}"
2024-05-09 15:29:45 -05:00
mkdir -p "${outDir}"
mkdir -p temp
#Handle additional commandline parameters
shift
while [[ $# -gt 0 ]]; do
queryString="${queryString}&$1=$2"
shift; shift
done
2024-05-09 15:29:45 -05:00
for range in ITM OTM NTM; do
2024-08-27 12:16:01 -05:00
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}" \
2024-05-09 15:29:45 -05:00
-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
2024-08-27 12:16:01 -05:00
throw "ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.${range}.tmp.$$.json_" err 2
2024-05-09 15:29:45 -05:00
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
2024-05-09 15:29:45 -05:00
pbzip2 -9 -c < temp/optionChain.${symbol}.tmp.$$.json > "${outDir}/${optionChainFileName}.bz2"
rm temp/*$$*