File: schnapp

package info (click to toggle)
beancounter 0.8.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 332 kB
  • ctags: 136
  • sloc: perl: 2,308; sh: 743; makefile: 138
file content (298 lines) | stat: -rw-r--r-- 7,906 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/perl -w
# vim: set sw=4 si et:
#
# schnapp - show the best dividend yield of your stocks
#
# Copyright 2002-2005 by Mathias Weidner
#
# Licensed under GPL2, look at end of file for details.
#
# Use 'perldoc schnapp' to view the manpage
# or  'pod2man schnapp > schnapp.1' to create a manpage file for *roff
#
use strict;
$ENV{PATH} = join ":", qw(/bin /usr/bin);
$|++;

use Getopt::Long;
use Finance::BeanCounter;

# global variables:
my $RCS_VERSION   = '$Id: schnapp,v 1.3 2005/02/10 21:26:27 mathias Exp $';
my $db_min_schema = "0.6.0";    # minimum version of the database that we need

use vars qw($help $index $debug $verbose $version
  $fxarg $datearg $prevdatearg
  $rcfile $extrafx $updatedate $type $max
  $dbsystem $dbname $fxupdate);

#
my $rcfile = $ENV{HOME} . "/.beancounterrc";
($prevdatearg, $datearg, $fxupdate, $max) = ("6 month ago", "today", 1, 10);

my %options = (
    "help"    => \$help,
    "index=s" => \$index,
    "max=i"   => \$max,
    "type=s"  => \$type,
    "version" => \$version,
);

GetOptions(%options) || die "ERROR: No such option. -help for help\n";
&help    if ($help);
&version if ($version);

my $command = shift @ARGV;

my %Config = GetConfig(
    $rcfile,     $debug,    $verbose, $fxarg,    $extrafx,
    $updatedate, $dbsystem, $dbname,  $fxupdate, $command
);

my $dbh = ConnectToDb();
if (TestInsufficientDatabaseSchema($dbh, $db_min_schema)) {
    warn "Database schema is not current. Please run
          'update_beancounter'\n";
}
else {
    schnapp();
}
CloseDB($dbh);

#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
sub schnapp
{
    my $stocks = select_stocks();
    my $portfl = select_portfolio();
    my $count  = 1;
    printf("Nr. %-10s %-10s %-20s %-6s %-6s %-5s %-5s %-6s\n",
        "Date", "Symbol", "Name", "Low", "High", "Div", "Divr", "Shares");
    my $sort = sub {
        return -1 if (not defined $stocks->{$b}->{divr});
        return 1  if (not defined $stocks->{$a}->{divr});
        return ($stocks->{$b}->{divr} <=> $stocks->{$a}->{divr});
    };
    foreach my $sym (sort $sort keys %{$stocks}) {
        if ($count <= $max or $portfl->{$sym}) {
            printf(
                "%3i %-10s %-10s %-20s %6.2f %6.2f %5.2f %5.2f %6i\n",
                $count,
                $stocks->{$sym}->{date},
                $sym,
                $stocks->{$sym}->{name},
                $stocks->{$sym}->{day_low},
                $stocks->{$sym}->{day_high},
                $stocks->{$sym}->{div},
                $stocks->{$sym}->{divr},
                $portfl->{$sym} ? $portfl->{$sym}->{shares} : 0
            );
        }
        $count++;
    }
}    # schnapp()

