2024-05-28 15:25:56 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
#set -x
|
2024-07-17 14:08:55 -05:00
|
|
|
set -e
|
2024-05-28 15:25:56 -05:00
|
|
|
IFS=$'\0'
|
2024-08-22 11:17:17 -05:00
|
|
|
myPID=$$
|
2024-08-27 12:16:01 -05:00
|
|
|
. helpers.sh
|
2024-08-26 15:18:24 -05:00
|
|
|
|
|
|
|
|
marketOpen=$([[ $(./getMarketHours.sh | jq '.option[].isOpen' | grep true | wc -l) -ne 0 ]] && echo true || echo false)
|
|
|
|
|
if [[ ${marketOpen} == false ]]; then
|
2024-08-27 12:16:01 -05:00
|
|
|
throw "The market is closed, exiting." info 2
|
2024-08-26 15:18:24 -05:00
|
|
|
fi
|
|
|
|
|
|
2024-05-28 15:25:56 -05:00
|
|
|
inputFile="$1"
|
|
|
|
|
if [[ "" == "$inputFile" ]]; then
|
2024-08-26 15:18:24 -05:00
|
|
|
inputFile=temp/orderInputFile.$$.json
|
|
|
|
|
./getOptionChain.sh '$SPX' fromDate $(date "+%Y-%m-%d" -d "+40 days") toDate $(date "+%Y-%m-%d" -d "+46 days") > "${inputFile}"
|
2024-05-28 15:25:56 -05:00
|
|
|
fi
|
|
|
|
|
if [[ ! -f "${inputFile}" ]]; then
|
2024-08-27 12:16:01 -05:00
|
|
|
throw "ERROR: Input file _${inputFile}_ does not exist." err 1
|
2024-05-28 15:25:56 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1)
|
2024-08-27 12:16:01 -05:00
|
|
|
log "Date-Key: _${dateKey}_" debug
|
2024-05-28 15:25:56 -05:00
|
|
|
|
2024-07-17 14:08:55 -05:00
|
|
|
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
|
2024-05-28 15:25:56 -05:00
|
|
|
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)
|
2024-07-17 14:08:55 -05:00
|
|
|
shortOptionRoot=$(jq -r '.optionRoot' < temp/shortLeg.json)
|
2024-08-27 12:16:01 -05:00
|
|
|
log "Short Symbol: _${shortSymbol}_ Strike: _${shortStrike}_ Bid: _${shortBid}_ Ask: _${shortAsk}_" info
|
2024-05-28 15:25:56 -05:00
|
|
|
|
2024-08-26 15:18:24 -05:00
|
|
|
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
|
2024-05-28 15:25:56 -05:00
|
|
|
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)
|
2024-08-27 12:16:01 -05:00
|
|
|
log "Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_" info
|
2024-05-28 15:25:56 -05:00
|
|
|
|
2024-08-15 11:30:27 -05:00
|
|
|
#Check if this order has already been submitted.
|
2024-08-22 12:22:04 -05:00
|
|
|
./getOrders.sh | (grep '\(WORKING\|FILLED\),'${longSymbol:0:12} || true) > temp/existingOrders.${myPID}
|
2024-08-22 11:17:17 -05:00
|
|
|
|
2024-08-22 12:22:04 -05:00
|
|
|
if [[ $(wc -l < temp/existingOrders.${myPID}) -eq 0 ]];then
|
2024-08-27 12:16:01 -05:00
|
|
|
log "Not Yet traded, continuing" info
|
2024-08-15 11:30:27 -05:00
|
|
|
else
|
2024-08-27 12:16:01 -05:00
|
|
|
throw "Already traded, exiting" info
|
2024-08-15 11:30:27 -05:00
|
|
|
fi
|
|
|
|
|
|
2024-05-30 10:53:17 -05:00
|
|
|
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
|
2024-05-28 15:25:56 -05:00
|
|
|
{
|
|
|
|
|
"orderType": "NET_CREDIT",
|
|
|
|
|
"session": "NORMAL",
|
2024-05-30 10:53:17 -05:00
|
|
|
"price": ${price},
|
2024-05-28 15:25:56 -05:00
|
|
|
"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
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-27 12:16:01 -05:00
|
|
|
log "Order: _${orderJson}_" debug
|
2024-05-30 10:53:17 -05:00
|
|
|
|
2024-08-26 15:18:24 -05:00
|
|
|
if [[ "${noconfirm}" != "true" ]]; then
|
|
|
|
|
echo Enter to continue, Ctrl+C to cancel _${noconfirm}_
|
|
|
|
|
read
|
|
|
|
|
fi
|
|
|
|
|
|
2024-05-28 15:25:56 -05:00
|
|
|
curl -s -X POST \
|
2024-08-26 21:44:43 -05:00
|
|
|
"https://api.schwabapi.com/trader/v1/accounts/$(./getAccountNumbers.sh Regular)/orders" \
|
2024-09-03 10:32:56 -05:00
|
|
|
-H "Authorization: Bearer $(./getNewAccessToken.sh)" \
|
2024-05-28 15:25:56 -05:00
|
|
|
-H 'accept: */*' \
|
|
|
|
|
-H 'Content-Type: application/json' \
|
2024-08-26 21:44:43 -05:00
|
|
|
-d "${orderJson}"
|
2024-05-28 15:25:56 -05:00
|
|
|
|