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

@@ -13,7 +13,7 @@ fi
inputFile="$1" inputFile="$1"
if [[ "" == "$inputFile" ]]; then if [[ "" == "$inputFile" ]]; then
inputFile=temp/orderInputFile.$$.json inputFile=temp/orderInputFile.$$.json
./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d" -d "+1 days") toDate $(date "+%Y-%m-%d" -d "+1 days") > "${inputFile}" ./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d" -d "+1 days") toDate $(date "+%Y-%m-%d" -d "+4 days") > "${inputFile}"
fi fi
if [[ ! -f "${inputFile}" ]]; then if [[ ! -f "${inputFile}" ]]; then
throw "ERROR: Input file _${inputFile}_ does not exist." err 1 throw "ERROR: Input file _${inputFile}_ does not exist." err 1
@@ -23,9 +23,9 @@ dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1)
log "Date-Key: _${dateKey}_" debug log "Date-Key: _${dateKey}_" debug
underlyingPrice=$(jq ".underlyingPrice" "${inputFile}") underlyingPrice=$(jq ".underlyingPrice" "${inputFile}")
log "Underlying Price: _${underlyingPrice}_" debug
atmStrike=$(jq -r '.callExpDateMap[]|keys[]' "${inputFile}" | sort -n | awk -e '{lastStrike=$1; if ($1 > '${underlyingPrice}') {print $1; exit } }') atmStrike=$(jq -r '.callExpDateMap[]|keys[]' "${inputFile}" | sort -n | awk -e '{lastStrike=$1; if ($1 > '${underlyingPrice}') {print $1; exit } }')
optionRoot=SPXW #I only want the PM expiry (SPX without the W is AM expiry) optionRoot=SPXW #I only want the PM expiry (SPX without the W is AM expiry)
jq '.putExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) |.[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice=='${atmStrike}') ' "${inputFile}" | tail -n 8 > temp/shortLegP.json jq '.putExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) |.[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice=='${atmStrike}') ' "${inputFile}" | tail -n 8 > temp/shortLegP.json

View File

@@ -23,6 +23,13 @@ if [[ -z "${markets}" ]]; then
markets="equity&markets=option&markets=bond&markets=future&markets=forex" markets="equity&markets=option&markets=bond&markets=future&markets=forex"
fi fi
cacheFileName=temp/marketHours.`date +%Y%m%d`.${markets//&markets=/_}.tmp
if [[ -f ${cacheFileName} ]]; then
jq < ${cacheFileName}
exit 0
fi
queryString="?markets=${markets}" queryString="?markets=${markets}"
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
@@ -33,12 +40,13 @@ done
log "Querying for markets: _${markets}_ query string: _${queryString}_" debug log "Querying for markets: _${markets}_ query string: _${queryString}_" debug
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/markets${queryString}" \ 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 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 if [[ "${ErrorLogLevel}" == "err" ]]; then
throw "${errMsg}" ${ErrorLogLevel} 2 throw "${errMsg}" ${ErrorLogLevel} 2
else else
@@ -46,8 +54,7 @@ if [[ ${marketsCheck} -eq 0 ]]; then
exit 2 exit 2
fi fi
else else
jq < temp/markets.tmp.$$.json jq < ${cacheFileName}
rm temp/markets.tmp.$$.json
fi fi

View File

@@ -21,6 +21,4 @@ echo $fromEnteredTime
./getNewAccessToken.sh ./getNewAccessToken.sh
curl -s -X GET "https://api.schwabapi.com/trader/v1/orders?fromEnteredTime=${fromEnteredTime}&toEnteredTime=${toEnteredTime}" -H "Authorization: Bearer $(<access_token.dat)" > ${tempFile} curl -s -X GET "https://api.schwabapi.com/trader/v1/orders?fromEnteredTime=${fromEnteredTime}&toEnteredTime=${toEnteredTime}" -H "Authorization: Bearer $(<access_token.dat)" > ${tempFile}
#jq -r 'map(.enteredTime)[]' < ${tempFile}
#jq -r '.[].orderLegCollection[].instrument.symbol' < ${tempFile}
jq -r '.[] | [.orderId | tostring][0] + "," + .status + "," + .orderLegCollection[].instrument.symbol' < ${tempFile} jq -r '.[] | [.orderId | tostring][0] + "," + .status + "," + .orderLegCollection[].instrument.symbol' < ${tempFile}