-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorder_sell_execute.php
94 lines (52 loc) · 1.84 KB
/
order_sell_execute.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
<?php
require_once './include/common.php';
// setting up end headers
$headers = [
'Content-Type' => 'application/json',
'X-Kite-Version' => '3',
'Authorization' => 'token '.KEY.':'.TOKEN
];
$client = new GuzzleHttp\Client([
'headers' => $headers
]);
//Fetching stock Symbol
$order_id = $_GET['id'];
$query = "SELECT * from stockAmo where order_id='$order_id'";
$result = mysqli_query($GLOBALS['mysqlConnect'],$query);
$data = mysqli_fetch_assoc($result);
$symbol = $data['symbol'];
$quantity = $data['quanity'];
$end_point = "https://api.kite.trade/quote?i=NSE:$symbol";
$res = $client->request('GET', $end_point);
$response = $res->getBody()->getContents();
$response = (json_decode($response, true));
$last_price = $response['data']["NSE:$symbol"]['last_price'];
$last_price = str_replace(",", "", $last_price); //last price
//Place Order
$end_point = "https://api.kite.trade/orders/regular/$order_id";
$res = $client->request('PUT', $end_point, [
'form_params' => [
'order_type' => 'MARKET',
'quantity' => $quantity,
'validity' => 'DAY'
]
]);
$response = $res->getBody()->getContents();
$response = (json_decode($response,true));
//Fetching order id
$order_id = $response['data']['order_id'];
//Fetch Average Price
$end_point = "https://api.kite.trade/orders/$order_id";
$res = $client->request('GET', $end_point);
$response = $res->getBody()->getContents();
$response = (json_decode($response,true));
$length = count($response['data']);
$length = $length-1;
$price = $response['data'][$length]['average_price'];
$price = str_replace(",", "", $price); //last price
$price = number_format($price,1);
$query = "UPDATE `stockAmo` SET `target`='$price' WHERE order_id='$order_id'";
$result = mysqli_query($GLOBALS['mysqlConnect'],$query);
header("location:stock_execution.php");
exit;
?>