| 1 | #!/usr/bin/env perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use PDL; | ||
| 5 | use PDL::NiceSlice; | ||
| 6 | use PDL::Finance::TA; | ||
| 7 | use PDL::Graphics::Gnuplot; | ||
| 8 | use JSON::XS qw(decode_json); | ||
| 9 | use LWP::UserAgent; | ||
| 10 | use DateTime; | ||
| 11 | use Try::Tiny; | ||
| 12 | use Path::Tiny; | ||
| 13 | |||
| 14 | sub get_data($) { | ||
| 15 | my $symbol = shift; | ||
| 16 | my $filename = lc "$symbol.json"; | ||
| 17 | my $content; | ||
| 18 | my $qdata; | ||
| 19 | my $url = sprintf("https://api.gemini.com/v2/candles/%s/%s", lc $symbol, '1day'); | ||
| 20 | if (-e $filename) { | ||
| 21 | print "Found $filename, loading data from that\n"; | ||
| 22 | $content = path($filename)->slurp; | ||
| 23 | } else { | ||
| 24 | my $lwp = LWP::UserAgent->new(timeout => 60); | ||
| 25 | $lwp->env_proxy; | ||
| 26 | my $resp = $lwp->get($url); | ||
| 27 | if ($resp->is_success) { | ||
| 28 | $content = $resp->decoded_content; | ||
| 29 | path($filename)->spew($content); | ||
| 30 | } else { | ||
| 31 | warn "Error from request to $url: " . $resp->status_line; | ||
| 32 | return undef; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | if (defined $content and length($content)) { | ||
| 36 | my $jquotes = decode_json $content; | ||
| 37 | if (ref $jquotes eq 'ARRAY' and scalar(@$jquotes)) { | ||
| 38 | ## sort quotes by timestamp | ||
| 39 | my @sorted = sort { $a->[0] <=> $b->[0] } @$jquotes; | ||
| 40 | foreach my $q (@sorted) { | ||
| 41 | ## timestamp is the first column in milliseconds | ||
| 42 | $q->[0] /= 1000; | ||
| 43 | } | ||
| 44 | ## convert the quotes to a PDL | ||
| 45 | $qdata = pdl(@sorted)->transpose; | ||
| 46 | } else { | ||
| 47 | warn "No quotes returned by $url or $filename"; | ||
| 48 | $qdata = undef; | ||
| 49 | } | ||
| 50 | } else { | ||
| 51 | warn "No content received from $url or $filename"; | ||
| 52 | $qdata = undef; | ||
| 53 | } | ||
| 54 | ## now we operate on the $qdata PDL object | ||
| 55 | return $qdata; | ||
| 56 | } | ||
| 57 | |||
| 58 | my $symbol = $ARGV[0] // 'DOGEUSD'; | ||
| 59 | my $qdata = get_data($symbol); | ||
| 60 | die "Unable to get data for $symbol" unless ref $qdata eq 'PDL'; | ||
| 61 | print $qdata; | ||
| 62 | |||
| 63 | my $timestamp = $qdata(, (0)); | ||
| 64 | my $open_px = $qdata(, (1)); | ||
| 65 | my $high_px = $qdata(, (2)); | ||
| 66 | my $low_px = $qdata(, (3)); | ||
| 67 | my $close_px = $qdata(, (4)); | ||
| 68 | ## use the default values | ||
| 69 | ## each of these are 1-D PDLs | ||
| 70 | my ($bb_upper, $bb_middle, $bb_lower) = PDL::ta_bbands($close_px, 5, 2, 2, 0); | ||
| 71 | my $buys = zeroes( $close_px->dims ); | ||
| 72 | my $sells = zeroes( $close_px->dims ); | ||
| 73 | ## use a 1 tick lookback | ||
| 74 | my $lookback = 1; | ||
| 75 | ## calculate the indexes of the lookback PDL based on LOW price | ||
| 76 | my $idx_0 = xvals( $low_px->dims ) - $lookback; | ||
| 77 | ## if the lookback index is negative set it to 0 | ||
| 78 | $idx_0 = $idx_0->setbadif( $idx_0 < 0 )->setbadtoval(0); | ||
| 79 | ## get the indexes of when the LOW Price < Lower Bollinger Band based on the lookback | ||
| 80 | my $idx_1 = which( | ||
| 81 | ($low_px->index($idx_0) > $bb_lower->index($idx_0)) & | ||
| 82 | ($low_px < $bb_lower) | ||
| 83 | ); | ||
| 84 | ## set the buys to be on the OPEN price for those indexes | ||
| 85 | $buys->index($idx_1) .= $open_px->index($idx_1); | ||
| 86 | ## set all 0 values to BAD to avoid plotting zeroes | ||
| 87 | $buys->inplace->setvaltobad(0); | ||
| 88 | |||
| 89 | ## calculate the indexes of the lookback PDL based on HIGH price | ||
| 90 | my $idx_2 = xvals( $high_px->dims ) - $lookback; | ||
| 91 | ## if the lookback index is negative set it to 0 | ||
| 92 | $idx_2 = $idx_2->setbadif( $idx_2 < 0 )->setbadtoval(0); | ||
| 93 | ## get the indexes of when the HIGH Price > Upper Bollinger Band based on the lookback | ||
| 94 | my $idx_3 = which( | ||
| 95 | ($high_px->index($idx_2) < $bb_upper->index($idx_2)) & | ||
| 96 | ($high_px > $bb_upper ) | ||
| 97 | ); | ||
| 98 | ## set the sells to be on the CLOSE price for those indexes | ||
| 99 | $sells->index($idx_3) .= $close_px->index($idx_3); | ||
| 100 | ## set all 0 values to BAD to avoid plotting zeroes | ||
| 101 | $sells->inplace->setvaltobad(0); | ||
| 102 | |||
| 103 | ## plot the data | ||
| 104 | my $pwin = gpwin(size => [1024, 768, 'px']); | ||
| 105 | $pwin->reset; | ||
| 106 | $pwin->multiplot; | ||
| 107 | $pwin->plot({ | ||
| 108 | object => '1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb "black" behind', | ||
| 109 | title => ["$symbol Open-High-Low-Close", textcolor => 'rgb "white"'], | ||
| 110 | key => ['on', 'outside', textcolor => 'rgb "yellow"'], | ||
| 111 | border => 'linecolor rgbcolor "white"', | ||
| 112 | xlabel => ['Date', textcolor => 'rgb "yellow"'], | ||
| 113 | ylabel => ['Price', textcolor => 'rgb "yellow"'], | ||
| 114 | xdata => 'time', | ||
| 115 | xtics => {format => '%Y-%m-%d', rotate => -90, textcolor => 'orange', }, | ||
| 116 | ytics => {textcolor => 'orange'}, | ||
| 117 | label => [1, $symbol, textcolor => 'rgb "cyan"', at => "graph 0.90,0.03"], | ||
| 118 | }, | ||
| 119 | { | ||
| 120 | with => 'financebars', | ||
| 121 | linecolor => 'white', | ||
| 122 | legend => 'Price', | ||
| 123 | }, | ||
| 124 | $timestamp, | ||
| 125 | $open_px, | ||
| 126 | $high_px, | ||
| 127 | $low_px, | ||
| 128 | $close_px, | ||
| 129 | ### Bollinger Bands plot | ||
| 130 | { | ||
| 131 | with => 'lines', | ||
| 132 | axes => 'x1y1', | ||
| 133 | linecolor => 'dark-green', | ||
| 134 | legend => 'Bollinger Band - Upper' | ||
| 135 | }, | ||
| 136 | $timestamp, | ||
| 137 | $bb_upper, #upper band | ||
| 138 | { | ||
| 139 | with => 'lines', | ||
| 140 | axes => 'x1y1', | ||
| 141 | linecolor => 'dark-magenta', | ||
| 142 | legend => 'Bollinger Band - Lower' | ||
| 143 | }, | ||
| 144 | $timestamp, | ||
| 145 | $bb_lower, #lower band | ||
| 146 | { | ||
| 147 | with => 'lines', | ||
| 148 | axes => 'x1y1', | ||
| 149 | linecolor => 'orange', | ||
| 150 | legend => 'Bollinger Band - Middle' | ||
| 151 | }, | ||
| 152 | $timestamp, | ||
| 153 | $bb_middle, #middle band | ||
| 154 | { | ||
| 155 | with => 'points', | ||
| 156 | pointtype => 5, #triangle | ||
| 157 | linecolor => 'green', | ||
| 158 | legend => 'Buys', | ||
| 159 | }, | ||
| 160 | $timestamp, | ||
| 161 | $buys, | ||
| 162 | { | ||
| 163 | with => 'points', | ||
| 164 | pointtype => 7, #inverted triangle | ||
| 165 | linecolor => 'red', | ||
| 166 | legend => 'Sells', | ||
| 167 | }, | ||
| 168 | $timestamp, | ||
| 169 | $sells, | ||
| 170 | ); | ||
| 171 | $pwin->end_multi; | ||
| 172 | |||
| 173 | $pwin->pause_until_close; |