Cleanup - centralize the log handling

This commit is contained in:
2024-08-27 12:16:01 -05:00
parent d8891ec75f
commit a6b7fc7afe
15 changed files with 124 additions and 180 deletions

View File

@@ -2,6 +2,8 @@
set -x set -x
IFS=$'\0' IFS=$'\0'
. helpers.sh
daysOutMin=90 daysOutMin=90
daysOutMax=91 daysOutMax=91
deltaMin=0.35 deltaMin=0.35
@@ -10,24 +12,20 @@ deltaMax=0.40
inputFile="$1" inputFile="$1"
if [[ "" == "$inputFile" ]]; then if [[ "" == "$inputFile" ]]; then
latestFile="$(ls data/`date +%Y-%m-%d`/*QQQ* | sort | tail -n 1)" 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 inputFile=temp/orderInputFile.json
bzip2 -dc < "${latestFile}" > "${inputFile}" bzip2 -dc < "${latestFile}" > "${inputFile}"
fi fi
if [[ ! -f "${inputFile}" ]]; then if [[ ! -f "${inputFile}" ]]; then
errMsg="ERROR: Input file _${inputFile}_ does not exist." throw "ERROR: Input file _${inputFile}_ does not exist." err 1
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi 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; } }') 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}) daysOut=$(cut -d : -f 2 <<< ${dateKey})
if [[ ${daysOut} -gt ${daysOutMax} ]]; then 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." throw "This option is too far in the future. _${daysOut}_ is bigger than the max days in the future is _${daysOutMax}_. Exiting." info
exit
fi fi
#Looking for leg, between the specified deltas #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 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) numLinesInLeg=$(wc -l temp/longLeg.json | cut -c1)
if [[ 7 -ne ${numLinesInLeg} ]]; then if [[ 7 -ne ${numLinesInLeg} ]]; then
errMsg="No suitable long leg found - exiting." throw "No suitable long leg found - exiting." warning 2
systemd-cat -t "`basename $0` $1" -p warning <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi fi
longStrike=$(jq -r '.strikePrice' < temp/longLeg.json) longStrike=$(jq -r '.strikePrice' < temp/longLeg.json)
longBid=$(jq -r '.bid' < temp/longLeg.json) longBid=$(jq -r '.bid' < temp/longLeg.json)
longAsk=$(jq -r '.ask' < temp/longLeg.json) longAsk=$(jq -r '.ask' < temp/longLeg.json)
longSymbol=$(jq -r '.symbol' < temp/longLeg.json) longSymbol=$(jq -r '.symbol' < temp/longLeg.json)
msg="Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" log "Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
#Check if this order has already been submitted.
#Check if this order has already been submitted.
./getOrders.sh | grep '\(WORKING\|FILLED\),'${longSymbol} > temp/existingOrders.$$ ./getOrders.sh | grep '\(WORKING\|FILLED\),'${longSymbol} > temp/existingOrders.$$
if [[ $(wc -l < temp/existingOrders.$$) -eq 0 ]];then if [[ $(wc -l < temp/existingOrders.$$) -eq 0 ]];then
msg="Not Yet traded, continuing" log "Not Yet traded, continuing" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
else else
msg="Already traded, exiting" throw "Already traded, exiting" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
exit 0
fi fi
priceOffset=0.0 priceOffset=0.0
@@ -87,7 +77,7 @@ orderJson=$(jq -c <<-EOM
EOM EOM
) )
systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" log "Order: _${orderJson}_" debug
if [[ "${noconfirm}" != "true" ]]; then if [[ "${noconfirm}" != "true" ]]; then
echo Enter to continue, Ctrl+C to cancel _${noconfirm}_ echo Enter to continue, Ctrl+C to cancel _${noconfirm}_

View File

