2024-07-17 14:08:55 -05:00
#!/bin/bash
set -x
IFS = $'\0'
daysOutMin = 90
daysOutMax = 91
deltaMin = 0.35
deltaMax = 0.40
inputFile = " $1 "
if [ [ "" = = " $inputFile " ] ] ; then
latestFile = " $( ls data/` date +%Y-%m-%d` /*QQQ* | 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 '.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 } _ "
daysOut = $( cut -d : -f 2 <<< ${ dateKey } )
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. "
exit
fi
#Looking for leg, between the specified deltas
# JTR - the "floor" is to only find the prices at the integer strikes, but I changed my mind about that...
#jq '.callExpDateMap."'${dateKey}'"[]|map({symbol, delta, strikePrice,bid,ask}) | .[] | select(.delta >= '${deltaMin}' and .delta <= '${deltaMax}') | select( (.strikePrice | floor) == .strikePrice)' "${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)
if [ [ 7 -ne ${ numLinesInLeg } ] ] ; then
errMsg = "No suitable long leg found - exiting."
systemd-cat -t " `basename $0 ` $1 " -p warning <<< ${ errMsg }
>& 2 echo " ${ errMsg } "
exit 1
fi
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-08-15 11:30:27 -05:00
#Check if this order has already been submitted.
./getOrders.sh | grep '\(WORKING\|FILLED\),' ${ longSymbol } > temp/existingOrders.$$
if [ [ $( wc -l < temp/existingOrders.$$ ) -eq 0 ] ] ; then
msg = "Not Yet traded, continuing"
systemd-cat -t " `basename $0 ` $1 " -p info <<< ${ msg }
else
msg = "Already traded, exiting"
systemd-cat -t " `basename $0 ` $1 " -p info <<< ${ msg }
exit 0
fi
2024-07-17 14:08:55 -05:00
priceOffset = 0.0
# Get the Middle price...
price = $( echo " scale=2; (( ${ longBid } + ${ longAsk } )/2) + ${ priceOffset } " | bc)
orderJson = $( jq -c <<-EOM
{
"orderType" : "LIMIT" ,
"session" : "NORMAL" ,
"price" : ${ price } ,
"duration" : "DAY" ,
"orderStrategyType" : "SINGLE" ,
"quantity" : 1,
"orderLegCollection" : [
{
"instruction" : "BUY_TO_OPEN" ,
"quantity" : 1,
"instrument" : {
"symbol" : " ${ longSymbol } " ,
"assetType" : "OPTION"
}
}
]
}
EOM
)
systemd-cat -t " `basename $0 ` $1 " -p debug <<< " Order: _ ${ orderJson } _ "
echo Enter to continue , Ctrl+C to cancel
read
./getNewAccessToken.sh
curl -s -X POST \
2024-08-15 11:30:27 -05:00
'https://api.schwabapi.com/trader/v1/accounts/5A5B921917D97B89FDA53E1E1D13D2EB11E488ADA20A20B3C787477DE59A770E/orders' \
2024-07-17 14:08:55 -05:00
-H " Authorization: Bearer $( <access_token.dat) " \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d " ${ orderJson } "