From 7ead918d33049105da16474676d61c538cfc2df8 Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Mon, 1 Jan 2018 15:08:10 +0200 Subject: [PATCH] added the percentage option --- functions.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++- getPrice.sh | 27 ++++++++++++++++++--- main.sh | 10 ++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/functions.sh b/functions.sh index 005e9fd..9c8505d 100644 --- a/functions.sh +++ b/functions.sh @@ -18,6 +18,39 @@ # 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 function runFactory () { # array of repos @@ -110,7 +143,7 @@ function getActiveCurrencyTarget () { # get price if not already set get_Price # get the price value - value="${CurrencyPair[${Currency}${Target}]}" + local value="${CurrencyPair[${Currency}${Target}]}" # check that we have a value if [ "${value}" != "null" ]; then # set send key @@ -202,11 +235,42 @@ function preform () { then # set message since we are above 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 echoTweak "${Currency} not ${target_type} ${target_value}${Target} at this time!" 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 function setMessage () { # set Args diff --git a/getPrice.sh b/getPrice.sh index 7442015..f1b1cb1 100755 --- a/getPrice.sh +++ b/getPrice.sh @@ -32,6 +32,10 @@ function main () { # run the factory runFactory else + # if Percentage is active update above and below based on history + if (( "$PercentSwitch" == 1 )); then + setPercentage + fi # run basic price get runBasicGet fi @@ -62,7 +66,8 @@ Getting Coin Value in Fiat Currency at set price 1 = once per/hour 2 = everyTime (default) 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 -A Value Above at which to send notice example: 17000 or 19000,18000 @@ -101,7 +106,7 @@ exit 1 # http://mywiki.wooledge.org/BashFAQ/035 # 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 I) 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) sendSwitch=$OPTARG ;; + p) + Percentage=$OPTARG + PercentSwitch=1 + ;; v) TargetValue=$OPTARG TargetAll=1 @@ -131,10 +140,12 @@ while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:k opt; do B) TargetBelowValue=$OPTARG TargetBelow=1 + BelowValue=1 ;; A) TargetAboveValue=$OPTARG TargetAbove=1 + AboveValue=1 ;; b) BelowValue=1 @@ -207,7 +218,7 @@ done # set call ID 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}" # make sure the tracker file is set if [ ! -f "$COINTracker" ] @@ -215,5 +226,15 @@ then > "$COINTracker" 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 main diff --git a/main.sh b/main.sh index 24ef5a7..bdcfdeb 100644 --- a/main.sh +++ b/main.sh @@ -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 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 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 . "$DIR/functions.sh" @@ -51,6 +52,8 @@ TargetBelow=0 TargetAbove=0 BelowValue=0 AboveValue=0 +Percentage=0 +PercentSwitch=0 # message settings send=0 @@ -72,6 +75,13 @@ API_cex="https://cex.io/api/last_price/" # (default) API_shapeshift="https://shapeshift.io/rate/" FilePath='' +# percentage values +COINvaluePath="null" +COINnewValue="null" +COINoldValue="null" +COINlineNr="null" +COINupdate=0 + # Some Messages arrays declare -A CurrencyPair declare -A aboveMessages