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
|
#!/usr/bin/perl
#http://table.finance.yahoo.com/table.csv?a=5&b=11&c=2002&d=5&e=16&f=2002&s=intc&y=0&g=d&ignore=.csv
# Pivot Data for INTC - Pivot 30.25 S1 29.5 S2 28.75 R1 31 R2 31.75
use Getopt::Std;
getopts('a');
my $symbol = $ARGV[0] || "intc";
my $fmm, $fdd, $fyy;
my $lmm, $ldd, $lyy;
$symbol =~ tr/a-z/A-Z/;
my $csymbol = $symbol;
for ($symbol) {
/^\$TRIN$/ && do { $symbol = "^STI.N"; last; };
/^\$TICK$/ && do { $symbol = "^TIC.N"; last; };
/^\$COMP$/ && do { $symbol = "^IXIC"; last; };
/^\$SPX$/ && do { $symbol = "^GSPC"; last; };
/./ && do { $symbol =~ s/\$/^/; last; };
}
#
# We get a few days worth of data, to account for the weekends,
# but only use the first quote that we get (which should be
# "yesterday's" market data.
#
# N.B. this method of getting the dates we need is a complete hack.
# Should find a nice perl module to do the dirty work.
#
# As of 9/14/02, Yahoo is broken if you ask for only a fews days
# worth of data. So we now ask for 60 days instead of 4.
#
open(DATE, 'date -d "60 days ago" +"%m/%d/%Y" |');
while (<DATE>) {
chop;
($fmm, $fdd, $fyy) = split("/");
}
close (DATE);
open(DATE, 'date +"%m/%d/%Y" |');
while (<DATE>) {
chop;
($lmm, $ldd, $lyy) = split("/");
}
close (DATE);
my $url = "http://table.finance.yahoo.com/table.csv?y=0&g=d&ignore=.csv"
. "&s=$symbol"
. "&a=$fmm&b=$fdd&c=$fyy"
. "&d=$lmm&e=$ldd&f=$lyy";
# print "$url\n";
open(CURL, "curl -s '$url' |");
while (<CURL>) {
next if (/^Date/);
# print $_;
#Date,Open,High,Low,Close,Volume
(my $date, my $open, my $high, my $low, my $close, my $vol)
= split(",");
$cpp = ($high + $low + $close) / 3;
$r1 = (2*$cpp) - $low;
$s1 = (2*$cpp) - $high;
$r2 = $cpp + ($r1 - $s1);
$s2 = $cpp - ($r1 - $s1);
if ($opt_a) {
# Alerts format
# SYM state var op val act
printf("%s 1 l = %.2f 5\n",
$csymbol, $cpp);
printf("%s 1 l < %.2f 5\n",
$csymbol, $s1);
printf("%s 1 l > %.2f 5\n",
$csymbol, $r1);
printf("%s 1 l < %.2f 5\n",
$csymbol, $s2);
printf("%s 1 l > %.2f 5\n",
$csymbol, $r2);
} else {
$buf = sprintf("%s P=%.2f S1=%.2f S2=%.2f R1=%.2f R2=%.2f"
. " (based on %s close)\n",
$csymbol, $cpp, $s1, $s2, $r1, $r2, $date);
print $buf;
}
last;
}
close(CURL);
|