| 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 JSON::XS qw(decode_json encode_json); | ||
| 8 | use LWP::UserAgent; | ||
| 9 | use DateTime; | ||
| 10 | use Try::Tiny; | ||
| 11 | use Path::Tiny; | ||
| 12 | use Template; | ||
| 13 | use Browser::Open; | ||
| 14 | |||
| 15 | sub get_data($) { | ||
| 16 | my $symbol = shift; | ||
| 17 | my $filename = lc "$symbol.json"; | ||
| 18 | my $content; | ||
| 19 | my $qdata; | ||
| 20 | my $url = sprintf("https://api.gemini.com/v2/candles/%s/%s", lc $symbol, '1day'); | ||
| 21 | if (-e $filename) { | ||
| 22 | print "Found $filename, loading data from that\n"; | ||
| 23 | $content = path($filename)->slurp; | ||
| 24 | } else { | ||
| 25 | my $lwp = LWP::UserAgent->new(timeout => 60); | ||
| 26 | $lwp->env_proxy; | ||
| 27 | my $resp = $lwp->get($url); | ||
| 28 | if ($resp->is_success) { | ||
| 29 | $content = $resp->decoded_content; | ||
| 30 | path($filename)->spew($content); | ||
| 31 | } else { | ||
| 32 | warn "Error from request to $url: " . $resp->status_line; | ||
| 33 | return undef; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | if (defined $content and length($content)) { | ||
| 37 | my $jquotes = decode_json $content; | ||
| 38 | if (ref $jquotes eq 'ARRAY' and scalar(@$jquotes)) { | ||
| 39 | ## sort quotes by timestamp | ||
| 40 | my @sorted = sort { $a->[0] <=> $b->[0] } @$jquotes; | ||
| 41 | foreach my $q (@sorted) { | ||
| 42 | ## timestamp is the first column in milliseconds | ||
| 43 | $q->[0] /= 1000; | ||
| 44 | } | ||
| 45 | ## convert the quotes to a PDL | ||
| 46 | $qdata = pdl(@sorted)->transpose; | ||
| 47 | } else { | ||
| 48 | warn "No quotes returned by $url or $filename"; | ||
| 49 | $qdata = undef; | ||
| 50 | } | ||
| 51 | } else { | ||
| 52 | warn "No content received from $url or $filename"; | ||
| 53 | $qdata = undef; | ||
| 54 | } | ||
| 55 | ## now we operate on the $qdata PDL object | ||
| 56 | return $qdata; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $symbol = $ARGV[0] // 'DOGEUSD'; | ||
| 60 | my $qdata = get_data($symbol); | ||
| 61 | die "Unable to get data for $symbol" unless ref $qdata eq 'PDL'; | ||
| 62 | print $qdata; | ||
| 63 | |||
| 64 | my $timestamp = $qdata(, (0)); | ||
| 65 | my $open_px = $qdata(, (1)); | ||
| 66 | my $high_px = $qdata(, (2)); | ||
| 67 | my $low_px = $qdata(, (3)); | ||
| 68 | my $close_px = $qdata(, (4)); | ||
| 69 | ## use the default values | ||
| 70 | ## each of these are 1-D PDLs | ||
| 71 | my ($bb_upper, $bb_middle, $bb_lower) = PDL::ta_bbands($close_px, 5, 2, 2, 0); | ||
| 72 | my $buys = zeroes( $close_px->dims ); | ||
| 73 | my $sells = zeroes( $close_px->dims ); | ||
| 74 | ## use a 1 tick lookback | ||
| 75 | my $lookback = 1; | ||
| 76 | ## calculate the indexes of the lookback PDL based on LOW price | ||
| 77 | my $idx_0 = xvals( $low_px->dims ) - $lookback; | ||
| 78 | ## if the lookback index is negative set it to 0 | ||
| 79 | $idx_0 = $idx_0->setbadif( $idx_0 < 0 )->setbadtoval(0); | ||
| 80 | ## get the indexes of when the LOW Price < Lower Bollinger Band based on the lookback | ||
| 81 | my $idx_1 = which( | ||
| 82 | ($low_px->index($idx_0) > $bb_lower->index($idx_0)) & | ||
| 83 | ($low_px < $bb_lower) | ||
| 84 | ); | ||
| 85 | ## set the buys to be on the OPEN price for those indexes | ||
| 86 | $buys->index($idx_1) .= $open_px->index($idx_1); | ||
| 87 | ## set all 0 values to BAD to avoid plotting zeroes | ||
| 88 | $buys->inplace->setvaltobad(0); | ||
| 89 | |||
| 90 | ## calculate the indexes of the lookback PDL based on HIGH price | ||
| 91 | my $idx_2 = xvals( $high_px->dims ) - $lookback; | ||
| 92 | ## if the lookback index is negative set it to 0 | ||
| 93 | $idx_2 = $idx_2->setbadif( $idx_2 < 0 )->setbadtoval(0); | ||
| 94 | ## get the indexes of when the HIGH Price > Upper Bollinger Band based on the lookback | ||
| 95 | my $idx_3 = which( | ||
| 96 | ($high_px->index($idx_2) < $bb_upper->index($idx_2)) & | ||
| 97 | ($high_px > $bb_upper ) | ||
| 98 | ); | ||
| 99 | ## set the sells to be on the CLOSE price for those indexes | ||
| 100 | $sells->index($idx_3) .= $close_px->index($idx_3); | ||
| 101 | ## set all 0 values to BAD to avoid plotting zeroes | ||
| 102 | $sells->inplace->setvaltobad(0); | ||
| 103 | |||
| 104 | ## plot the data using Javascript in a Browser | ||
| 105 | ## we have to create multiple objects | ||
| 106 | my @charts = (); | ||
| 107 | ## convert the PDL to javascript and write to a file | ||
| 108 | ## HighCharts requires timestamp in milliseconds | ||
| 109 | ## each object should have the 5 dimensions: timestamp_ms, open, high, low, close - hence we transpose the PDL | ||
| 110 | my $px_pdl_js = encode_json pdl($timestamp * 1000, $open_px, $high_px, $low_px, $close_px)->transpose->unpdl; | ||
| 111 | push @charts, { | ||
| 112 | title => $symbol, | ||
| 113 | data => $px_pdl_js, | ||
| 114 | type => 'candlestick', | ||
| 115 | id => lc "candlestick-$symbol", | ||
| 116 | y_axis => 0, | ||
| 117 | }; | ||
| 118 | ## add the indicator chart. Bollinger Bands are on the same axis as the price, so y_axis is 0 | ||
| 119 | ## we need to remove the BAD values completely from the new PDL | ||
| 120 | my $bb_upper_2 = pdl($timestamp * 1000, $bb_upper)->transpose; | ||
| 121 | my $bbu_idx = $bb_upper_2((1))->which; | ||
| 122 | my $bb_upper_clean = $bb_upper_2->dice_axis(1, $bbu_idx); | ||
| 123 | my $bb_upper_js = encode_json $bb_upper_clean->unpdl; | ||
| 124 | |||
| 125 | my $bb_middle_2 = pdl($timestamp * 1000, $bb_middle)->transpose; | ||
| 126 | my $bbm_idx = $bb_middle_2((1))->which; | ||
| 127 | my $bb_middle_clean = $bb_middle_2->dice_axis(1, $bbm_idx); | ||
| 128 | my $bb_middle_js = encode_json $bb_middle_clean->unpdl; | ||
| 129 | |||
| 130 | my $bb_lower_2 = pdl($timestamp * 1000, $bb_lower)->transpose; | ||
| 131 | my $bbl_idx = $bb_lower_2((1))->which; | ||
| 132 | my $bb_lower_clean = $bb_lower_2->dice_axis(1, $bbl_idx); | ||
| 133 | my $bb_lower_js = encode_json $bb_lower_clean->unpdl; | ||
| 134 | |||
| 135 | push @charts, { | ||
| 136 | title => 'Bollinger Band - Upper', | ||
| 137 | type => 'line', | ||
| 138 | data => $bb_upper_js, | ||
| 139 | id => lc "bb-upper-$symbol", | ||
| 140 | }, { | ||
| 141 | title => 'Bollinger Band - Middle', | ||
| 142 | type => 'line', | ||
| 143 | data => $bb_middle_js, | ||
| 144 | id => lc "bb-middle-$symbol", | ||
| 145 | }, { | ||
| 146 | title => 'Bollinger Band - Lower', | ||
| 147 | type => 'line', | ||
| 148 | data => $bb_lower_js, | ||
| 149 | id => lc "bb-lower-$symbol", | ||
| 150 | }; | ||
| 151 | |||
| 152 | ## for buys and sells we just want to avoid empty data | ||
| 153 | my $buys_2 = pdl($timestamp * 1000, $buys)->transpose; | ||
| 154 | my $bidx = $buys_2((1))->which;## check if !0 is true | ||
| 155 | my $clean_buys = $buys_2->dice_axis(1, $bidx); | ||
| 156 | my $buys_js = encode_json $clean_buys->unpdl; | ||
| 157 | push @charts, { | ||
| 158 | title => 'Buy Signals', | ||
| 159 | data => $buys_js, | ||
| 160 | y_axis => 0, | ||
| 161 | type => 'line', | ||
| 162 | marker_symbol => 'triangle', | ||
| 163 | marker_color => 'green', | ||
| 164 | is_signal => 1, | ||
| 165 | }; | ||
| 166 | |||
| 167 | my $sells_2 = pdl($timestamp * 1000, $sells)->transpose; | ||
| 168 | my $sidx = $sells_2((1))->which;## check if !0 is true | ||
| 169 | my $clean_sells = $sells_2->dice_axis(1, $sidx); | ||
| 170 | my $sells_js = encode_json $clean_sells->unpdl; | ||
| 171 | push @charts, { | ||
| 172 | title => 'Sell Signals', | ||
| 173 | data => $sells_js, | ||
| 174 | y_axis => 0, | ||
| 175 | type => 'line', | ||
| 176 | marker_symbol => 'triangle-down', | ||
| 177 | marker_color => 'red', | ||
| 178 | is_signal => 1, | ||
| 179 | }; | ||
| 180 | ## create variables to pass to the template | ||
| 181 | my $ttconf = { | ||
| 182 | page => { title => "Plot $symbol with HighCharts" }, | ||
| 183 | chart => { height => "600px", yaxes_index => [0], charts => \@charts, title => $symbol }, | ||
| 184 | }; | ||
| 185 | ## load a pre-designed Template file | ||
| 186 | my $ttcontent = do { local $/ = undef; <DATA> }; | ||
| 187 | ## dump it as a template file for the browser to load it | ||
| 188 | my $ttfile = path('pdlchart.tt')->realpath; | ||
| 189 | path($ttfile)->spew($ttcontent) unless -e $ttfile; | ||
| 190 | print "TTFile: $ttfile\n"; | ||
| 191 | my $htmlfile = path('pdlchart.html')->realpath; | ||
| 192 | print "HTMLFile: $htmlfile\n"; | ||
| 193 | |||
| 194 | my $tt = Template->new({ ABSOLUTE => 1 }); | ||
| 195 | my $ret = $tt->process("$ttfile", $ttconf, "$htmlfile", { binmode => ':utf8' }); | ||
| 196 | if ($ret) { | ||
| 197 | my $url = "file://$htmlfile"; | ||
| 198 | print "opening $url\n"; | ||
| 199 | my $ok = Browser::Open::open_browser($url, 1); | ||
| 200 | if (not defined $ok or $ok != 0) { | ||
| 201 | die "Failed to open $url in a browser. Return value: $ok"; | ||
| 202 | } else { | ||
| 203 | print "Successfully opened $url in browser\n"; | ||
| 204 | } | ||
| 205 | } else { | ||
| 206 | die "Error processing template $ttfile: " . $tt->error() . "\n"; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | __DATA__ |