@@ -3,14 +3,11 @@
set -e set -e
IFS=$'\0' IFS=$'\0'
myPID=$$ myPID=$$
. helpers.sh
marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false) marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false)
if [[ ${marketOpen} == false ]]; then if [[ ${marketOpen} == false ]]; then
errMsg="The market is closed, exiting." throw "The market is closed, exiting." info 2
systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg}
>&2 echo "${errMsg}"
exit 2
fi fi
inputFile="$1" 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}" ./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d" -d "+40 days") toDate $(date "+%Y-%m-%d" -d "+46 days") > "${inputFile}"
fi fi
if [[ ! -f "${inputFile}" ]]; then if [[ ! -f "${inputFile}" ]]; then
errMsg="ERROR: Input file _${inputFile}_ does not exist." throw "ERROR: Input file _${inputFile}_ does not exist." err 1
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi fi
dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1) dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1)
systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" log "Date-Key: _${dateKey}_" debug
echo "Date-Key: _${dateKey}_"
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 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) 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) shortAsk=$(jq -r '.ask' < temp/shortLeg.json)
shortSymbol=$(jq -r '.symbol' < temp/shortLeg.json) shortSymbol=$(jq -r '.symbol' < temp/shortLeg.json)
shortOptionRoot=$(jq -r '.optionRoot' < temp/shortLeg.json) shortOptionRoot=$(jq -r '.optionRoot' < temp/shortLeg.json)
msg="Short Symbol: _${shortSymbol}_ Strike: _${shortStrike}_ Bid: _${shortBid}_ Ask: _${shortAsk}_" log "Short Symbol: _${shortSymbol}_ Strike: _${shortStrike}_ Bid: _${shortBid}_ Ask: _${shortAsk}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
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 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) longStrike=$(jq -r '.strikePrice' < temp/longLeg.json)
longBid=$(jq -r '.bid' < temp/longLeg.json) longBid=$(jq -r '.bid' < temp/longLeg.json)
longAsk=$(jq -r '.ask' < temp/longLeg.json) longAsk=$(jq -r '.ask' < temp/longLeg.json)
longSymbol=$(jq -r '.symbol' < temp/longLeg.json) longSymbol=$(jq -r '.symbol' < temp/longLeg.json)
msg="Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" log "Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
#Check if this order has already been submitted. #Check if this order has already been submitted.
./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbol:0:12} || true) > temp/existingOrders.${myPID} ./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbol:0:12} || true) > temp/existingOrders.${myPID}
if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then
msg="Not Yet traded, continuing" log "Not Yet traded, continuing" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo ${msg}
else else
msg="Already traded, exiting" throw "Already traded, exiting" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo ${msg}
exit 0
fi fi
priceOffset=0.1 # try to get $10 more in premium... priceOffset=0.1 # try to get $10 more in premium...
@@ -95,7 +79,7 @@ orderJson=$(jq -c <<-EOM
EOM EOM
) )
systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" log "Order: _${orderJson}_" debug
if [[ "${noconfirm}" != "true" ]]; then if [[ "${noconfirm}" != "true" ]]; then
echo Enter to continue, Ctrl+C to cancel _${noconfirm}_ echo Enter to continue, Ctrl+C to cancel _${noconfirm}_

View File

