Giter Site home page Giter Site logo

dotnetkiteconnect's Introduction

Kite Connect .Net library

The official .Net client for communicating with Kite Connect API.

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, stream live market data (WebSockets), and more, with the simple HTTP API collection.

Zerodha Technology Pvt. Ltd. © 2023. Licensed under the MIT License.

Documentation

Install Client Library

Using NuGet

Execute in Tools » NuGet Package Manager » Package Manager Console

Install-Package Tech.Zerodha.KiteConnect

Using .Net CLI

dotnet add package Tech.Zerodha.KiteConnect

Getting started

// Import library
using KiteConnect;

// Initialize Kiteconnect using apiKey. Enabling Debug will give logs of requests and responses
Kite kite = new Kite(MyAPIKey, Debug: true);

// Collect login url to authenticate user. Load this URL in browser or WebView. 
// After successful authentication this will redirect to your redirect url with request token.
kite.GetLoginURL();

// Collect tokens and user details using the request token
User user = kite.GenerateSession(RequestToken, MySecret);

// Persist these tokens in database or settings
string MyAccessToken = user.AccessToken;
string MyPublicToken = user.PublicToken;

// Initialize Kite APIs with access token
kite.SetAccessToken(MyAccessToken);

// Set session expiry callback. Method can be separate function also.
kite.SetSessionExpiryHook(() => Console.WriteLine("Need to login again"));

// Example call for functions like "PlaceOrder" that returns Dictionary
Dictionary<string, dynamic> response = kite.PlaceOrder(
    Exchange: Constants.EXCHANGE_CDS,
    TradingSymbol: "USDINR17AUGFUT",
    TransactionType: Constants.TRANSACTION_TYPE_SELL,
    Quantity: 1,
    Price: 64.0000m,
    OrderType: Constants.ORDER_TYPE_MARKET,
    Product: Constants.PRODUCT_MIS
);
Console.WriteLine("Order Id: " + response["data"]["order_id"]);

// Example call for functions like "GetHoldings" that returns a data structure
List<Holding> holdings = kite.GetHoldings();
Console.WriteLine(holdings[0].AveragePrice);

For more examples, take a look at Program.cs of KiteConnect Sample project in this repository.

WebSocket live streaming data

This library uses Events to get ticks. These events are non blocking and can be used without additional threads. Create event handlers and attach it to Ticker instance as shown in the example below.

/* 
To get live price use KiteTicker websocket connection. 
It is recommended to use only one websocket connection at any point of time and make sure you stop connection, 
once user goes out of app.
*/

// Create a new Ticker instance
Ticker ticker = new Ticker(MyAPIKey, MyAccessToken);

// Add handlers to events
ticker.OnTick += onTick;
ticker.OnOrderUpdate += OnOrderUpdate;
ticker.OnReconnect += onReconnect;
ticker.OnNoReconnect += oNoReconnect;
ticker.OnError += onError;
ticker.OnClose += onClose;
ticker.OnConnect += onConnect;

// Engage reconnection mechanism and connect to ticker
ticker.EnableReconnect(Interval: 5,Retries: 50);
ticker.Connect();

// Subscribing to NIFTY50 and setting mode to LTP
ticker.Subscribe(Tokens: new UInt32[] { 256265 });
ticker.SetMode(Tokens: new UInt32[] { 256265 }, Mode: Constants.MODE_LTP);

// Example onTick handler
private static void onTick(Tick TickData)
{
    Console.WriteLine("LTP: " + TickData.LastPrice);
}

private static void OnOrderUpdate(Order OrderData)
{
    Console.WriteLine("OrderUpdate " + Utils.JsonSerialize(OrderData));
}

// Disconnect ticker before closing the application
ticker.Close();

For more details about different mode of quotes and subscribing for them, take a look at KiteConnect Sample project in this repository and Kite Connect HTTP API documentation.

dotnetkiteconnect's People

Contributors

ajinasokan avatar vividvilla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dotnetkiteconnect's Issues

Code for Request Token

Can I have code for getting RequestToken programmatically. It is not available in Python API too.

