From a6b7fc7afe7851b93ff4eddd240ea7e54ab43d6e Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Tue, 27 Aug 2024 12:16:01 -0500 Subject: [PATCH] Cleanup - centralize the log handling --- autoTradeQQQLongCall.sh | 34 +++++++----------- autoTradeSPXPutCreditSpread.sh | 34 +++++------------- autoTradeSPX_IB_0DTE_25pctProfitTaker.sh | 41 ++++++---------------- autoTradeSPX_IB_1DTE.sh | 41 ++++++---------------- autoTradeSPYLongCall.sh | 39 +++++++------------- getMarketHours.sh | 10 +++--- getNewAccessToken.sh | 5 +-- getNewRefreshToken.sh | 10 ++---- getOptionChain.sh | 10 +++--- helpers.sh | 32 +++++++++++++++++ record90dayOut.sh | 10 +++--- recordAllSymbolsSingleRun.sh | 13 +++---- recordOptionChainSingleSymbolSingleRun.sh | 10 +++--- recordOptionChainSingleSymbolSpecialRun.sh | 10 +++--- recordQuoteSingleSymbolSingleRun.sh | 5 ++- 15 files changed, 124 insertions(+), 180 deletions(-) create mode 100644 helpers.sh diff --git a/autoTradeQQQLongCall.sh b/autoTradeQQQLongCall.sh index cb3ffac..dd1f265 100755 --- a/autoTradeQQQLongCall.sh +++ b/autoTradeQQQLongCall.sh @@ -2,6 +2,8 @@ set -x IFS=$'\0' +. helpers.sh + daysOutMin=90 daysOutMax=91 deltaMin=0.35 @@ -10,24 +12,20 @@ deltaMax=0.40 inputFile="$1" if [[ "" == "$inputFile" ]]; then latestFile="$(ls data/`date +%Y-%m-%d`/*QQQ* | sort | tail -n 1)" - systemd-cat -t "`basename $0` $1" -p debug <<< "Latest File: _${latestFile}_" + log "Latest File: _${latestFile}_" debug inputFile=temp/orderInputFile.json bzip2 -dc < "${latestFile}" > "${inputFile}" fi if [[ ! -f "${inputFile}" ]]; then - errMsg="ERROR: Input file _${inputFile}_ does not exist." - systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "ERROR: Input file _${inputFile}_ does not exist." err 1 fi dateKey=$(jq -r '.callExpDateMap | keys | .[]' temp/orderInputFile.json | sort -r -t ":" +2 | awk -F ":" -e '{theDay=$2; if (theDay < '${daysOutMin}') { print lastDay; exit; } else { lastDay=$0; } }') -systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" +log "Date-Key: _${dateKey}_" debug daysOut=$(cut -d : -f 2 <<< ${dateKey}) if [[ ${daysOut} -gt ${daysOutMax} ]]; then - systemd-cat -t "`basename $0` $1" -p info <<< "This option is too far in the future. _${daysOut}_ is bigger than the max days in the future is _${daysOutMax}_. Exiting." - exit + throw "This option is too far in the future. _${daysOut}_ is bigger than the max days in the future is _${daysOutMax}_. Exiting." info fi #Looking for leg, between the specified deltas @@ -36,29 +34,21 @@ fi jq '.callExpDateMap."'${dateKey}'"[]|map({symbol, delta, strikePrice,bid,ask}) | .[] | select(.delta >= '${deltaMin}' and .delta <= '${deltaMax}')' "${inputFile}" | tail -n 7 > temp/longLeg.json numLinesInLeg=$(wc -l temp/longLeg.json | cut -c1) if [[ 7 -ne ${numLinesInLeg} ]]; then - errMsg="No suitable long leg found - exiting." - systemd-cat -t "`basename $0` $1" -p warning <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "No suitable long leg found - exiting." warning 2 fi longStrike=$(jq -r '.strikePrice' < temp/longLeg.json) longBid=$(jq -r '.bid' < temp/longLeg.json) longAsk=$(jq -r '.ask' < temp/longLeg.json) longSymbol=$(jq -r '.symbol' < temp/longLeg.json) -msg="Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" -#Check if this order has already been submitted. +log "Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" info +#Check if this order has already been submitted. ./getOrders.sh | grep '\(WORKING\|FILLED\),'${longSymbol} > temp/existingOrders.$$ if [[ $(wc -l < temp/existingOrders.$$) -eq 0 ]];then - msg="Not Yet traded, continuing" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} + log "Not Yet traded, continuing" info else - msg="Already traded, exiting" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - exit 0 + throw "Already traded, exiting" info fi priceOffset=0.0 @@ -87,7 +77,7 @@ orderJson=$(jq -c <<-EOM EOM ) -systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" +log "Order: _${orderJson}_" debug if [[ "${noconfirm}" != "true" ]]; then echo Enter to continue, Ctrl+C to cancel _${noconfirm}_ diff --git a/autoTradeSPXPutCreditSpread.sh b/autoTradeSPXPutCreditSpread.sh index a4a2a57..e3f24d5 100755 --- a/autoTradeSPXPutCreditSpread.sh +++ b/autoTradeSPXPutCreditSpread.sh @@ -3,14 +3,11 @@ set -e IFS=$'\0' myPID=$$ - +. helpers.sh marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false) if [[ ${marketOpen} == false ]]; then - errMsg="The market is closed, exiting." - systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg} - >&2 echo "${errMsg}" - exit 2 + throw "The market is closed, exiting." info 2 fi inputFile="$1" @@ -19,15 +16,11 @@ if [[ "" == "$inputFile" ]]; then ./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d" -d "+40 days") toDate $(date "+%Y-%m-%d" -d "+46 days") > "${inputFile}" fi if [[ ! -f "${inputFile}" ]]; then - errMsg="ERROR: Input file _${inputFile}_ does not exist." - systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "ERROR: Input file _${inputFile}_ does not exist." err 1 fi dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1) -systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" -echo "Date-Key: _${dateKey}_" +log "Date-Key: _${dateKey}_" debug jq '.putExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) |.[] | select(.delta >= -0.30 and .delta <= -0.25) ' "${inputFile}" | tail -n 8 > temp/shortLeg.json shortStrike=$(jq -r '.strikePrice' < temp/shortLeg.json) @@ -35,31 +28,22 @@ shortBid=$(jq -r '.bid' < temp/shortLeg.json) shortAsk=$(jq -r '.ask' < temp/shortLeg.json) shortSymbol=$(jq -r '.symbol' < temp/shortLeg.json) shortOptionRoot=$(jq -r '.optionRoot' < temp/shortLeg.json) -msg="Short Symbol: _${shortSymbol}_ Strike: _${shortStrike}_ Bid: _${shortBid}_ Ask: _${shortAsk}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Short Symbol: _${shortSymbol}_ Strike: _${shortStrike}_ Bid: _${shortBid}_ Ask: _${shortAsk}_" info jq '.putExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) | .[] | select(.optionRoot == "'${shortOptionRoot}'" and .strikePrice >= '${shortStrike}'-50 and .strikePrice < '${shortStrike}')' "${inputFile}" | head -n 8 > temp/longLeg.json longStrike=$(jq -r '.strikePrice' < temp/longLeg.json) longBid=$(jq -r '.bid' < temp/longLeg.json) longAsk=$(jq -r '.ask' < temp/longLeg.json) longSymbol=$(jq -r '.symbol' < temp/longLeg.json) -msg="Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" info #Check if this order has already been submitted. ./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbol:0:12} || true) > temp/existingOrders.${myPID} if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then - msg="Not Yet traded, continuing" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - echo ${msg} + log "Not Yet traded, continuing" info else - msg="Already traded, exiting" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - echo ${msg} - exit 0 + throw "Already traded, exiting" info fi priceOffset=0.1 # try to get $10 more in premium... @@ -95,7 +79,7 @@ orderJson=$(jq -c <<-EOM EOM ) -systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" +log "Order: _${orderJson}_" debug if [[ "${noconfirm}" != "true" ]]; then echo Enter to continue, Ctrl+C to cancel _${noconfirm}_ diff --git a/autoTradeSPX_IB_0DTE_25pctProfitTaker.sh b/autoTradeSPX_IB_0DTE_25pctProfitTaker.sh index 7cf03ae..ddda3f0 100755 --- a/autoTradeSPX_IB_0DTE_25pctProfitTaker.sh +++ b/autoTradeSPX_IB_0DTE_25pctProfitTaker.sh @@ -3,13 +3,11 @@ set -x set -e IFS=$'\0' myPID=$$ +. helpers.sh marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false) if [[ ${marketOpen} == false ]]; then - errMsg="The market is closed, exiting." - systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg} - >&2 echo "${errMsg}" - exit 2 + throw "The market is closed, exiting." info 2 fi inputFile="$1" @@ -18,15 +16,11 @@ if [[ "" == "$inputFile" ]]; then ./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d") toDate $(date "+%Y-%m-%d") > "${inputFile}" fi if [[ ! -f "${inputFile}" ]]; then - errMsg="ERROR: Input file _${inputFile}_ does not exist." - systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "ERROR: Input file _${inputFile}_ does not exist." err 1 fi dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1) -systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" -echo "Date-Key: _${dateKey}_" +log "Date-Key: _${dateKey}_" debug underlyingPrice=$(jq ".underlyingPrice" "${inputFile}") atmStrike=$(jq -r '.callExpDateMap[]|keys[]' "${inputFile}" | sort -n | awk -e '{lastStrike=$1; if ($1 > '${underlyingPrice}') {print $1; exit } }') @@ -37,27 +31,21 @@ shortBidP=$(jq -r '.bid' < temp/shortLegP.json) shortAskP=$(jq -r '.ask' < temp/shortLegP.json) shortSymbolP=$(jq -r '.symbol' < temp/shortLegP.json) optionRoot=$(jq -r '.optionRoot' < temp/shortLegP.json) -msg="Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_" info jq '.callExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) |.[] | select(.strikePrice=='${atmStrike}') ' "${inputFile}" | tail -n 8 > temp/shortLegC.json shortStrikeC=$(jq -r '.strikePrice' < temp/shortLegC.json) shortBidC=$(jq -r '.bid' < temp/shortLegC.json) shortAskC=$(jq -r '.ask' < temp/shortLegC.json) shortSymbolC=$(jq -r '.symbol' < temp/shortLegC.json) -msg="Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" info jq '.putExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) | .[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice >= '${shortStrikeP}'-10 and .strikePrice < '${shortStrikeP}')' "${inputFile}" | head -n 8 > temp/longLegP.json longStrikeP=$(jq -r '.strikePrice' < temp/longLegP.json) longBidP=$(jq -r '.bid' < temp/longLegP.json) longAskP=$(jq -r '.ask' < temp/longLegP.json) longSymbolP=$(jq -r '.symbol' < temp/longLegP.json) -msg="Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" info #Check if this order has already been submitted. jq '.callExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) | .[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice >= '${shortStrikeC}'+10 and .strikePrice > '${shortStrikeP}')' "${inputFile}" | head -n 8 > temp/longLegC.json @@ -65,22 +53,15 @@ longStrikeC=$(jq -r '.strikePrice' < temp/longLegC.json) longBidC=$(jq -r '.bid' < temp/longLegC.json) longAskC=$(jq -r '.ask' < temp/longLegC.json) longSymbolC=$(jq -r '.symbol' < temp/longLegC.json) -msg="Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" info #Check if this order has already been submitted. ./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbolC:0:12} || true) > temp/existingOrders.${myPID} if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then - msg="Not Yet traded, continuing" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - echo ${msg} + log "Not Yet traded, continuing" info else - msg="Already traded, exiting" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - echo ${msg} -# exit 0 + throw "Already traded, exiting" info fi priceOffset=1.1 # try to get $10 more in premium... @@ -185,7 +166,7 @@ orderJson=$(jq -c <<-EOM EOM ) -systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" +log "Order: _${orderJson}_" debug if [[ "${noconfirm}" != "true" ]]; then echo ${orderJSON} | jq diff --git a/autoTradeSPX_IB_1DTE.sh b/autoTradeSPX_IB_1DTE.sh index f953877..c7f0ca0 100755 --- a/autoTradeSPX_IB_1DTE.sh +++ b/autoTradeSPX_IB_1DTE.sh @@ -3,13 +3,11 @@ set -x set -e IFS=$'\0' myPID=$$ +. helpers.sh marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false) if [[ ${marketOpen} == false ]]; then - errMsg="The market is closed, exiting." - systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg} - >&2 echo "${errMsg}" - exit 2 + throw "The market is closed, exiting." info 2 fi inputFile="$1" @@ -18,15 +16,11 @@ if [[ "" == "$inputFile" ]]; then ./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d" -d "+1 days") toDate $(date "+%Y-%m-%d" -d "+1 days") > "${inputFile}" fi if [[ ! -f "${inputFile}" ]]; then - errMsg="ERROR: Input file _${inputFile}_ does not exist." - systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "ERROR: Input file _${inputFile}_ does not exist." err 1 fi dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1) -systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" -echo "Date-Key: _${dateKey}_" +log "Date-Key: _${dateKey}_" debug underlyingPrice=$(jq ".underlyingPrice" "${inputFile}") atmStrike=$(jq -r '.callExpDateMap[]|keys[]' "${inputFile}" | sort -n | awk -e '{lastStrike=$1; if ($1 > '${underlyingPrice}') {print $1; exit } }') @@ -39,27 +33,21 @@ shortStrikeP=$(jq -r '.strikePrice' < temp/shortLegP.json) shortBidP=$(jq -r '.bid' < temp/shortLegP.json) shortAskP=$(jq -r '.ask' < temp/shortLegP.json) shortSymbolP=$(jq -r '.symbol' < temp/shortLegP.json) -msg="Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_}" info jq '.callExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) |.[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice=='${atmStrike}') ' "${inputFile}" | tail -n 8 > temp/shortLegC.json shortStrikeC=$(jq -r '.strikePrice' < temp/shortLegC.json) shortBidC=$(jq -r '.bid' < temp/shortLegC.json) shortAskC=$(jq -r '.ask' < temp/shortLegC.json) shortSymbolC=$(jq -r '.symbol' < temp/shortLegC.json) -msg="Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" info jq '.putExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) | .[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice >= '${shortStrikeP}'-5 and .strikePrice < '${shortStrikeP}')' "${inputFile}" | head -n 8 > temp/longLegP.json longStrikeP=$(jq -r '.strikePrice' < temp/longLegP.json) longBidP=$(jq -r '.bid' < temp/longLegP.json) longAskP=$(jq -r '.ask' < temp/longLegP.json) longSymbolP=$(jq -r '.symbol' < temp/longLegP.json) -msg="Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" info #Check if this order has already been submitted. jq '.callExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) | .[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice >= '${shortStrikeC}'+5 and .strikePrice > '${shortStrikeP}')' "${inputFile}" | head -n 8 > temp/longLegC.json @@ -67,22 +55,15 @@ longStrikeC=$(jq -r '.strikePrice' < temp/longLegC.json) longBidC=$(jq -r '.bid' < temp/longLegC.json) longAskC=$(jq -r '.ask' < temp/longLegC.json) longSymbolC=$(jq -r '.symbol' < temp/longLegC.json) -msg="Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" +log "Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" info #Check if this order has already been submitted. ./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbolC:0:12} || true) > temp/existingOrders.${myPID} if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then - msg="Not Yet traded, continuing" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - echo ${msg} + log "Not Yet traded, continuing" info else - msg="Already traded, exiting" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - echo ${msg} -# exit 0 + throw "Already traded, exiting" info fi priceOffset=0.00 # try to get a little better price @@ -140,7 +121,7 @@ orderJson=$(jq -c <<-EOM EOM ) -systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" +log "Order: _${orderJson}_" debug if [[ "${noconfirm}" != "true" ]]; then echo ${orderJson} | jq diff --git a/autoTradeSPYLongCall.sh b/autoTradeSPYLongCall.sh index 07105e5..1b9db74 100755 --- a/autoTradeSPYLongCall.sh +++ b/autoTradeSPYLongCall.sh @@ -2,6 +2,8 @@ set -x IFS=$'\0' +. helpers.sh + daysOutMin=90 daysOutMax=95 deltaMin=0.35 @@ -9,33 +11,26 @@ deltaMax=0.40 marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false) if [[ ${marketOpen} == false ]]; then - errMsg="The market is closed, exiting." - systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg} - >&2 echo "${errMsg}" - exit 2 + throw "The market is closed, exiting." info 2 fi inputFile="$1" if [[ "" == "$inputFile" ]]; then latestFile="$(ls data/`date +%Y-%m-%d`/*SPY* | sort | tail -n 1)" - systemd-cat -t "`basename $0` $1" -p debug <<< "Latest File: _${latestFile}_" + log "Latest File: _${latestFile}_" debug inputFile=temp/orderInputFile.json bzip2 -dc < "${latestFile}" > "${inputFile}" fi if [[ ! -f "${inputFile}" ]]; then - errMsg="ERROR: Input file _${inputFile}_ does not exist." - systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "ERROR: Input file _${inputFile}_ does not exist." err 1 fi dateKey=$(jq -r '.callExpDateMap | keys | .[]' "${inputFile}" | sort -r -t ":" +2 | awk -F ":" -e '{theDay=$2; if (theDay < '${daysOutMin}') { print lastDay; exit; } else { lastDay=$0; } }') -systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" +log "Date-Key: _${dateKey}_" debug daysOut=$(cut -d : -f 2 <<< ${dateKey}) if [[ ${daysOut} -gt ${daysOutMax} ]]; then - systemd-cat -t "`basename $0` $1" -p info <<< "This option is too far in the future. _${daysOut}_ is bigger than the max days in the future is _${daysOutMax}_. Exiting." - exit + throw "This option is too far in the future. _${daysOut}_ is bigger than the max days in the future is _${daysOutMax}_. Exiting." info fi #Looking for leg, between the specified deltas @@ -44,29 +39,21 @@ fi jq '.callExpDateMap."'${dateKey}'"[]|map({symbol, delta, strikePrice,bid,ask}) | .[] | select(.delta >= '${deltaMin}' and .delta <= '${deltaMax}')' "${inputFile}" | tail -n 7 > temp/longLeg.json numLinesInLeg=$(wc -l temp/longLeg.json | cut -c1) if [[ 7 -ne ${numLinesInLeg} ]]; then - errMsg="No suitable long leg found - exiting." - systemd-cat -t "`basename $0` $1" -p warning <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "No suitable long leg found - exiting." warning 1 fi longStrike=$(jq -r '.strikePrice' < temp/longLeg.json) longBid=$(jq -r '.bid' < temp/longLeg.json) longAsk=$(jq -r '.ask' < temp/longLeg.json) longSymbol=$(jq -r '.symbol' < temp/longLeg.json) -msg="Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" -systemd-cat -t "`basename $0` $1" -p info <<< ${msg} -echo "${msg}" -#Check if this order has already been submitted. +log "Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" info +#Check if this order has already been submitted. ./getOrders.sh | grep '\(WORKING\|FILLED\),'${longSymbol} > temp/existingOrders.$$ if [[ $(wc -l < temp/existingOrders.$$) -eq 0 ]];then - msg="Not Yet traded, continuing" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} + log "Not Yet traded, continuing" info else - msg="Already traded, exiting" - systemd-cat -t "`basename $0` $1" -p info <<< ${msg} - exit 0 + throw "Already traded, exiting" info fi priceOffset=0.0 @@ -95,7 +82,7 @@ orderJson=$(jq -c <<-EOM EOM ) -systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" +log "Order: _${orderJson}_" debug if [[ "${noconfirm}" != "true" ]]; then echo Enter to continue, Ctrl+C to cancel _${noconfirm}_ diff --git a/getMarketHours.sh b/getMarketHours.sh index 4735f6f..e1c6482 100755 --- a/getMarketHours.sh +++ b/getMarketHours.sh @@ -1,6 +1,7 @@ #!/bin/bash #set -x set -e +. helpers.sh mkdir -p temp ErrorLogLevel=err @@ -29,8 +30,7 @@ while [[ $# -gt 0 ]]; do shift; shift done -msg="Querying for markets: _${markets}_ query string: _${queryString}_" -systemd-cat -t "`basename $0` $1" -p debug <<< ${msg} +log "Querying for markets: _${markets}_ query string: _${queryString}_" debug curl -s -X GET "https://api.schwabapi.com/marketdata/v1/markets${queryString}" \ -H "Authorization: Bearer $( temp/markets.tmp.$$.json @@ -39,11 +39,11 @@ curl -s -X GET "https://api.schwabapi.com/marketdata/v1/markets${queryString}" \ marketsCheck=$(jq -e -r '.. | .isOpen?' temp/markets.tmp.$$.json | 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_" - systemd-cat -t "`basename $0` $1" -p ${ErrorLogLevel} <<< ${errMsg} if [[ "${ErrorLogLevel}" == "err" ]]; then - >&2 echo "${errMsg}" + throw ${errMsg} ${ErrorLogLevel} + else + log ${errMsg} ${ErrorLogLevel} fi - exit 2 else jq < temp/markets.tmp.$$.json rm temp/markets.tmp.$$.json diff --git a/getNewAccessToken.sh b/getNewAccessToken.sh index 951bbc8..6113ff7 100755 --- a/getNewAccessToken.sh +++ b/getNewAccessToken.sh @@ -9,11 +9,8 @@ curl -s -X POST https://api.schwabapi.com/v1/oauth/token \ -d "grant_type=refresh_token&refresh_token=$( temp/accessTokenReqResp.$$.json jq -e -r .access_token temp/accessTokenReqResp.$$.json > access_token.dat if [[ $? -ne 0 ]]; then - errMsg="ERROR: GETTING THE ACCESS TOKEN FAILED! $(&2 echo "${errMsg}" curl -d "" -X POST "https://deadswitch.kawomi.com/api/v1?topic=trade.schwab.error.getNewAccessToken&secs=1&email=joe@kawomi.com" - exit 1 + throw "ERROR: GETTING THE ACCESS TOKEN FAILED! $( temp/refresh_token.$$.dat if [[ $? -eq 0 ]]; then - errMsg="New Refresh Token created." - systemd-cat -t "`basename $0`" -p info <<< ${errMsg} - echo "${errMsg}" + log "New Refresh Token created." info cp temp/refresh_token.$$.dat refresh_token.dat else - errMsg="Error getting new refresh token _$(&2 echo "${errMsg}" + throw "Error getting new refresh token _$( temp/optionChain.${symbol}.tmp.$$.json @@ -31,11 +31,11 @@ jqQuery=".symbol" symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.tmp.$$.json) if [[ "${symbol}" != "${symbolCheck}" ]]; then errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.tmp.$$.json_" - systemd-cat -t "`basename $0` $1" -p ${ErrorLogLevel} <<< ${errMsg} if [[ "${ErrorLogLevel}" == "err" ]]; then - >&2 echo "${errMsg}" + throw ${errMsg} ${ErrorLogLevel} 2 + else + log ${errMsg} ${ErrorLogLevel} fi - exit 2 else jq < temp/optionChain.${symbol}.tmp.$$.json rm temp/optionChain.${symbol}.tmp.$$.json diff --git a/helpers.sh b/helpers.sh new file mode 100644 index 0000000..f1a1535 --- /dev/null +++ b/helpers.sh @@ -0,0 +1,32 @@ +function log() { + logMsg="$1" + logLevel="$2" + + if [[ "${logLevel}" == "" ]]; then + logLevel=info + fi + + systemd-cat -t "`basename $0`" -p ${logLevel} <<< ${logMsg} + + if [[ "${NoConsoleLogging}" != true ]]; then + >&2 echo $(date "+%Y-%m-%d %H:%M:%S")" ${logLevel} ${logMsg}" + fi +} + +function throw() { + errMsg="$1" + errLevel="$2" + errCode="$3" + + if [[ "${errLevel}" == "" ]]; then + errLevel=err + fi + + log "${errMsg}" "${errLevel}" + + if [[ "${NoConsoleLogging}" != true ]]; then + >&2 echo $(date "+%Y-%m-%d %H:%M:%S")" ${errLevel} ${errMsg}" + fi + + exit ${errCode} +} diff --git a/record90dayOut.sh b/record90dayOut.sh index 8f46a1b..06b898a 100755 --- a/record90dayOut.sh +++ b/record90dayOut.sh @@ -1,5 +1,6 @@ #!/bin/bash #set -x +. helpers.sh IFS=$'\0' daysOutMin=90 @@ -9,19 +10,16 @@ inputFile="$1" latestFile="$1" if [[ "" == "$inputFile" ]]; then latestFile="$(ls data/`date +%Y-%m-%d`/*SPY* | sort | tail -n 1)" - systemd-cat -t "`basename $0` $1" -p debug <<< "Latest File: _${latestFile}_" + log "Latest File: _${latestFile}_" debug inputFile=temp/orderInputFile.json bzip2 -dc < "${latestFile}" > "${inputFile}" fi if [[ ! -f "${inputFile}" ]]; then - errMsg="ERROR: Input file _${inputFile}_ does not exist." - systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg} - >&2 echo "${errMsg}" - exit 1 + throw "ERROR: Input file _${inputFile}_ does not exist." err 1 fi dateKey=$(jq -r '.callExpDateMap | keys | .[]' temp/orderInputFile.json | sort -r -t ":" +2 | awk -F ":" -e '{theDay=$2; if (theDay < '${daysOutMin}') { print lastDay; exit; } else { lastDay=$0; } }') -systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" +log "Date-Key: _${dateKey}_" debug daysOut=$(cut -d : -f 2 <<< ${dateKey}) if [[ ${daysOut} -gt ${daysOutMax} ]]; then diff --git a/recordAllSymbolsSingleRun.sh b/recordAllSymbolsSingleRun.sh index 38afcc8..909ce41 100755 --- a/recordAllSymbolsSingleRun.sh +++ b/recordAllSymbolsSingleRun.sh @@ -1,12 +1,13 @@ #!/bin/bash #set -x -systemd-cat -t "`basename $0`" -p info <<< "Started" +. helpers.sh + +log Started ./getNewAccessToken.sh if [[ $? -ne 0 ]]; then - systemd-cat -t "`basename $0`" -p err <<< "Child process to get access token reported error. Exiting." - exit 1 + log "Child process to get access token reported error. Exiting." err 1 fi jq -r '.[].Symbol' allSymbols.json | sort | while read theSymbol; do @@ -14,7 +15,7 @@ jq -r '.[].Symbol' allSymbols.json | sort | while read theSymbol; do if [[ '$XSP' = ${theSymbol} ]]; then ./recordOptionChainSingleSymbolSingleRun.sh -ErrorAsWarning ${theSymbol} toDate $(date "+%Y-%m-%d" -d "+400 days") if [[ $? -ne 0 ]]; then - systemd-cat -t "`basename $0`" -p warning <<< "falling back to stitching for ${theSymbol}" + log "falling back to stitching for ${theSymbol}" warning ./recordOptionChainSingleSymbolSpecialRun.sh ${theSymbol} toDate $(date "+%Y-%m-%d" -d "+400 days") if [[ $? -ne 0 ]]; then touch temp/hadErrors.$$.tmp @@ -27,7 +28,7 @@ jq -r '.[].Symbol' allSymbols.json | sort | while read theSymbol; do #Default behavior - fall back to stitching on error ./recordOptionChainSingleSymbolSingleRun.sh -ErrorAsWarning ${theSymbol} if [[ $? -ne 0 ]]; then - systemd-cat -t "`basename $0`" -p warning <<< "falling back to stitching for ${theSymbol}" + log "falling back to stitching for ${theSymbol}" warning ./recordOptionChainSingleSymbolSpecialRun.sh ${theSymbol} if [[ $? -ne 0 ]]; then touch temp/hadErrors.$$.tmp @@ -40,4 +41,4 @@ if [[ -e temp/hadErrors.$$.tmp ]]; then curl -d "" -X POST "https://deadswitch.kawomi.com/api/v1?topic=trade.schwab.error.recordAllSymbolsSingleRun&secs=1&email=joe@kawomi.com" fi -systemd-cat -t "`basename $0`" -p info <<< "Finished" +log "Finished" diff --git a/recordOptionChainSingleSymbolSingleRun.sh b/recordOptionChainSingleSymbolSingleRun.sh index 2dbb46e..3b2fcc8 100755 --- a/recordOptionChainSingleSymbolSingleRun.sh +++ b/recordOptionChainSingleSymbolSingleRun.sh @@ -1,5 +1,6 @@ #!/bin/bash #set -x +. helpers.sh outDir=data/$(date +%Y-%m-%d) mkdir -p "${outDir}" mkdir -p temp @@ -23,8 +24,7 @@ while [[ $# -gt 0 ]]; do shift; shift done -msg="Querying for Symbol: _${symbol}_ query string: _${queryString}_" -systemd-cat -t "`basename $0` $1" -p debug <<< ${msg} +log "Querying for Symbol: _${symbol}_ query string: _${queryString}_" debug curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}" \ -H "Authorization: Bearer $( temp/optionChain.${symbol}.tmp.$$.json @@ -33,11 +33,11 @@ jqQuery=".symbol" symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.tmp.$$.json) if [[ "${symbol}" != "${symbolCheck}" ]]; then errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.tmp.$$.json_" - systemd-cat -t "`basename $0` $1" -p ${ErrorLogLevel} <<< ${errMsg} if [[ "${ErrorLogLevel}" == "err" ]]; then - >&2 echo "${errMsg}" + throw ${errMsg} ${ErrorLogLevel} 2 + else + log ${errMsg} ${ErrorLogLevel} fi - exit 2 else optionChainFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.json #echo writing to file "${outDir}/${optionChainFileName}" diff --git a/recordOptionChainSingleSymbolSpecialRun.sh b/recordOptionChainSingleSymbolSpecialRun.sh index 1702f05..2acba46 100755 --- a/recordOptionChainSingleSymbolSpecialRun.sh +++ b/recordOptionChainSingleSymbolSpecialRun.sh @@ -3,6 +3,8 @@ # 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 +. helpers.sh + symbol=$1 outDir=data/$(date +%Y-%m-%d) queryString="?symbol=${symbol}" @@ -19,8 +21,7 @@ while [[ $# -gt 0 ]]; do done for range in ITM OTM NTM; do - msg="Querying for Symbol: _${symbol}_ query string: _${queryString}&range=${range}_" - systemd-cat -t "`basename $0` $1" -p debug <<< ${msg} + 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}" \ -H "Authorization: Bearer $( temp/optionChain.${symbol}.${range}.tmp.$$.json @@ -28,10 +29,7 @@ for range in ITM OTM NTM; do 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} - >&2 echo "${errMsg}" - exit 2 + throw "ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.${range}.tmp.$$.json_" err 2 fi optionChainPartFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.${range}.json diff --git a/recordQuoteSingleSymbolSingleRun.sh b/recordQuoteSingleSymbolSingleRun.sh index 0cdd6d7..21608a6 100755 --- a/recordQuoteSingleSymbolSingleRun.sh +++ b/recordQuoteSingleSymbolSingleRun.sh @@ -1,5 +1,6 @@ #!/bin/bash #set -x +. helpers.sh symbol=$1 outDir=data/$(date +%Y-%m-%d) @@ -13,9 +14,7 @@ jqQuery=".\"${symbol}\".symbol" symbolCheck=$(jq -r ${jqQuery} < temp/quote.${symbol}.tmp.$$.json) if [[ "${symbol}" != "${symbolCheck}" ]]; then errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/quote.${symbol}.tmp.$$.json_" - systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg} - >&2 echo "${errMsg}" - exit 2 + throw ${errMsg} err 2 else quoteFileName=$(date "+%Y-%m-%d %H_%M_%S")_quote.${symbol}.json #echo writing to file "${outDir}/${quoteFileName}"