@@ -3,13 +3,11 @@ set -x
set -e set -e
IFS=$'\0' IFS=$'\0'
myPID=$$ myPID=$$
. helpers.sh
marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false) marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false)
if [[ ${marketOpen} == false ]]; then if [[ ${marketOpen} == false ]]; then
errMsg="The market is closed, exiting." throw "The market is closed, exiting." info 2
systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg}
>&2 echo "${errMsg}"
exit 2
fi fi
inputFile="$1" inputFile="$1"
@@ -18,15 +16,11 @@ if [[ "" == "$inputFile" ]]; then
./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d") toDate $(date "+%Y-%m-%d") > "${inputFile}" ./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d") toDate $(date "+%Y-%m-%d") > "${inputFile}"
fi fi
if [[ ! -f "${inputFile}" ]]; then if [[ ! -f "${inputFile}" ]]; then
errMsg="ERROR: Input file _${inputFile}_ does not exist." throw "ERROR: Input file _${inputFile}_ does not exist." err 1
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi fi
dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1) dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1)
systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" log "Date-Key: _${dateKey}_" debug
echo "Date-Key: _${dateKey}_"
underlyingPrice=$(jq ".underlyingPrice" "${inputFile}") underlyingPrice=$(jq ".underlyingPrice" "${inputFile}")
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 } }')
@@ -37,27 +31,21 @@ shortBidP=$(jq -r '.bid' < temp/shortLegP.json)
shortAskP=$(jq -r '.ask' < temp/shortLegP.json) shortAskP=$(jq -r '.ask' < temp/shortLegP.json)
shortSymbolP=$(jq -r '.symbol' < temp/shortLegP.json) shortSymbolP=$(jq -r '.symbol' < temp/shortLegP.json)
optionRoot=$(jq -r '.optionRoot' < temp/shortLegP.json) optionRoot=$(jq -r '.optionRoot' < temp/shortLegP.json)
msg="Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_" log "Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
jq '.callExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) |.[] | select(.strikePrice=='${atmStrike}') ' "${inputFile}" | tail -n 8 > temp/shortLegC.json 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) shortStrikeC=$(jq -r '.strikePrice' < temp/shortLegC.json)
shortBidC=$(jq -r '.bid' < temp/shortLegC.json) shortBidC=$(jq -r '.bid' < temp/shortLegC.json)
shortAskC=$(jq -r '.ask' < temp/shortLegC.json) shortAskC=$(jq -r '.ask' < temp/shortLegC.json)
shortSymbolC=$(jq -r '.symbol' < temp/shortLegC.json) shortSymbolC=$(jq -r '.symbol' < temp/shortLegC.json)
msg="Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" log "Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
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 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) longStrikeP=$(jq -r '.strikePrice' < temp/longLegP.json)
longBidP=$(jq -r '.bid' < temp/longLegP.json) longBidP=$(jq -r '.bid' < temp/longLegP.json)
longAskP=$(jq -r '.ask' < temp/longLegP.json) longAskP=$(jq -r '.ask' < temp/longLegP.json)
longSymbolP=$(jq -r '.symbol' < temp/longLegP.json) longSymbolP=$(jq -r '.symbol' < temp/longLegP.json)
msg="Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" log "Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
#Check if this order has already been submitted. #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 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) longBidC=$(jq -r '.bid' < temp/longLegC.json)
longAskC=$(jq -r '.ask' < temp/longLegC.json) longAskC=$(jq -r '.ask' < temp/longLegC.json)
longSymbolC=$(jq -r '.symbol' < temp/longLegC.json) longSymbolC=$(jq -r '.symbol' < temp/longLegC.json)
msg="Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" log "Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
#Check if this order has already been submitted. #Check if this order has already been submitted.
./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbolC:0:12} || true) > temp/existingOrders.${myPID} ./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbolC:0:12} || true) > temp/existingOrders.${myPID}
if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then
msg="Not Yet traded, continuing" log "Not Yet traded, continuing" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo ${msg}
else else
msg="Already traded, exiting" throw "Already traded, exiting" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo ${msg}
# exit 0
fi fi
priceOffset=1.1 # try to get $10 more in premium... priceOffset=1.1 # try to get $10 more in premium...
@@ -185,7 +166,7 @@ orderJson=$(jq -c <<-EOM
EOM EOM
) )
systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" log "Order: _${orderJson}_" debug
if [[ "${noconfirm}" != "true" ]]; then if [[ "${noconfirm}" != "true" ]]; then
echo ${orderJSON} | jq echo ${orderJSON} | jq

View File

