Skip to content

Commit

Permalink
Merge pull request #16 from ranjanrak/refactor
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
vividvilla authored Apr 6, 2021
2 parents e282f42 + d4ccc1f commit 11b2e48
Show file tree
Hide file tree
Showing 47 changed files with 5,291 additions and 1,519 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea
.php_cs
.php_cs.cache
.phpunit.result.cache
build
composer.lock
coverage
docs
phpunit.xml
psalm.xml
vendor
index.php
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
New features
=============
# Changelog

All notable changes to `phpkiteconnect` will be documented in this file.

## 1.0.0 - 202X-XX-XX

- initial release
#### New Features
- method: `getProfile`
- method: `getOHLC`
- method: `getLTP`
Expand Down Expand Up @@ -42,9 +48,12 @@ API method name changes

Params and other changes
========================
- `placeOrder` method takes all the params as array including `variety`
- `modifyOrder` method takes all the params as array including `variety` and `order_id`
- `cancelOrder` method takes all the params as array
- `convertPosition` method takes all the params as array
- `getHistoricalData` method takes all the params as array
- [Changes in `generateSession` response structure](https://kite.trade/docs/connect/v3/user/#response-attributes)
- [Changes in `requestAccessToken` response structure](https://kite.trade/docs/connect/v3/user/#response-attributes)
- [Changes in `getPositions` response structure](https://kite.trade/docs/connect/v3/portfolio/#response-attributes_1)
- [Changes in `getQuote` response structure](https://kite.trade/docs/connect/v3/market-quotes/#retrieving-full-market-quotes)
- [Changes in `placeOrder` params](https://kite.trade/docs/connect/v3/orders/#bracket-order-bo-parameters)
Expand Down
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) [Zerodha Technology](http://zerodha.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
# The Kite Connect API PHP client - v3
The official PHP client for communicating with the [Kite Connect API](https://kite.trade).
The Official PHP client for communicating with the [Kite Connect API](https://kite.trade).

Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio and more, with the simple HTTP API collection.

[Zerodha Technology](http://zerodha.com) (c) 2018. Licensed under the MIT License.
[Zerodha Technology](http://zerodha.com) (c) 2021. Licensed under the MIT License.

## Documentation
- [PHP client documentation](https://kite.trade/docs/phpkiteconnect/v3)
- [Kite Connect HTTP API documentation](https://kite.trade/docs/connect/v3)

## Installing
Download `kiteconnect.php` and `include()` it in your application.
### Requirements
1. Install [PHP](https://www.php.net/manual/en/install.php) version 7.3 or higher.
2. Install [Composer](https://getcomposer.org/download/), which is used to install PHP packages.

You can install the package via composer:
```bash
composer require zerodha/phpkiteconnect
```

## Usage
```php
<?php
include dirname(__FILE__)."/kiteconnect.php";
require_once __DIR__ . '/vendor/autoload.php';

use KiteConnect\KiteConnect;

// Initialise.
$kite = new KiteConnect("your_api_key");
Expand All @@ -25,10 +34,8 @@ Download `kiteconnect.php` and `include()` it in your application.
// user to $kite->login_url()
try {
$user = $kite->generateSession("request_token_obtained", "your_api_secret");

echo "Authentication successful. \n";
print_r($user);

$kite->setAccessToken($user->access_token);
} catch(Exception $e) {
echo "Authentication failed: ".$e->getMessage();
Expand All @@ -42,7 +49,7 @@ Download `kiteconnect.php` and `include()` it in your application.
print_r($kite->getPositions());

// Place order.
$o = $kite->placeOrder("regular", [
$order = $kite->placeOrder("regular", [
"tradingsymbol" => "INFY",
"exchange" => "NSE",
"quantity" => 1,
Expand All @@ -51,12 +58,20 @@ Download `kiteconnect.php` and `include()` it in your application.
"product" => "NRML"
]);

echo "Order id is ".$o->order_id;
echo "Order id is ".$order->order_id;
?>
```

## Examples

Check [examples folder](https://github.com/zerodha/phpkiteconnect/tree/master/examples) for more examples.

Refer to the [PHP client documentation](https://kite.trade/docs/phpkiteconnect/v3) for the complete list of supported methods.

## Changelog
[Check CHANGELOG.md](CHANGELOG.md)
## Run unit tests
```
phpunit tests/KiteConnectTest.php
```

## Changelog
[Check CHANGELOG.md](CHANGELOG.md)
52 changes: 52 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "zerodha/phpkiteconnect",
"description": "The PHP client library for the Kite Connect trading APIs Resources",
"type": "library",
"keywords": [
"zerodha",
"phpkiteconnect"
],
"homepage": "https://github.com/zerodha/phpkiteconnect",
"license": "MIT",
"authors": [
{
"name": "zerodha",
"email": "[email protected]",
"role": "Owner"
}
],
"require": {
"php": ">=7.3",
"ext-curl": "*",
"ext-json": "*",
"ext-zlib": "*",
"guzzlehttp/guzzle": "^7.2",
"guzzlehttp/psr7": "^1.7"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.17",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.3"
},
"autoload": {
"psr-4": {
"KiteConnect\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"KiteConnect\\Tests\\": "tests"
}
},
"scripts": {
"psalm": "vendor/bin/psalm",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
134 changes: 78 additions & 56 deletions examples/kiteconnect_sample.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,80 @@
<?php
include dirname(__FILE__)."/kiteconnect.php";

// Initialise.
$kite = new KiteConnect("your_api_key");

// Assuming you have obtained the `request_token`
// after the auth flow redirect by redirecting the
// user to $kite->login_url()
try {
$user = $kite->generateSession("request_token_obtained", "your_api_secret");

echo "Authentication successful. \n";
print_r($user);

$kite->setAccessToken($user->access_token);
} catch(Exception $e) {
echo "Authentication failed: ".$e->getMessage();
throw $e;
}

echo $user->user_id." has logged in";

// Get the list of positions.
echo "Positions: \n";
print_r($kite->getPositions());

// Get the list of holdings.
echo "Holdings: \n";
print_r($kite->getHoldings());

// Retrieve quote and market depth for list of instruments.
echo "Quote: \n";
print_r($kite->getQuote(["NSE:INFY", "NSE:SBIN"]));

// Place order.
$order_id = $kite->placeOrder("regular", [
"tradingsymbol" => "INFY",
"exchange" => "NSE",
"quantity" => 1,
"transaction_type" => "BUY",
"order_type" => "MARKET",
"product" => "NRML"
])["order_id"];

echo "Order id is ".$order_id;

// fetch order margin
$order_param = array(array("exchange" => "NSE",
"tradingsymbol" => "INFY",
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
"variety" => $kite::VARIETY_REGULAR,
"product" => $kite::PRODUCT_CNC,
"order_type" => $kite::ORDER_TYPE_MARKET,
"quantity" => 1
),);
print_r($kite->orderMargins($order_param));

require_once __DIR__ . '/vendor/autoload.php';

use KiteConnect\KiteConnect;

// Initialise.
$kite = new KiteConnect("api_key");

// Assuming you have obtained the `request_token`
// after the auth flow redirect by redirecting the
// user to $kite->login_url()
try {
$user = $kite->generateSession("request_token", "secret_key");

echo "Authentication successful. \n";
print_r($user);

$kite->setAccessToken($user->access_token);
} catch (Exception $e) {
echo "Authentication failed: " . $e->getMessage();

throw $e;
}

echo $user->user_id . " has logged in";

// Get the list of positions.
echo "Positions: \n";
print_r($kite->getPositions());

// Get the list of holdings.
echo "Holdings: \n";
print_r($kite->getHoldings());

// Retrieve quote and market depth for list of instruments.
echo "Quote: \n";
print_r($kite->getQuote(["NSE:INFY", "NSE:SBIN"]));

// Place order.
$order = $kite->placeOrder("regular", [
"tradingsymbol" => "INFY",
"exchange" => "NSE",
"quantity" => 1,
"transaction_type" => "BUY",
"order_type" => "MARKET",
"product" => "CNC",
]);

echo "Order id is " . $order->order_id;

// fetch order margin
$order_param = [["exchange" => "NSE",
"tradingsymbol" => "INFY",
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
"variety" => $kite::VARIETY_REGULAR,
"product" => $kite::PRODUCT_CNC,
"order_type" => $kite::ORDER_TYPE_MARKET,
"quantity" => 1,
],];

print_r($kite->orderMargins($order_param));

$place_GTT = $kite->placeGTT([
"trigger_type" => $kite::GTT_TYPE_SINGLE,
"tradingsymbol" => "TATAMOTORS",
"exchange" => "NSE",
"trigger_values" => array(310),
"last_price" => 315,
"orders" => array([
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
"quantity" => 1,
"product" => $kite::PRODUCT_CNC,
"order_type" => $kite::ORDER_TYPE_LIMIT,
"price" => 314
])
]);
echo "Trigger id is ".$place_GTT->trigger_id;

?>
Loading

0 comments on commit 11b2e48

Please sign in to comment.