Welcome to It-Slav.Net blog
Peter Andersson
peter@it-slav.net

I've already got a female to worry about. Her name is the Enterprise.
-- Kirk, "The Corbomite Maneuver", stardate 1514.0
11
Dec

As everybody else I’m dreaming of getting rich. I live in Sweden, the country with the highest taxes in the world so getting rich by working is impossible. One way of getting rich is winning a lottery and probably the only social accepted way of getting rich in Sweden.

One way of getting rich is buying the correct stock at the right time and price, and sell it after a while for much more. The chance of picking the ultimate stock is very low and I think it can be compared to winning a lottery. The probability of picking a not very bad stock at not that very bad time is higher if you have some knowledge and uses some tools.

I’m a computer geek that totally failed to study Economics at the university (I did that in parallel with my Master Of Science studies) mainly because it was not science, it was more of black magic.

So the conclusion is that I probably will not get rich by my knowledge in Economy but why not try to do it more fun by creating my own graphs of stock values:-)

#!/usr/bin/perl
#Script that graphs some stocks
#peter@it-slav.net
use RRDs;
use Finance::YahooQuote;
$Finance::YahooQuote::TIMEOUT = 60;
useExtendedQueryFormat();

my @symbols=('SWEDA.ST', 'CLASB.ST', 'AXIS.ST', "NOVL", 'ERICB.ST', "INVEB.ST", "\^OMXSPI" );

my $rrd = '/var/rrd_db/stocks';
# define location of images
my $img = '/home/peter/public_html/stocks';

my $cur_time = time();                # set current time
my $end_time = $cur_time ;     # set end

#$value=int(rand(1000));

foreach (@symbols) {
        @quote = getonequote ("$_"); # Get a quote for a single symbol
        #print"@quote\n";
        $quote[0]=&trim($quote[0]);
        print "Ticker:$quote[0]\tName:$quote[1]\tLast_price:$quote[2]\t50d_Mov_Avg:$quote[35]\n";
        if ($quote[35] eq "N/A") {
                $quote[35]=0;
        }

        #&graph (ticket,description,price,movingavarage-50-days)
        &graph ($quote[0],$quote[1],$quote[2],$quote[35]);
}

sub graph
{

        # if rrdtool database doesn't exist, create it
        if (! -e "$rrd/stock$_[0].rrd")
        {
                print "creating rrd database for stock $_[0]...\n";
                RRDs::create "$rrd/stock$_[0].rrd",
                        "-s 86400", #1 day = 24h/day*60min/h*60sec/min= 86400 sec/day
                        "DS:price:GAUGE:172800:U:U", #Heartbeat 2 days = 172800 sec
                         "RRA:AVERAGE:0.5:1:365", #1 year 365 days
                         "RRA:AVERAGE:0.5:1:3650", #10 years 10*365=3650
                        "DS:movingavarage:GAUGE:172800:U:U", #Heartbeat 2 days = 172800 sec
                         "RRA:AVERAGE:0.5:1:365", #1 year 365 days
                         "RRA:AVERAGE:0.5:1:3650"; #10 years 10*365=3650

        }
        if ($ERROR = RRDs::error) { print "$0: failed to create $_[0] database file: $ERROR\n"; }

        # insert values into rrd
        #print "Insert:$_[2]:$_[3] into $rrd/mail$_[0].rrd\n";
        RRDs::update "$rrd/stock$_[0].rrd",
                        "-t", "price:movingavarage",
                        "N:$_[2]:$_[3]";
        if ($ERROR = RRDs::error) { print "$0: failed to insert $_[0] data into rrd: $ERROR\n"; }

        #print "$_[0], \"year\", $_[2], $start_time_year, $_[3], $_[1]\n";
        #SWEDA.ST, "year", 75.75, 1193058922, 127.669, SWEDBANK -A-
        if ($_[3] != 0 ) {
                &CreateGraph($_[0], "year", $start_time_year, "Aktie:$_[1] Ticker:$_[0]");
        } else {
                &CreateGraphLight($_[0], "year", $start_time_year, "Aktie:$_[1] Ticker:$_[0]");
        }
        #&CreateGraph($_[0], "decade", $start_time_decade, $_[1]);

}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: ticketname
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: starttime
#         $_[3]: Full name

        RRDs::graph "$img/stocks$_[0]-$_[1].png",