@@ -3,13 +3,11 @@ set -x
set -e set -e
IFS=$'\0' IFS=$'\0'
myPID=$$ myPID=$$
. helpers.sh
marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false) marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false)
if [[ ${marketOpen} == false ]]; then if [[ ${marketOpen} == false ]]; then
errMsg="The market is closed, exiting." throw "The market is closed, exiting." info 2
systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg}
>&2 echo "${errMsg}"
exit 2
fi fi
inputFile="$1" 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}" ./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d" -d "+1 days") toDate $(date "+%Y-%m-%d" -d "+1 days") > "${inputFile}"
fi fi
if [[ ! -f "${inputFile}" ]]; then if [[ ! -f "${inputFile}" ]]; then
errMsg="ERROR: Input file _${inputFile}_ does not exist." throw "ERROR: Input file _${inputFile}_ does not exist." err 1
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi fi
dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1) dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1)
systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_" log "Date-Key: _${dateKey}_" debug
echo "Date-Key: _${dateKey}_"
underlyingPrice=$(jq ".underlyingPrice" "${inputFile}") underlyingPrice=$(jq ".underlyingPrice" "${inputFile}")
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 } }')
@@ -39,27 +33,21 @@ shortStrikeP=$(jq -r '.strikePrice' < temp/shortLegP.json)
shortBidP=$(jq -r '.bid' < temp/shortLegP.json) shortBidP=$(jq -r '.bid' < temp/shortLegP.json)
shortAskP=$(jq -r '.ask' < temp/shortLegP.json) shortAskP=$(jq -r '.ask' < temp/shortLegP.json)
shortSymbolP=$(jq -r '.symbol' < temp/shortLegP.json) shortSymbolP=$(jq -r '.symbol' < temp/shortLegP.json)
msg="Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_" log "Short Put Symbol: _${shortSymbolP}_ Strike: _${shortStrikeP}_ Bid: _${shortBidP}_ Ask: _${shortAskP}_}" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
jq '.callExpDateMap."'${dateKey}'"[]|map({symbol,delta,strikePrice,bid,ask,optionRoot}) |.[] | select(.optionRoot == "'${optionRoot}'" and .strikePrice=='${atmStrike}') ' "${inputFile}" | tail -n 8 > temp/shortLegC.json 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) shortStrikeC=$(jq -r '.strikePrice' < temp/shortLegC.json)
shortBidC=$(jq -r '.bid' < temp/shortLegC.json) shortBidC=$(jq -r '.bid' < temp/shortLegC.json)
shortAskC=$(jq -r '.ask' < temp/shortLegC.json) shortAskC=$(jq -r '.ask' < temp/shortLegC.json)
shortSymbolC=$(jq -r '.symbol' < temp/shortLegC.json) shortSymbolC=$(jq -r '.symbol' < temp/shortLegC.json)
msg="Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" log "Short Call Symbol: _${shortSymbolC}_ Strike: _${shortStrikeC}_ Bid: _${shortBidC}_ Ask: _${shortAskC}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
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 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) longStrikeP=$(jq -r '.strikePrice' < temp/longLegP.json)
longBidP=$(jq -r '.bid' < temp/longLegP.json) longBidP=$(jq -r '.bid' < temp/longLegP.json)
longAskP=$(jq -r '.ask' < temp/longLegP.json) longAskP=$(jq -r '.ask' < temp/longLegP.json)
longSymbolP=$(jq -r '.symbol' < temp/longLegP.json) longSymbolP=$(jq -r '.symbol' < temp/longLegP.json)
msg="Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" log "Long Symbol: _${longSymbolP}_ Strike: _${longStrikeP}_ Bid: _${longBidP}_ Ask: _${longAskP}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
#Check if this order has already been submitted. #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 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) longBidC=$(jq -r '.bid' < temp/longLegC.json)
longAskC=$(jq -r '.ask' < temp/longLegC.json) longAskC=$(jq -r '.ask' < temp/longLegC.json)
longSymbolC=$(jq -r '.symbol' < temp/longLegC.json) longSymbolC=$(jq -r '.symbol' < temp/longLegC.json)
msg="Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" log "Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
#Check if this order has already been submitted. #Check if this order has already been submitted.
./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbolC:0:12} || true) > temp/existingOrders.${myPID} ./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbolC:0:12} || true) > temp/existingOrders.${myPID}
if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then
msg="Not Yet traded, continuing" log "Not Yet traded, continuing" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo ${msg}
else else
msg="Already traded, exiting" throw "Already traded, exiting" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo ${msg}
# exit 0
fi fi
priceOffset=0.00 # try to get a little better price priceOffset=0.00 # try to get a little better price
@@ -140,7 +121,7 @@ orderJson=$(jq -c <<-EOM
EOM EOM
) )
systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" log "Order: _${orderJson}_" debug
if [[ "${noconfirm}" != "true" ]]; then if [[ "${noconfirm}" != "true" ]]; then
echo ${orderJson} | jq echo ${orderJson} | jq

View File

