App-financeta

 view release on metacpan or  search on metacpan

share/testpdljs.pl  view on Meta::CPAN

push @charts, {
    title => 'Bollinger Band - Upper',
    type => 'line',
    data => $bb_upper_js,
    id => lc "bb-upper-$symbol",
}, {
    title => 'Bollinger Band - Middle',
    type => 'line',
    data => $bb_middle_js,
    id => lc "bb-middle-$symbol",
}, {
    title => 'Bollinger Band - Lower',
    type => 'line',
    data => $bb_lower_js,
    id => lc "bb-lower-$symbol",
};

## for buys and sells we just want to avoid empty data
my $buys_2 = pdl($timestamp * 1000, $buys)->transpose;
my $bidx = $buys_2((1))->which;## check if !0 is true
my $clean_buys = $buys_2->dice_axis(1, $bidx);
my $buys_js = encode_json $clean_buys->unpdl;
push @charts, {
    title => 'Buy Signals',
    data => $buys_js,
    y_axis => 0,
    type => 'line',
    marker_symbol => 'triangle',
    marker_color => 'green',
    is_signal => 1,
};

my $sells_2 = pdl($timestamp * 1000, $sells)->transpose;
my $sidx = $sells_2((1))->which;## check if !0 is true
my $clean_sells = $sells_2->dice_axis(1, $sidx);
my $sells_js = encode_json $clean_sells->unpdl;
push @charts, {
    title => 'Sell Signals',
    data => $sells_js,
    y_axis => 0,
    type => 'line',
    marker_symbol => 'triangle-down',
    marker_color => 'red',
    is_signal => 1,
};
## create variables to pass to the template
my $ttconf = {
    page => { title => "Plot $symbol with HighCharts" },
    chart => { height => "600px", yaxes_index => [0], charts => \@charts, title => $symbol },
};
## load a pre-designed Template file 
my $ttcontent = do { local $/ = undef; <DATA> };
## dump it as a template file for the browser to load it
my $ttfile = path('pdlchart.tt')->realpath;
path($ttfile)->spew($ttcontent) unless -e $ttfile;
print "TTFile: $ttfile\n";
my $htmlfile = path('pdlchart.html')->realpath;
print "HTMLFile: $htmlfile\n";

my $tt = Template->new({ ABSOLUTE => 1 });
my $ret = $tt->process("$ttfile", $ttconf, "$htmlfile", { binmode => ':utf8' });
if ($ret) {
    my $url = "file://$htmlfile";
    print "opening $url\n";
    my $ok = Browser::Open::open_browser($url, 1);
    if (not defined $ok or $ok != 0) {
        die "Failed to open $url in a browser. Return value: $ok";
    } else {
        print "Successfully opened $url in browser\n";
    }
} else {
    die "Error processing template $ttfile: " . $tt->error() . "\n";
}


__DATA__
<!DOCTYPE HTML>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="copyright" content="App::financeta Authors">
    <meta name="author" content="Vikas N Kumar <vikas@cpan.org>">
    <meta name="description" content="App::financeta">
    <link rel="icon" href="chart-line-solid.png" type="image/png">
    <title>[% page.title %]</title>
    <script src="https://code.highcharts.com/stock/highstock.js"></script>
    <style>
    #chart-container {
        min-width: 600px;
        min-height: 400px;
        height: [% chart.height %];
        width: 95%;
        margin: 20px;
    };
    </style>
    </head>
    <body>
        <h1>[% page.title %]</h1>
        <hr/>
        <div id="chart-container">
        </div>
        <hr/>
    <script type="text/javascript">
        [% IF chart %]
        var yaxes = [];
        [% FOREACH el IN chart.yaxes_index %]
            [% SWITCH el %]
            [% CASE 0 %]
                yaxes.push({
                    labels: { align: 'left' },
                    height: "400px",
                    resize: { enabled: true },
                });
            [% CASE 1 %]
                yaxes.push({
                    labels: { align: 'left' },
                    top: "400px",
                    height: "200px",
                    opposite: true,
                    offset: 0,



( run in 2.332 seconds using v1.01-cache-2.11-cpan-5735350b133 )