根据http://blog.csdn.net/ustbhacker/article/details/8457341上的提示,我把他的程序改了一下,主要实现了显示自定义我的股票,通过设置购买价格和持有量,来粗略计算自己的盈利。
use utf8;
use Encode;
use strict;
use warnings;
use LWP::UserAgent;
use constant URL => 'http://qt.gtimg.cn/q=';
use constant PROXY => 'http://135.240.131.128:61250'; #使用代理修改此处并去掉下边代码中的注释
use constant MAXNUMBER => 50; #腾讯每次最多取60个左右的股票信息,设置一个整数,HTTP GET中使用
#use constant COLUMN => qw(1 2 3 4 5 7 8 10 9 19 20 31 32 36 37 38 39 46 44 45); #要显示的列,自己根据需要修改
use constant COLUMN => qw(1 2 3 4 5 6 31 32 33 34 38 49 50 51); #要显示的列,自己根据需要修改
use constant ORDER => 7; #要排序的列
my @result = ();
#大盘指数
my @zs_result = ();
my %stocklist = (
# Symbol => [ 'buy price', 'holdings']
's_sh000001' => '',
's_sz399001' => '',
'000001' => ['',''], # 上证指数
'600000' => ['',''], # 浦发银行
'002340' => ['',''], # 格林美
'601377' => ['',''], # 兴业证券
'600048' => ['',''], # 保利地产
'000410' => ['',''], # 沈阳机床
'601009' => ['',''], # 南京银行
'601008' => ['8.12','800'], # 连云港
'600317' => ['',''], # 营口港
'601166' => ['13.00','800'], # 兴业银行
);
my %config = (
# index => [ 'Word Width', 'English Name', 'Chinese Name' ]
0 => ['','','未知',],
1 => ['9','Name','名字',],
2 => ['8','Symbol','代码',],
3 => ['9','Price','当前价格',],
4 => ['9','PreClose','昨收',],
5 => ['9','Open','今开',],
6 => ['10','Vol','成交量',],
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 => ['7','Chg','涨跌',],
32 => ['9','ChgRate','涨跌%',],
33 => ['9','High','最高',],
34 => ['9','Low','最低',],
35 => ['','','价格/成交量(手)/成交额',],
36 => ['','','成交量',],
37 => ['','','成交:万',],
38 => ['11','Turnover','换手率',],
39 => ['9','P/E','市盈率',],
40 => ['','','',],
41 => ['','','最高',],
42 => ['','','最低',],
43 => ['','','振幅%',],
44 => ['','','流通市值',],
45 => ['9','MktCap','总市值',],
46 => ['9','P/B','市净率',],
47 => ['','','涨停价',],
48 => ['','','跌停价',],
# constomize
49 => ['9','BuyPrice','买入价'],
50 => ['9','Profit','盈利'],
51 => ['9','PrfRate','盈利率'],
52 => ['9','Holdings','持有量'],
53 => ['9','Cost','成本'],
);
GetStockInfomation(\%stocklist);
@result = sort { $a->[ORDER] <=> $b->[ORDER] } @result; #可以使用grep添加更多过滤条件
PrintStock();
=head
Get stock information from QQ
=cut
sub GetStockInfomation{
my $stock_map = shift ;
my @stock = map { if (/^6/ ) {"sh$_";}
elsif (/^0/) {"sz$_";}
elsif (/^3/) {"cy$_";}
else {"$_";}
} keys(%$stock_map);
die "Length > MAXNUMBER" if @stock>MAXNUMBER;
my $ua = LWP::UserAgent->new();
$ua->proxy('http', PROXY);
#print URL.join(',', @stock),"\n";
my $res = $ua->get(URL.join(',', @stock));
if($res->is_success){
for(split /;/,$res->content){
if(/"(.+)"/){
my @tmp = split /~/,$1;
if (@tmp < 48) {
splice (@tmp, 4, 0, ('',''));
push @zs_result, \@tmp;
next;
}
# add buy price
if ($stock_map->{$tmp[2]}->[0] ne '') {
my $myprice = $stock_map->{$tmp[2]}->[0];
my $holdings = $stock_map->{$tmp[2]}->[1];
my $mycost = $myprice * $holdings;
my $cur_mktvalue = $tmp[3] * $holdings;
my $profit = $cur_mktvalue - $mycost;
$profit = sprintf("%d", $profit);
my $prf_rate = ($tmp[3]-$myprice)/$tmp[3]*100;
$prf_rate = sprintf("%.2f%%", $prf_rate);
push @tmp, ($stock_map->{$tmp[2]}->[0],"$profit","$prf_rate");
}
else
{
push @tmp, ('','','');
}
push @result, \@tmp;
}
}
}
}
=head
Print stock information
=cut
sub PrintStock{
my $format = join '', map { "%$config{$_}->[0]s" } COLUMN;
my $wordwidth = 0;
map { $wordwidth += $config{$_}->[0] } COLUMN;
print "\n";
printf "$format", map {encode('utf8',$config{$_}->[1])} COLUMN;
print "\n", '=' x $wordwidth, "\n";
for(@zs_result){
my $zs_format = join '', map { "%$config{$_}->[0]s" } (1..6,31,32);
printf "$zs_format", @$_[1..5,8,6,7];
print "\n";
}
for(@result){
printf "$format", @$_[COLUMN];
print "\n";
}
#print "\nTotal: ",$#result;
}