Jugaad trader implements reverse engineered API for Zerodha in python (hence the name Jugaad). With this library you can programatically execute trades, retrieve your order and trade books, holdings, margins among other things.
Zerodha’s trading platoform - kite, uses REST API to talk to its backend (Kite portal API
). All the functionality of logging in, placing orders, getting portfolio among other funtionalities is implemented using REST APIs. Most of this functionality of Kite portal API
is same as their paid API product - Kite connect API
.
jugaad-trader
is a Python library which implements the Kite portal API
and provides all the functionality that the Kite portal
offers using Python.
No, We use Python’s requests library. While you can automate the trading with Selenium, but that is a very slow and un-reliable method. Selenium implementation is very hard/expensive to deploy to cloud. Whereas with jugaad-trader
you can implement your logic to bare metal, containers, Google cloud functions or AWS Lambda.
pip install jugaad-trader
Similar to how you would need to log in to your Zerodha account from its website, we need to log in to our account in Python with our credentials.
There are two methods to log in and create a session
Beginner friendly method: Using jtrader
command (CLI), this is installed along with jugaad-trader
library
Fully automated method: Directly inside Python code using pyotp library
Here we will use Begineer friendly method
Step 1 - Start session with jtrader
CLI using your zerodha credentials
$ jtrader zerodha startsession
User ID >: Zerodha User Id
Password >:
Pin >:
Logged in successfully
Above command stores the session object in pickle format in your app directory. (Run jtrader zerodha configdir
to find out the config directory.)
Please read CLI reference for more details
Step 2 - Instantiate Zerodha
and issue commands
from jugaad_trader import Zerodha
kite = Zerodha()
# Set access token loads the stored session.
# Name chosen to keep it compatible with kiteconnect.
kite.set_access_token()
# Get profile
profile = kite.profile()
print(profile)
# Get margin
margins = kite.margins()
print(margins)
# Get holdings
holdings = kite.holdings()
print(holdings)
# Get today's positions
positions = kite.positions()
print(positions)
# Get today's orders
orders = kite.orders()
print(orders)
# Finally placing an order
order_resp = kite.place_order(variety=z.VARIETY_REGULAR,
tradingsymbol="INFY",
exchange=kite.EXCHANGE_NSE,
transaction_type=kite.TRANSACTION_TYPE_BUY,
quantity=1,
order_type=kite.ORDER_TYPE_MARKET,
product=kite.PRODUCT_CNC)
print(order_resp)
This class is mostly compatible with official KiteConnect class. There are some methods that are not supported thru browser (for example ‘instruments’), I would request community to report which methods are working and which are not.
from jugaad_trader import Zerodha
kite = Zerodha()
# Set access token loads the stored session.
# Name chosen to keep it compatible with kiteconnect.
kite.set_access_token()
kws = kite.ticker()
def on_ticks(ws, ticks):
# Callback to receive ticks.
print("Ticks: {}".format(ticks))
def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe([738561, 5633])
# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, [738561])
def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
ws.stop()
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
https://github.com/jugaad-py/jugaad-trader.git
Jugaad trader provides access to Console portal functionality as well
2023-11-18Introduction The jtrader command provides set of utilities to manage how you log in and interact with your broker’s account. The idea is that you should not use your credentials in the code ever. Additionally it may provide utilities to interact with your account from command line. Currently it support Zerodha only. Getting started jtrader is the root command, it will then have sub-commands for each of the brokers $ jtrader Usage: jtrader [OPTIONS] COMMAND [ARGS].
2020-06-19 This is part of jugaad-trader documentation, with detailed reference for Upstox
related functions