You need to install the jugaad-trader
library, you can do this using pip-
pip install jugaad-trader --upgrade
You can follow the documentation here for more details
>> from jugaad_trader import Zerodha
>> user_id = "USERID"
>> password = "PASSWORD"
>> two_fa = "PIN"
⚠️ Using your password directly in the code is not the best way, recommend you to follow this documentation for more details
Now log in to Your account-
>> z = Zerodha(user_id, password, two_fa)
>> print(z.login())
{'status': 'success', 'data': {'profile': {}}}
Let us fetch your profile information first
>> print(z.profile())
{'user_id': 'USERID',
'user_type': 'individual',
'email': 'xyz@abc.com',
'user_name': 'XYZ ABC',
'user_shortname': 'ABC',
'broker': 'ZERODHA',
'exchanges': ['BFO', 'CDS', 'BSE', 'NFO', 'MF', 'NSE'],
'products': ['CNC', 'NRML', 'MIS', 'BO', 'CO'],
'order_types': ['MARKET', 'LIMIT', 'SL', 'SL-M'],
'avatar_url': '',
'meta': {'demat_consent': 'physical'}}
I’m writing this article in off-market hours so I could not place a normal order, instead I had to place an “After Market” order.
>> order_id = z.place_order(tradingsymbol="INFY",
exchange=z.EXCHANGE_NSE,
transaction_type=z.TRANSACTION_TYPE_BUY,
quantity=1,
order_type=z.ORDER_TYPE_MARKET,
product=z.PRODUCT_CNC,
variety=z.VARIETY_AMO)
>> print(order_id)
2xxyyxxeejjddd
You might have observed that the API is really very similar to KiteConnect
and this is intentional design choice. You can pretty much follow KiteConnect documentation to explore other features. There are few things that might now work as of now like instruments
method or KiteTicker
.
If you are a beginner, I would recommend you to read more on Zerodha product types, what is AMO etc, so that you can relate better to what is the meaning of z.ORDER_TYPE_MARKET
, z.PRODUCT_CNC
z.VARIETY_AMO
and most importantly you know what you are getting into, Because trading without knowing can be very risky, you can loose money very quickly.
Now let us look at our order book-
>> print(z.orders())
>> [{'placed_by': 'USERID', 'order_id': '2xxyyxxeejjddd', 'exchange_order_id': None, 'parent_order_id': None, 'status': 'AMO REQ RECEIVED', 'status_message': None, 'status_message_raw': None, 'order_timestamp': datetime.datetime(2020, 7, 1, 9, 10), 'exchange_update_timestamp': None, 'exchange_timestamp': None, 'variety': 'amo', 'exchange': 'NSE', 'tradingsymbol': 'INFY', 'instrument_token': 408065, 'order_type': 'MARKET', 'transaction_type': 'BUY', 'validity': 'DAY', 'product': 'CNC', 'quantity': 1, 'disclosed_quantity': 0, 'price': 0, 'trigger_price': 0, 'average_price': 0, 'filled_quantity': 0, 'pending_quantity': 1, 'cancelled_quantity': 0, 'market_protection': 0, 'meta': {}, 'tag': None, 'guid': 'xxxxxxxxxxxxxx'}]
If you have managed to execute everything till this point, congratulations! you are now ready to automate your trades, If not let me know in comments or in github issues page
Putting it all together in a single script-
from jugaad_trader import Zerodha
user_id = "USERID"
password = "PASSWORD"
two_fa = "PIN"
z = Zerodha(user_id, password, two_fa)
print(z.login())
order_id = z.place_order(tradingsymbol="INFY",
exchange=z.EXCHANGE_NSE,
transaction_type=z.TRANSACTION_TYPE_BUY,
quantity=1,
order_type=z.ORDER_TYPE_MARKET,
product=z.PRODUCT_CNC,
variety=z.VARIETY_AMO)
print(order_id)
print(z.orders())
You might be really excited now to try new things with this library, I dont want to curb your enthusiasm, but I just wanted to make you aware of few things-
As of today, this library is working, but it is highly subject to Zerodha not changing their website APIs. In my experience they do change the API every few months if not daily, and when they do that, your code will break. For any serious trading, consider buying KiteConnect
subscription.
Play nice, Do not bombard Zerodha server with frequent requests, your IP will get blocked. Avoid running it in loops or atleast add some delay with sleep
.
Like your session with Zerodha expires after some time in browser and you have to login again, your python session will expire after some time, This is very much possible to handle programatically but you will have to write some extra code to take care of that scenario.
In any algo trading system, “Execution” is the last part. but as a programmer, this part is really tempting for me and I developed this library. But what trades will you execute without any strategy. I myself am really a beginner in this regards.
So I recommend you to learn a lot about strategies and backtesting, once you master strategies, Its all going to rain money.