slowandsteady has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; use List::Util qw(min max); use GD::Graph::candlesticks; my @msft = ( # open high low close ["2007/12/18", "34.6400", "35.0000", "34.2100", "34.7400"], ["2007/12/19", "34.6900", "35.1400", "34.3800", "34.7900"], ["2007/12/20", "35.2900", "35.7900", "35.0800", "35.5200"], ["2007/12/21", "35.9000", "36.0600", "35.7500", "36.0600"], ["2007/12/24", "36.1300", "36.7200", "36.0500", "36.5800"], ["2007/12/26", "36.4100", "36.6400", "36.2600", "36.6100"], ["2007/12/27", "36.3500", "36.5500", "35.9400", "35.9700"], ["2007/12/28", "36.1000", "36.2300", "35.6700", "36.1200"], ["2007/12/31", "35.9000", "35.9900", "35.5200", "35.6000"], ["2008/01/02", "35.7900", "35.9600", "35.0000", "35.2200"], ["2008/01/03", "35.2200", "35.6500", "34.8600", "35.3700"], ["2008/01/04", "35.1900", "35.2000", "34.0900", "34.3800"], ["2008/01/07", "34.5500", "34.8000", "34.2500", "34.6100"], ["2008/01/08", "34.7100", "34.7100", "33.4000", "33.4500"], ["2008/01/09", "33.3600", "34.5400", "33.3500", "34.4400"], ["2008/01/10", "34.3500", "34.5000", "33.7800", "34.3300"], ["2008/01/11", "34.1400", "34.2400", "33.7200", "33.9100"], ["2008/01/14", "34.4600", "34.5700", "34.0800", "34.3900"], ["2008/01/15", "34.0300", "34.3800", "34.0000", "34.0000"], ["2008/01/16", "33.4200", "33.6500", "32.5100", "33.2300"], ["2008/01/17", "33.5400", "33.8000", "32.9700", "33.1100"], ["2008/01/18", "33.1600", "34.0000", "32.9700", "33.0100"], ["2008/01/22", "31.5400", "32.5300", "31.5000", "31.9600"], ["2008/01/23", "31.4800", "32.0500", "31.0400", "31.9300"], ["2008/01/24", "32.3500", "33.3600", "32.1200", "33.2500"], ["2008/01/25", "34.9000", "35.0000", "32.8700", "32.9400"], ["2008/01/28", "33.0200", "33.1000", "32.4200", "32.7200"], ["2008/01/29", "32.8500", "32.8900", "32.3500", "32.6000"], ["2008/01/30", "32.5600", "32.8000", "32.0500", "32.2000"], ["2008/01/31", "31.9100", "32.7400", "31.7200", "32.6000"], ); my @all_points = map {@$_1 .. 4} @msft; my $min_point = min(@all_points); my $max_point = max(@all_points); my $graph = GD::Graph::candlesticks->new(800, 400); $graph->set( x_labels_vertical => 1, x_label => 'Trade Date', y_label => 'NASDAQ:MSFT', title => "Example OHLC", transparent => 0, candlestick_width => 7, dclrs => qw(blue), y_min_value => $min_point-0.2, y_max_value => $max_point+0.2, y_number_format => '%0.2f', ) or warn $graph->error; my $data_candlesticks = [ [ map {$_->[0]} @msft ], # date [ map {[@$_1 .. 4]} @msft ], # candlesticks ]; my $gd = $graph->plot($data_candlesticks) or die $graph->error; open my $dump, ">candlesticks_example.png" or die $!; print $dump $gd->png; close $dump;
i want to use a csv file which contains
[2007/12/28, 36.1000, 36.2300, 35.6700, 36.1200] [2007/12/29, 36.1100, 36.2400, 35.6100, 36.1000]
etc.

to populate @msft above
help please

Replies are listed 'Best First'.
Re: candlestick from csv
by desemondo (Hermit) on Dec 17, 2009 at 03:15 UTC
    Firstly, welcome to the Monastery.

    Good effort on trying to use <code></code> tags, but you would have noticed that your post didn't look quite right when you clicked preview, before clicking create. Could you please edit your post and fix it up.

    (from what I can see it looks like you've put every line inside a <p> tag, which isn't necesssary. You're meant to use 1 set of <p> tags around an entire paragraph, not each line. And if you've opened a <p> tag you need to close it </p> before opening a <code> tag.)

    Your question, "i want to use a csv file which contains 2007/12/28, 36.1000, 36.2300, 35.6700, 36.1200
    2007/12/29, 36.1100, 36.2400, 35.6100, 36.1000
    etc.
    to populate @msft above
    help please
    "

    I'll leave you with a few hints, and leave you to work out the solution.

    1. You'll need to open a file handle to your csv file.
    2. You'll need to read the file with some kind of loop.
    while(my $line = <$fh>){ #block }

    3. Then you'll need to push it onto your @msft array.

    happy learning, and remember Perldoc is your friend when writing code. Even the saints among us still refer to it.

      Hope this helps

      my @msft = (); open(F,"FILE_NAME.txt") or die "Cannot open File:$!\n"; while (<F>){ chomp; my $tuple; @$tuple = split(/,/, $_); push (@msft, $tuple); }
Re: candlestick from csv
by karlgoethebier (Abbot) on May 23, 2015 at 15:41 UTC

    If your data really looks like this...

    ["2007/12/18", "34.6400", "35.0000", "34.2100", "34.7400"], ["2007/12/19", "34.6900", "35.1400", "34.3800", "34.7900"], ["2007/12/20", "35.2900", "35.7900", "35.0800", "35.5200"],

    ...and i understood you right i would try something like this:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dump; use Path::Tiny; my $data = path("data.txt")->slurp; # $data = qq(\($data\)); $data = qq{($data)}; my @data = eval($data); dd \@data; __END__ karls-mac-mini:monks karl$ ./slowandsteady.pl [ ["2007/12/18", "34.6400", "35.0000", "34.2100", "34.7400"], ["2007/12/19", "34.6900", "35.1400", "34.3800", "34.7900"], ["2007/12/20", "35.2900", "35.7900", "35.0800", "35.5200"], ]

    See also perldsc, eval and Path::Tiny for further information.

    Edit: Removed frivolous escapades.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Note that  qq// and its ilk are capable of handling balanced paired embedded delimiters  () {} [] <> without frivolous escapades:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $data = 'this is my data'; my $s = qq(($data)); print qq{'$s'}; " '(this is my data)'


      Give a man a fish:  <%-(-(-(-<

        "... without frivolous escapades"

        Thank you for the hint. I should have known this.

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»