Initial commit, created get-prices
commit
38c3ebf383
|
@ -0,0 +1 @@
|
|||
config.env
|
|
@ -0,0 +1,21 @@
|
|||
# MIT License
|
||||
|
||||
**Copyright (c) 2022 Keith Irwin**
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,39 @@
|
|||
# hledger scripts
|
||||
|
||||
Some scripts for hledger
|
||||
|
||||
## get-prices
|
||||
|
||||
Gets prices from Polygon.io api, and hopefully some alternatives in the future. Run this as a timer or cronjob to keep your prices file up-to-date with your assets.
|
||||
|
||||
Create a config file:
|
||||
|
||||
```sh
|
||||
$ cp example.config.env config.env
|
||||
```
|
||||
|
||||
Edit the config file, then run `./get-prices` regularly
|
||||
|
||||
## License (MIT)
|
||||
|
||||
**Copyright (c) 2022 Keith Irwin**
|
||||
|
||||
```
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
|
@ -0,0 +1,16 @@
|
|||
# Paths
|
||||
PRICES_FILE="~/hledger/$(date +%Y).prices"
|
||||
|
||||
# API KEYS
|
||||
POLYGON_API_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
||||
|
||||
# Assets
|
||||
declare -A assets; assets+=(
|
||||
# Key is the name that goes in the journal
|
||||
# Value is the ticker searched in the API
|
||||
["BTC"]="X:BTCUSD"
|
||||
["ETH"]="X:ETHUSD"
|
||||
["XMR"]="X:XMRUSD"
|
||||
["MKR"]="X:MKRUSD"
|
||||
["AMD"]="AMD"
|
||||
)
|
|
@ -0,0 +1,59 @@
|
|||
#!/bin/bash
|
||||
# hledger-prices
|
||||
#
|
||||
# Retrieve and update hledger prices file
|
||||
|
||||
|
||||
# Check bash version
|
||||
# https://askubuntu.com/a/916978/533341
|
||||
if [ ! "${BASH_VERSINFO:-0}" -ge 4 ]; then
|
||||
echo "Bash version 4 or higher required!"
|
||||
echo "Your version: ${BASH_VERSION}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Import config.env
|
||||
if [ ! -f "config.env" ]; then
|
||||
echo "config.env not found!"
|
||||
exit 2
|
||||
fi; . config.env
|
||||
if [ "${PRICES_FILE}" == "" ]; then
|
||||
echo "PRICES_FILE is not defined in config.env"
|
||||
exit 3
|
||||
elif [ "${POLYGON_API_KEY}" == "" ]; then
|
||||
echo "POLYGON_API_KEY is not defined in config.env"
|
||||
exit 3
|
||||
fi
|
||||
|
||||
# Create prices file if it doesn't exist
|
||||
if [ ! -f "${PRICES_FILE}" ]; then
|
||||
mkdir -p "$(dirname PRICES_FILE)" 2>/dev/null
|
||||
touch "$PRICES_FILE"
|
||||
fi
|
||||
|
||||
# Do the thing
|
||||
for asset in ${!assets[@]}; do
|
||||
echo "Checking $asset..."
|
||||
url="https://api.polygon.io/v2/aggs/ticker/${assets[${asset}]}/prev?apiKey=${POLYGON_API_KEY}"
|
||||
echo " URL: $url"
|
||||
res=$(curl --silent "$url")
|
||||
|
||||
# Check the result
|
||||
if [ "$(echo $res | jq -r '.status')" == "ERROR" ]; then
|
||||
echo " $(echo $res | jq -r '.error')"
|
||||
elif [ "$(echo $res | jq -r '.resultsCount')" == "0" ]; then
|
||||
echo " No results found!"
|
||||
elif [ "$(echo $res | jq -r '.results[0].vw')" == "null" ]; then
|
||||
echo " Result was null!"
|
||||
|
||||
# Looks good; add it
|
||||
else
|
||||
price=$(echo $res | jq '.results[0].vw')
|
||||
echo " Price: $price"
|
||||
line="P $(date -I --date=yesterday) $asset"
|
||||
grep "$line" "$PRICES_FILE" \
|
||||
|| echo "$line \$$price" >> "$PRICES_FILE"
|
||||
sleep 10 # Optional, check API limits
|
||||
fi
|
||||
|
||||
done
|
Loading…
Reference in New Issue