CPPKiteConnect
kiteconnect::ticker Class Reference

ticker wraps around the websocket API provided by KiteConnect and provides a native interface. More...

#include <ws.hpp>

Public Member Functions

 ticker (string Key, unsigned int ConnectTimeout=DEFAULT_CONNECT_TIMEOUT, bool EnableReconnect=false, unsigned int MaxReconnectDelay=DEFAULT_MAX_RECONNECT_DELAY, unsigned int MaxReconnectTries=DEFAULT_MAX_RECONNECT_TRIES)
 Construct a new kiteWS object. More...
 
void setApiKey (const string &key)
 Set the API key. More...
 
string getApiKey () const
 Get API key set at the moment. More...
 
void setAccessToken (const string &token)
 Set the access token. More...
 
string getAccessToken () const
 Get access token set at the moment. More...
 
void connect ()
 Connect to the websocket server.
 
bool isConnected () const
 Check if client is connected.
 
std::chrono::time_point< std::chrono::system_clock > getLastBeatTime () const
 Get the last time heartbeat was received. Should be used in conjunction with the isConnected() method. More...
 
void run ()
 Start the client. Should always be called after connect().
 
void stop ()
 Stop the client. Closes the connection if connected. Should be the last method that is called.
 
void subscribe (const std::vector< int > &instrumentTokens)
 Subscribe to a list of instrument tokens. More...
 
void unsubscribe (const std::vector< int > &instrumentTokens)
 Unsubscribe. More...
 
void setMode (const string &mode, const std::vector< int > &instrumentTokens)
 Set the subscription mode for a list of instrument tokens. More...
 

Public Attributes

std::function< void(ticker *ws)> onConnect
 Called on successful connect.
 
std::function< void(ticker *ws, const std::vector< kc::tick > &ticks)> onTicks
 Called when ticks are received.
 
std::function< void(ticker *ws, const kc::postback &postback)> onOrderUpdate
 Called when an order update is received.
 
std::function< void(ticker *ws, const string &message)> onMessage
 Called when a message is received.
 
std::function< void(ticker *ws, int code, const string &message)> onError
 Called when connection is closed with an error or websocket server sends an error message.
 
std::function< void(ticker *ws)> onConnectError
 Called when an error occures while trying to connect.
 
std::function< void(ticker *ws, unsigned int attemptCount)> onTryReconnect
 Called when reconnection is being attempted. More...
 
std::function< void(ticker *ws)> onReconnectFail
 Called when reconnect attempts exceed maximum reconnect attempts set by user i.e., when ticker is unable to reconnect.
 
std::function< void(ticker *ws, int code, const string &message)> onClose
 Called when connection is closed.
 

Friends

class tickerTest_binaryParsingTest_Test
 

Detailed Description

ticker wraps around the websocket API provided by KiteConnect and provides a native interface.

Constructor & Destructor Documentation

◆ ticker()

kiteconnect::ticker::ticker ( string  Key,
unsigned int  ConnectTimeout = DEFAULT_CONNECT_TIMEOUT,
bool  EnableReconnect = false,
unsigned int  MaxReconnectDelay = DEFAULT_MAX_RECONNECT_DELAY,
unsigned int  MaxReconnectTries = DEFAULT_MAX_RECONNECT_TRIES 
)
inlineexplicit

Construct a new kiteWS object.

Parameters
apikeyAPI key
connecttimeoutConnection timeout
enablereconnectShould be set to true for enabling reconnection
maxreconnectdelayMaximum reconnect delay for reconnection
maxreconnecttriesMaximum reconnection attempts after which onReconnectFail will be called and no further attempt to reconnect will be made.

Construct a new ticker object. All durations are in seconds.

Parameters
KeyAPI key
ConnectTimeoutconnection timeout
EnableReconnectauto reconnect is enabled if EnableReconnect is set to true
MaxReconnectDelayMaximum delay after which subsequent reconnection interval will become constant
MaxReconnectTriesMaximum number of retries before ticker quits trying to reconnect.

Member Function Documentation

◆ getAccessToken()

string kiteconnect::ticker::getAccessToken ( ) const
inline

Get access token set at the moment.

Returns
string access token

◆ getApiKey()

string kiteconnect::ticker::getApiKey ( ) const
inline

Get API key set at the moment.

Returns
string API key

◆ getLastBeatTime()

std::chrono::time_point< std::chrono::system_clock > kiteconnect::ticker::getLastBeatTime ( ) const
inline

Get the last time heartbeat was received. Should be used in conjunction with the isConnected() method.

Returns
std::chrono::time_point<std::chrono::system_clock> time

◆ setAccessToken()

void kiteconnect::ticker::setAccessToken ( const string &  token)
inline

Set the access token.

Parameters
tokenaccess token is set to token.

example

// Kite.setAccessToken(accessToken);
// Kite.setAccessToken(std::getenv("KITE_ACCESS_TOKEN"));

◆ setApiKey()

void kiteconnect::ticker::setApiKey ( const string &  key)
inline

Set the API key.

Parameters
keyAPI key is set to key

◆ setMode()

void kiteconnect::ticker::setMode ( const string &  mode,
const std::vector< int > &  instrumentTokens 
)
inline

Set the subscription mode for a list of instrument tokens.

Parameters
modemode to set
instrumentTokenslist of instrument tokens whose mode should be set

◆ subscribe()

void kiteconnect::ticker::subscribe ( const std::vector< int > &  instrumentTokens)
inline

Subscribe to a list of instrument tokens.

Parameters
instrumentTokenslist of instrument tokens that should be subscribed

◆ unsubscribe()

void kiteconnect::ticker::unsubscribe ( const std::vector< int > &  instrumentTokens)
inline

Unsubscribe.

Parameters
instrumentTokenslist of instrument tokens that should be unsubscribed

Member Data Documentation

◆ onTryReconnect

std::function<void(ticker* ws, unsigned int attemptCount)> kiteconnect::ticker::onTryReconnect

Called when reconnection is being attempted.

Note
Auto reconnection: Auto reconnection is disabled by default and can be enabled by setting EnableReconnect to true in ticker's constructor. Auto reonnection mechanism is based on exponential backoff algorithm in which next retry interval will be increased exponentially. MaxReconnectDelay and MaxReconnectTries params can be used to tweak the alogrithm. MaxReconnectDelay is the maximum delay after which subsequent reconnection interval will become constant and MaxReconnectTries is maximum number of retries before ticker quits trying to reconnect.

The documentation for this class was generated from the following files: