Trading Objects#

Orders#

Orders can be created using the Order object, shown below. The different order types and requirements are documented following.

class autotrader.brokers.trading.Order(instrument: str | None = None, direction: int | None = None, order_type: str = 'market', size: float | None = None, order_limit_price: float | None = None, order_stop_price: float | None = None, stop_loss: float | None = None, stop_type: str | None = None, take_profit: float | None = None, **kwargs)#

AutoTrader Order object.

instrument#

The trading instrument of the order.

Type:

str

direction#

The direction of the order (1 for long, -1 for short).

Type:

int

order_type#

The type of order. The default is ‘market’.

Type:

str

size#

The number of units.

Type:

float

base_size#

The number of units, in the base currency (pre-HCF conversion).

Type:

float

target_value#

The target value of the resulting trade, specified in the home currency of the account.

Type:

float

order_limit_price#

The limit price of the order (for ‘limit’ and ‘stop-limit’ order types).

Type:

float

order_stop_price#

The stop price of the order (for ‘stop-limit’ order types).

Type:

float

order_price#

The price of the instrument when the order was placed.

Type:

float

order_time#

The time at which the order was placed.

Type:

datetime

stop_loss#

The price to set the stop-loss at.

Type:

float

stop_distance#

The pip distance between the order price and the stop-loss.

Type:

float

stop_type#

The type of stop-loss (limit or trailing). The default is ‘limit’.

Type:

str

take_profit#

The price to set the take-profit at.

Type:

float

take_distance#

The pip distance between the order price and the take-profit.

Type:

float

related_orders#

A list of related order/trade ID’s.

Type:

list

id#

The order ID.

Type:

int

pip_value#

The pip value of the product being traded. Specify this for non-FX products when using stop_distance/take_distance arguments. The default is None.

Type:

float, optional

currency#

The base currency of the order (IB only).

Type:

str

secType#

The security type (IB only).

Type:

str

contract_month#

The contract month string (IB only).

Type:

str

localSymbol#

The exchange-specific instrument symbol (IB only).

Type:

str

post_only#

Enforce that the order is placed as a maker order (dYdX only). The default is False.

Type:

bool, optional

limit_fee#

The maximum fee to accept as a percentage (dYdX only). The default is ‘0.015’.

Type:

str, optional

exchange#

The exchange to which the order should be submitted.

Type:

str

ccxt_params#

The CCXT parameters dictionary to pass when creating an order. The default is {}.

Type:

dict, optional

__init__(instrument: str | None = None, direction: int | None = None, order_type: str = 'market', size: float | None = None, order_limit_price: float | None = None, order_stop_price: float | None = None, stop_loss: float | None = None, stop_type: str | None = None, take_profit: float | None = None, **kwargs) Order#
_calculate_exit_prices(broker=None, working_price: float | None = None) None#

Calculates the prices of the exit targets from the pip distance values.

Parameters:
  • broker (AutoTrader Broker Interface, optional) – The autotrade-broker instance. The default is None.

  • working_price (float, optional) – The working price used to calculate amount risked. The default is None.

Returns:

The exit prices will be assigned to the order instance.

Return type:

None

_calculate_position_size(broker=None, working_price: float | None = None, HCF: float = 1, risk_pc: float = 0, sizing: str | float = 'risk', amount_risked: float | None = None) None#

Calculates trade size for order.

Parameters:
  • broker (AutoTrader Broker Interface, optional) – The autotrade-broker instance. The default is None.

  • working_price (float, optional) – The working price used to calculate amount risked. The default is None.

  • HCF (float, optional) – The home conversion factor. The default is 1.

  • risk_pc (float, optional) – The percentage of the account NAV to risk on the trade. The default is 0.

  • sizing (str | float, optional) – The sizing option. The default is ‘risk’.

  • amount_risked (float, optional) – The dollar amount risked on the trade. The default is None.

Returns:

The trade size will be assigned to the order instance.

Return type:

None

_set_working_price(order_price: float | None = None) None#

Sets the Orders’ working price, for calculating exit targets.

Parameters:

order_price (float, optional) – The order price.

Returns:

The working price will be saved as a class attribute.

Return type:

None

as_dict() dict#

Converts Order object to dictionary.

Returns:

The order instance returned as a dict object.

Return type:

dict

Notes

This method enables legacy code operation, returning order/trade objects as a dictionary.

Summary of Order Types#

AutoTrader is intelligent when it comes to order types. If your strategy has no stop loss, you do not need to include it in the signal dictionary. If you prefer to set a stop loss in terms of distance in pips, you can do that instead. Same goes for take profit levels, specify price or distance in pips - whatever is more convenient. The following tables provides accepted values for the order_type of an order.

Order Type

Description

Additional keys required

market

A market order type

None

limit

A limit order type

order_limit_price

stop-limit

A stop-limit order type

order_limit_price and order_stop_price

Empty Order#

Useful when no signal is present. AutoTrader will recognise this as an empty order and skip over it.

empty_order = Order()

Market Order#

A market order triggers a trade immediately at the best available price, provided there is enough liquidity to accomodate the order. Read more here. Note that this is the default order type, so order_type does not need to be specified.

