Understand common order types and backtrader support in one article

Introduction

The order reflects the investor's decision-making, indicating the price at which the investor wants to buy/sell the corresponding investment target. The order price has a great impact on the final investment result, and everyone understands it.

The order types commonly used by investors include market orders, limit orders, stop-loss/stop-profit orders, trailing stop-loss/stop-profit orders, additional orders, etc. However, exchanges only have two basic order types: market orders and limit orders. Other order types are provided by the broker's system. The broker's software system will help monitor the price. Once the triggering conditions are met, it will be converted into market orders and limit orders and issued to the exchange for execution.

Backtrader supports a variety of trading order types to meet different trading needs. Backtrader basically supports several order types commonly used by investors. Backtrader creates orders by calling Strategy's buy(), sell(), and close() methods.

Common order types

1. Market order (Market)

A market order refers to an order to buy or sell at the market price. There is no need to set the price yourself.

  • Advantages: Guarantee immediate completion of transaction, prevent shortfall or stop profit/stop loss as soon as possible
  • Disadvantages: Unable to accurately control the transaction price. When the market changes rapidly or liquidity is insufficient, the transaction price may be far different from the price when the order was placed, which means the slippage is large.
  • backtrader Order.Market type **: ** Press the opening price of the next Bar to execute the transaction. Usage example: self.buy(exectype=bt.Order.Market), backtrader defaults to market order
  • backtrader Order.Close type**:** Press the closing price of the next Bar to execute the transaction. Usage example: self.buy(exectype=bt.Order.Close)

2. Limit order (Limit)

A limit order means that the order will only be executed when the specified price (limit Price) or a better price is available, that is, buying at the specified price or lower than the indicated price, and selling at the indicated price or higher.

  • Advantages: Can clearly know the transaction price
  • Disadvantages: The transaction speed cannot be guaranteed and the transaction may not be completed.
  • backtrader Order.Limit type**: **After the order is generated, it is judged whether the order is completed by comparing the limit Price with the open/high/low/close market data of the subsequent Bar. If the open of the next Bar touches the specified price limit Price, the order will be filled at the open price at the beginning of this Bar; if the open of the next Bar does not touch the limit price, but the limit price is between the high/low prices of this bar , the transaction is made at the limit price. If it is still not within the range, continue to detect the subsequent Bar. Usage example: self.buy(exectype=bt.Order.Limit, price=price, valid=valid) . Among them, valid represents the validity period of the order. The available values ​​are: None, which means that the order is valid until the transaction is completed or canceled; datetime instance, date instance, and date in numerical form, which means that the order is valid before the set date; Order.DAY, 0, imedelta(), indicating that the order is valid on that day

3. Market price take profit/stop loss order (Stop)

Market price take-profit/stop-loss order means that when the latest price reaches the set take-profit/stop-loss price (Stop Price), an order will be placed in the form of a market price order .

  • Advantages: Place a market order immediately after the price reaches the take-profit/stop-loss point
  • Disadvantages: Unable to accurately control transaction price
  • backtrader Order.Stop type**:** Determine whether the order is completed by comparing the Stop Price with the open/high/low/close market data of the subsequent Bar. If the open of the next Bar touches the specified price limit Price, the order will be filled at the open price at the beginning of this Bar; if the open of the next Bar does not touch the limit price, but the limit price is between the high/low prices of this bar , the transaction is done at the stop price. Usage example: self.buy(exectype=bt.Order.Stop, price=price, valid=valid)

4. Limit price/stop loss order (Stop-Limit)

A limit-price take-profit/stop-loss order means that once the stock price reaches the set stop-loss price, an order will be placed as a limit order, and the stop-loss price and limit price need to be specified.

  • Advantages: Submit a limit order after the price reaches the take-profit/stop-loss point, and you can accurately know the transaction price
  • Disadvantages: The transaction speed cannot be guaranteed and the transaction may not be completed.
  • backtrader Order.StopLimit type**:** In the next Bar, the order is triggered according to the logic of Order.Stop, and then the order is executed according to the logic of Order.Limit. Usage example: self.buy(exectype=bt.Order.StopLimit, price=price, valid=valid, plimit=plimit)

5. Stop-Trailing

