upstox/upstox-php-sdk

The official PHP client for communicating with the Upstox API

v1.10.0 2024-10-14 13:42 UTC

This package is auto-updated.

Last update: 2024-11-14 13:51:38 UTC


README

Latest Stable Version

Introduction

The official PHP client for communicating with the Upstox API.

Upstox API is a set of rest APIs that provide data required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (using Websocket), and more, with the easy to understand API collection.

  • API version: v2
  • Build package: io.swagger.codegen.v3.generators.php.PhpClientCodegen

This PHP package is automatically generated by the Swagger Codegen project:

Documentation.

Upstox API Documentation

Requirements

PHP 7.4 and later

Installation & Usage

Composer

Run composer require upstox/upstox-php-sdk to install the SDK from Packagist.

Github

Composer Installation

Download the latest release and run composer install

Manual Installation

Download the files and include autoload.php:

    require_once('/path/to/UpstoxClient/vendor/autoload.php');

Tests

To run the unit tests:

composer install
./vendor/bin/phpunit

Examples

Sample Implementations can be found within /examples folder.

Documentation for API Endpoints

All URIs are relative to https://api.upstox.com/v2/

Documentation for Feeder Functions

Connecting to the WebSocket for market and portfolio updates is streamlined through two primary Feeder functions:

  1. MarketDataStreamer: Offers real-time market updates, providing a seamless way to receive instantaneous information on various market instruments.
  2. PortfolioDataStreamer: Delivers updates related to the user's orders, enhancing the ability to track order status and portfolio changes effectively.

Both functions are designed to simplify the process of subscribing to essential data streams, ensuring users have quick and easy access to the information they need.

Detailed Explanation of Feeder Interface

MarketDataStreamer

The MarketDataStreamer interface is designed for effortless connection to the market WebSocket, enabling users to receive instantaneous updates on various instruments. The following example demonstrates how to quickly set up and start receiving market updates for selected instrument keys:

use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;

function on_message($streamer, $data)
{
    print($data);
}

config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new MarketDataStreamer($config, ["NSE_INDEX|Nifty 50", "NSE_INDEX|Nifty Bank"], "full");

$streamer->on("message", 'on_message');
$streamer->connect();

EventLoop::run();

In this example, you first authenticate using an access token, then instantiate MarketDataStreamer with specific instrument keys and a subscription mode. Upon connecting, the streamer listens for market updates, which are logged to the console as they arrive.

Feel free to adjust the access token placeholder and any other specifics to better fit your actual implementation or usage scenario.

Exploring the MarketDataStreamer Functionality

Modes

  • ltpc: ltpc provides information solely about the most recent trade, encompassing details such as the last trade price, time of the last trade, quantity traded, and the closing price from the previous day.
  • full: The full option offers comprehensive information, including the latest trade prices, D5 depth, 1-minute, 30-minute, and daily candlestick data, along with some additional details.

Functions

  1. constructor MarketDataStreamer(apiClient, instrumentKeys, mode): Initializes the streamer with optional instrument keys and mode (full or ltpc).
  2. connect(): Establishes the WebSocket connection.
  3. subscribe(instrumentKeys, mode): Subscribes to updates for given instrument keys in the specified mode. Both parameters are mandatory.
  4. unsubscribe(instrumentKeys): Stops updates for the specified instrument keys.
  5. changeMode(instrumentKeys, mode): Switches the mode for already subscribed instrument keys.
  6. disconnect(): Ends the active WebSocket connection.
  7. autoReconnect(enable, interval, retryCount): Customizes auto-reconnect functionality. Parameters include a flag to enable/disable it, the interval(in seconds) between attempts, and the maximum number of retries.

Events

  • open: Emitted upon successful connection establishment.
  • close: Indicates the WebSocket connection has been closed.
  • message: Delivers market updates.
  • error: Signals an error has occurred.
  • reconnecting: Announced when a reconnect attempt is initiated.
  • autoReconnectStopped: Informs when auto-reconnect efforts have ceased after exhausting the retry count.

The following documentation includes examples to illustrate the usage of these functions and events, providing a practical understanding of how to interact with the MarketDataStreamer effectively.


  1. Subscribing to Market Data on Connection Open with MarketDataStreamer
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;

