Skip to content

BrokerageSetupHandler

QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler

BrokerageSetupHandler()

Bases: Object, ISetupHandler

Defines a set up handler that initializes the algorithm instance using values retrieved from the user's brokerage account

Initializes a new BrokerageSetupHandler

max_allocation_limit_config

max_allocation_limit_config: str = 'max-allocation-limit'

Max allocation limit configuration variable name

worker_thread

worker_thread: WorkerThread

The worker thread instance the setup handler should use

errors

errors: List[Exception]

Any errors from the initialization stored here:

maximum_runtime

maximum_runtime: timedelta

Get the maximum runtime for this algorithm job.

starting_portfolio_value

starting_portfolio_value: float

Algorithm starting capital for statistics calculations

starting_date

starting_date: datetime

Start date for analysis loops to search for data.

max_orders

max_orders: int

Maximum number of orders for the algorithm run -- applicable for backtests only.

create_algorithm_instance

create_algorithm_instance(
    algorithm_node_packet: AlgorithmNodePacket,
    assembly_path: str,
) -> IAlgorithm

Create a new instance of an algorithm from a physical dll path.

Parameters:

Name Type Description Default
assembly_path str

The path to the assembly's location

required
algorithm_node_packet AlgorithmNodePacket

Details of the task required

required

Returns:

Type Description
IAlgorithm

A new instance of IAlgorithm, or throws an exception if there was an error.

create_brokerage

create_brokerage(
    algorithm_node_packet: AlgorithmNodePacket,
    uninitialized_algorithm: IAlgorithm,
    factory: Optional[IBrokerageFactory],
) -> Tuple[IBrokerage, IBrokerageFactory]

Creates the brokerage as specified by the job packet

Parameters:

Name Type Description Default
algorithm_node_packet AlgorithmNodePacket

Job packet

required
uninitialized_algorithm IAlgorithm

The algorithm instance before Initialize has been called

required
factory Optional[IBrokerageFactory]

The brokerage factory

required

Returns:

Type Description
Tuple[IBrokerage, IBrokerageFactory]

The brokerage instance, or throws if error creating instance.

dispose

dispose() -> None

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

get_open_orders

get_open_orders(
    algorithm: IAlgorithm,
    result_handler: IResultHandler,
    transaction_handler: ITransactionHandler,
    brokerage: IBrokerage,
) -> None

Get the open orders from a brokerage. Adds Orders.Order and Orders.OrderTicket to the transaction handler

This codeEntityType is protected.

Parameters:

Name Type Description Default
algorithm IAlgorithm

Algorithm instance

required
result_handler IResultHandler

The configured result handler

required
transaction_handler ITransactionHandler

The configurated transaction handler

required
brokerage IBrokerage

Brokerage output instance

required

load_existing_holdings_and_orders

load_existing_holdings_and_orders(
    brokerage: IBrokerage,
    algorithm: IAlgorithm,
    parameters: SetupHandlerParameters,
) -> bool

Loads existing holdings and orders

This codeEntityType is protected.

setup

setup(parameters: SetupHandlerParameters) -> bool

Primary entry point to setup a new algorithm

Parameters:

Name Type Description Default
parameters SetupHandlerParameters

The parameters object to use

required

Returns:

Type Description
bool

True on successfully setting up the algorithm state, or false on error.