long_market_order = Order(direction=1)
short_market_order = Order(direction=-1)

Limit Order#

A limit order is allows a trader to buy or sell an instrument at a specified price (or better). Limit orders can be used to avoid slippage. However, a limit order is not gauranteed to be filled. When using this order type, the limit price must be specified. Read more about limit orders here.

limit_order = Order(direction=1, order_type='limit', order_limit_price=1.2312)

Stop-Limit Order#

A stop-limit order type provides a means to place a conditional limit order. This order type can be used to place an order, under the condition that price first moves to the stop price, at which point, a limit order is placed. In addition to a limit price, a stop price must also be specified. These prices are usually the same, although need not be. Read more about stop-limit orders here.

stop_limit_order = Order(direction=1, order_type='stop-limit', 
                         order_limit_price=1.2312, order_stop_price=1.2300)

Stop Loss Types#

AutoTrader supports both limit stop loss orders, and trailing stop loss orders. The keys required to specify these stop loss types are provided in the table below. Note that limit stop losses are the default type, so if a stop_price (or stop_distance) is provided with no stop_type, a limit stop-loss will be placed.

Stop Type

Description

Additional keys required

limit

A regular stop loss order

None

trailing

A trailing stop loss order

stop_loss or stop_distance

A note about setting stop losses: the stop_price takes precedence over stop_distance. That is, if stop_price is provided along with a stop_distance, the stop loss will be set at the price defined by stop_price. To avoid ambiguity, only specify one.

Note that for the purpose of calculating trading fees, stop loss orders are treated as limit order type (liquidity providing).

Limit Stop Loss#

stop_type='limit'

A 'limit' stop loss type will place a limit order at the price specified by the stop_loss key. If the price crosses a stop loss order, the trade associated with it will be closed. Read more about stop losses here.

Trailing Stop Loss#

stop_type='trailing'

A trailing stop loss can be used to protect unrealised gains by moving with price in favour of the trade direction. When using the trailing stop loss type, the initial stop loss position can be set by the price provided to the stop_loss key, or by providing the stop loss distance (in pips) to the stop_distance key. Read more about trailing stop loss orders here.

Take Profit Orders#

Take-profit orders can be attached to an order using the take_profit or take_price attribute. Note that take profit orders are treated as market order type (liquidity consuming) when calculating trading costs.

Trades#

class autotrader.brokers.trading.Trade(instrument: str, order_price: float, order_time: datetime, order_type: str, size: float, last_price: float, fill_time: datetime, fill_price: float, fill_direction: int, fee: float, **kwargs)#

Bases: object

AutoTrader Trade object. Represents an exchange of value.

__init__(instrument: str, order_price: float, order_time: datetime, order_type: str, size: float, last_price: float, fill_time: datetime, fill_price: float, fill_direction: int, fee: float, **kwargs) Trade#

Trade constructor.

Isolated Positions#

class autotrader.brokers.trading.IsolatedPosition(order: Order | None = None, **kwargs)#

AutoTrader IsolatedPosition. Use to connect SL and TP orders to individual trades.

unrealised_PL#

The floating PnL of the trade.

Type:

float

margin_required#

The margin required to maintain the trade.

Type:

float

time_filled#

The time at which the trade was filled.

Type:

datetime

fill_price#

The price at which the trade was filled.

Type:

float

last_price#

The last price observed for the instrument associated with the trade.

Type:

float

last_time#

The last time observed for the instrument associated with the trade.

Type:

datetime

exit_price#

The price at which the trade was closed.

Type:

float

exit_time#

The time at which the trade was closed.

Type:

datetime

fees#

The fees associated with the trade.

Type:

float

parent_id#

The ID of the order which spawned the trade.

Type:

int

id#

The trade ID.

Type:

int

status#

The status of the trade (open or closed).

Type:

str

split#

If the trade has been split.

Type:

bool

Notes

When a trade is created from an Order, the Order will be marked as filled.

Positions#

class autotrader.brokers.trading.Position(**kwargs)#

AutoTrader Position object.

instrument#

The trade instrument of the position.

Type:

str

pnl#

The pnl of the position.

Type:

float

long_units#

The number of long units in the position.

Type:

float

long_PL#

The PnL of the long units in the position.

Type:

float

long_margin#

The margin required to maintain the long units in the position.

Type:

float

short_units#

The number of short units in the position.

Type:

float

short_PL#

The PnL of the short units in the position.

Type:

float

short_margin#

The margin required to maintain the short units in the position.

Type:

float

total_margin#

The total margin required to maintain the position.

Type:

float

trade_IDs#

The trade ID’s associated with the position.

Type:

list[int]

net_position#

The total number of units in the position.

Type:

float

net_exposure#

The net exposure (in $ value) of the position.

Type:

float

PL#

The floating PnL (IB only).

Type:

float

contracts#

The contracts associated with the position (IB only).

Type:

list

portfolio_items#

The portfolio items associated with the position (IB only).

Type:

list

as_dict() dict#

Converts Position object to dictionary.

Returns:

The Position instance returned as a dict object.

Return type:

dict

Notes

This method enables legacy code operation, returning order/trade objects as a dictionary.