官术网_书友最值得收藏!

Technical requirements

You will need the following to successfully execute the recipes in this chapter:

  • Python 3.7+
  • The pyalgotrading Python package ($ pip install pyalgotrading)

The latest Jupyter notebook for this chapter can be found on GitHub at https://github.com/PacktPublishing/Python-Algorithmic-Trading-Cookbook.

The following code will help you set up the broker connection with Zerodha, which will be used in all the recipes in this chapter. Please make sure you have followed these steps before trying out any recipes.

The first thing needed for setting up connectivity with the broker is getting the API keys. The broker would provide 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 copy of api-key and api-secret from the broker website before starting this. You can refer to Appendix I for more details.

Execute the following steps:

  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 output:

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

If you are running this for the first time and kiteconnect is not installed, pyalgotrading will automatically install it for you. The final output of step 2 will be a link. Click on the link and log in with your Zerodha credentials. If the authentication is successful, you will see a link in your browser's address bar similar to this: https://127.0.0.1/?request_token=<alphanumeric-token>&action=login&status=success.

The following is an example:

https://127.0.0.1/?request_token=H06I6Ydv95y23D2Dp7NbigFjKweGwRP7&action=login&status=success
  1. Copy the alphanumeric-token and paste it in request_token:
>>> request_token = "<your-request-token>"
>>> broker_connection.set_access_token(request_token)

The broker_connection instance is now ready to perform API calls.

The pyalgotrading package supports multiple brokers and provides a connection object class per broker, with the same methods. It abstracts broker APIs behind a unified interface, so you need not worry about the underlying broker API calls and you can use all the recipes in this chapter as is. Only the procedure to set up the broker connection will vary from broker to broker. You can refer to the pyalgotrading documentation for setting up the broker connection if you are not using Zerodha as your broker. For Zerodha users, the previous steps will suffice.

主站蜘蛛池模板: 乐陵市| 泰州市| 镇坪县| 松阳县| 平山县| 朔州市| 广灵县| 贵定县| 兰西县| 夏河县| 文登市| 汉沽区| 广水市| 浙江省| 玛曲县| 清苑县| 堆龙德庆县| 巴青县| 南通市| 惠东县| 娱乐| 湖北省| 大化| 连云港市| 砀山县| 宁陵县| 德昌县| 娱乐| 上虞市| 赫章县| 承德市| 苏尼特左旗| 应城市| 永顺县| 中阳县| 财经| 洱源县| 渑池县| 泽州县| 乐昌市| 海安县|