|
1 | 1 | <?php
|
2 |
| - include dirname(__FILE__)."/kiteconnect.php"; |
3 |
| - |
4 |
| - // Initialise. |
5 |
| - $kite = new KiteConnect("your_api_key"); |
6 |
| - |
7 |
| - // Assuming you have obtained the `request_token` |
8 |
| - // after the auth flow redirect by redirecting the |
9 |
| - // user to $kite->login_url() |
10 |
| - try { |
11 |
| - $user = $kite->generateSession("request_token_obtained", "your_api_secret"); |
12 |
| - |
13 |
| - echo "Authentication successful. \n"; |
14 |
| - print_r($user); |
15 |
| - |
16 |
| - $kite->setAccessToken($user->access_token); |
17 |
| - } catch(Exception $e) { |
18 |
| - echo "Authentication failed: ".$e->getMessage(); |
19 |
| - throw $e; |
20 |
| - } |
21 |
| - |
22 |
| - echo $user->user_id." has logged in"; |
23 |
| - |
24 |
| - // Get the list of positions. |
25 |
| - echo "Positions: \n"; |
26 |
| - print_r($kite->getPositions()); |
27 |
| - |
28 |
| - // Get the list of holdings. |
29 |
| - echo "Holdings: \n"; |
30 |
| - print_r($kite->getHoldings()); |
31 |
| - |
32 |
| - // Retrieve quote and market depth for list of instruments. |
33 |
| - echo "Quote: \n"; |
34 |
| - print_r($kite->getQuote(["NSE:INFY", "NSE:SBIN"])); |
35 |
| - |
36 |
| - // Place order. |
37 |
| - $order_id = $kite->placeOrder("regular", [ |
38 |
| - "tradingsymbol" => "INFY", |
39 |
| - "exchange" => "NSE", |
40 |
| - "quantity" => 1, |
41 |
| - "transaction_type" => "BUY", |
42 |
| - "order_type" => "MARKET", |
43 |
| - "product" => "NRML" |
44 |
| - ])["order_id"]; |
45 |
| - |
46 |
| - echo "Order id is ".$order_id; |
47 |
| - |
48 |
| - // fetch order margin |
49 |
| - $order_param = array(array("exchange" => "NSE", |
50 |
| - "tradingsymbol" => "INFY", |
51 |
| - "transaction_type" => $kite::TRANSACTION_TYPE_BUY, |
52 |
| - "variety" => $kite::VARIETY_REGULAR, |
53 |
| - "product" => $kite::PRODUCT_CNC, |
54 |
| - "order_type" => $kite::ORDER_TYPE_MARKET, |
55 |
| - "quantity" => 1 |
56 |
| - ),); |
57 |
| - print_r($kite->orderMargins($order_param)); |
| 2 | + |
| 3 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 4 | + |
| 5 | +use KiteConnect\KiteConnect; |
| 6 | + |
| 7 | +// Initialise. |
| 8 | +$kite = new KiteConnect("api_key"); |
| 9 | + |
| 10 | +// Assuming you have obtained the `request_token` |
| 11 | +// after the auth flow redirect by redirecting the |
| 12 | +// user to $kite->login_url() |
| 13 | +try { |
| 14 | + $user = $kite->generateSession("request_token", "secret_key"); |
| 15 | + |
| 16 | + echo "Authentication successful. \n"; |
| 17 | + print_r($user); |
| 18 | + |
| 19 | + $kite->setAccessToken($user->access_token); |
| 20 | +} catch (Exception $e) { |
| 21 | + echo "Authentication failed: " . $e->getMessage(); |
| 22 | + |
| 23 | + throw $e; |
| 24 | +} |
| 25 | + |
| 26 | +echo $user->user_id . " has logged in"; |
| 27 | + |
| 28 | +// Get the list of positions. |
| 29 | +echo "Positions: \n"; |
| 30 | +print_r($kite->getPositions()); |
| 31 | + |
| 32 | +// Get the list of holdings. |
| 33 | +echo "Holdings: \n"; |
| 34 | +print_r($kite->getHoldings()); |
| 35 | + |
| 36 | +// Retrieve quote and market depth for list of instruments. |
| 37 | +echo "Quote: \n"; |
| 38 | +print_r($kite->getQuote(["NSE:INFY", "NSE:SBIN"])); |
| 39 | + |
| 40 | +// Place order. |
| 41 | +$order = $kite->placeOrder("regular", [ |
| 42 | + "tradingsymbol" => "INFY", |
| 43 | + "exchange" => "NSE", |
| 44 | + "quantity" => 1, |
| 45 | + "transaction_type" => "BUY", |
| 46 | + "order_type" => "MARKET", |
| 47 | + "product" => "CNC", |
| 48 | +]); |
| 49 | + |
| 50 | +echo "Order id is " . $order->order_id; |
| 51 | + |
| 52 | +// fetch order margin |
| 53 | +$order_param = [["exchange" => "NSE", |
| 54 | + "tradingsymbol" => "INFY", |
| 55 | + "transaction_type" => $kite::TRANSACTION_TYPE_BUY, |
| 56 | + "variety" => $kite::VARIETY_REGULAR, |
| 57 | + "product" => $kite::PRODUCT_CNC, |
| 58 | + "order_type" => $kite::ORDER_TYPE_MARKET, |
| 59 | + "quantity" => 1, |
| 60 | +],]; |
| 61 | + |
| 62 | +print_r($kite->orderMargins($order_param)); |
| 63 | + |
| 64 | +$place_GTT = $kite->placeGTT([ |
| 65 | + "trigger_type" => $kite::GTT_TYPE_SINGLE, |
| 66 | + "tradingsymbol" => "TATAMOTORS", |
| 67 | + "exchange" => "NSE", |
| 68 | + "trigger_values" => array(310), |
| 69 | + "last_price" => 315, |
| 70 | + "orders" => array([ |
| 71 | + "transaction_type" => $kite::TRANSACTION_TYPE_BUY, |
| 72 | + "quantity" => 1, |
| 73 | + "product" => $kite::PRODUCT_CNC, |
| 74 | + "order_type" => $kite::ORDER_TYPE_LIMIT, |
| 75 | + "price" => 314 |
| 76 | + ]) |
| 77 | +]); |
| 78 | +echo "Trigger id is ".$place_GTT->trigger_id; |
| 79 | + |
58 | 80 | ?>
|
0 commit comments