Files
OptionRecorderSchwab/autoTradeSPX_IB_1DTE.sh

140 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
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
throw "The market is closed, exiting." info 2
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 "+4 days") > "${inputFile}"
fi
if [[ ! -f "${inputFile}" ]]; then
throw "ERROR: Input file _${inputFile}_ does not exist." err 1
fi
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
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)
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)
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)
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
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)
log "Long Symbol: _${longSymbolC}_ Strike: _${longStrikeC}_ Bid: _${longBidC}_ Ask: _${longAskC}_" info
#Check if this order has already been submitted.
./getOrders.sh -daysBack 1 | (grep '\(WORKING\|FILLED\),'${longSymbolC:0:12} || true) > temp/existingOrders.${myPID}
if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then
log "Not Yet traded, continuing" info
else
throw "Already traded, exiting" info
fi
priceOffset=0.00 # try to get a little better price
#For SPX we need to submit orders at the closest 5cent price
#To find the closest 5cent price the mathematical solution is
# 1) Multiply by 20
# 2) Round to the Coloses full number
# 3) Divide by 20
priceOpen=$(echo "scale=2; exact=((${shortBidC}+${shortAskC})/2) + ((${shortBidP}+${shortAskP})/2) - ((${longBidC}+${longAskC})/2) - ((${longBidP}+${longAskP})/2) + ${priceOffset}; scale=0; round5=exact*20/1; scale=2; round5/20" | bc)
priceOpen=4.75 # All nice calculating a price, but I want those to be filled and reasonably, changing to 475 as 480 failed me on 2024-12-03
orderJson=$(jq -c <<-EOM
{
"orderType": "NET_CREDIT",
"session": "NORMAL",
"price": ${priceOpen},
"duration": "GOOD_TILL_CANCEL",
"orderStrategyType": "SINGLE",
"quantity": 1,
"orderLegCollection": [
{
"instruction": "SELL_TO_OPEN",
"quantity": 1,
"instrument": {
"symbol": "${shortSymbolP}",
"assetType": "OPTION"
}
},
{
"instruction": "SELL_TO_OPEN",
"quantity": 1,
"instrument": {
"symbol": "${shortSymbolC}",
"assetType": "OPTION"
}
},
{
"instruction": "BUY_TO_OPEN",
"quantity": 1,
"instrument": {
"symbol": "${longSymbolP}",
"assetType": "OPTION"
}
},
{
"instruction": "BUY_TO_OPEN",
"quantity": 1,
"instrument": {
"symbol": "${longSymbolC}",
"assetType": "OPTION"
}
}
]
}
EOM
)
log "Order: _${orderJson}_" debug
if [[ "${noconfirm}" != "true" ]]; then
echo ${orderJson} | jq
echo Enter to continue, Ctrl+C to cancel _${noconfirm}_
read
fi
curl -s -X POST \
"https://api.schwabapi.com/trader/v1/accounts/$(./getAccountNumbers.sh Regular)/orders" \
-H "Authorization: Bearer $(./getNewAccessToken.sh)" \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d "${orderJson}"