| 1 | package App::financeta::data::gemini; | ||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use JSON::XS qw(decode_json); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DateTime; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use App::financeta::utils qw(dumper log_filter); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Log::Any '$log', filter => \&App::financeta::utils::log_filter; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use PDL::Lite;#for pdl | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||||
| 14 | 1 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||||
| 15 | #our @EXPORT_OK = (qw(ohlcv)); | ||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub ohlcv { | ||||||||||||||||||||||||||||
|
Subroutine
ohlcv: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 18 | 0 | my ($symbol, $start_date, $end_date) = @_; | |||||||||||||||||||||||||||||
| 19 | 0 | my $data; | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | try { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||||
| 21 | 0 | $log->info("Starting to download quotes for $symbol for date range: $start_date -> $end_date"); | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $start_time = (ref $start_date eq 'DateTime') ? $start_date->epoch() : DateTime->new($start_date)->set_time_zone('UTC')->epoch; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $end_time = (ref $end_date eq 'DateTime') ? $end_date->epoch() : DateTime->new($end_date)->set_time_zone('UTC')->epoch; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 24 | 0 | my $difftime = abs($end_time - $start_time); | |||||||||||||||||||||||||||||
| 25 | 0 | $log->debug("Start Date $start_date is $start_time in UNIX time"); | |||||||||||||||||||||||||||||
| 26 | 0 | $log->debug("End Date $end_date is $end_time in UNIX time"); | |||||||||||||||||||||||||||||
| 27 | 0 | my $granularity; | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ($difftime <= 86400) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 29 | 0 | $granularity = '1m'; | |||||||||||||||||||||||||||||
| 30 | } elsif ($difftime <= 5 * 86400) { | ||||||||||||||||||||||||||||||
| 31 | 0 | $granularity = '5m'; | |||||||||||||||||||||||||||||
| 32 | } elsif ($difftime <= 30 * 86400) { | ||||||||||||||||||||||||||||||
| 33 | 0 | $granularity = '15m'; | |||||||||||||||||||||||||||||
| 34 | } elsif ($difftime <= 60 * 86400) { | ||||||||||||||||||||||||||||||
| 35 | 0 | $granularity = '30m'; | |||||||||||||||||||||||||||||
| 36 | } elsif ($difftime <= 90 * 86400) { | ||||||||||||||||||||||||||||||
| 37 | 0 | $granularity = '1hr'; | |||||||||||||||||||||||||||||
| 38 | } elsif ($difftime <= 180 * 86400) { | ||||||||||||||||||||||||||||||
| 39 | 0 | $granularity = '6hr'; | |||||||||||||||||||||||||||||
| 40 | } else { | ||||||||||||||||||||||||||||||
| 41 | 0 | $granularity = '1day'; | |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||
| 43 | 0 | $log->debug("Granularity selected is $granularity"); | |||||||||||||||||||||||||||||
| 44 | 0 | my $url = sprintf ('https://api.gemini.com/v2/candles/%s/%s', lc($symbol), $granularity); | |||||||||||||||||||||||||||||
| 45 | 0 | $log->debug("Performing GET request to $url"); | |||||||||||||||||||||||||||||
| 46 | 0 | my $lwp = LWP::UserAgent->new(timeout => 60); | |||||||||||||||||||||||||||||
| 47 | 0 | $lwp->env_proxy; | |||||||||||||||||||||||||||||
| 48 | 0 | my $res = $lwp->get($url); | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 50 | 0 | my $content = $res->decoded_content; | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (defined $content and length($content)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $content and length $content
MC/DC0%0/2
defined $content and length $content
defined $content
length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
| |||||||||||||||||||||||||||||||
| 52 | 0 | my $jquotes = decode_json($content); | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (ref $jquotes eq 'ARRAY' and scalar(@$jquotes)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $jquotes eq "ARRAY" and scalar @$jquotes
MC/DC0%0/1
ref $jquotes eq "ARRAY" and scalar @$jquotes
ref $jquotes eq "ARRAY"
Truth table0%0/2
A
A= ref $jquotes eq "ARRAY"
| |||||||||||||||||||||||||||||||
| 54 | ## sort quotes by timestamp | ||||||||||||||||||||||||||||||
| 55 | 0 | my @sorted = sort { $a->[0] <=> $b->[0] } @$jquotes; | |||||||||||||||||||||||||||||
| 56 | 0 | $log->info("No. of rows downloaded: " . scalar(@sorted)); | |||||||||||||||||||||||||||||
| 57 | 0 | foreach my $q (@sorted) { | |||||||||||||||||||||||||||||
| 58 | 0 | $q->[0] /= 1000;#remove millisecond scale | |||||||||||||||||||||||||||||
| 59 | #push @quotes, pdl(@$q); | ||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | 0 | $data = pdl(@sorted); | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $log->debug("Conversion of the data into a PDL object completed") if defined $data; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 63 | 0 | $data = $data->transpose;##why is this done ? | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $log->debug("Transpose of the data into a PDL object completed") if defined $data; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||||
| 66 | 0 | $log->error("No quotes were returned in the content from $url: $content"); | |||||||||||||||||||||||||||||
| 67 | 0 | $data = undef; | |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||||
| 70 | 0 | $log->error("No content was returned from URL $url"); | |||||||||||||||||||||||||||||
| 71 | 0 | $data = undef; | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||||
| 74 | 0 | $log->error("Error getting URL $url: " . $res->status_line); | |||||||||||||||||||||||||||||
| 75 | 0 | $data = undef; | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 78 | 0 | $log->error(__PACKAGE__ . " Error: " . $_); | |||||||||||||||||||||||||||||
| 79 | 0 | $data = undef; | |||||||||||||||||||||||||||||
| 80 | 0 | }; | |||||||||||||||||||||||||||||
| 81 | 0 | return $data; | |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 1 | 1; | |||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||