Book Image

Python Algorithmic Trading Cookbook

By : Pushpak Dagade
Book Image

Python Algorithmic Trading Cookbook

By: Pushpak Dagade

Overview of this book

If you want to find out how you can build a solid foundation in algorithmic trading using Python, this cookbook is here to help. Starting by setting up the Python environment for trading and connectivity with brokers, you’ll then learn the important aspects of financial markets. As you progress, you’ll learn to fetch financial instruments, query and calculate various types of candles and historical data, and finally, compute and plot technical indicators. Next, you’ll learn how to place various types of orders, such as regular, bracket, and cover orders, and understand their state transitions. Later chapters will cover backtesting, paper trading, and finally real trading for the algorithmic strategies that you've created. You’ll even understand how to automate trading and find the right strategy for making effective decisions that would otherwise be impossible for human traders. By the end of this book, you’ll be able to use Python libraries to conduct key tasks in the algorithmic trading ecosystem. Note: For demonstration, we're using Zerodha, an Indian Stock Market broker. If you're not an Indian resident, you won't be able to use Zerodha and therefore will not be able to test the examples directly. However, you can take inspiration from the book and apply the concepts across your preferred stock market broker of choice.
Table of Contents (16 chapters)

Placing a simple DELIVERY order

This recipe demonstrates how to place a DELIVERY order on the exchange via the broker. A DELIVERY order is delivered to the user's Demat account and exists until it is explicitly squared-off by the user. Positions created by delivery orders at the end of a trading session are carried forwarded to the next trading session. They are not explicitly squared-off by the broker. After trying out this recipe, check your broking account by logging into the broker's website; you will find that an order has been placed there. You can match the order ID with the one that's returned in the last code snippet shown in this recipe.

Getting ready

Make sure the broker_connection object is available in your Python namespace. Refer to the first recipe of this chapter to learn how to set up this object.

How to do it…

We execute the following steps to complete this recipe:

  1. Import the necessary modules:
>>> from pyalgotrading.constants import *
  1. Fetch an instrument for a specific trading symbol and exchange:
>>> instrument = broker_connection.get_instrument(segment='NSE', 
tradingsymbol='AXISBANK')
  1. Place a simple DELIVERY order – a SELL, REGULAR, DELIVERY, MARKET order:
>>> order_id = broker_connection.place_order(
instrument=instrument,
order_transaction_type= \
BrokerOrderTransactionTypeConstants.SELL,
order_type=BrokerOrderTypeConstants.REGULAR,
order_code=BrokerOrderCodeConstants.DELIVERY,
order_variety= \
BrokerOrderVarietyConstants.MARKET,
quantity=1)
>>> order_id

We'll get the following output:

191212001268956

How it works…

In step 1, you import the constants from pyalgotrading. In step 2, you fetch the financial instrument with segment = 'NSE' and tradingsymbol = 'AXISBANK' using the get_instrument() method of broker_connection. In step 3, you place a DELIVERY order using the place_order() method of broker_connection. This method accepts the following arguments:

  • instrument: The financial instrument for which the order must be placed. Should be an instance of the Instrument class. You pass instrument here.
  • order_transaction_type: The order transaction type. Should be an enum of type BrokerOrderTransactionTypeConstants. You pass BrokerOrderTransactionTypeConstants.SELL here.
  • order_type: The order type. Should be an enum of type BrokerOrderTypeConstants. You pass BrokerOrderTypeConstants.REGULAR here.
  • order_code: The order code. Should be an enum of type BrokerOrderCodeConstants. You pass BrokerOrderCodeConstants.DELIVERY here.
  • order_variety: The order variety. Should be an enum of type BrokerOrderVarietyConstants. You pass BrokerOrderVarietyConstants.MARKET here.
  • quantity: The number of shares to be traded for the given instrument. Should be a positive integer. We pass 1 here.

If the order placement is successful, the method returns an order ID which you can use at any point in time later on for querying the status of the order.

A detailed explanation of the different types of parameters will be covered in Chapter 6, Placing Trading Orders on the Exchange. This recipe is intended to give you an idea of how to place a DELIVERY order, one of the various types of possible orders.