2024-05-28 15:25:56 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
#set -x
|
|
|
|
|
IFS=$'\0'
|
|
|
|
|
|
|
|
|
|
inputFile="$1"
|
|
|
|
|
if [[ "" == "$inputFile" ]]; then
|
|
|
|
|
latestFile="$(ls data/`date +%Y-%m-%d`/*\$SPX* | sort | tail -n 1)"
|
|
|
|
|
systemd-cat -t "`basename $0` $1" -p debug <<< "Latest File: _${latestFile}_"
|
|
|
|
|
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
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
dateKey=$(jq -r '.putExpDateMap | keys | .[]' "${inputFile}" | head -n 1)
|
|
|
|
|
systemd-cat -t "`basename $0` $1" -p debug <<< "Date-Key: _${dateKey}_"
|
|
|
|
|
|
|
|
|
|
jq '.putExpDateMap."'${dateKey}'"[]|map({symbol, delta, strikePrice,bid,ask}) |.[] | select(.delta >= -0.30 and .delta <= -0.25) ' "${inputFile}" | tail -n 7 > 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)
|
|
|
|
|
msg="Short Symbol: _${shortSymbol}_ Strike: _${shortStrike}_ Bid: _${shortBid}_ Ask: _${shortAsk}_"
|
|
|
|
|
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
|
|
|
|
|
echo "${msg}"
|
|
|
|
|
|
2024-05-30 10:53:17 -05:00
|
|
|
jq '.putExpDateMap."'${dateKey}'"[]|map({symbol, delta, strikePrice,bid,ask}) | .[] | select(.strikePrice >= '${shortStrike}'-50 and .strikePrice < '${shortStrike}')' temp/orderInputFile.json | head -n 7 > 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)
|
|
|
|
|
msg="Long Symbol: _${longSymbol}_ Strike: _${longStrike}_ Bid: _${longBid}_ Ask: _${longAsk}_"
|
|
|
|
|
systemd-cat -t "`basename $0` $1" -p info <<< ${msg}
|
|
|
|
|
echo "${msg}"
|
|
|
|
|
|
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-05-30 10:53:17 -05:00
|
|
|
systemd-cat -t "`basename $0` $1" -p debug <<< "Order: _${orderJson}_"
|
|
|
|
|
|
|
|
|
|
#echo Enter to continue, Ctrl+C to cancel
|
|
|
|
|
#read
|
2024-05-28 15:25:56 -05:00
|
|
|
./getNewAccessToken.sh
|
|
|
|
|
|
|
|
|
|
curl -s -X POST \
|
2024-05-30 10:53:17 -05:00
|
|
|
'https://api.schwabapi.com/trader/v1/accounts/C819AC820A502BA92E55D775B22BDF0E1BC91D86C62685714347734A5827CCB3/orders' \
|
2024-05-28 15:25:56 -05:00
|
|
|
-H "Authorization: Bearer $(<access_token.dat)" \
|
|
|
|
|
-H 'accept: */*' \
|
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
|
-d "${orderJson}"
|
|
|
|
|
|