broker – Order management classes

Base module and classes

class pyalgotrade.broker.Order(type_, action, instrument, quantity)

Base class for orders.

Parameters:
  • type (Order.Type) – The order type
  • action (Order.Action) – The order action.
  • instrument (string.) – Instrument identifier.
  • quantity (int.) – Order quantity.

Note

Valid action parameter values are:

  • Order.Action.BUY
  • Order.Action.BUY_TO_COVER
  • Order.Action.SELL
  • Order.Action.SELL_SHORT

This is a base class and should not be used directly.

getAction()

Returns the order action.

getAllOrNone()

Returns True if the order should be completely filled or else canceled.

getExecutionInfo()

Returns the order execution info if the order was filled, or None otherwise.

Return type:OrderExecutionInfo.
getGoodTillCanceled()

Returns True if the order is good till canceled.

getInstrument()

Returns the instrument identifier.

getQuantity()

Returns the quantity.

getState()

Returns the order state.

Valid order states are:
  • Order.State.ACCEPTED (the initial state).
  • Order.State.CANCELED
  • Order.State.FILLED
getType()

Returns the order type

isAccepted()

Returns True if the order state is Order.State.ACCEPTED.

isCanceled()

Returns True if the order state is Order.State.CANCELED.

isFilled()

Returns True if the order state is Order.State.FILLED.

setAllOrNone(allOrNone)

Sets the All-Or-None property for this order.

Parameters:allOrNone (boolean.) – True if the order should be completely filled or else canceled.
setGoodTillCanceled(goodTillCanceled)

Sets if the order should be good till canceled. Orders that are not filled by the time the session closes will be will be automatically canceled if they were not set as good till canceled

Parameters:goodTillCanceled (boolean.) – True if the order should be good till canceled.
setQuantity(quantity)

Updates the quantity.

class pyalgotrade.broker.MarketOrder(action, instrument, quantity, onClose)

Base class for market orders.

Note

This is a base class and should not be used directly.

getFillOnClose()

Returns True if the order should be filled as close to the closing price as possible (Market-On-Close order).

setFillOnClose(onClose)

Sets if the order should be filled as close to the closing price as possible (Market-On-Close order).

class pyalgotrade.broker.LimitOrder(action, instrument, limitPrice, quantity)

Base class for limit orders.

Note

This is a base class and should not be used directly.

getLimitPrice()

Returns the limit price.

setLimitPrice(limitPrice)

Updates the limit price.

class pyalgotrade.broker.StopOrder(action, instrument, stopPrice, quantity)

Base class for stop orders.

Note

This is a base class and should not be used directly.

getStopPrice()

Returns the stop price.

setStopPrice(stopPrice)

Updates the stop price.

class pyalgotrade.broker.StopLimitOrder(action, instrument, limitPrice, stopPrice, quantity)

Base class for stop limit orders.

Note

This is a base class and should not be used directly.

getLimitPrice()

Returns the limit price.

getStopPrice()

Returns the stop price.

isLimitOrderActive()

Returns True if the limit order is active.

setLimitPrice(limitPrice)

Updates the limit price.

setStopPrice(stopPrice)

Updates the stop price.

class pyalgotrade.broker.OrderExecutionInfo(price, quantity, commission, dateTime)

Execution information for a filled order.

getCommission()

Returns the commission applied.

getDateTime()

Returns the datatime.datetime when the order was executed.

getPrice()

Returns the fill price.

getQuantity()

Returns the quantity.

class pyalgotrade.broker.Broker

Base class for brokers.

Note

This is a base class and should not be used directly.

cancelOrder(order)

Requests an order to be canceled. If the order is filled an Exception is raised.

Parameters:order (Order.) – The order to cancel.
createLimitOrder(action, instrument, limitPrice, quantity)

Creates a Limit order. A limit order is an order to buy or sell a stock at a specific price or better. A buy limit order can only be executed at the limit price or lower, and a sell limit order can only be executed at the limit price or higher.

Parameters:
  • action (Order.Action.BUY, or Order.Action.BUY_TO_COVER, or Order.Action.SELL or Order.Action.SELL_SHORT.) – The order action.
  • instrument (string.) – Instrument identifier.
  • limitPrice (float) – The order price.
  • quantity (int.) – Order quantity.
Return type:

A LimitOrder subclass.

createMarketOrder(action, instrument, quantity, onClose=False)

Creates a Market order. A market order is an order to buy or sell a stock at the best available price. Generally, this type of order will be executed immediately. However, the price at which a market order will be executed is not guaranteed.

Parameters:
  • action (Order.Action.BUY, or Order.Action.BUY_TO_COVER, or Order.Action.SELL or Order.Action.SELL_SHORT.) – The order action.
  • instrument (string.) – Instrument identifier.
  • quantity (int.) – Order quantity.
  • onClose (boolean.) – True if the order should be filled as close to the closing price as possible (Market-On-Close order). Default is False.
Return type:

A MarketOrder subclass.

createStopLimitOrder(action, instrument, stopPrice, limitPrice, quantity)

