-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstop_loss.php
97 lines (57 loc) · 2.21 KB
/
stop_loss.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
<?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
]);
$field = array("id,order_id,symbol,price,quanity,stop_loss,target,created_date");
$table = "optionAmo";
$condition = "sl_order_id = ''";
$arugment = array( "field" => $field , "table" => $table, 'condition' => $condition);
$data = select($arugment,"many");
foreach ($data as $value) {
$last_price = $value['price'];
$symbol = $value['symbol'];
$quantity = $value['quanity'];
$stop_loss_trigger_percentage = (5/100) ;
$stop_loss_diff = $last_price * $stop_loss_trigger_percentage;
$stop_loss_trigger = $last_price - $stop_loss_diff;
$stop_loss_trigger = number_format($stop_loss_trigger ,1);
$stop_loss_trigger = str_replace(",","",$stop_loss_trigger);
$stop_loss_percentage = (10/100) ;
$stop_loss_diff = $last_price * $stop_loss_percentage;
$stop_loss = $last_price - $stop_loss_diff;
$stop_loss = number_format($stop_loss ,1);
$stop_loss = str_replace(",","",$stop_loss);
//Set Stoploss
$end_point = "https://api.kite.trade/orders/regular";
$res = $client->request('POST', $end_point, [
'form_params' => [
'tradingsymbol' => $symbol,
'exchange' => 'NFO',
'transaction_type' => "SELL",
'order_type' => 'SL',
'price' => $stop_loss,
'trigger_price' => $stop_loss_trigger,
'quantity' => $quantity,
'product' => 'NRML',
'validity' => 'DAY'
]
]);
$response = $res->getBody()->getContents();
$response = (json_decode($response,true));
//Fetching order id
$order_id = $response['data']['order_id'];
echo "StopLose Order for $symbol : $stop_loss";
echo "\n";
#reseting the dailyentry
$id = $value['id'];
$query = "UPDATE `optionAmo` SET `sl_order_id`='$order_id', `stop_loss`='$stop_loss' WHERE id = '$id'";
$result = mysqli_query($GLOBALS['mysqlConnect'],$query);
}
?>