sub select_stocks
{
    my $sqli = <<EOSQL;
        select sp.date as date
               ,dividend as div,dividend / day_low * 100 as divr
               ,day_low,day_high,volume
               ,si.symbol as symbol,name
          from stockprices as sp,stockinfo as si,indices as ix,tmp
         where ix.symbol     = si.symbol
           and si.symbol     = sp.symbol
           and sp.symbol     = tmp.symbol
           and sp.date       = tmp.date
           and ix.stockindex = ?
EOSQL
    my $sql = <<EOSQL;
        select sp.date as date
               ,dividend as div,dividend / day_low * 100 as divr
               ,day_low,day_high,volume
               ,si.symbol as symbol,name
          from stockprices as sp,stockinfo as si,tmp
         where si.symbol     = sp.symbol
           and sp.symbol     = tmp.symbol
           and sp.date       = tmp.date
EOSQL
    $dbh->do('create temporary table tmp (symbol varchar(12),date date)');
    $dbh->do(
        'insert into tmp
                         select symbol, max(date)
                           from stockprices group by symbol'
    );
    my $sth;
    if ($index) {
        $sth = $dbh->prepare($sqli);
        $sth->execute($index);
    }
    else {
        $sth = $dbh->prepare($sql);
        $sth->execute();
    }
    my $stocks = $sth->fetchall_hashref('symbol');
    $dbh->do('drop table tmp');
    return $stocks;
}    # select_stocks()

sub select_portfolio
{
    my $sql = <<EOSQL;
        select symbol,shares from portfolio
EOSQL
    my $sqlt = <<EOSQL;
        select symbol,shares from portfolio
         where type = ?
EOSQL
    my $sth;
    if ($type) {
        $sth = $dbh->prepare($sqlt);
        $sth->execute($type);
    }
    else {
        $sth = $dbh->prepare($sql);
        $sth->execute();
    }
    my $portfolio = $sth->fetchall_hashref('symbol');
    return $portfolio;
}    # select_portfolio()

sub version
{
    print <<EOV;
schnapp - show the best dividend yield of your stocks
Version: $RCS_VERSION
EOV
    exit(0);
}    # version()

sub help
{
    print <<EOH;
schnapp - show the best dividend yield of your stocks

Usage:
    schnapp [options]

Options:
    --help        show this help and exit
    --index=index limit investigated stocks to index
    --max=count   limit display to the first count stocks
    --type=type   limit the stocks from portfolio to type
    --version     show version and exit
EOH
    exit;
}
__END__ 

=head1 NAME

schnapp - show the best dividend yield of your stocks

=head1 SYNOPSIS

 schnapp [--help]
 schnapp [--index=i] [--max=count] [--type=t]

=head1 DESCRIPTION

This programs looks at your beancounter database, takes the last
prices and the dividend from B<stockprices> and B<stockinfo> and
finally shows you a list of stocks ordered by descending dividend
yield.

The I<dividend yield> is determined by dividing the C<dividend> from
table B<stockinfo> by C<day_low> from table B<stockprices> and
multiplying the result with 100 for each stock.

=head1 OPTIONS

=over 4

=item B<--help>

A short message listing the purpose and the options of the program.

=item B<--index>=I<index>

You may limit the stocks which will be investigated to a certain stock
index that must be known in beancounter (see C<beancounter addindex ...>
in the L<beancounter> manual).

=item B<--max>=I<count>

You may limit the count of stocks that will be displayed.

The program lists I<count> stocks with descending dividend yield and
thereafter only those stocks that are in the portfolio.

=item B<--type>=I<type>

You may limit the stocks of your portfolio which will be displayed to
I<type>. This relates to the type in the beancounter portfolio. See
the L<beancounter> manpage.

=item B<--version>

Shows the version and exits.

=back

=head1 ENVIRONMENT

=over 4

=item C<$HOME>

The value of the environment variable C<$HOME> is used to determine
the location of the B<beancounter> configuration file.

=item C<$PATH>

The environment variable C<$PATH> is set to C</bin:/usr/bin>.

=back

=head1 FILES

=head2 B<$HOME/.beancounterrc>

The configuration file for B<beancounter> is used to get access to the
database.

=head1 NOTES

This program uses the database filled by your daily run of
C<beancounter update> but takes only the last dates of each stock.
It determines automatically the last date when beancounter updated the
stockprices.

=head1 SEE ALSO

L<beancounter(1p)>

=head1 AUTHOR

Copyright 2002-2005 by Mathias Weidner

=head1 COPYRIGHT AND LICENSE

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., 675 Mass Ave, Cambridge, MA 02139, USA.

=head1 HISTORY

 2005-02-10 mw Copyright, License, Manpage, Option '--version'
 2002       mw Initial Version