1
0
mirror of https://github.com/Llewellynvdm/CoinRate.git synced 2024-11-21 18:45:10 +00:00

added the percentage option

This commit is contained in:
Llewellyn van der Merwe 2018-01-01 15:08:10 +02:00
parent 6cf42ef064
commit 7ead918d33
No known key found for this signature in database
GPG Key ID: CAD7B16D27AF28C5
3 changed files with 99 additions and 4 deletions

View File

@ -18,6 +18,39 @@
# FUNCTIONS # FUNCTIONS
#================================================================================================================================= #=================================================================================================================================
# workout the values based on percentage at which to send/show notice
function setPercentage () {
# get price if not already set
get_Price
# get the price value
local value="${CurrencyPair[${Currency}${Target}]}"
# check that we have a value
if [ "${value}" != "null" ]; then
# new value
COINnewValue="${Currency}${Target} $value"
# check if we have price set before
COINlineNr=$( awk "/${Currency}${Target}/{ print NR; exit }" "$COINvaluePath" )
re='^[0-9]+$'
# Update coin value keeper
if ! [[ $COINlineNr =~ $re ]] ; then
# set the price for the first time and send notice
echo "${COINnewValue}" >> "$COINvaluePath"
else
# set updater
COINupdate=1
# old price found
COINoldValue=$(sed -n "${COINlineNr}p" < "$COINvaluePath")
# get the keys
IFS=$' '
local oldArray=( $COINoldValue )
# set the value
value="${oldArray[1]}"
fi
# set the above below values
setAboveBelowValues "${value}"
fi
}
# run with the advance field options # run with the advance field options
function runFactory () { function runFactory () {
# array of repos # array of repos
@ -110,7 +143,7 @@ function getActiveCurrencyTarget () {
# get price if not already set # get price if not already set
get_Price get_Price
# get the price value # get the price value
value="${CurrencyPair[${Currency}${Target}]}" local value="${CurrencyPair[${Currency}${Target}]}"
# check that we have a value # check that we have a value
if [ "${value}" != "null" ]; then if [ "${value}" != "null" ]; then
# set send key # set send key
@ -202,11 +235,42 @@ function preform () {
then then
# set message since we are above target value # set message since we are above target value
setMessage "$target_type" "$current_value" "$target_value" setMessage "$target_type" "$current_value" "$target_value"
# if we run percentage we must update local coin value watcher
if (( "$COINupdate" == 1 )); then
# update the old price
sed -i "${COINlineNr}s/$COINoldValue/$COINnewValue/" "$COINvaluePath"
fi
else else
echoTweak "${Currency} not ${target_type} ${target_value}${Target} at this time!" echoTweak "${Currency} not ${target_type} ${target_value}${Target} at this time!"
fi fi
} }
# set the above and below value based on percentage
function setAboveBelowValues () {
# set Args
local value="$1"
# get the percent
local percent=$(echo "scale=20; $Percentage/100*$value" | bc)
# check if this is already set
if (( "$COINupdate" == 0 )); then
# adapt
local perFirst=$(echo "$Percentage*2" | bc)
# get the percent
local centFirst=$(echo "scale=20; $perFirst/100*$value" | bc)
# ajust value
value=$(echo "scale=20; $centFirst - $value" | bc | awk ' sub("\\.*0+$","") ')
fi
# get above value
TargetAboveValue=$(echo "scale=20; $percent + $value" | bc | awk ' sub("\\.*0+$","") ')
# get below value
TargetBelowValue=$(echo "scale=20; $value - $percent" | bc | awk ' sub("\\.*0+$","") ')
# set the switches
TargetBelow=1
BelowValue=1
TargetAbove=1
AboveValue=1
}
# set message # set message
function setMessage () { function setMessage () {
# set Args # set Args

View File

@ -32,6 +32,10 @@ function main () {
# run the factory # run the factory
runFactory runFactory
else else
# if Percentage is active update above and below based on history
if (( "$PercentSwitch" == 1 )); then
setPercentage
fi
# run basic price get # run basic price get
runBasicGet runBasicGet
fi fi
@ -62,7 +66,8 @@ Getting Coin Value in Fiat Currency at set price
1 = once per/hour 1 = once per/hour
2 = everyTime (default) 2 = everyTime (default)
3 = only once 3 = only once
-v Value (above or below) at which to send/send notice -p The percentage up or down at which to send/show notice
-v Value (above or below) at which to send/show notice
example: 17000 or 14000,15000 example: 17000 or 14000,15000
-A Value Above at which to send notice -A Value Above at which to send notice
example: 17000 or 19000,18000 example: 17000 or 19000,18000
@ -101,7 +106,7 @@ exit 1
# http://mywiki.wooledge.org/BashFAQ/035 # http://mywiki.wooledge.org/BashFAQ/035
# http://wiki.bash-hackers.org/howto/getopts_tutorial # http://wiki.bash-hackers.org/howto/getopts_tutorial
while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:k opt; do while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:kp: opt; do
case $opt in case $opt in
I) I)
if (( "$OPTARG" == 2 )); then if (( "$OPTARG" == 2 )); then
@ -124,6 +129,10 @@ while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:k opt; do
o) o)
sendSwitch=$OPTARG sendSwitch=$OPTARG
;; ;;
p)
Percentage=$OPTARG
PercentSwitch=1
;;
v) v)
TargetValue=$OPTARG TargetValue=$OPTARG
TargetAll=1 TargetAll=1
@ -131,10 +140,12 @@ while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:k opt; do
B) B)
TargetBelowValue=$OPTARG TargetBelowValue=$OPTARG
TargetBelow=1 TargetBelow=1
BelowValue=1
;; ;;
A) A)
TargetAboveValue=$OPTARG TargetAboveValue=$OPTARG
TargetAbove=1 TargetAbove=1
AboveValue=1
;; ;;
b) b)
BelowValue=1 BelowValue=1
@ -207,7 +218,7 @@ done
# set call ID # set call ID
CALL_ID=$(echo -n "${API_target}${smsID}${smstoID}${TelegramID}" | md5sum | sed 's/ .*$//') CALL_ID=$(echo -n "${API_target}${smsID}${smstoID}${TelegramID}" | md5sum | sed 's/ .*$//')
# BUILD Cointracker file per/API # BUILD Cointracker file per/API (user)
COINTracker="${VDMHOME}/.cointracker_${CALL_ID}" COINTracker="${VDMHOME}/.cointracker_${CALL_ID}"
# make sure the tracker file is set # make sure the tracker file is set
if [ ! -f "$COINTracker" ] if [ ! -f "$COINTracker" ]
@ -215,5 +226,15 @@ then
> "$COINTracker" > "$COINTracker"
fi fi
# only set if we have percentage
if (( "$PercentSwitch" == 1 )); then
# BUILD Coin value tracker file per/API (user)
COINvaluePath="${VDMHOME}/.coinvalue_${CALL_ID}"
# make sure the tracker file is set
if [ ! -f "$COINvaluePath" ]
then
> "$COINvaluePath"
fi
fi
# Run the script # Run the script
main main