function on_open($streamer)
{
    print("Connection Established");
    $streamer->subscribe(["NSE_INDEX|Nifty 50", "NSE_INDEX|Nifty Bank"], "full");
}

function on_message($streamer, $data)
{
    print($data);
}


config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new MarketDataStreamer($config);

$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();

EventLoop::run();

  1. Subscribing to Instruments with Delays
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
use function Amp\delay;

function on_open($streamer)
{
    print("Connection Established");
    $streamer->subscribe(["NSE_INDEX|Nifty 50"], "full");
    delay(5);
    $streamer->subscribe(["NSE_INDEX|Nifty Bank"], "full");
}

function on_message($streamer, $data)
{
    print($data);
}


config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new MarketDataStreamer($config);

$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();

EventLoop::run();

  1. Subscribing and Unsubscribing to Instruments
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
use function Amp\delay;

function on_open($streamer)
{
    print("Connection Established");
    $streamer->subscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"]);
    delay(5);
    $streamer->unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"]);
}

function on_message($streamer, $data)
{
    print($data);
}


config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new MarketDataStreamer($config);

$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();

EventLoop::run();

  1. Subscribe, Change Mode and Unsubscribe
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;
use function Amp\delay;

function on_open($streamer)
{
    print("Connection Established");
    $streamer->subscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"]);
    delay(5);
    $streamer->changeMode(
        ["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "ltpc");
}

function on_message($streamer, $data)
{
    print($data);
}


config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new MarketDataStreamer($config);

$streamer->on("open", 'on_open');
$streamer->on("message", 'on_message');
$streamer->connect();

EventLoop::run();

  1. Disable Auto-Reconnect
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;

function on_reconnectstopped($streamer, $data)
{
    print($data);
}


config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new MarketDataStreamer($config);

$streamer->on("autoReconnectStopped", 'on_reconnectstopped');

$streamer->autoReconnect(false);
$streamer->connect();

EventLoop::run();

  1. Modify Auto-Reconnect parameters
use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;

config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new MarketDataStreamer($config);

$streamer->autoReconnect(true, 10, 3);
$streamer->connect();

EventLoop::run();

PortfolioDataStreamer

Connecting to the Portfolio WebSocket for real-time order updates is straightforward with the PortfolioDataStreamer function. Below is a concise guide to get you started on receiving updates:

use Upstox\Client\Configuration;
use Upstox\Client\Feeder\MarketDataStreamer;
use Revolt\EventLoop;

function on_message($streamer, $data)
{
    print($data);
}

config = Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);

$streamer = new PortfolioDataStreamer($config);

$streamer->on("message", 'on_message');
$streamer->connect();

EventLoop::run();

Position and holding updates can be enabled by setting the corresponding flag to true in the constructor of the PortfolioDataStreamer class.

use Upstox\Client\Configuration;
use Upstox\Client\Feeder\PortfolioDataStreamer;
use Revolt\EventLoop;

function on_message($streamer,$data)
{
    print("on_message= " . ($data) . "\n");
}

$config = Upstox\Client\Configuration::getDefaultConfiguration()->setAccessToken(<ACCESS_TOKEN>);
$streamer = new PortfolioDataStreamer($config,orderUpdate:true,holdingUpdate:true,positionUpdate:true);

$streamer->on("message", 'on_message');
$streamer->connect();

EventLoop::run();

This example demonstrates initializing the PortfolioDataStreamer, connecting it to the WebSocket, and setting up an event listener to receive and print order updates. Replace <ACCESS_TOKEN> with your valid access token to authenticate the session.

Exploring the PortfolioDataStreamer Functionality

Functions

  1. constructor PortfolioDataStreamer(): Initializes the streamer.
  2. connect(): Establishes the WebSocket connection.
  3. disconnect(): Ends the active WebSocket connection.
  4. autoReconnect(enable, interval, retryCount): Customizes auto-reconnect functionality. Parameters include a flag to enable/disable it, the interval(in seconds) between attempts, and the maximum number of retries.

Events

  • open: Emitted upon successful connection establishment.
  • close: Indicates the WebSocket connection has been closed.
  • message: Delivers market updates.
  • error: Signals an error has occurred.
  • reconnecting: Announced when a reconnect attempt is initiated.
  • autoReconnectStopped: Informs when auto-reconnect efforts have ceased after exhausting the retry count.

Documentation For Models