Add caching to market hour and cater for (long) weekends in the 1DTE trade automation

This commit is contained in:
2024-08-30 15:39:18 -05:00
parent 51b4d17cd7
commit e0be7c1889
3 changed files with 14 additions and 9 deletions

View File

@@ -23,6 +23,13 @@ if [[ -z "${markets}" ]]; then
markets="equity&markets=option&markets=bond&markets=future&markets=forex"
fi
cacheFileName=temp/marketHours.`date +%Y%m%d`.${markets//&markets=/_}.tmp
if [[ -f ${cacheFileName} ]]; then
jq < ${cacheFileName}
exit 0
fi
queryString="?markets=${markets}"
while [[ $# -gt 0 ]]; do
@@ -33,12 +40,13 @@ done
log "Querying for markets: _${markets}_ query string: _${queryString}_" debug
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/markets${queryString}" \
-H "Authorization: Bearer $(<access_token.dat)" > temp/markets.tmp.$$.json
-H "Authorization: Bearer $(<access_token.dat)" > ${cacheFileName}
marketsCheck=$(jq -e -r '.. | .isOpen?' temp/markets.tmp.$$.json | grep -v null | wc -l)
marketsCheck=$(jq -e -r '.. | .isOpen?' ${cacheFileName} | grep -v null | wc -l)
if [[ ${marketsCheck} -eq 0 ]]; then
errMsg="ERROR: THE RESPONSE IS INCORRECT! markets: _${markets}_ Check: _${marketsCheck}_ check file _temp/markets.tmp.$$.json_"
errMsg="ERROR: THE RESPONSE IS INCORRECT! markets: _${markets}_ Check: _${marketsCheck}_ check file _${cacheFileName}_"
rm ${cacheFileName}
if [[ "${ErrorLogLevel}" == "err" ]]; then
throw "${errMsg}" ${ErrorLogLevel} 2
else
@@ -46,8 +54,7 @@ if [[ ${marketsCheck} -eq 0 ]]; then
exit 2
fi
else
jq < temp/markets.tmp.$$.json
rm temp/markets.tmp.$$.json
jq < ${cacheFileName}
fi