10
main.sh
View File

@ -27,6 +27,7 @@ command -v curl >/dev/null 2>&1 || { echo >&2 "We require curl for this script t
command -v bc >/dev/null 2>&1 || { echo >&2 "We require bc for this script to run, but it's not installed. Aborting."; exit 1; } command -v bc >/dev/null 2>&1 || { echo >&2 "We require bc for this script to run, but it's not installed. Aborting."; exit 1; }
command -v sed >/dev/null 2>&1 || { echo >&2 "We require sed for this script to run, but it's not installed. Aborting."; exit 1; } command -v sed >/dev/null 2>&1 || { echo >&2 "We require sed for this script to run, but it's not installed. Aborting."; exit 1; }
command -v md5sum >/dev/null 2>&1 || { echo >&2 "We require md5sum for this script to run, but it's not installed. Aborting."; exit 1; } command -v md5sum >/dev/null 2>&1 || { echo >&2 "We require md5sum for this script to run, but it's not installed. Aborting."; exit 1; }
command -v awk >/dev/null 2>&1 || { echo >&2 "We require awk for this script to run, but it's not installed. Aborting."; exit 1; }
# load notify # load notify
. "$DIR/functions.sh" . "$DIR/functions.sh"
@ -51,6 +52,8 @@ TargetBelow=0
TargetAbove=0 TargetAbove=0
BelowValue=0 BelowValue=0
AboveValue=0 AboveValue=0
Percentage=0
PercentSwitch=0
# message settings # message settings
send=0 send=0
@ -72,6 +75,13 @@ API_cex="https://cex.io/api/last_price/" # (default)
API_shapeshift="https://shapeshift.io/rate/" API_shapeshift="https://shapeshift.io/rate/"
FilePath='' FilePath=''
# percentage values
COINvaluePath="null"
COINnewValue="null"
COINoldValue="null"
COINlineNr="null"
COINupdate=0
# Some Messages arrays # Some Messages arrays
declare -A CurrencyPair declare -A CurrencyPair
declare -A aboveMessages declare -A aboveMessages