-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
322 lines (219 loc) · 12.8 KB
/
database.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
header("Content-type: text/html; charset=iso-8859-1");
$con=mysqli_connect();
mysqli_set_charset($con, 'utf8');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/*
* Sätter in en ny aktie i databasen stocks.
*/
function insertStock($con, $name, $symbol, $ysymbol, $valuta, $isin, $sektor, $icb, $owned) {
$sql = "INSERT INTO stocks VALUES ('$name', '$symbol', '$ysymbol', '$valuta', '$isin', '$sektor', '$icb', '$owned');";
$result = mysqli_query($con, $sql);
}
/*
* Lägger till informationen i tabellen stockHistory.
*/
function insertStockHistory($con, $ysymbol, $date, $open, $high, $low, $close, $volume, $adj_close) {
$sql = "INSERT INTO stockHistory VALUES ('$ysymbol', '$date', '$open', '$high', '$low', '$close', '$volume', '$adj_close');";
$result = mysqli_query($con, $sql);
}
/*
* Hämtar historisk information för varje aktie och vidarbefodrar till insertStockHistory funktionen.
* Symbol, Date, Open, High, Low, Close, Volume, Adj_Close
*/
function updateStockHistory($con) {
$symbolArray = array();
$sql = "SELECT Ysymbol, MAX(Date) , COUNT(Ysymbol) FROM stockHistory GROUP BY Ysymbol";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
array_push($symbolArray, [$row['Ysymbol'],$row['MAX(Date)'],$row['COUNT(Ysymbol)']]);
}
//Datumintervall för historik
$yesterday = new DateTime('NOW');
$yesterday->modify('-1 day');
$interval1 = new DateTime('NOW');
$interval1->modify('-1 year');
$interval2 = new DateTime('NOW');
$interval2->modify('-2 year');
$interval3 = new DateTime('NOW');
$interval3->modify('-3 year');
$dateInterval = array($interval3->format('Y-m-d'), $interval2->format('Y-m-d'), $interval1->format('Y-m-d'), $yesterday->format('Y-m-d'));
foreach ($symbolArray as $symbol) {
$maxDate = new DateTime($symbol[1]);
if($symbol[2]<780) {
echo "Get more history: ".$symbol[0]." count ".$symbol[2]."\n";
for ($x=0; $x<3; $x++) {
$histData = rawurlencode("select * from yahoo.finance.historicaldata where symbol in ('".$symbol[0]."') and startDate='".$dateInterval[$x]."' and endDate='".$dateInterval[$x+1]."';");
$url = 'http://query.yahooapis.com/v1/public/yql';
$json = file_get_contents($url."?q=".$histData."&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys");
$arr = json_decode($json,true);
$quote = $arr['query']['results']['quote'];
for($y=0; $y<count($arr['query']['results']['quote']); $y++) {
$ysymbol = $quote[$y]['Symbol'];
$date = $quote[$y]['Date'];
$open = $quote[$y]['Open'];
$high = $quote[$y]['High'];
$low = $quote[$y]['Low'];
$close = $quote[$y]['Close'];
$volume = $quote[$y]['Volume'];
$adj_close = $quote[$y]['Adj_Close'];
insertStockHistory($con, $ysymbol, $date, $open, $high, $low, $close, $volume, $adj_close);
}
}
}
elseif($maxDate->format('Y-m-d')<$yesterday->format('Y-m-d')) {
echo "Update stocks: ".$symbol[0]."-".$maxDate->format('Y-m-d')."<".$yesterday->format('Y-m-d')."\n";
$histData = rawurlencode("select * from yahoo.finance.historicaldata where symbol in ('".$symbol[0]."') and startDate='".$maxDate->format('Y-m-d')."' and endDate='".$yesterday->format('Y-m-d')."';");
$url = 'http://query.yahooapis.com/v1/public/yql';
$json = file_get_contents($url."?q=".$histData."&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys");
$arr = json_decode($json,true);
$quote = $arr['query']['results']['quote'];
for($y=0; $y<count($arr['query']['results']['quote']); $y++) {
$ysymbol = $quote[$y]['Symbol'];
$date = $quote[$y]['Date'];
$open = $quote[$y]['Open'];
$high = $quote[$y]['High'];
$low = $quote[$y]['Low'];
$close = $quote[$y]['Close'];
$volume = $quote[$y]['Volume'];
$adj_close = $quote[$y]['Adj_Close'];
insertStockHistory($con, $ysymbol, $date, $open, $high, $low, $close, $volume, $adj_close);
}
}
}
echo "updateStockHistory done"."\n";
}
/*
Lägger till aktier som saknas i aktiehistoriken.
*/
function addStockHistory($con) {
$symbolArray = array();
$sql = "SELECT Ysymbol FROM stocks WHERE Ysymbol NOT IN (SELECT Ysymbol FROM stockHistory GROUP BY Ysymbol);";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
array_push($symbolArray, $row['Ysymbol']);
}
foreach ($symbolArray as $symbol) {
echo "Adding ".$symbol." to stockHistory\n";
for ($x=0; $x<3; $x++) {
$histData = rawurlencode("select * from yahoo.finance.historicaldata where symbol in ('".$symbol."') and startDate='2013-08-28' and endDate='2014-08-28';");
$url = 'http://query.yahooapis.com/v1/public/yql';
$json = file_get_contents($url."?q=".$histData."&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys");
$arr = json_decode($json,true);
$quote = $arr['query']['results']['quote'];
for($y=0; $y<count($arr['query']['results']['quote']); $y++) {
$ysymbol = $quote[$y]['Symbol'];
$date = $quote[$y]['Date'];
$open = $quote[$y]['Open'];
$high = $quote[$y]['High'];
$low = $quote[$y]['Low'];
$close = $quote[$y]['Close'];
$volume = $quote[$y]['Volume'];
$adj_close = $quote[$y]['Adj_Close'];
insertStockHistory($con, $ysymbol, $date, $open, $high, $low, $close, $volume, $adj_close);
}
}
}
echo "addStockHistory done"."\n";
}
/*
Hämtar färsk information om varje aktie och uppdaterar databasen.
*/
function updateStocksInfo($con){
$sql = "SELECT * FROM stocks;";
$result = mysqli_query($con,$sql);
$x = 0;
$i = 0;
$length = $result->num_rows;
$symbolString = "";
$symbolStringArray = [];
while($row = mysqli_fetch_array($result)) {
if($x >= 100){
$symbolString .= $row['Ysymbol'];
array_push($symbolStringArray, $symbolString);
$x = 0;
$symbolString = "";
}
elseif($i+1 == $length) {
$symbolString .= $row['Ysymbol'];
array_push($symbolStringArray, $symbolString);
}
$symbolString .= $row['Ysymbol'].",";
$x++;
}
$con2=mysqli_connect('192.168.1.5','SA','SAmysql2014','SA');
mysqli_set_charset($con2, 'utf8');
$date = new DateTime('NOW');
foreach($symbolStringArray as $symbolString) {
$histData = rawurlencode("select * from yahoo.finance.quotes where symbol in ('".$symbolString."');");
$url = 'http://query.yahooapis.com/v1/public/yql';
$json = file_get_contents($url."?q=".$histData."&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
$arr = json_decode($json,true);
$stockInfoArray = $arr['query']['results']['quote'];
for($i=0;$i<count($stockInfoArray);$i++) {
$s = $stockInfoArray[$i];
$sql2 = "UPDATE stockInfo SET Updated='".$date->format('Y-m-d H:i:s')."', Ask='".$s['Ask']."', AskRealtime='".$s['AskRealtime']."', AverageDailyVolume='".$s['AverageDailyVolume']."',Bid='".$s['Bid']."',BidRealtime='".$s['BidRealtime']."',`Change`='".$s['Change']."',ChangeFromFiftydayMovingAverage='".$s['ChangeFromFiftydayMovingAverage']."',ChangeFromTwoHundreddayMovingAverage='".$s['ChangeFromTwoHundreddayMovingAverage']."',ChangeFromYearHigh='".$s['ChangeFromYearHigh']."',ChangeFromYearLow='".$s['ChangeFromYearLow=']."',ChangePercentRealtime='".$s['ChangePercentRealtime']."',ChangeRealtime='".$s['ChangeRealtime']."',Change_PercentChange='".$s['Change_PercentChange']."',ChangeinPercent='".$s['ChangeinPercent']."',DaysHigh='".$s['DaysHigh']."',DaysLow='".$s['DaysLow']."',DividendPayDate='".$s['DividendPayDate']."',DividendShare='".$s['DividendShare']."',DividendYield='".$s['DividendYield']."',LastTradeDate='".$s['LastTradeDate']."',LastTradePriceOnly='".$s['LastTradePriceOnly']."',Open='".$s['Open']."',PercebtChangeFromYearHigh='".$s['PercebtChangeFromYearHigh']."',PercentChange='".$s['PercentChange']."',PercentChangeFromFiftydayMovingAverage='".$s['PercentChangeFromFiftydayMovingAverage']."',PercentChangeFromTwoHundreddayMovingAverage='".$s['PercentChangeFromTwoHundreddayMovingAverage']."',PercentChangeFromYearLow='".$s['PercentChangeFromYearLow']."',PreviousClose='".$s['PreviousClose']."',Volume='".$s['Volume']."',YearHigh='".$s['YearHigh']."',YearLow='".$s['YearLow']."'WHERE Ysymbol='".$s['Symbol']."';";
mysqli_query($con2, $sql2);
}
mysqli_close($con2);
}
}
/*
Omvandlar array till Json
*/
function getStockInfo($con){
$sql = "SELECT stocks.Name as Name, stocks.Symbol as Symbol, stocks.Ysymbol as Ysymbol, Valuta as Valuta, ISIN as ISIN, Sektor as Sektor, ICB as ICB, Owned as Owned, BuyPrice as BuyPrice, Category as Category, Ask as Ask, AskRealtime as AskRealtime, AverageDailyVolume as AverageDailyVolume ,Bid as Bid, BidRealtime as BidRealtime,`Change` as `Change`, ChangeFromFiftydayMovingAverage as ChangeFromFiftydayMovingAverage, ChangeFromTwoHundreddayMovingAverage as ChangeFromTwoHundreddayMovingAverage, ChangeFromYearHigh as ChangeFromYearHigh, ChangeFromYearLow as ChangeFromYearLow, ChangePercentRealtime as ChangePercentRealtime, ChangeRealtime as ChangeRealtime, Change_PercentChange as Change_PercentChange, ChangeinPercent as ChangeinPercent, DaysHigh as DaysHigh, DaysLow as DaysLow, DividendPayDate as DividendPayDate, DividendShare as DividendShare, DividendYield as DividendYield, LastTradeDate as LastTradeDate, LastTradePriceOnly as LastTradePriceOnly, Open as Open, PercebtChangeFromYearHigh as PercebtChangeFromYearHigh, PercentChange as PercentChange, PercentChangeFromFiftydayMovingAverage as PercentChangeFromFiftydayMovingAverage, PercentChangeFromTwoHundreddayMovingAverage as PercentChangeFromTwoHundreddayMovingAverage, PercentChangeFromYearLow as PercentChangeFromYearLow, PreviousClose as PreviousClose, Volume as Volume, YearHigh as YearHigh, YearLow as YearLow FROM `stockInfo` JOIN stocks ON stockInfo.Ysymbol = stocks.Ysymbol;";
$result = mysqli_query($con,$sql);
$jsonArray = [];
while($row = mysqli_fetch_array($result)) {
echo get_object_vars($row)."\n";
array_push($jsonArray, $row);
}
echo json_encode($jsonArray);
}
if($_GET['action']) {
$action = $_GET['action'];
switch($action) {
case 'insertStock':
$name = $_GET['name'];
$symbol = $_GET['symbol'];
$ysymbol = $_GET['ysymbol'];
$valuta = $_GET['valuta'];
$isin = $_GET['isin'];
$sektor = $_GET['sektor'];
$icb = $_GET['icb'];
$owned = $_GET['owned'];
insertStock($con, $name, $symbol, $ysymbol, $valuta, $isin, $sektor, $icb, $owned);
break;
case 'insertStockHistory':
$ysymbol = $_GET['symbol'];
$date = $_GET['date'];
$open = $_GET['open'];
$high = $_GET['high'];
$low = $_GET['low'];
$close = $_GET['close'];
$volume = $_GET['volume'];
$adj_close = $_GET['adj_close'];
insertStockHistory($con, $ysymbol, $date, $open, $high, $low, $close, $volume, $adj_close);
break;
case 'updateStockHistory':
updateStockHistory($con);
break;
case 'addStockHistory':
addStockHistory($con);
break;
case 'updateStocksInfo':
updateStocksInfo($con);
break;
case 'getStockInfo':
getStockInfo($con);
break;
}
}
mysqli_close($con);
?>
/*
Name as Name, stocks.Symbol as Symbol, stocks.Ysymbol as Ysymbol, Valuta as Valuta, ISIN as ISIN, Sektor as Sektor, ICB as ICB, Owned as Owned, BuyPrice as BuyPrice, Category as Category, Ask as Ask, AskRealtime as AskRealtime, AverageDailyVolume as AverageDailyVolume ,Bid as Bid, BidRealtime as BidRealtime,`Change` as Change, ChangeFromFiftydayMovingAverage as ChangeFromFiftydayMovingAverage, ChangeFromTwoHundreddayMovingAverage as ChangeFromTwoHundreddayMovingAverage, ChangeFromYearHigh as ChangeFromYearHigh, ChangeFromYearLow as ChangeFromYearLow, ChangePercentRealtime as ChangePercentRealtime, ChangeRealtime as ChangeRealtime, Change_PercentChange as Change_PercentChange, ChangeinPercent as ChangeinPercent, DaysHigh as DaysHigh, DaysLow as DaysLow, DividendPayDate as DividendPayDate, DividendShare as DividendShare, DividendYield as DividendYield, LastTradeDate as LastTradeDate, LastTradePriceOnly as LastTradePriceOnly, Open as Open, PercebtChangeFromYearHigh as PercebtChangeFromYearHigh, PercentChange as PercentChange, PercentChangeFromFiftydayMovingAverage as PercentChangeFromFiftydayMovingAverage, PercentChangeFromTwoHundreddayMovingAverage as PercentChangeFromTwoHundreddayMovingAverage, PercentChangeFromYearLow as PercentChangeFromYearLow, PreviousClose as PreviousClose, Volume as Volume, YearHigh as YearHigh, YearLow as YearLow*/