Python Algorithmic Trading Cookbook by Pushpak Dagade

Python Algorithmic Trading Cookbook by Pushpak Dagade

Author:Pushpak Dagade [Pushpak Dagade]
Language: eng
Format: epub
Tags: COM027000 - COMPUTERS / Desktop Applications / Personal Finance Applications, COM062000 - COMPUTERS / Data Modeling and Design, COM018000 - COMPUTERS / Data Processing
Publisher: Packt Publishing
Published: 2020-08-28T13:12:37+00:00


(The attributes passed to the place_order() method are broker-agnostic constants, imported earlier from the pyalgotrading.constants module.)

On placing the order in step 2, you get an order ID from the broker, which you assign to a new attribute, order1_id. The order1_id object is a string. If the order placement is not successful for some reason, you may not get an order ID. Observe that the price and trigger_price parameters are passed a value of ltp+1. This means the order is placed above the market price, which is a necessary condition for placing buy stoploss-limit orders. The stoploss parameter is specified as 2. This means the stoploss order would be placed at the price that is 2 price units lower than the execution price of the initial order. Similarly, the target parameter is specified as 2. This means the target order would be placed at the price that is 2 price units higher than the execution price of the Initial Order.

In step 3, you fetch the status of the placed order using the get_order_status() method of the broker_connection object. You pass order1_id as a parameter to the get_order_status() method. You get the order status as 'TRIGGER PENDING', a string. You can use order1_id to fetch the status of the placed order at any later point of time as well. In step 4, you fetch the order status again, and if the order is completed, you get the order status as 'COMPLETE'. Immediately after this, the target and stoploss orders are placed, at the prices mentioned earlier. The target order executes as a regular limit order. The stoploss order executes as a regular stoploss-limit order. When one of them gets executed and reaches the 'COMPLETE' state, the other order is automatically canceled by the broker, so it moves to the 'CANCELLED' state. Recall, both target and stoploss orders are on opposite sides of the initial order, so both target and stoploss orders cannot execute at the same time.

In step 3, if you see the status as 'COMPLETE' instead of 'TRIGGER PENDING', this could be due to high volatility. If you want the order to stay in the 'OPEN' state for a while, try placing the order further away from the market price. The following are references to more details on the execution of the initial order, target order, and stoploss order:

Initial order: Refer to the Placing a regular stoploss-limit order recipe in the previous chapter.

Target order: Refer to the Placing a regular limit order recipe in the previous chapter.

Stoploss order: Refer to the Placing a regular stoploss-limit order recipe in the previous chapter.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.