@@ -2,6 +2,8 @@
set -x set -x
IFS=$'\0' IFS=$'\0'
. helpers.sh
daysOutMin=90 daysOutMin=90
daysOutMax=95 daysOutMax=95
deltaMin=0.35 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) marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false)
if [[ ${marketOpen} == false ]]; then if [[ ${marketOpen} == false ]]; then
errMsg="The market is closed, exiting." throw "The market is closed, exiting." info 2
systemd-cat -t "`basename $0` $1" -p info <<< ${errMsg}
>&2 echo "${errMsg}"
exit 2
fi fi
inputFile="$1" inputFile="$1"
if [[ "" == "$inputFile" ]]; then if [[ "" == "$inputFile" ]]; then
latestFile="$(ls data/`date +%Y-%m-%d`/*SPY* | sort | tail -n 1)" 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 inputFile=temp/orderInputFile.json
bzip2 -dc < "${latestFile}" > "${inputFile}" bzip2 -dc < "${latestFile}" > "${inputFile}"
fi fi
if [[ ! -f "${inputFile}" ]]; then if [[ ! -f "${inputFile}" ]]; then
errMsg="ERROR: Input file _${inputFile}_ does not exist." throw "ERROR: Input file _${inputFile}_ does not exist." err 1
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi 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; } }') 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}) daysOut=$(cut -d : -f 2 <<< ${dateKey})
if [[ ${daysOut} -gt ${daysOutMax} ]]; then 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." throw "This option is too far in the future. _${daysOut}_ is bigger than the max days in the future is _${daysOutMax}_. Exiting." info
exit
fi fi
#Looking for leg, between the specified deltas #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 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) numLinesInLeg=$(wc -l temp/longLeg.json | cut -c1)
if [[ 7 -ne ${numLinesInLeg} ]]; then if [[ 7 -ne ${numLinesInLeg} ]]; then
errMsg="No suitable long leg found - exiting." throw "No suitable long leg found - exiting." warning 1
systemd-cat -t "`basename $0` $1" -p warning <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi fi
longStrike=$(jq -r '.strikePrice' < temp/longLeg.json) longStrike=$(jq -r '.strikePrice' < temp/longLeg.json)
longBid=$(jq -r '.bid' < temp/longLeg.json) longBid=$(jq -r '.bid' < temp/longLeg.json)
longAsk=$(jq -r '.ask' < temp/longLeg.json) longAsk=$(jq -r '.ask' < temp/longLeg.json)
longSymbol=$(jq -r '.symbol' < temp/longLeg.json) longSymbol=$(jq -r '.symbol' < temp/longLeg.json)
msg="Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" log "Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo "${msg}"
#Check if this order has already been submitted.
#Check if this order has already been submitted.
./getOrders.sh | grep '\(WORKING\|FILLED\),'${longSymbol} > temp/existingOrders.$$ ./getOrders.sh | grep '\(WORKING\|FILLED\),'${longSymbol} > temp/existingOrders.$$
if [[ $(wc -l < temp/existingOrders.$$) -eq 0 ]];then if [[ $(wc -l < temp/existingOrders.$$) -eq 0 ]];then
msg="Not Yet traded, continuing" log "Not Yet traded, continuing" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
else else
msg="Already traded, exiting" throw "Already traded, exiting" info
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
exit 0
fi fi
priceOffset=0.0 priceOffset=0.0
@@ -95,7 +82,7 @@ orderJson=$(jq -c <<-EOM
EOM EOM
) )
systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_" log "Order: _${orderJson}_" debug
if [[ "${noconfirm}" != "true" ]]; then if [[ "${noconfirm}" != "true" ]]; then
echo Enter to continue, Ctrl+C to cancel _${noconfirm}_ echo Enter to continue, Ctrl+C to cancel _${noconfirm}_

View File

