Files
OptionRecorderSchwab/autoTradeSPXPutCreditSpread.sh
Joe Tretter 5535bdae7f - Separate getting option chains from recording
- Add checking for market open
- Remove SPX from recording
- Hopefully precent the refresh token from being
  overwritten sometimes
2024-08-26 15:18:24 -05:00

114 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
#set -x
set -e
IFS=$'\0'
myPID=$$
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
fi
inputFile="$1"
if [[ "" == "$inputFile" ]]; then
inputFile=temp/orderInputFile.$$.json
./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
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}_"
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)
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}"
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}"
#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}
else
msg="Already traded, exiting"
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
echo ${msg}
exit 0
fi
priceOffset=0.1 # try to get $10 more in premium...
price=$(echo "scale=2; ((${shortBid}+${shortAsk})/2 - (${longBid}+${longAsk})/2) + ${priceOffset}" | bc)
orderJson=$(jq -c <<-EOM
{
"orderType": "NET_CREDIT",
"session": "NORMAL",
"price": ${price},
"duration": "DAY",
"orderStrategyType": "SINGLE",
"quantity": 1,
"orderLegCollection": [
{
"instruction": "SELL_TO_OPEN",
"quantity": 1,
"instrument": {
"symbol": "${shortSymbol}",
"assetType": "OPTION"
}
},
{
"instruction": "BUY_TO_OPEN",
"quantity": 1,
"instrument": {
"symbol": "${longSymbol}",
"assetType": "OPTION"
}
}
]
}
EOM
)
systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_"
if [[ "${noconfirm}" != "true" ]]; then
echo Enter to continue, Ctrl+C to cancel _${noconfirm}_
read
fi
./getNewAccessToken.sh
curl -s -X POST \
'https://api.schwabapi.com/trader/v1/accounts/C819AC820A502BA92E55D775B22BDF0E1BC91D86C62685714347734A5827CCB3/orders' \
-H "Authorization: Bearer $(<access_token.dat)" \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d "${orderJson}"