GNU Cash i polskie instrumenty finansowe

Kategoria: FAQ, etykiety: oprogramowanie

Dodany: 2011-01-08 22:49 (zmodyfikowany: 2011-01-08 23:12)
Przez: jaccki

Wyświetleń: 6721

GNU Cash to program do zarządzania naszymi finansami. Jedną z dostępnych opcji jest automatyczne pobieranie danych o kursach instrumentów finansowych. W sieci znalazłem rozwiązanie, które pozwala na pobieranie notowań instrumentów notowanych na GPW (poprzez portal Bossa.pl). Przygotował je Pan Maciej Pasternacki Zobacz oryginał. I wielkie dzięki dla niego za to rozwiązanie.

Wstawiamy poniższy kod do katalogu Finance::Quote (/usr/share/perl5/Finance/Quote) i nazywamy go np. "Bossa.pm"

#!/usr/bin/perl -w
#    This modules is based on the Finance::Quote::ASEGR module
#
#    The code has been modified by Maciej Pasternacki <maciekp@japhy.fnord.org>
#    to be able to retrieve Polish stock and  mutual fund information
#    from the Bossa.pl service.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#    02111-1307, USA
require 5.005;

use strict;

package Finance::Quote::Bossa;

use vars qw($VERSION $NDOHLCV_URL);

use LWP::UserAgent;
use HTTP::Request::Common;

$VERSION='0.1';

my $NDOHLCV_URL = 'http://bossa.pl/pub/ciagle/omega/cgl/ndohlcv.txt';


sub methods { return ( poland => \&bossa,
                       bossa => \&bossa,
                       europe => \&bossa); }
{
    my @labels = qw/name date isodate open high low close last currency method/;

    sub labels { return (poland => \@labels,
                 bossa => \@labels,
                 europe => \@labels); } 
}

sub bossa {
    my $quoter = shift;
    my @stocks = @_;
    my (%info,@ndohlcv,$success);
        my $ua = $quoter->user_agent();

        {
          my $reply = $ua->request(GET $NDOHLCV_URL);
          $success = $reply->is_success;
          @ndohlcv = split /\s*\n\s*/, $reply->content;
        }

    foreach my $symbol (@stocks) {
          if ($success) {
            my @data = sort grep /^$symbol,/, @ndohlcv;
            unless ( @data ) {
              $info {$symbol, "success"} = 0;
              $info {$symbol, "errormsg"} = "Stock name $symbol not found";
              next;
            }

            # Use newest data available.
            my @columns = $quoter->parse_csv($data[-1]);

            # NDOHLCV: Name, Date, Opening, Maximum, Minimum, Closing, Cycle
            $info {$symbol, "success"} = 1;
            $info {$symbol, "method"} = "bossa";
            $info {$symbol, "name"} = $columns[0];
            $quoter->store_date(\%info, $symbol,
                                {year => substr($columns[1], 0, 4),
                                 month => substr($columns[1], 4, 2),
                                 day => substr($columns[1], 6, 2)});
            $info {$symbol, "open"} = $columns[2];
            $info {$symbol, "high"} = $columns[3];
            $info {$symbol, "low"} = $columns[4];
            $info {$symbol, "close"} = $columns[5];
            $info {$symbol, "last"} = $columns[5]; # is it correct?
            $info {$symbol, "currency"} = "PLN";
          } else {
            $info{$symbol, "success"}=0;
            $info{$symbol, "errormsg"}="Error retreiving $symbol";
          }
        }
 return wantarray() ? %info : \%info;
 return \%info;
}
1;


=head1 NAME

Finance::Quote::Bossa Obtain Polish stock and mutual fund quotes from Bossa.pl service.

=head1 SYNOPSIS

    use Finance::Quote;

    $q = Finance::Quote->new;

    %info = Finance::Quote->fetch("bossa","AROK");  # Only query Bossa.pl
    %info = Finance::Quote->fetch("poland","AROK"); # Failover to other sources OK.

=head1 DESCRIPTION

This module fetches information from http://www.bossa.pl/ Polish stock
market service. All stocks are available.

This module is loaded by default on a Finance::Quote object. It's
also possible to load it explicity by placing "Bossa" in the argument
list to Finance::Quote->new().

This module provides both the "bossa" and "poland" fetch methods.
Please use the "poland" fetch method if you wish to have failover
with future sources for Polish stocks. Using the "bossa" method
will guarantee that your information only comes from the Bossa.pl
service.

=head1 LABELS RETURNED

The following labels may be returned by Finance::Quote::Bossa :
name date isodate open high low close last currency method

=head1 SEE ALSO

http://www.bossa.pl/

=cut

Dodajemy wyżej zapisany moduł do listy zapisanych modułów w pliku Quote.pm (/usr/share/perl5/Finance/Quote.pm). W tym celu modyfikujemy linijkę z "@modules=" dodając na końcu wpis z nazwą modułu (czyli "Bossa" przed średnikiem)

Po odpaleniu GNU Cash w menu "Narzędzia/Edytor papierów wartościowych" możemy dodać dowolny papier wpisując jako "Symbol/skrót" jego kod np. "BRE". Jako "Rodzaj" wpisujemy z ręki cokolwiek, np. "WSE", zaznaczamy "Pobierz notowania online" i z "Nieznanych" wybieramy "Bossa". Następnie w "Narzędzia/Edytor cen" po kliknięciu "Get Quotes" ostatni kurs powinien zostać pobrany.

Powyższy opis dotyczy notowań ciągłych dostępnych na stronie Bossa.pl w pliku http://bossa.pl/pub/ciagle/omega/cgl/ndohlcv.txt. Tam też można sprawdzić kody poszczególnych instrumentów jeżeli ktoś nie pamięta.

W przypadku innych notowań (np. derywatów) można utworzyć dodatkowy plik, o treści jak wyżej, zmieniając jego nazwę na np. "Bossader", zmieniając w nim "bossa" na "bossader" , podmieniając link na nas interesujący i dodając w "Quote.pm" moduł "Bossader". Pewnie można to zrobić bardziej elegancko ale tak też działa.

W ten sposób uzyskujemy notowania praktycznie wszystkich istotnych polskich instrumentów finansowych.

Have Fun !

OSnews Wykop Blip Flaker Kciuk Śledzik Facebook Identi.ca Twitter del.icio.us Google Bookmarks