Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Algorithmic Trading Cookbook

You're reading from   Python Algorithmic Trading Cookbook All the recipes you need to implement your own algorithmic trading strategies in Python

Arrow left icon
Product type Paperback
Published in Aug 2020
Publisher Packt
ISBN-13 9781838989354
Length 542 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Pushpak Dagade Pushpak Dagade
Author Profile Icon Pushpak Dagade
Pushpak Dagade
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Handling and Manipulating Date, Time, and Time Series Data 2. Stock Markets - Primer on Trading FREE CHAPTER 3. Fetching Financial Data 4. Computing Candlesticks and Historical Data 5. Computing and Plotting Technical Indicators 6. Placing Regular Orders on the Exchange 7. Placing Bracket and Cover Orders on the Exchange 8. Algorithmic Trading Strategies - Coding Step by Step 9. Algorithmic Trading - Backtesting 10. Algorithmic Trading - Paper Trading 11. Algorithmic Trading - Real Trading 12. Other Books You May Enjoy Appendix I
1. Appendix II
2. Appendix III

Setting up Python connectivity with the broker

The first thing you need to set up connectivity with the broker is API keys. The broker provides unique keys to each customer, typically as an api-key and api-secret key pair. These API keys are chargeable, usually on a monthly subscription basis. You need to get your copies of api-key and api-secret from the broker's website before you start this recipe. Please refer to Appendix I for more details.

How to do it…

We execute the following steps to complete this recipe:

  1. Import the necessary modules:
>>> from pyalgotrading.broker.broker_connection_zerodha import BrokerConnectionZerodha
  1. Get the api_key and api_secret keys from the broker. These are unique to you and will be used by the broker to identify your Demat account:
>>> api_key = "<your-api-key>"
>>> api_secret = "<your-api-secret>"
>>> broker_connection = BrokerConnectionZerodha(api_key, api_secret)

You will get the following result:

Installing package kiteconnect via pip...
Please login to this link to generate your request token: https://kite.trade/connect/login?api_key=<your-api-key>&v=3
  1. Get the request token from the preceding URL:
>>> request_token = "<your-request-token>"
>>> broker_connection.set_access_token(request_token)

How it works...

In step 1, you import the BrokerConnectionZerodha class from pyalgotrading. The BrokerConnectionZerodha class provides an abstraction around the broker-specific APIs. For step 2, you need your API key and API secret from the broker. If you do not have them, please refer to Appendix I for detailed instructions with screenshots on getting this keys. In step 2, you assign your API key and API secret to the new api_key and api_secret variables and use them to create broker_connection, an instance of the BrokerConnectionZerodha class. If you are running this for the first time and kiteconnect is not installed, pyalgotrading will automatically install it for you. (kiteconnect is the official Python package that talks to the Zerodha backend; BrokerConnectionZerodha is a wrapper on top of kiteconnect.) Step 2 generates a login URL. Here, you need to click on the link and log in with your Zerodha credentials. If the authentication process is successful, you will see a link in your browser's address bar that looks similar to the following:

https://127.0.0.1/?request_token=&action=login&status=success

For example, the full link would be as follows:

https://127.0.0.1/?request_token=H06I6Ydv95y23D2Dp7NbigFjKweGwRP7&action=login&status=success

Copy the alphanumeric-token, H06I6Ydv95y23D2Dp7NbigFjKweGwRP7, and paste it into request_token as part of step 3. The broker_connection instance is now ready to perform API calls.

You have been reading a chapter from
Python Algorithmic Trading Cookbook
Published in: Aug 2020
Publisher: Packt
ISBN-13: 9781838989354
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image