A trailing stop loss order is a stop loss order in which the stop loss price is automatically adjusted. The adjustment range is determined by setting the difference between the stop loss price and the market price. When the market price rises, the stop loss price will rise accordingly; if the stock price When the stop-loss price is hit, the order will be executed in the form of a market order; if the market price drops or remains unchanged, the stop-loss price will remain unchanged.

  • Advantages: Lock in profits in advance or prevent losses from expanding through moving stop-profit/stop-loss
  • Disadvantages: If the price difference is too small, it is easy to be shaken out by fluctuations, and you can only get a small part of the profit; if the price difference is too large, the effect of moving stop profit/stop loss will not be achieved
  • backtrader Order.StopTrail type **: **Usage example: self.buy(exectype=bt.Order.StopTrail, price=xxx, trailamount=xxx), the price difference can be expressed by the amount trailamount, or it can be expressed by the percentage of the market price trailpercent
  • backtrader Order.StopTrailLimit type**:** Tracking stop-loss limit order is a stop-loss limit order in which the stop-loss price will be automatically adjusted. The limit price in the order will not change, but the stop-loss price will. change. Usage example: self.buy(exectype=bt.Order.StopTrailLimit, plimit=xxx, trailamount=xxx)

6.Additional orders

An additional order refers to an order that is attached to an ordinary opening order for the user to close a position. The additional order will be submitted after the parent order is fully completed and reaches the trigger price. The main order will have a stop-profit or stop-loss effect, and the closing will eventually be completed. warehouse. Additional order types include take-profit orders, stop-loss orders, bracket orders, associated cancellation orders, etc.

  • A take-profit order is a special limit order. Its code and quantity are consistent with the parent order, and its direction is opposite to the parent order. The order price is the take-profit price. After the parent order is fully filled, the take-profit order that reaches the take-profit price will be submitted.
  • A stop-loss order is a special stop-loss market order. Its code and quantity are consistent with the parent order, and its direction is opposite to the parent order. The order triggers the stop-loss price. After the parent order is fully filled, the stop-loss order that reaches the stop-loss price will be submitted.

6**.1 Bracket Order**

A bracket order consists of a take-profit order and a stop-loss order. After the parent order is fully executed, the order that reaches the trigger price first will be submitted, and the other order will be automatically canceled by the system. When stop-profit and stop-loss orders are set separately, they are "independent of each other". For example, if the stop-loss is triggered, the stop-profit order will not be automatically canceled and needs to be canceled manually.

  • If you want to create a stop-profit and stop-loss order that will be "automatically canceled", one is to add a "bracket order (additional order)" when placing the main order (opening/opening a position). If the main order is not executed, the additional order will not be activated. , after the main order is completed, the additional order is activated
  • Bracket orders can be set up with take-profit and stop-loss at the same time. After the take-profit is triggered, the stop-loss order will be automatically canceled.
  • Whether it is a market master order with a stop-profit and stop-loss order or a limit master order with a stop-profit and stop-loss order, you can set a stop-profit and stop-loss bracket order before the market master order is transmitted to the exchange. If you forget to add it, as long as the limit order has not been filled (the market order will be filled immediately), you can continue to add the bracket order. After the transaction, you cannot add the bracket order.
  • Backtrader provides 2 methods in the Strategy class to operate bracket orders: buy_bracketandsell_bracket

6.2 Related cancellation order (One Cancel Others)

OCO refers to the execution, cancellation or expiration of an order, which will automatically cancel other related orders. The buy function parameter of backtrader ocois used to specify the oco order group

def next(self):
    ...
    o1 = self.buy(...)
    ...
    o2 = self.buy(..., oco=o1)
    ...
    o3 = self.buy(..., oco=o1)

The above code forms an oco order group, of which o1 is the group leader. The execution, cancellation or expiration of any order in the group will automatically cancel other associated orders in the group.

6.3 One-Cancels-All

OCA means that the customer places multiple orders waiting to be completed and groups them. As long as one order in this group is completed, all other orders will be canceled immediately. In fact, bracket orders and OCO are also full orders with one cancellation.

Conclusion & Communication

Follow v: Zhuge Talk for more content. At the same time, you can also get an invitation to join the quantitative investment seminar group to communicate and discuss with many practitioners and technical experts. The number of places is limited, so don’t miss it.

Writing articles is not easy. If you think this article is helpful to you, please give it a thumbs up and forward it to give me the motivation to keep writing good articles.

reference

Guess you like

Origin blog.csdn.net/richardzhutalk/article/details/128058341