Created
July 31, 2011 10:01
-
-
Save poppen/1116662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Finance::Quote; | |
use URI; | |
use Web::Scraper; | |
my $q = Finance::Quote->new; | |
my %index_quotes = $q->fetch( | |
"bloomberg_stocks_index", 'MXKO:IND', | |
'MXEF:IND', 'VIX:IND', | |
'SPX:IND', 'INDU:IND' | |
); | |
my %etf_quotes | |
= $q->fetch( "bloomberg_etf", 'SPY:US', '1550:JP', '1557:JP', '1680:JP', | |
'1681:JP', '1552:JP', '2029:JP' ); | |
my $topix_per = _get_topix_per(); | |
my $usdjpy = $q->currency( 'USD', 'JPY' ); | |
my $audjpy = $q->currency( 'AUD', 'JPY' ); | |
my $usdjpy_p_change = _get_usdjpy_p_change(); | |
my $dow = sprintf "%s (%s%%)", $index_quotes{ 'INDU:IND', 'price' }, | |
$index_quotes{ 'INDU:IND', 'p_change' }; | |
my $spx = sprintf "%s (%s%%)", $index_quotes{ 'SPX:IND', 'price' }, | |
$index_quotes{ 'SPX:IND', 'p_change' }; | |
my $mxko = sprintf "%s (%s%%)", $index_quotes{ 'MXKO:IND', 'price' }, | |
$index_quotes{ 'MXKO:IND', 'p_change' }; | |
my $mxef = sprintf "%s (%s%%)", $index_quotes{ 'MXEF:IND', 'price' }, | |
$index_quotes{ 'MXEF:IND', 'p_change' }; | |
my $vix = sprintf "%s (%s%%)", $index_quotes{ 'VIX:IND', 'price' }, | |
$index_quotes{ 'VIX:IND', 'p_change' }; | |
my $spy = sprintf "%s USD == %s JPY", $etf_quotes{ 'SPY:US', 'nav' }, | |
$etf_quotes{ 'SPY:US', 'nav' } * $usdjpy; | |
my $mxko1680 = sprintf "%s : %s:%s", $etf_quotes{ '1680:JP', 'nav' }, | |
$etf_quotes{ '1680:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'MXKO:IND', 'p_change' } / 100 ) ), | |
$etf_quotes{ '1680:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'MXKO:IND', 'p_change' } / 100 ) ) | |
* ( 1 + $usdjpy_p_change / 100 ); | |
my $mxef1681 = sprintf "%s : %s:%s", $etf_quotes{ '1681:JP', 'nav' }, | |
$etf_quotes{ '1681:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'MXEF:IND', 'p_change' } / 100 ) ), | |
$etf_quotes{ '1681:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'MXEF:IND', 'p_change' } / 100 ) ) | |
* ( 1 + $usdjpy_p_change / 100 ); | |
my $mxko1550 = sprintf "%s : %s:%s", $etf_quotes{ '1550:JP', 'nav' }, | |
$etf_quotes{ '1550:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'MXKO:IND', 'p_change' } / 100 ) ), | |
$etf_quotes{ '1550:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'MXKO:IND', 'p_change' } / 100 ) ) | |
* ( 1 + $usdjpy_p_change / 100 ); | |
my $vix1552 = sprintf "%s : %s:%s", $etf_quotes{ '1552:JP', 'nav' }, | |
$etf_quotes{ '1552:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'VIX:IND', 'p_change' } / 100 ) ), | |
$etf_quotes{ '1552:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'VIX:IND', 'p_change' } / 100 ) ) | |
* ( 1 + $usdjpy_p_change / 100 ); | |
my $vix2029 = sprintf "%s : %s:%s", $etf_quotes{ '2029:JP', 'nav' }, | |
$etf_quotes{ '2029:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'VIX:IND', 'p_change' } / 100 ) ), | |
$etf_quotes{ '2029:JP', 'nav' } | |
* ( 1 + ( $index_quotes{ 'VIX:IND', 'p_change' } / 100 ) ) | |
* ( 1 + $usdjpy_p_change / 100 ); | |
my $body = << "EOF"; | |
INDEX | |
----- | |
### TOPIX PER | |
$topix_per | |
### DOW | |
$dow | |
### S&P500 | |
$spx | |
### MXKO:IND | |
$mxko | |
### MXEF:IND | |
$mxef | |
### VIX:IND | |
$vix | |
ETF/ETN | |
------- | |
### 1557 estmated inav | |
$spy | |
### 1680 estmated inav | |
$mxko1680 | |
### 1550 estmated inav | |
$mxko1550 | |
### 1681 estmated inav | |
$mxef1681 | |
### 1552 estmated inav | |
$vix1552 | |
### 2029 estmated inav | |
$vix2029 | |
Currency | |
-------- | |
### USD/JPY | |
$usdjpy | |
#### Percent Change | |
$usdjpy_p_change | |
### AUD/JPY | |
$audjpy | |
EOF | |
print $body; | |
# Fetching TOPIX average PER | |
sub _get_topix_per { | |
my $uri | |
= URI->new( | |
"http://www.morningstar.co.jp/RankingWeb/SectorPart.do?sectorCode=0000" | |
); | |
my $scraper = scraper { | |
process '//div[@id="ms_main"]/div/table/tbody/tr[1]/td[1]', | |
'date' => 'TEXT'; | |
process '//div[@id="ms_main"]/div/table/tbody/tr[1]/td[9]', | |
'per' => 'TEXT'; | |
}; | |
my $result = $scraper->scrape($uri); | |
return sprintf "%.2f (%s)", $result->{per}, $result->{date}; | |
} | |
# Fetching usd-jpy percent change | |
sub _get_usdjpy_p_change { | |
my $uri = URI->new("http://www.bloomberg.com/markets/"); | |
my $scraper = scraper { | |
process | |
'//td[@class="name" and text()="USD-JPY"]/following-sibling::node()[3]', | |
p_change => [ 'TEXT', sub {s/%$//} ]; | |
}; | |
my $result = $scraper->scrape($uri); | |
return $result->{p_change}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment