Instrument APIΒΆ

The Instrument object is passed to the on_tick(), on_bar(), on_quote(), on_orderbook() and on_fill() methods in your strategy:

# startegy.py
def on_tick(self, instrument):
    instrument.MethodName(...)

def on_bar(self, instrument):
    instrument.MethodName(...)

def on_quote(self, instrument):
    instrument.MethodName(...)

def on_orderbook(self, instrument):
    instrument.MethodName(...)

def on_fill(self, instrument, order):
    instrument.MethodName(...)
class qtpylib.instrument.Instrument[source]

A string subclass that provides easy access to misc symbol-related methods and information.

get_bars(lookback=None, as_dict=False)[source]

Get bars for this instrument

Parameters:
lookback
: int

Max number of bars to get (None = all available bars)

as_dict
: bool

Return a dict or a pd.DataFrame object

Retruns:
bars
: pd.DataFrame / dict

The bars for this instruments

get_bar()[source]

Shortcut to self.get_bars(lookback=1, as_dict=True)

get_ticks(lookback=None, as_dict=False)[source]

Get ticks for this instrument

Parameters:
lookback
: int

Max number of ticks to get (None = all available ticks)

as_dict
: bool

Return a dict or a pd.DataFrame object

Retruns:
ticks
: pd.DataFrame / dict

The ticks for this instruments

get_tick()[source]

Shortcut to self.get_ticks(lookback=1, as_dict=True)

get_quote()[source]

Get last quote for this instrument

Retruns:
quote
: dict

The quote for this instruments

get_orderbook()[source]

Get orderbook for the instrument

Retruns:
orderbook
: dict

orderbook dict for the instrument

order(direction, quantity, **kwargs)[source]

Send an order for this instrument

Parameters:
direction
: string

Order Type (BUY/SELL, EXIT/FLATTEN)

quantity
: int

Order quantity

Optional:
limit_price
: float

In case of a LIMIT order, this is the LIMIT PRICE

expiry
: int

Cancel this order if not filled after n seconds (default 60 seconds)

order_type
: string

Type of order: Market (default), LIMIT (default when limit_price is passed), MODIFY (required passing or orderId)

orderId
: int

If modifying an order, the order id of the modified order

target
: float

target (exit) price

initial_stop
: float

price to set hard stop

stop_limit: bool

Flag to indicate if the stop should be STOP or STOP LIMIT (default False=STOP)

trail_stop_at
: float

price at which to start trailing the stop

trail_stop_by
: float

% of trailing stop distance from current price

fillorkill: bool

fill entire quantiry or none at all

iceberg: bool

is this an iceberg (hidden) order

tif: str

time in force (DAY, GTC, IOC, GTD). default is DAY

cancel_order(orderId)[source]

Cancels an order for this instrument

Parameters:
orderId
: int

Order ID

market_order(direction, quantity, **kwargs)[source]

Shortcut for instrument.order(...) and accepts all of its optional parameters

Parameters:
direction
: string

Order Type (BUY/SELL, EXIT/FLATTEN)

quantity
: int

Order quantity

limit_order(direction, quantity, price, **kwargs)[source]

Shortcut for instrument.order(...) and accepts all of its optional parameters

Parameters:
direction
: string

Order Type (BUY/SELL, EXIT/FLATTEN)

quantity
: int

Order quantity

price
: float

Limit price

buy(quantity, **kwargs)[source]

Shortcut for instrument.order("BUY", ...) and accepts all of its optional parameters

Parameters:
quantity
: int

Order quantity

buy_market(quantity, **kwargs)[source]

Shortcut for instrument.order("BUY", ...) and accepts all of its optional parameters

Parameters:
quantity
: int

Order quantity

buy_limit(quantity, price, **kwargs)[source]

Shortcut for instrument.order("BUY", ...) and accepts all of its optional parameters

Parameters:
quantity
: int

Order quantity

price
: float

Limit price

sell(quantity, **kwargs)[source]

Shortcut for instrument.order("SELL", ...) and accepts all of its optional parameters

Parameters:
quantity
: int

Order quantity

sell_market(quantity, **kwargs)[source]

Shortcut for instrument.order("SELL", ...) and accepts all of its optional parameters

Parameters:
quantity
: int

Order quantity

sell_limit(quantity, price, **kwargs)[source]

Shortcut for instrument.order("SELL", ...) and accepts all of its optional parameters

Parameters:
quantity
: int

Order quantity

price
: float

Limit price

exit()[source]

Shortcut for instrument.order("EXIT", ...) (accepts no parameters)

