From ba1113d21b387cc1d42288f48a4dd029cfe5a0b2 Mon Sep 17 00:00:00 2001 From: Arne Baeumler Date: Sun, 7 Feb 2021 19:28:55 +0100 Subject: [PATCH] crypto Alert to matrix --- scripts/cryptoAlert.pl | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 scripts/cryptoAlert.pl diff --git a/scripts/cryptoAlert.pl b/scripts/cryptoAlert.pl new file mode 100755 index 0000000..6de67a7 --- /dev/null +++ b/scripts/cryptoAlert.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl +# +use strict; +use warnings; +use RRDs; +use Data::Dumper; +use Template; + +my $file = $ARGV[0] or die("Usage: $0 csv\n"); + +my $rrdpath = "/var/www/localhost/rrd"; +my $cryptoMap = { + "AAVE" => "AAVE", + "ALGO" => "Algorand", + "BTC" => "Bitcoin", + "CVC" => "Civic", + "DNT" => "district0x", + "EGLD" => "Elrond eGold", + "ETH" => "Etherium", + "LRC" => "Loopring", + "REN" => "REN", + "SNX" => "Synthetix", + "XLM" => "Stellar Lumen", + "XTZ" => "Tezos" +}; +my $v; +my $alarmThresh = 15; + +sub getCurrentPrice { + my ($symbol) = @_; + my ($start,$step,$name,$data) = RRDs::fetch("${rrdpath}/crypto-${symbol}.rrd","LAST","--start","-10m"); + return($data->[0]->[0]); +} + +sub getLast4hAverage { + my ($symbol) = @_; + my ($start,$step,$name,$data) = RRDs::fetch("${rrdpath}/crypto-${symbol}.rrd","AVERAGE","--start","-4h","-r",'4h'); + return($data->[0]->[0]); +} + +open(CSV,"<$file") or die("open($file): $!\n"); + +while () { + chomp($_); + my ($date,$currency,$coin,$price,$fee,$buyrate,$sellthres) = split /;/; + my $curPrice = sprintf("%.4f",&getCurrentPrice($currency)); + my $avg4hPrice = sprintf("%.4f",&getLast4hAverage($currency)); + if($avg4hPrice > ((1 + int($alarmThresh)/100) * $curPrice)) { + $v->{$currency} = "current price dropped by more than ${alarmThresh}% from $avg4hPrice Euro/$currency to $curPrice Euro/$currency within 4h"; + } +} +close(CSV); + +foreach my $c (sort keys %{$v}) { + print "$cryptoMap->{$c}: $v->{$c}
\n"; +}