Debian Codegraph @

awklib/eg/network/stoxpred.awk

raw
1 BEGIN {
2 Init()
3 ReadQuotes()
4 CleanUp()
5 Prediction()
6 Report()
7 SendMail()
8 }
9 function Init() {
10 if (ARGC != 1) {
11 print "STOXPRED - daily stock share prediction"
12 print "IN:\n no parameters, nothing on stdin"
13 print "PARAM:\n -v Proxy=MyProxy -v ProxyPort=80"
14 print "OUT:\n commented predictions as email"
15 print "JK 09.10.2000"
16 exit
17 }
18 # Remember ticker symbols from Dow Jones Industrial Index
19 StockCount = split("AA GE JNJ MSFT AXP GM JPM PG BA HD KO \
20 SBC C HON MCD T CAT HWP MMM UTX DD IBM MO WMT DIS INTC \
21 MRK XOM EK IP", name);
22 # Remember the current date as the end of the time series
23 day = strftime("%d")
24 month = strftime("%m")
25 year = strftime("%Y")
26 if (Proxy == "") Proxy = "chart.yahoo.com"
27 if (ProxyPort == 0) ProxyPort = 80
28 YahooData = "/inet/tcp/0/" Proxy "/" ProxyPort
29 }
30 function ReadQuotes() {
31 # Retrieve historical data for each ticker symbol
32 FS = ","
33 for (stock = 1; stock <= StockCount; stock++) {
34 URL = "http://chart.yahoo.com/table.csv?s=" name[stock] \
35 "&a=" month "&b=" day "&c=" year-1 \
36 "&d=" month "&e=" day "&f=" year \
37 "g=d&q=q&y=0&z=" name[stock] "&x=.csv"
38 printf("GET " URL " HTTP/1.0\r\n\r\n") |& YahooData
39 while ((YahooData |& getline) > 0) {
40 if (NF == 6 && $1 ~ /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/) {
41 if (stock == 1)
42 days[++daycount] = $1;
43 quote[$1, stock] = $5
44 }
45 }
46 close(YahooData)
47 }
48 FS = " "
49 }
50 function CleanUp() {
51 # clean up time series; eliminate incomplete data sets
52 for (d = 1; d <= daycount; d++) {
53 for (stock = 1; stock <= StockCount; stock++)
54 if (! ((days[d], stock) in quote))
55 stock = StockCount + 10
56 if (stock > StockCount + 1)
57 continue
58 datacount++
59 for (stock = 1; stock <= StockCount; stock++)
60 data[datacount, stock] = int(0.5 + quote[days[d], stock])
61 }
62 delete quote
63 delete days
64 }
65 function Prediction() {
66 # Predict each ticker symbol by prolonging yesterday's trend
67 for (stock = 1; stock <= StockCount; stock++) {
68 if (data[1, stock] > data[2, stock]) {
69 predict[stock] = "up"
70 } else if (data[1, stock] < data[2, stock]) {
71 predict[stock] = "down"
72 } else {
73 predict[stock] = "neutral"
74 }
75 if ((data[1, stock] > data[2, stock]) && (data[2, stock] > data[3, stock]))
76 hot[stock] = 1
77 if ((data[1, stock] < data[2, stock]) && (data[2, stock] < data[3, stock]))
78 avoid[stock] = 1
79 }
80 # Do a plausibility check: how many predictions proved correct?
81 for (s = 1; s <= StockCount; s++) {
82 for (d = 1; d <= datacount-2; d++) {
83 if (data[d+1, s] > data[d+2, s]) {
84 UpCount++
85 } else if (data[d+1, s] < data[d+2, s]) {
86 DownCount++
87 } else {
88 NeutralCount++
89 }
90 if (((data[d, s] > data[d+1, s]) && (data[d+1, s] > data[d+2, s])) ||
91 ((data[d, s] < data[d+1, s]) && (data[d+1, s] < data[d+2, s])) ||
92 ((data[d, s] == data[d+1, s]) && (data[d+1, s] == data[d+2, s])))
93 CorrectCount++
94 }
95 }
96 }
97 function Report() {
98 # Generate report
99 report = "\nThis is your daily "
100 report = report "stock market report for "strftime("%A, %B %d, %Y")".\n"
101 report = report "Here are the predictions for today:\n\n"
102 for (stock = 1; stock <= StockCount; stock++)
103 report = report "\t" name[stock] "\t" predict[stock] "\n"
104 for (stock in hot) {
105 if (HotCount++ == 0)
106 report = report "\nThe most promising shares for today are these:\n\n"
107 report = report "\t" name[stock] "\t\thttp://biz.yahoo.com/n/" \
108 tolower(substr(name[stock], 1, 1)) "/" tolower(name[stock]) ".html\n"
109 }
110 for (stock in avoid) {
111 if (AvoidCount++ == 0)
112 report = report "\nThe stock shares to avoid today are these:\n\n"
113 report = report "\t" name[stock] "\t\thttp://biz.yahoo.com/n/" \
114 tolower(substr(name[stock], 1, 1)) "/" tolower(name[stock]) ".html\n"
115 }
116 report = report "\nThis sums up to " HotCount+0 " winners and " AvoidCount+0
117 report = report " losers. When using this kind\nof prediction scheme for"
118 report = report " the 12 months which lie behind us,\nwe get " UpCount
119 report = report " 'ups' and " DownCount " 'downs' and " NeutralCount
120 report = report " 'neutrals'. Of all\nthese " UpCount+DownCount+NeutralCount
121 report = report " predictions " CorrectCount " proved correct next day.\n"
122 report = report "A success rate of "\
123 int(100*CorrectCount/(UpCount+DownCount+NeutralCount)) "%.\n"
124 report = report "Random choice would have produced a 33% success rate.\n"
125 report = report "Disclaimer: Like every other prediction of the stock\n"
126 report = report "market, this report is, of course, complete nonsense.\n"
127 report = report "If you are stupid enough to believe these predictions\n"
128 report = report "you should visit a doctor who can treat your ailment."
129 }
130 function SendMail() {
131 # send report to customers
132 customer["[email protected]"] = "Uncle Scrooge"
133 customer["[email protected]" ] = "Sir Thomas More"
134 customer["[email protected]" ] = "Baruch de Spinoza"
135 customer["[email protected]" ] = "Karl Marx"
136 customer["[email protected]" ] = "John Maynard Keynes"
137 customer["[email protected]" ] = "Ambrose Bierce"
138 customer["[email protected]" ] = "Pierre Simon de Laplace"
139 for (c in customer) {
140 MailPipe = "mail -s 'Daily Stock Prediction Newsletter'" c
141 print "Good morning " customer[c] "," | MailPipe
142 print report "\n.\n" | MailPipe
143 close(MailPipe)
144 }
145 }