Creates a Stop-Limit order. A stop-limit order is an order to buy or sell a stock that combines the features of a stop order and a limit order. Once the stop price is reached, a stop-limit order becomes a limit order that will be executed at a specified price (or better). The benefit of a stop-limit order is that the investor can control the price at which the order can be executed.

Parameters:
  • action (Order.Action.BUY, or Order.Action.BUY_TO_COVER, or Order.Action.SELL or Order.Action.SELL_SHORT.) – The order action.
  • instrument (string.) – Instrument identifier.
  • stopPrice (float) – The trigger price.
  • limitPrice (float) – The price for the limit order.
  • quantity (int.) – Order quantity.
Return type:

A StopLimitOrder subclass.

createStopOrder(action, instrument, stopPrice, quantity)

Creates a Stop order. A stop order, also referred to as a stop-loss order, is an order to buy or sell a stock once the price of the stock reaches a specified price, known as the stop price. When the stop price is reached, a stop order becomes a market order. A buy stop order is entered at a stop price above the current market price. Investors generally use a buy stop order to limit a loss or to protect a profit on a stock that they have sold short. A sell stop order is entered at a stop price below the current market price. Investors generally use a sell stop order to limit a loss or to protect a profit on a stock that they own.

Parameters:
  • action (Order.Action.BUY, or Order.Action.BUY_TO_COVER, or Order.Action.SELL or Order.Action.SELL_SHORT.) – The order action.
  • instrument (string.) – Instrument identifier.
  • stopPrice (float) – The trigger price.
  • quantity (int.) – Order quantity.
Return type:

A StopOrder subclass.

getActiveOrders()

Returns a sequence with the orders that are still active.

getPositions()

Returns a dictionary that maps instruments to shares.

getShares(instrument)

Returns the number of shares for an instrument.

placeOrder(order)

Submits an order.

Parameters:order (Order.) – The order to submit.

Note

If the order is filled or canceled, an exception will be raised.

Backtesting module and classes

class pyalgotrade.broker.backtesting.Broker(cash, barFeed, commission=None)

Backtesting broker.

Parameters:
  • cash (int or float.) – The initial amount of cash.
  • barFeed (pyalgotrade.barfeed.BarFeed) – The bar feed that will provide the bars.
  • commission (Commission) – An object responsible for calculating order commissions.
getCash()

Returns the available cash.

getCommission()

Returns the commission instance.

getEquity()

Returns the portfolio value (cash + shares).

getFillStrategy()

Returns the FillStrategy currently set.

setCash(cash)

Sets the available cash.

setCommission(commission)

Sets the commission instance.

setFillStrategy(strategy)

Sets the FillStrategy to use.

class pyalgotrade.broker.backtesting.FillStrategy

Base class for order filling strategies.

fillLimitOrder(order, broker_, bar)

Override to return the fill price for a limit order or None if the order can’t be filled at the given time.

Parameters:
Return type:

An int/float with the fill price or None if the order should not be filled.

fillMarketOrder(order, broker_, bar)

Override to return the fill price for a market order or None if the order can’t be filled at the given time.

Parameters:
Return type:

An int/float with the fill price or None if the order should not be filled.

fillStopLimitOrder(order, broker_, bar, justHitStopPrice)

Override to return the fill price for a stop limit order or None if the order can’t be filled at the given time.

Parameters:
Return type:

An int/float with the fill price or None if the order should not be filled.

fillStopOrder(order, broker_, bar)

Override to return the fill price for a stop order or None if the order can’t be filled at the given time.

Parameters:
Return type:

An int/float with the fill price or None if the order should not be filled.

class pyalgotrade.broker.backtesting.DefaultStrategy

This strategy works as follows:

  • A pyalgotrade.broker.MarketOrder is always filled using the open/close price.

  • A pyalgotrade.broker.LimitOrder will be filled like this:
    • If the limit price was penetrated with the open price, then the open price is used.
    • If the bar includes the limit price, then the limit price is used.
    • Note that when buying the price is penetrated if it gets <= the limit price, and when selling the price is penetrated if it gets >= the limit price
  • A pyalgotrade.broker.StopOrder will be filled like this:
    • If the stop price was penetrated with the open price, then the open price is used.
    • If the bar includes the stop price, then the stop price is used.
    • Note that when buying the price is penetrated if it gets >= the stop price, and when selling the price is penetrated if it gets <= the stop price
  • A pyalgotrade.broker.StopLimitOrder will be filled like this:
    • If the stop price was penetrated with the open price, or if the bar includes the stop price, then the limit order becomes active.

    • If the limit order is active:
      • If the limit order was activated in this same bar and the limit price is penetrated as well, then the best between the stop price and the limit fill price (as described earlier) is used.
      • If the limit order was activated at a previous bar then the limit fill price (as described earlier) is used.

Note

This is the default strategy used by the Broker.

Table Of Contents

Previous topic

technical – Technical indicators

Next topic

strategy – Basic strategy classes

This Page