Why it is not there?

When i try to get the RequestToken , I receive "405" Error Code.,

Unable to get Quote Change value

Could you update the GetQuote() to return change and change_percent? current GetQuote() method returns Change = data["net_change"]

As per kite3 documentation
net_change(float) | The absolute change from yesterday's close to last traded price

change(float) | Absolute change from the last traded price to last close price

or could you confirm that these values are same?

Migration to .Net Standard

This issue is to track the library migration to .Net Standard target.
This will allow devs to use library cross platform and in both .Net Core and .Net Framework projects.

Redirect URL

Im writing c# trading application ,do i require to host redirect page in any webserver?
is there any sample php file for redirection ?

Compatibility with .NET Core ??

The documentation mentions that the package is compatible with Framework 4.5.

Just wanted to know if the package can be used with the latest .NET Core 3.1 version as the Framework is being depreciated and the future is .NET Core.

.net core 3.1 compatability

Hi,
with .net core, getting this error.

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.'

Any possible solution?

How to RenewAccessToken using refresh token?

The initial RefreshToken that we get from User object is an empty string. So, it is not working while calling RenewAccessToken() to renew the access token. Is there any sample code which shows how to use it correctly?

utils.cs

i am using visual studio 2022. I use .NET Framework 4.8. But in Utils.cs , NotVisualBasic.FileIO and System.Text.Json both are missing. How can i resolve this issue?

Issue with subscribing to LTP

Hello,

I believe this might be an issue, w.r.t the way we subscribes to ticks, for an instrument. I am interested in getting the LTP for an instrument and following is the code to achieve the same:

var testToken = new uint[]
{
13406210
};
_ticker.Subscribe(Tokens: testToken);
_ticker.SetMode(Tokens: testToken, Mode: Constants.MODE_LTP);

Even though the mode specified is LTP, the OnTick() method is called every second with the same LTP value (please check the issue raised in Kite forum: Kite Forum Link).

Can you please have a look into this?

Thanks!

After Buy Order

After i made intraday MIS buy Order ,how do i close the order, Exit the buy order ?
should i cancel the order ,or should i open sell position.

Not Compatible with .net core

Hi Team,
Requesting you to change the code as per .NET Standard, so that it will be compatible with both .net framework and .net core.

Please find an alternative for the below two library.
System.Web.Script.Serialization;
Microsoft.VisualBasic.FileIO;

Regards
Sujay Babu

Static Class Mapping

Hi,

I was just wondering the reason behind not having static class mapping over dynamic responses, which is far more error prone than classes. Also, the library can have API links as constant pushed to somewhere in other class and utilized RestSharp like libraries for making the calls...

No clear documentation for .net

there is no clear documentation on how to access API using .net. I have created an application in kiteconnect and its showing only API key, there is no API secret. As per documentation I need API secret. Why don't you include a working snippet for .net login

Unable to get quote for Bank Nifty

kite.GetQuote(InstrumentId: new string[] { "NSE:NIFTY BANK"});

throws error.

An exception of type 'KiteConnect.DataException' occurred in KiteConnect.dll but was not handled in user code

Additional information: Unable to parse data. {"instrument_token":260105,"timestamp":"2018-02-05 17:23:35","last_price":26098.75,"last_quantity":0,"last_trade_time":"","average_price":0,"volume":0,"buy_quantity":0,"sell_quantity":0,"ohlc":{"open":25968.15,"high":26215.1,"low":25917.55,"close":26451.15},"net_change":4294967000,"oi":0,"oi_day_high":0,"oi_day_low":0,"depth":{"buy":null,"sell":null}}

Request to update Readme...

Hi,

The library available is not compatible with .net 4.5, please update it to c# 7 i.e 4.7 for rebuild purposes.

Thanks

Issue is with GetHistoricalData()

Instrument Token: 3901185
From Date: 2015-12-22
To Date: 2016-03-21
OI: false

Contonuous: false
Error Message: System.OverflowException: 'Value was either too large or too small for a UInt32.'

vs-ss

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.