Add caching to market hour and cater for (long) weekends in the 1DTE trade automation
This commit is contained in:
@@ -13,7 +13,7 @@ fi
|
||||
inputFile="$1"
|
||||
if [[ "" == "$inputFile" ]]; then
|
||||
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
|
||||
if [[ ! -f "${inputFile}" ]]; then
|
||||
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
|
||||
|
||||
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 } }')
|
||||
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,4 @@ echo $fromEnteredTime
|
||||
./getNewAccessToken.sh
|
||||
|
||||
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}
|
||||
|
||||
Reference in New Issue
Block a user