@@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
#set -x #set -x
set -e set -e
. helpers.sh
mkdir -p temp mkdir -p temp
ErrorLogLevel=err ErrorLogLevel=err
@@ -29,8 +30,7 @@ while [[ $# -gt 0 ]]; do
shift; shift shift; shift
done done
msg="Querying for markets: _${markets}_ query string: _${queryString}_" log "Querying for markets: _${markets}_ query string: _${queryString}_" debug
systemd-cat -t "`basename $0` $1" -p debug <<< ${msg}
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)" > 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) marketsCheck=$(jq -e -r '.. | .isOpen?' temp/markets.tmp.$$.json | 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 _temp/markets.tmp.$$.json_"
systemd-cat -t "`basename $0` $1" -p ${ErrorLogLevel} <<< ${errMsg}
if [[ "${ErrorLogLevel}" == "err" ]]; then if [[ "${ErrorLogLevel}" == "err" ]]; then
>&2 echo "${errMsg}" throw ${errMsg} ${ErrorLogLevel}
else
log ${errMsg} ${ErrorLogLevel}
fi fi
exit 2
else else
jq < temp/markets.tmp.$$.json jq < temp/markets.tmp.$$.json
rm temp/markets.tmp.$$.json rm temp/markets.tmp.$$.json

View File

@@ -9,11 +9,8 @@ curl -s -X POST https://api.schwabapi.com/v1/oauth/token \
-d "grant_type=refresh_token&refresh_token=$(<refresh_token.dat)" > temp/accessTokenReqResp.$$.json -d "grant_type=refresh_token&refresh_token=$(<refresh_token.dat)" > temp/accessTokenReqResp.$$.json
jq -e -r .access_token temp/accessTokenReqResp.$$.json > access_token.dat jq -e -r .access_token temp/accessTokenReqResp.$$.json > access_token.dat
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
errMsg="ERROR: GETTING THE ACCESS TOKEN FAILED! $(<temp/accessTokenReqResp.$$.json)"
systemd-cat -t "`basename $0`" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
curl -d "" -X POST "https://deadswitch.kawomi.com/api/v1?topic=trade.schwab.error.getNewAccessToken&secs=1&email=joe@kawomi.com" 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/accessTokenReqResp.$$.json)" err
fi fi
#echo Access Token: $(<access_token.dat) #echo Access Token: $(<access_token.dat)

View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
#set -x #set -x
. helpers.sh
mkdir -p temp mkdir -p temp
echo Set Dyn-IP echo Set Dyn-IP
@@ -23,14 +23,10 @@ curl -s -X POST "https://api.schwabapi.com/v1/oauth/token" \
jq -e -r .refresh_token temp/refreshTokenReqResp.$$.json > temp/refresh_token.$$.dat jq -e -r .refresh_token temp/refreshTokenReqResp.$$.json > temp/refresh_token.$$.dat
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
errMsg="New Refresh Token created." log "New Refresh Token created." info
systemd-cat -t "`basename $0`" -p info <<< ${errMsg}
echo "${errMsg}"
cp temp/refresh_token.$$.dat refresh_token.dat cp temp/refresh_token.$$.dat refresh_token.dat
else else
errMsg="Error getting new refresh token _$(<temp/refresh_token.$$.dat)_" throw "Error getting new refresh token _$(<temp/refresh_token.$$.dat)_" err 1
systemd-cat -t "`basename $0`" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
fi fi
echo Refresh Token: $(<refresh_token.dat) echo Refresh Token: $(<refresh_token.dat)

View File

@@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
#set -x #set -x
. helpers.sh
mkdir -p temp mkdir -p temp
ErrorLogLevel=err ErrorLogLevel=err
@@ -21,8 +22,7 @@ while [[ $# -gt 0 ]]; do
shift; shift shift; shift
done done
msg="Querying for Symbol: _${symbol}_ query string: _${queryString}_" log "Querying for Symbol: _${symbol}_ query string: _${queryString}_" debug
systemd-cat -t "`basename $0` $1" -p debug <<< ${msg}
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}" \ curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}" \
-H "Authorization: Bearer $(<access_token.dat)" > temp/optionChain.${symbol}.tmp.$$.json -H "Authorization: Bearer $(<access_token.dat)" > temp/optionChain.${symbol}.tmp.$$.json
@@ -31,11 +31,11 @@ jqQuery=".symbol"
symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.tmp.$$.json) symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.tmp.$$.json)
if [[ "${symbol}" != "${symbolCheck}" ]]; then if [[ "${symbol}" != "${symbolCheck}" ]]; then
errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.tmp.$$.json_" 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 if [[ "${ErrorLogLevel}" == "err" ]]; then
>&2 echo "${errMsg}" throw ${errMsg} ${ErrorLogLevel} 2
else
log ${errMsg} ${ErrorLogLevel}
fi fi
exit 2
else else
jq < temp/optionChain.${symbol}.tmp.$$.json jq < temp/optionChain.${symbol}.tmp.$$.json
rm temp/optionChain.${symbol}.tmp.$$.json rm temp/optionChain.${symbol}.tmp.$$.json

