Jugaad Trader

Introduction

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.

(Upstox support is under development)

Features

Installation

pip install jugaad-trader

Getting Started with Zerodha

Library provides a CLI to manage your session/credentials. It is not recommended to use the credentials directly in the code.

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.)

Zerodha CLI reference

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.

Working with ticker

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()


Getting Started with Upstox

Step 1 Save your credentials configuration using jtrader CLI

$ jtrader upstox savecreds
Saves your creds in app config folder in file named .ucred
User ID >: USERID
Password >:
Pin >:
Saved credentials successfully

Step 2 - Instantiate Upstox and issue commands

from jugaad_trader import Upstox
user_id = "USERID"
password = "PASSWORD"
twofa = "TWOFA"
u = Upstox(user_id, password, twofa)

# Login
u.login()

# Get profile information
profile = u.get_client_info()

# More documentation to follow

Detailed Upstox documentation

Please note that Upstox support is still work in progress. Upstox websites uses websockets for all server interaction as opposed normal HTTP requests which makes it a slightly difficultf to reverse engineer.

Source code

https://github.com/jugaad-py/jugaad-trader.git

Command line interface reference

Introduction 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

Jugaad Trader - Upstox documentation

This is part of jugaad-trader documentation, with detailed reference for Upstox related functions

2020-06-19