forked from perlarenee/google-finance-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
236 lines (205 loc) · 11.9 KB
/
index.php
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="z-ua-compatible" content="ie=edge">
<title>Google Market Data - Sandbox</title>
<link rel="stylesheet" href="libraries/bootstrap/css/bootstrap.min.css">
</head>
<body>
<?php
//An alternate treatment using finance history
/*
Reference codes for finance history api
http://www.google.com/finance/getprices?q=000001&x=SHA&i=86400&p=40Y&f=d,c,v,k,o,h,l&df=cpct&auto=0&ei=Ef6XUYDfCqSTiAKEMg
http://www.google.com/finance/getprices
?q=000001 (stock symbol)
&x=SHA (stock exchange symbol)
&i=86400 (interval size in seconds (86400 = 1 day intervals)
&p=40Y (period. a number followed by a ÔdÕ or ÔyÕ e.g. days or years. ex: 40Y = 40 years)
&f=d,c,v,k,o,h,l (d=date/timestamp,c-close,v=volume,k=cday,o=opening price,h=high,l=low)
&df=cpct (?)
&auto=0 (?)
&ei=Ef6XUYDfCqSTiAKEMg (?)
*/
/*
//create array of data that can't be retrieved via an api
$marketsArray = array(
"SHA:000001"=>array("exchange"=>"SHA","stock"=>"000001","name"=>"Shanghai","deviation"=>""),
"INDEXNIKKEI:NI225"=>array("exchange"=>"INDEXNIKKEI","stock"=>"NI225","name"=>"Nikkei 225","deviation"=>0),
"INDEXHANGSENG:HSI"=>array("exchange"=>"INDEXHANGSENG","stock"=>"HSI","name"=>"Hang Seng Index","deviation"=>0),
"TPE:TAIEX"=>array("exchange"=>"TPE","stock"=>"TAIEX","name"=>"TSEC","deviation"=>0),
"INDEXFTSE:UKX"=>array("exchange"=>"INDEXFTSE","stock"=>"UKX","name"=>"FTSE 100","deviation"=>0),
"INDEXSTOXX:SX5E"=>array("exchange"=>"INDEXSTOXX","stock"=>"SX5E","name"=>"EURO STOXX 50","deviation"=>0),
"INDEXEURO:PX1"=>array("exchange"=>"INDEXEURO","stock"=>"PX1","name"=>"CAC 40","deviation"=>0),
"INDEXTSI:OSPTX"=>array("exchange"=>"INDEXTSI","stock"=>"OSPTX","name"=>"S&P TSX","deviation"=>0),
"INDEXASX:XJO"=>array("exchange"=>"INDEXASX","stock"=>"XJO","name"=>"S&P/ASX 200","deviation"=>0),
"INDEXBOM:SENSEX"=>array("exchange"=>"INDEXBOM","stock"=>"SENSEX","name"=>"BSE Sensex","deviation"=>0),
"TLV:T25"=>array("exchange"=>"TLV","stock"=>"T25","name"=>"TA25","deviation"=>0),
"INDEXSWX:SMI"=>array("exchange"=>"INDEXSWX","stock"=>"SMI","name"=>"SMI","deviation"=>0),
"INDEXVIE:ATX"=>array("exchange"=>"INDEXVIE","stock"=>"ATX","name"=>"ATX","deviation"=>0),
"INDEXBVMF:IBOV"=>array("exchange"=>"INDEXBVMF","stock"=>"IBOV","name"=>"IBOVESPA","deviation"=>0),
"INDEXBKK:SET"=>array("exchange"=>"INDEXBKK","stock"=>"SET","name"=>"SET","deviation"=>0),
"INDEXIST:XU100"=>array("exchange"=>"INDEXIST","stock"=>"XU100","name"=>"BIST100","deviation"=>0),
"INDEXBME:IB"=>array("exchange"=>"INDEXBME","stock"=>"IB","name"=>"IBEX","deviation"=>0),
"WSE:WIG"=>array("exchange"=>"WSE","stock"=>"WIG","name"=>"WIG","deviation"=>0),
"TADAWUL:TASI"=>array("exchange"=>"TADAWUL","stock"=>"TASI","name"=>"TASI","deviation"=>0),
"BCBA:IAR"=>array("exchange"=>"BCBA","stock"=>"IAR","name"=>"MERVAL","deviation"=>0),
"INDEXBMV:ME"=>array("exchange"=>"INDEXBMV","stock"=>"ME","name"=>"IPC","deviation"=>0),
"IDX:COMPOSITE"=>array("exchange"=>"IDX","stock"=>"COMPOSITE","name"=>"IDX Composite","deviation"=>0)
);
//return csv of all codes only
$marketsSymbols = implode(',',array_keys($marketsArray));
//loop through each market as key=>val
foreach($marketsArray as $key =>$val){
$stock = $val['stock'];
$exchange = $val['exchange'];
$symbol = $key;
//This method would be way more accurate but I'm leaving it commented out because the actual assigment was to use the percentage provided which appears to be from the http://www.google.com/finance/info?client
//get historical data
$url = 'http://www.google.com/finance/getprices?q='.$stock.'&x='.$exchange.'&i=86400&p=5d&f=c&df=cpct&auto=0&ei=Ef6XUYDfCqSTiAKEMg';
$obj = file_get_contents($url);
if($obj){
//explode to array and convert string values to int and filter by numeric...strips out instructional lines
$lines = array_map('intval',array_filter(explode("\n", $obj),'is_numeric'));
var_dump($lines);
echo '<br/>';
//sum up values. there's only a single value being returned on each line so no need to split lines up
$linesTotal = array_sum($lines);
//var_dump($linesTotal);
//echo '<br/>';
//count lines
$linesCount = count($lines);
//var_dump($linesCount);
//echo '<br/>';
//find mean as float val
$mean = floatval($linesTotal/$linesCount);
var_dump($mean);
echo '<br/>';
//loop through lines and add up
$janitor = array();
$endSum = 0;
foreach($lines as $line){
//devide each value by the mean
$val = $line-$mean;
//square new values
$endSum += $val*$val;
}
//var_dump($endSum);
//devide endsum by item count -1, get sqrt and round
$endSum = round(sqrt($endSum/($linesCount-1)),2);
//var_dump($endSum);
//update value in marketsArray
$marketsArray[$symbol]['deviation'] = $endSum;
//var_dump($marketsArray[$symbol]);
//last value in lines...is todays
$todaysClosing = end($lines);
//var_dump($todaysClosing);
}
}//end each market
*/
?>
<div class="container">
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">World Markets</a>
</div>
</div>
</nav>
<div class="row">
<div class="col-lg-12 table-responsive">
<!--tables for tabular data :) ... only-->
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Name</th>
<th>Symbol</th>
<th>Last Price</th>
<th>Change (%)</th>
<th>Standard Dev.</th>
<th>Date/Time</th>
</tr>
</thead>
<tbody>
<?php
$marketsArray = array(
"SHA:000001"=>array("name"=>"Shanghai"),
"INDEXNIKKEI:NI225"=>array("name"=>"Nikkei 225"),
"INDEXHANGSENG:HSI"=>array("name"=>"Hang Seng Index"),
"TPE:TAIEX"=>array("name"=>"TSEC"),
"INDEXFTSE:UKX"=>array("name"=>"FTSE 100"),
"INDEXSTOXX:SX5E"=>array("name"=>"EURO STOXX 50"),
"INDEXEURO:PX1"=>array("name"=>"CAC 40"),
"INDEXTSI:OSPTX"=>array("name"=>"S&P TSX"),
"INDEXASX:XJO"=>array("name"=>"S&P/ASX 200"),
"INDEXBOM:SENSEX"=>array("name"=>"BSE Sensex"),
"TLV:T25"=>array("name"=>"TA25"),
"INDEXSWX:SMI"=>array("name"=>"SMI"),
"INDEXVIE:ATX"=>array("name"=>"ATX"),
"INDEXBVMF:IBOV"=>array("name"=>"IBOVESPA"),
"INDEXBKK:SET"=>array("name"=>"SET"),
"INDEXIST:XU100"=>array("name"=>"BIST100"),
"INDEXBME:IB"=>array("name"=>"IBEX"),
"WSE:WIG"=>array("name"=>"WIG"),
"TADAWUL:TASI"=>array("name"=>"TASI"),
"BCBA:IAR"=>array("name"=>"MERVAL"),
"INDEXBMV:ME"=>array("name"=>"IPC"),
"IDX:COMPOSITE"=>array("name"=>"IDX Composite")
);
//this isn't the most accurate as it's only taking into account 2 data points. However, I'm using this one as it was requested specifically as far as I can tell. An alternate treatment is in comments above.
foreach($marketsArray as $key => $val){
//get url for this symbol
$infoUrl = 'http://www.google.com/finance/info?client=ig&q='.$key;
//get contents, remove //
$infoObj = str_replace('//','',file_get_contents($infoUrl));
if($infoObj){
//clean control chars and convert to asc array
$infoObj = json_decode(utf8_encode($infoObj),true);
/*"id": "338568" - internal google security id
,"t" : "IDX" - stock symbol
,"e" : "NYSEARCA" - exchange name
,"l" : "19.72" - last trade price
,"l_fix" : "19.72" - last trade ?
,"l_cur" : "19.72" - last trade with currency
,"s": "0" - last trade size
,"ltt":"3:59PM EDT" - last trade time
,"lt" : "May 4, 3:59PM EDT" - last trade date time long
,"lt_dts" : "2016-05-04T15:59:59Z" - last trade date time
,"c" : "-0.31" - change
,"c_fix" : "-0.31" - ?
,"cp" : "-1.55" - ? percentage
,"cp_fix" : "-1.55" - ?
,"ccol" : "chr" - ?
,"pcls_fix" : "20.03" - previous close price
*/
$stock = $infoObj[0]['t'];
$exchange = $infoObj[0]['e'];
$code = $stock.':'.$exchange;
$cPercentage = $infoObj[0]['cp'];//this is the percentage, which is basically just comparing the change between the end of the last day, and the most recent transaction
$cAmount = $infoObj[0]['c'];
$prevClosePrice = floatval(str_replace(',','',$infoObj[0]['pcls_fix']));
$lastTradePrice = floatval(str_replace(',','',$infoObj[0]['l']));
$lastTradePriceCur = $infoObj[0]['l_cur'];
$lastTradeDate = $infoObj[0]['lt'];
//mean
$mean = ($prevClosePrice+$lastTradePrice)/2;
//subtract mean from each, and square
$newPrev = ($prevClosePrice-$mean)*($prevClosePrice-$mean);
$newLast = ($lastTradePrice-$mean)*($lastTradePrice-$mean);
//sum/devide by 1 (so nothing) and sqrt
$final = round(sqrt($newPrev+$newLast),2);
//output row
echo '<tr><td>'.$val['name'].'</td><td>'.$code.'</td><td>'.$lastTradePriceCur.'</td><td>'.$cAmount.' ('.$cPercentage.')</td><td>'.$final.'</td><td>'.$lastTradeDate.'</td></tr>';
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="libraries/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>