32
helpers.sh Normal file
View File

@@ -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}
}

View File

@@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
#set -x #set -x
. helpers.sh
IFS=$'\0' IFS=$'\0'
daysOutMin=90 daysOutMin=90
@@ -9,19 +10,16 @@ inputFile="$1"
latestFile="$1" latestFile="$1"
if [[ "" == "$inputFile" ]]; then if [[ "" == "$inputFile" ]]; then
latestFile="$(ls data/`date +%Y-%m-%d`/*SPY* | sort | tail -n 1)" 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 inputFile=temp/orderInputFile.json
bzip2 -dc < "${latestFile}" > "${inputFile}" bzip2 -dc < "${latestFile}" > "${inputFile}"
fi fi
if [[ ! -f "${inputFile}" ]]; then if [[ ! -f "${inputFile}" ]]; then
errMsg="ERROR: Input file _${inputFile}_ does not exist." throw "ERROR: Input file _${inputFile}_ does not exist." err 1
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
exit 1
fi 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; } }') 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}) daysOut=$(cut -d : -f 2 <<< ${dateKey})
if [[ ${daysOut} -gt ${daysOutMax} ]]; then if [[ ${daysOut} -gt ${daysOutMax} ]]; then

View File

@@ -1,12 +1,13 @@
#!/bin/bash #!/bin/bash
#set -x #set -x
systemd-cat -t "`basename $0`" -p info <<< "Started" . helpers.sh
log Started
./getNewAccessToken.sh ./getNewAccessToken.sh
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
systemd-cat -t "`basename $0`" -p err <<< "Child process to get access token reported error. Exiting." log "Child process to get access token reported error. Exiting." err 1
exit 1
fi fi
jq -r '.[].Symbol' allSymbols.json | sort | while read theSymbol; do 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 if [[ '$XSP' = ${theSymbol} ]]; then
./recordOptionChainSingleSymbolSingleRun.sh -ErrorAsWarning ${theSymbol} toDate $(date "+%Y-%m-%d" -d "+400 days") ./recordOptionChainSingleSymbolSingleRun.sh -ErrorAsWarning ${theSymbol} toDate $(date "+%Y-%m-%d" -d "+400 days")
if [[ $? -ne 0 ]]; then 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") ./recordOptionChainSingleSymbolSpecialRun.sh ${theSymbol} toDate $(date "+%Y-%m-%d" -d "+400 days")
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
touch temp/hadErrors.$$.tmp 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 #Default behavior - fall back to stitching on error
./recordOptionChainSingleSymbolSingleRun.sh -ErrorAsWarning ${theSymbol} ./recordOptionChainSingleSymbolSingleRun.sh -ErrorAsWarning ${theSymbol}
if [[ $? -ne 0 ]]; then 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} ./recordOptionChainSingleSymbolSpecialRun.sh ${theSymbol}
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
touch temp/hadErrors.$$.tmp 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" curl -d "" -X POST "https://deadswitch.kawomi.com/api/v1?topic=trade.schwab.error.recordAllSymbolsSingleRun&secs=1&email=joe@kawomi.com"
fi fi
systemd-cat -t "`basename $0`" -p info <<< "Finished" log "Finished"

View File

@@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
#set -x #set -x
. helpers.sh
outDir=data/$(date +%Y-%m-%d) outDir=data/$(date +%Y-%m-%d)
mkdir -p "${outDir}" mkdir -p "${outDir}"
mkdir -p temp mkdir -p temp
@@ -23,8 +24,7 @@ while [[ $# -gt 0 ]]; do
shift; shift shift; shift
done done
msg="Querying for Symbol: _${symbol}_ query string: _${queryString}_" log "Querying for Symbol: _${symbol}_ query string: _${queryString}_" debug
systemd-cat -t "`basename $0` $1" -p debug <<< ${msg}
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}" \ curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}" \
-H "Authorization: Bearer $(<access_token.dat)" > temp/optionChain.${symbol}.tmp.$$.json -H "Authorization: Bearer $(<access_token.dat)" > temp/optionChain.${symbol}.tmp.$$.json
@@ -33,11 +33,11 @@ jqQuery=".symbol"
symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.tmp.$$.json) symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.tmp.$$.json)
if [[ "${symbol}" != "${symbolCheck}" ]]; then if [[ "${symbol}" != "${symbolCheck}" ]]; then
errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.tmp.$$.json_" 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 if [[ "${ErrorLogLevel}" == "err" ]]; then
>&2 echo "${errMsg}" throw ${errMsg} ${ErrorLogLevel} 2
else
log ${errMsg} ${ErrorLogLevel}
fi fi
exit 2
else else
optionChainFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.json optionChainFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.json
#echo writing to file "${outDir}/${optionChainFileName}" #echo writing to file "${outDir}/${optionChainFileName}"