#                "-s -1$_[1]",
                "-t $_[3]",
#                "--lazy",
                "--start=$_[2]",
                "--end=$end_time",
                "-h", "500", "-w", "1100",
                "-a", "PNG",
                "-v It-Slav.Net",
                "--slope-mode",
                "DEF:price=$rrd/stock$_[0].rrd:price:AVERAGE",
                "LINE2:price#FF0000:Kurs:",
                "DEF:movingavarage=$rrd/stock$_[0].rrd:movingavarage:AVERAGE",
                "LINE1:movingavarage#0000FF:Medelvarde 50 dagar:",
                "GPRINT:price:MIN:  Min\\: %6.lf",
                "GPRINT:price:MAX:  Max\\: %6.lf",
                "GPRINT:price:AVERAGE: Avarage\\: %6.lf",
                "GPRINT:price:LAST: Last\\: %6.lf $_[0]\\n";
        if ($ERROR = RRDs::error) { print "$0: unable to generate graph $_[0] $_[1] graph: $ERROR\n"; }
}

sub CreateGraphLight
{
# creates graph
# inputs: $_[0]: ticketname
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: starttime
#         $_[3]: Full name

        RRDs::graph "$img/stocks$_[0]-$_[1].png",
#                "-s -1$_[1]",
                "-t $_[3]",
#                "--lazy",
                "--start=$_[2]",
                "--end=$end_time",
                "-h", "500", "-w", "1100",
                "-a", "PNG",
                "-v It-Slav.Net",
                "--slope-mode",
                "DEF:price=$rrd/stock$_[0].rrd:price:AVERAGE",
                "LINE2:price#FF0000:Kurs:",
#                "DEF:movingavarage=$rrd/stock$_[0].rrd:movingavarage:AVERAGE",
#                "LINE1:price#0000FF:Movingavarage_50days:",
                "GPRINT:price:MIN:  Min\\: %6.lf",
                "GPRINT:price:MAX:  Max\\: %6.lf",
                "GPRINT:price:AVERAGE: Avarage\\: %6.lf",
                "GPRINT:price:LAST: Last\\: %6.lf $_[0]\\n";
        if ($ERROR = RRDs::error) { print "$0: unable to generate graph $_[0] $_[1] graph: $ERROR\n"; }
}

sub trim($)
{
        my $string = shift;
#       $string =~ s/^\s+//; #Remove left whitespaces
#       $string =~ s/\s+$//; #Remove right whitespaces
        $string =~ s/\^//; #Remove ^
        return $string;
}

7 Responses to “Stock graphs”

  1. digital eq | Digg hot tags Says:

    […] Vote Stock graphs […]

  2. home eq | Digg hot tags Says:

    […] Vote Stock graphs […]

  3. jeremiah Says:

    Great post! And the code is very useful too. I have been using GD to do graphs with perl, (I graph access.log hits: http://yaalr.org/graphs.html), which I think is pretty good, but nothing beats using RRD tools for creating sweet, good-looking graphs. I think I may have to steal some of your code!

  4. admin Says:

    You could click on some of the google ads and the it is not stealing 🙂

    I forgot to tell that I run this as a cronjob every day but that is probably obvius.

    Regards
    Peter Andersson

  5. Dmitry Says:

    Peter,

    I have come across your post while I was looking for some info on RDD. Great sense of humor and amazing use of RDD.
    I am trying to get rich and use RDD for this too 🙂

    Regards,
    Dmitry

  6. Bjarne Says:

    Nice, thanks for sharing!!!

    One minor correction: Sweden is no longer the country with the highest tax rate, we recently beat you in Denmark, see: http://www.thelocal.se/10402/20080311/ – wuhuu, we win… NOT :o)

    Cheers!

  7. Jim Says:

    Trying to get your script going…..

    What parameters, if any, do you give the script when you run it?

    I’m finding errors like:
    stock_graphs.plx: unable to generate graph ERIC-B.ST year graph: start time: unparsable time:

Leave a Reply

You must be logged in to post a comment.

Filled Under: english, Geek stuff, graph, Hints




Book reviews
FreePBX 2.5
Powerful Telephony Solutions






Asterisk 1.6
Build a feature rich telephony system with Asterisk






Learning NAGIOS 3.0





Cacti 0.8 Network Monitoring,
Monitor your network with ease!