Reader small image

You're reading from  Python Algorithmic Trading Cookbook

Product typeBook
Published inAug 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838989354
Edition1st Edition
Languages
Right arrow
Author (1)
Pushpak Dagade
Pushpak Dagade
author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade

Right arrow
Placing Bracket and Cover Orders on the Exchange

This chapter introduces various types of bracket and cover orders that can be placed on exchanges via the broker APIs. The recipes include code for placing 12 types of orders and querying their statuses, canceling open orders, and exiting completed orders. These recipes will be a fundamental part of your algorithmic trading strategies. Understanding all of the types of orders and knowing which one to place for the given requirement is crucial for building a successful trading strategy.

Each order has four attributes that together define the order completely:

  • Order transaction type
  • Order type
  • Order code
  • Order variety

For placing an order, all four attributes should be known precisely. To know more about these attributes, refer to the introduction to Chapter 6, Placing Regular Orders on the Exchange.

The recipes in this chapter...

Technical requirements

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

  • Python 3.7+
  • Python packages: pyalgotrading ($ pip install pyalgotrading)

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

The first thing needed for setting connectivity with the broker is getting the API keys. The broker will provide each customer with unique keys, 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.

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

  1. Import the necessary modules...

Placing a bracket limit order

Bracket orders are complex orders that are meant to help to make a profit when trade becomes favorable, or limit the loss when it becomes unfavorable, with predefined values. A bracket order is essentially a combination of three regular orders together—an initial order, a target order, and a stoploss order—which act together to help to achieve the specified profit or limit the loss. Along with the regular order parameters, a bracket order takes additional parameters—target, stoploss, and trailing stoploss (optional). The three regular orders are described as follows:

  • Initial order: This order is equivalent to a regular limit order or regular stoploss-limit order. Once placed, it remains in the 'OPEN' state until the market price reaches its trigger price value. Once the market crosses the trigger price value, this order moves from the 'OPEN' to 'COMPLETE' state and the target and stoploss orders are placed...

Placing a bracket stoploss-limit order

Bracket orders are complex orders that are meant to help to make a profit when trade becomes favorable, or limit the loss when it becomes unfavorable, with predefined values. A bracket order is essentially a combination of three regular orders together —an initial order, a target order, and a stoploss order, which act together to help to achieve the specified profit or limit the loss. Along with the regular order parameters, a bracket order takes additional parameterstarget, stoploss, and trailing stoploss (optional).

Please refer to the introduction of the Placing a bracket limit order recipe for an in-depth understanding of the working of bracket orders. You can use a bracket stoploss-limit order if you want to place a buy bracket order above the market price or a sell bracket order below the market price.

This recipe demonstrates the placing of the following bracket stoploss-limit orders and querying their statuses:

  • The BUY, BRACKET...

Placing a bracket limit order with trailing stoploss

Bracket orders are complex orders that are meant to help to make a profit when trade becomes favorable, or limit the loss when it becomes unfavorable, with predefined values. A bracket order is essentially a combination of three regular orders togetheran initial order, a target order, and a stoploss order, which act together to help to achieve the specified profit or limit the loss. Along with the regular order parameters, a bracket order takes additional parameterstarget, stoploss, and trailing stoploss (optional).

Please refer to the introduction of the Placing a bracket limit order recipe for an in-depth understanding of the working of bracket orders.

You can use a bracket limit order if you want to place a buy bracket order below the market price or a sell bracket order above the market price. The trailing stoploss feature improvises the positioning of the stoploss order by modifying its price in the direction of...

Placing a bracket stoploss-limit order with trailing stoploss

Bracket orders are complex orders that are meant to help to make a profit when trade becomes favorable, or limit the loss when it becomes unfavorable, with predefined values. A bracket order is essentially a combination of three regular orders togetheran initial order, a target order, and a stoploss order, which act together to help to achieve the specified profit or limit the loss. Along with the regular order parameters, a bracket order takes additional parameterstarget, stoploss, and trailing stoploss (optional).

Please refer to the introduction of the Placing a bracket limit order recipe for an in-depth understanding of the working of bracket orders.

You can use a bracket stoploss-limit order if you want to place a buy bracket order above the market price or a sell bracket order below the market price. The trailing stoploss improvises the positioning of the stoploss order by modifying its price in the direction...

Placing a cover market order

Cover orders are complex orders that are meant to help to limit the loss within predefined values if trade becomes unfavorable. A cover order is essentially a combination of two regular orders togetheran initial order and a stoploss order:

  • Initial order: This order can be equivalent to a regular market order or regular limit order, depending on whether you are placing a cover market order or cover limit order. Once the order moves to the 'COMPLETE' state, the stoploss order is placed, which is described next.
  • Stoploss order: This order is equivalent to a regular stoploss-market order (the Placing a regular stoploss-market order recipe in the previous chapter), with the specified trigger price value as its trigger price and a transaction type opposite to that of the initial order. For a buy initial order, the stoploss order is placed at a lower price than the initial order. This would be vice versa for a sell initial order. The quantity matches...

Placing a cover limit order

Cover orders are complex orders that are meant to help to limit the loss within predefined values if trade becomes unfavorable. A cover order is essentially a combination of two regular orders—an initial order and a stoploss order, which act together to help to limit the loss in case trade becomes unfavorable.

Please refer to the introduction of the Placing a cover market order recipe for an in-depth understanding of the working of cover orders. You can use a cover limit order if you want to place a buy cover order below the market price or a sell cover order above the market price. This recipe demonstrates the placing of the following cover limit orders and querying their statuses:

  • The BUY, COVER, INTRADAY, LIMIT order
  • The SELL, COVER, INTRADAY, LIMIT order
The following are references to the state machine diagrams for a cover limit order:
  • Initial order: Refer to the state machine diagram from the Placing a regular limit order recipe in the previous...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python Algorithmic Trading Cookbook
Published in: Aug 2020Publisher: PacktISBN-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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade