Orders

Orders specify how brokers handle the purchase and sale of assets. Strategies create orders and place them with its assigned broker attribute. In the case of a backtest, this places the order with a BacktestBroker object, and in the case of live execution the broker objects reformats the order into the appropriate API call

Note

The precise handling of orders differs from broker to broker, as well as the variety and complexity of available order types. This library handles the most common order types common to most brokers. In some instances, if an order type is not available with a broker it is handled by combining local logic and the available order options (e.g. a trailing stop order where price data is monitored locally and then placing a market or limit order).

class src.boatwright.Orders.Order.Order(symbol, side, type, configuration, time_opened, time_in_force='gtc', order_id=None)
Parameters:
  • symbol (str) – e.g. “BTC” or “AAPL”

  • side (str) – “BUY” or “SELL”

  • type (str) – “MARKET”, “LIMIT”, “STOP”, or “TRAILING”

  • configuration (dict) – dict indicating order details according to order type

  • time_opened (datetime) – indicating when the order is created

  • time_in_force – “gtc” good till cancelled, “gfd” good for day, or datetime object indicating good till date

  • order_id (str) – unique identifier

class src.boatwright.Orders.Order.MarketOrder(symbol, side, time_opened, size=None, frac=1, order_id=None)

Market Order

Parameters:
  • symbol (str) – i.e. BTC or AAPL

  • side (str) – “BUY” or “SELL”

  • time_opened (datetime) – when the order is created

  • size (float) – amount of base asset to be bought or sold

  • frac (float) – alternative to size, 0-1 indicating the fractional amount of the base asset to be bought or sold

  • order_id (str) – unique identifier for the order, defaults to random uuid

class src.boatwright.Orders.Order.LimitOrder(symbol, side, limit_price, time_opened, time_in_force='gtc', size=None, frac=1, order_id=None)

Limit orders execute trades at the specified price or better. They may not execute immediately.

Parameters:
  • symbol (str) – i.e. BTC or AAPL

  • side (str) – “BUY” or “SELL”

  • limit_price (float) – price which asset is bought or sold for

  • time_opened (datetime) – when the order is created

  • time_in_force – either “gtc” good till cancelled, “gfd” good for day, or a datetime object indicating “gtd” good till date

  • size (float) – amount of base asset to be bought or sold

  • frac (float) – alternative to size, 0-1 indicating the fractional amount of the base asset to be bought or sold

  • order_id (str) – unique identifier for the order, defaults to random uuid

triggered(open, high, low, close, volume)

Checks if limit order could potentially execute at the given OHLCV bar

class src.boatwright.Orders.Order.StopOrder(order, target_price, less_than, time_opened, time_in_force='gtc', order_id=None)

Stop orders are executed once the price of an asset hits the target price

Parameters:
  • order (Order) – the market or limit order to be executed at the target price

  • target_price (float) – the target price which the order will be executed

  • less_than (bool) – if True, than order executes when price dips below, else, when price rises above the target price

  • time_opened (datetime) – when the order is created

  • time_in_force – either “gtc” good till cancelled, “gfd” good for day, or a datetime object indicating “gtd” good till date

  • order_id (str) – unique identifier for the order, defaults to random uuid

triggered(price)

Checks if stop order would be triggered for the given price

class src.boatwright.Orders.Order.TrailingOrder(order, price, time_opened, pct_greater_than_min=None, pct_less_than_max=None, time_in_force='gtc', order_id=None)

a trailing order initiates an order based on price action relative to the min or max price since time opened. e.g. a trailing stop loss order initiates a sell order when the price drops some percent below the maximum price.

Parameters:
  • order (Order) – the market or limit order to be executed when trailing stop is triggered

  • price (float) – current asset price, which is used as the first min & max values

  • time_opened (datetime) – when the order is created

  • pct_greater_than_min (float) – order is triggered if 100*(price - min) / min > pct_greater_than_min

  • pct_less_than_max (float) – order is triggered if 100*(price - max) / max < pct_greater_than_min

  • time_in_force – either “gtc” good till cancelled, “gfd” good for day, or a datetime object indicating “gtd” good till date

  • order_id (str) – unique identifier for the order, defaults to random uuid