View File

@@ -3,6 +3,8 @@
# This is for big chains, that cause the Schwab API to error out (like $XSP) # 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 # I add the range parameter (ITM/NTM/OTM) so that I get less data per request
. helpers.sh
symbol=$1 symbol=$1
outDir=data/$(date +%Y-%m-%d) outDir=data/$(date +%Y-%m-%d)
queryString="?symbol=${symbol}" queryString="?symbol=${symbol}"
@@ -19,8 +21,7 @@ while [[ $# -gt 0 ]]; do
done done
for range in ITM OTM NTM; do for range in ITM OTM NTM; do
msg="Querying for Symbol: _${symbol}_ query string: _${queryString}&range=${range}_" log "Querying for Symbol: _${symbol}_ query string: _${queryString}&range=${range}_" debug
systemd-cat -t "`basename $0` $1" -p debug <<< ${msg}
curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}&range=${range}" \ curl -s -X GET "https://api.schwabapi.com/marketdata/v1/chains${queryString}&range=${range}" \
-H "Authorization: Bearer $(<access_token.dat)" > temp/optionChain.${symbol}.${range}.tmp.$$.json -H "Authorization: Bearer $(<access_token.dat)" > temp/optionChain.${symbol}.${range}.tmp.$$.json
@@ -28,10 +29,7 @@ for range in ITM OTM NTM; do
jqQuery=".symbol" jqQuery=".symbol"
symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.${range}.tmp.$$.json) symbolCheck=$(jq -r ${jqQuery} < temp/optionChain.${symbol}.${range}.tmp.$$.json)
if [[ "${symbol}" != "${symbolCheck}" ]]; then if [[ "${symbol}" != "${symbolCheck}" ]]; then
errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.${range}.tmp.$$.json_" throw "ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/optionChain.${symbol}.${range}.tmp.$$.json_" err 2
systemd-cat -t "`basename $0` $1" -p err <<< ${errMsg}
>&2 echo "${errMsg}"
exit 2
fi fi
optionChainPartFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.${range}.json optionChainPartFileName=$(date "+%Y-%m-%d %H_%M_%S")_optionChain.${symbol}.${range}.json

View File

@@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
#set -x #set -x
. helpers.sh
symbol=$1 symbol=$1
outDir=data/$(date +%Y-%m-%d) outDir=data/$(date +%Y-%m-%d)
@@ -13,9 +14,7 @@ jqQuery=".\"${symbol}\".symbol"
symbolCheck=$(jq -r ${jqQuery} < temp/quote.${symbol}.tmp.$$.json) symbolCheck=$(jq -r ${jqQuery} < temp/quote.${symbol}.tmp.$$.json)
if [[ "${symbol}" != "${symbolCheck}" ]]; then if [[ "${symbol}" != "${symbolCheck}" ]]; then
errMsg="ERROR: THE RESPONSE IS INCORRECT! Symbol: _${symbol}_ Check: _${symbolCheck}_ check file _temp/quote.${symbol}.tmp.$$.json_" 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} throw ${errMsg} err 2
>&2 echo "${errMsg}"
exit 2
else else
quoteFileName=$(date "+%Y-%m-%d %H_%M_%S")_quote.${symbol}.json quoteFileName=$(date "+%Y-%m-%d %H_%M_%S")_quote.${symbol}.json
#echo writing to file "${outDir}/${quoteFileName}" #echo writing to file "${outDir}/${quoteFileName}"