Comprehensive algorithmic trading framework for Python with backtesting, simulation, and live trading capabilities
—
Order placement and management functions supporting stocks, futures, options, and bonds with various order types, position effects, and execution styles. All trading functions are available within strategy context and enforce proper execution phases.
Order placement functions that specify quantities in terms of shares or contracts.
def order_shares(id_or_ins, amount, price_or_style=None):
"""
Order by number of shares.
Parameters:
- id_or_ins (str|Instrument): Instrument ID or Instrument object
- amount (int): Number of shares (positive=buy, negative=sell)
- price_or_style (float|OrderStyle, optional): Limit price or order style
Returns:
Order: Order object or None if order failed
"""
def order(order_book_id, quantity, price_or_style=None):
"""
Universal smart order function.
Parameters:
- order_book_id (str): Instrument identifier
- quantity (float): Quantity to order (positive=buy, negative=sell)
- price_or_style (float|OrderStyle, optional): Price or order style
Returns:
Order: Order object or None if order failed
"""Order placement functions that specify order size in terms of cash value.
def order_value(id_or_ins, cash_amount, price_or_style=None):
"""
Order by cash value.
Parameters:
- id_or_ins (str|Instrument): Instrument ID or Instrument object
- cash_amount (float): Cash amount to invest (positive=buy, negative=sell)
- price_or_style (float|OrderStyle, optional): Limit price or order style
Returns:
Order: Order object or None if order failed
"""
def order_percent(id_or_ins, percent, price_or_style=None):
"""
Order by portfolio percentage.
Parameters:
- id_or_ins (str|Instrument): Instrument ID or Instrument object
- percent (float): Percentage of portfolio to invest (0.0-1.0)
- price_or_style (float|OrderStyle, optional): Limit price or order style
Returns:
Order: Order object or None if order failed
"""Order placement functions that target specific position sizes.
def order_target_value(id_or_ins, cash_amount, price_or_style=None):
"""
Target position by cash value.
Parameters:
- id_or_ins (str|Instrument): Instrument ID or Instrument object
- cash_amount (float): Target position value
- price_or_style (float|OrderStyle, optional): Limit price or order style
Returns:
Order: Order object or None if order failed
"""
def order_target_percent(id_or_ins, percent, price_or_style=None):
"""
Target position by portfolio percentage.
Parameters:
- id_or_ins (str|Instrument): Instrument ID or Instrument object
- percent (float): Target percentage of portfolio (0.0-1.0)
- price_or_style (float|OrderStyle, optional): Limit price or order style
Returns:
Order: Order object or None if order failed
"""
def order_to(order_book_id, quantity, price_or_style=None):
"""
Universal position targeting function.
Parameters:
- order_book_id (str): Instrument identifier
- quantity (float): Target position quantity
- price_or_style (float|OrderStyle, optional): Price or order style
Returns:
Order: Order object or None if order failed
"""Specialized order functions for futures trading with position effects.
def buy_open(id_or_ins, amount, price_or_style=None):
"""
Buy to open position (futures).
Parameters:
- id_or_ins (str|Instrument): Futures instrument ID or object
- amount (int): Number of contracts to buy
- price_or_style (float|OrderStyle, optional): Limit price or order style
Returns:
Order: Order object or None if order failed
"""
def buy_close(id_or_ins, amount, price_or_style=None, close_today=False):
"""
Buy to close position (futures).
Parameters:
- id_or_ins (str|Instrument): Futures instrument ID or object
- amount (int): Number of contracts to close
- price_or_style (float|OrderStyle, optional): Limit price or order style
- close_today (bool): Whether to close today's positions first
Returns:
Order: Order object or None if order failed
"""
def sell_open(id_or_ins, amount, price_or_style=None):
"""
Sell to open position (futures).
Parameters:
- id_or_ins (str|Instrument): Futures instrument ID or object
- amount (int): Number of contracts to sell
- price_or_style (float|OrderStyle, optional): Limit price or order style
Returns:
Order: Order object or None if order failed
"""
def sell_close(id_or_ins, amount, price_or_style=None, close_today=False):
"""
Sell to close position (futures).
Parameters:
- id_or_ins (str|Instrument): Futures instrument ID or object
- amount (int): Number of contracts to close
- price_or_style (float|OrderStyle, optional): Limit price or order style
- close_today (bool): Whether to close today's positions first
Returns:
Order: Order object or None if order failed
"""Exercise functions for options and convertible bonds.
def exercise(id_or_ins, amount, convert=False):
"""
Exercise options or convertible bonds.
Parameters:
- id_or_ins (str|Instrument): Option or convertible bond ID/object
- amount (int): Number of contracts/bonds to exercise
- convert (bool): Whether to convert (for convertible bonds)
Returns:
Order: Exercise order object or None if failed
"""Functions for managing active orders and order lifecycle.
def get_open_orders():
"""
Get all pending orders.
Returns:
list[Order]: List of pending Order objects
"""
def submit_order(id_or_ins, amount, side, price=None, position_effect=None):
"""
Submit order manually with full control.
Parameters:
- id_or_ins (str|Instrument): Instrument ID or object
- amount (float): Order amount/quantity
- side (SIDE): Order side (BUY, SELL, etc.)
- price (float, optional): Limit price
- position_effect (POSITION_EFFECT, optional): Position effect
Returns:
Order: Order object or None if order failed
"""
def cancel_order(order):
"""
Cancel pending order.
Parameters:
- order (Order): Order object to cancel
Returns:
bool: True if cancellation successful
"""Functions for managing the strategy's trading universe.
def update_universe(id_or_symbols):
"""
Update strategy universe.
Parameters:
- id_or_symbols (list[str]): List of instrument IDs to include in universe
Returns:
None
"""
def subscribe(id_or_symbols):
"""
Subscribe to instruments for data.
Parameters:
- id_or_symbols (str|list[str]): Instrument ID(s) to subscribe to
Returns:
None
"""
def unsubscribe(id_or_symbols):
"""
Unsubscribe from instruments.
Parameters:
- id_or_symbols (str|list[str]): Instrument ID(s) to unsubscribe from
Returns:
None
"""Order styles define how orders should be executed in the market.
class MarketOrder:
"""
Market order style for immediate execution at current market price.
Usage:
order_shares("000001.XSHE", 100, MarketOrder())
"""class LimitOrder:
"""
Limit order style with specified price.
Parameters:
- limit_price (float): Maximum price for buy orders, minimum for sell orders
Usage:
order_shares("000001.XSHE", 100, LimitOrder(10.50))
"""class TWAPOrder:
"""
Time-weighted average price order.
Parameters:
- start_time (str): Start time for execution
- end_time (str): End time for execution
Usage:
order_shares("000001.XSHE", 1000, TWAPOrder("09:30", "15:00"))
"""
class VWAPOrder:
"""
Volume-weighted average price order.
Parameters:
- start_time (str): Start time for execution
- end_time (str): End time for execution
Usage:
order_shares("000001.XSHE", 1000, VWAPOrder("09:30", "15:00"))
"""def handle_bar(context, bar_dict):
# Buy 100 shares at market price
order_shares("000001.XSHE", 100)
# Buy with limit price
order_shares("000002.XSHE", 100, LimitOrder(15.50))
# Target 50% of portfolio in stock
order_target_percent("000001.XSHE", 0.5)
# Invest 10000 cash in stock
order_value("000002.XSHE", 10000)def handle_bar(context, bar_dict):
future = "RB2401" # Steel rebar future
# Open long position
buy_open(future, 1)
# Close long position
sell_close(future, 1)
# Open short position
sell_open(future, 1)
# Close short position
buy_close(future, 1)def handle_bar(context, bar_dict):
# Place a limit order
order = order_shares("000001.XSHE", 100, LimitOrder(10.50))
# Check pending orders
open_orders = get_open_orders()
for order in open_orders:
if order.instrument.order_book_id == "000001.XSHE":
# Cancel the order if conditions change
cancel_order(order)def init(context):
# Set initial universe
context.stocks = ["000001.XSHE", "000002.XSHE", "600000.XSHG"]
update_universe(context.stocks)
def handle_bar(context, bar_dict):
# Dynamically update universe based on conditions
new_stocks = get_high_volume_stocks() # Custom function
update_universe(new_stocks)
# Subscribe to additional data
subscribe(["000858.XSHE", "002415.XSHE"])Install with Tessl CLI
npx tessl i tessl/pypi-rqalpha