-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch_price.php
132 lines (73 loc) · 2.95 KB
/
fetch_price.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
<?php
require_once './include/common.php';
$headers = [
'Content-Type' => 'application/json',
'X-Kite-Version' => '3',
'Authorization' => 'token '.KEY.':'.TOKEN
];
$client = new GuzzleHttp\Client([
'headers' => $headers
]);
$date = date('d-m-Y');
#reseting the order_status
$query = "UPDATE stocklistbackup SET `order_status`='0' ";
$result = mysqli_query($GLOBALS['mysqlConnect'],$query);
$delete_query = "DELETE FROM stockvaluesbackup WHERE `createdDate` = '$date'";
$delete_result = mysqli_query($GLOBALS['mysqlConnect'],$delete_query);
//Getting all stocks
$query = "Select cSymbol from stocklistbackup";
$result = mysqli_query($GLOBALS['mysqlConnect'],$query);
$row = mysqli_fetch_all($result);
$i = 1;
//Concating all stocks at onces
foreach ($row as $record) {
$api_symbol = $record[0];
if (str_contains($api_symbol, "&")) {
$api_symbol = str_replace("&", "%26", $api_symbol);
}
$all_stocks[] = "i=NSE:" . $api_symbol;
}
//Got all data from kite instruments
$all_stocks = implode("&",$all_stocks);
$end_point = "https://api.kite.trade/quote?$all_stocks";
$res = $client->request('GET', $end_point);
$response = $res->getBody()->getContents();
$response = (json_decode($response, true));
//Updating in Database
foreach ($row as $record) {
$api_symbol = $record[0];
if(str_contains($api_symbol,"%26")) {
$api_symbol = str_replace("%26", "&", $api_symbol);
}
$fetch = $response['data']["NSE:$api_symbol"]["ohlc"];
$open = $fetch['open'];
$open = str_replace(",", "", $open);
$low = $fetch['low'];
$low = str_replace(",", "", $low);
$high = $fetch['high'];
$high = str_replace(",", "", $high);
$close = $response['data']["NSE:$api_symbol"]['last_price'];
$close = str_replace(",", "", $close);
$prev_close = $fetch['close'];
$prev_close = str_replace(",", "", $prev_close);
$volume = $response['data']["NSE:$api_symbol"]['volume'];
$alllow = 0;
$allhigh = 0;
$value = 0;
$chng_percentage = 0;
$chng = $close - $prev_close;
$chng = number_format($chng, 1);
$query = "Select id from stocklistbackup where cSymbol = '$api_symbol' ";
$result = mysqli_query($GLOBALS['mysqlConnect'], $query);
$id = $result->fetch_all(MYSQLI_ASSOC);
$sid = $id[0]['id'];
$query = "INSERT INTO stockvaluesbackup(sid, open, high, allHigh, low, allLow, close, schange, schangePercent, volume, stockvalues, addClear, createdDate)
VALUES ('$sid','$open','$high','$allhigh','$low','$alllow','$close','$chng','$chng_percentage','$volume','$value',1,'$date')";
$result = mysqli_query($GLOBALS['mysqlConnect'], $query);
$query = "UPDATE stocklistbackup SET dailyEntry='yes',current_volume='$volume',qbuy='$chng' WHERE id = '$sid'";
$result = mysqli_query($GLOBALS['mysqlConnect'], $query);
echo "$api_symbol Completed - $chng";
echo "\n";
$i++;
}
?>