flatten()[source]

Shortcut for instrument.order("FLATTEN", ...) (accepts no parameters)

get_contract()[source]

Get contract object for this instrument

Retruns:
contract
: Object

IB Contract object

get_contract_details()[source]

Get contract details for this instrument

Retruns:
contract_details
: dict

IB Contract details

get_tickerId()[source]

Get contract’s tickerId for this instrument

Retruns:
tickerId
: int

IB Contract’s tickerId

get_positions(attr=None)[source]

Get the positions data for the instrument

Optional:
attr
: string

Position attribute to get (optional attributes: symbol, position, avgCost, account)

Retruns:
positions
: dict (positions) / float/str (attribute)

positions data for the instrument

get_portfolio()[source]

Get portfolio data for the instrument

Retruns:
portfolio
: dict

portfolio data for the instrument

get_orders()[source]

Get orders for the instrument

Retruns:
orders
: list

list of order data as dict

get_pending_orders()[source]

Get pending orders for the instrument

Retruns:
orders
: list

list of pending order data as dict

get_active_order(order_type='STOP')[source]

Get artive order id for the instrument by order_type

Optional:
order_type
: string

the type order to return: STOP (default), LIMIT, MARKET

Retruns:
order
: object

IB Order object of instrument

get_trades()[source]

Get orderbook for the instrument

Retruns:
trades
: pd.DataFrame

instrument’s trade log as DataFrame

get_symbol()[source]

Get symbol of this instrument

Retruns:
symbol
: string

instrument’s symbol

modify_order(orderId, quantity=None, limit_price=None)[source]

Modify quantity and/or limit price of an active order for the instrument

Parameters:
orderId
: int

the order id to modify

Optional:
quantity
: int

the required quantity of the modified order

limit_price
: int

the new limit price of the modified order

modify_order_group(orderId, entry=None, target=None, stop=None, quantity=None)[source]

Modify bracket order

Parameters:
orderId
: int

the order id to modify

Optional:
entry: float

new entry limit price (for unfilled limit orders only)

target: float

new target limit price (for unfilled limit orders only)

stop: float

new stop limit price (for unfilled limit orders only)

quantity
: int

the required quantity of the modified order

move_stoploss(stoploss)[source]

Modify stop order. Auto-discover orderId and quantity and invokes self.modify_order(...).

Parameters:
stoploss
: float

the new stoploss limit price

get_margin_requirement()[source]

Get margin requirements for intrument (futures only)

Retruns:
margin
: dict

margin requirements for instrument (all values are None for non-futures instruments)

get_max_contracts_allowed(overnight=True)[source]

Get maximum contracts allowed to trade baed on required margin per contract and current account balance (futures only)

Parameters:
overnight
: bool

Calculate based on Overnight margin (set to False to use Intraday margin req.)

Retruns:
contracts
: int

maximum contracts allowed to trade (returns None for non-futures)

get_margin_max_contracts(overnight=True)[source]

Deprecated (renamed to get_max_contracts_allowed)

get_ticksize(fallback=None)[source]

Get instrument ticksize

Parameters:
fallback
: flaot

fallback ticksize (deprecated and ignored)

Retruns:
ticksize
: int

Min. tick size

log_signal(signal)[source]

Log Signal for instrument

Parameters:
signal
: integer

signal identifier (1, 0, -1)

bars

(Property) Shortcut to self.get_bars()

bar

(Property) Shortcut to self.get_bar()

ticks

(Property) Shortcut to self.get_ticks()

tick

(Property) Shortcut to self.get_tick()

quote

(Property) Shortcut to self.get_quote()

orderbook

(Property) Shortcut to self.get_orderbook()

symbol

(Property) Shortcut to self.get_symbol()

contract

(Property) Shortcut to self.get_contract()

contract_details

(Property) Shortcut to self.get_contract_details()

tickerId

(Property) Shortcut to self.get_tickerId()

positions

(Property) Shortcut to self.get_positions()

position

(Property) Shortcut to self.get_positions(position)

portfolio

(Property) Shortcut to self.get_portfolio()

orders

(Property) Shortcut to self.get_orders()

pending_orders

(Property) Shortcut to self.get_pending_orders()

trades

(Property) Shortcut to self.get_trades()

margin_requirement

(Property) Shortcut to self.get_margin_requirement()

margin_max_contracts

Deprecated (renamed to max_contracts_allowed)

max_contracts_allowed

(Property) Shortcut to self.get_max_contracts_allowed()

ticksize

(Property) Shortcut to self.get_ticksize()