Skip to content

Api

QuantConnect.Api.Api

Api()

Bases: Object, IApi, IDownloadProvider

QuantConnect.com Interaction Via API.

Creates a new instance of Api

serializer_settings

serializer_settings: Any

Serializer settings to use

This codeEntityType is protected.

api_connection

api_connection: ApiConnection

Returns the underlying API connection

This codeEntityType is protected.

connected

connected: bool

Check if Api is successfully connected with correct credentials

create_live_algorithm

create_live_algorithm(
    project_id: int,
    compile_id: str,
    node_id: str,
    brokerage_settings: Any,
    version_id: str = "-1",
    data_providers: Any = None,
) -> CreateLiveAlgorithmResponse
create_live_algorithm(
    project_id: int,
    compile_id: str,
    node_id: str,
    brokerage_settings: Dictionary[str, Object],
    version_id: str = "-1",
    data_providers: Dictionary[str, Object] = None,
) -> CreateLiveAlgorithmResponse

Create a live algorithm.

Parameters:

Name Type Description Default
project_id int

Id of the project on QuantConnect

required
compile_id str

Id of the compilation on QuantConnect

required
node_id str

Id of the node that will run the algorithm

required
brokerage_settings Any | Dictionary[str, Object]

Python Dictionary with brokerage specific settings. Each brokerage requires certain specific credentials

required
version_id str

The version of the Lean used to run the algorithm.

'-1'
data_providers Any | Dictionary[str, Object]

Python Dictionary with data providers credentials. Each data provider requires certain credentials

None

Returns:

Type Description
CreateLiveAlgorithmResponse

Information regarding the new algorithm CreateLiveAlgorithmResponse.

abort_optimization

abort_optimization(optimization_id: str) -> RestResponse

Abort an optimization

Parameters:

Name Type Description Default
optimization_id str

Optimization id for the optimization we want to abort

required

Returns:

Type Description
RestResponse

RestResponse.

add_project_file

add_project_file(
    project_id: int, name: str, content: str
) -> RestResponse

Add a file to a project

Parameters:

Name Type Description Default
project_id int

The project to which the file should be added

required
name str

The name of the new file

required
content str

The content of the new file

required

Returns:

Type Description
RestResponse

ProjectFilesResponse that includes information about the newly created file.

broadcast_live_command

broadcast_live_command(
    organization_id: str,
    exclude_project_id: Optional[int],
    command: Any,
) -> RestResponse

Broadcast a live command

Parameters:

Name Type Description Default
organization_id str

Organization ID of the projects we would like to broadcast the command to

required
exclude_project_id Optional[int]

Project for the live instance we want to exclude from the broadcast list

required
command Any

The command to run

required

Returns:

Type Description
RestResponse

RestResponse.

create_backtest

create_backtest(
    project_id: int, compile_id: str, backtest_name: str
) -> Backtest

Create a new backtest request and get the id.

Parameters:

Name Type Description Default
project_id int

Id for the project to backtest

required
compile_id str

Compile id for the project

required
backtest_name str

Name for the new backtest

required

Returns:

Type Description
Backtest

Backtestt.

create_compile

create_compile(project_id: int) -> Compile

Create a new compile job request for this project id.

Parameters:

Name Type Description Default
project_id int

Project id we wish to compile.

required

Returns:

Type Description
Compile

Compile object result.

create_live_command

create_live_command(
    project_id: int, command: Any
) -> RestResponse

Create a live command

Parameters:

Name Type Description Default
project_id int

Project for the live instance we want to run the command against

required
command Any

The command to run

required

Returns:

Type Description
RestResponse

RestResponse.

create_optimization

create_optimization(
    project_id: int,
    name: str,
    target: str,
    target_to: str,
    target_value: Optional[float],
    strategy: str,
    compile_id: str,
    parameters: HashSet[OptimizationParameter],
    constraints: Sequence[Constraint],
    estimated_cost: float,
    node_type: str,
    parallel_nodes: int,
) -> OptimizationSummary

Create an optimization with the specified parameters via QuantConnect.com API

Parameters:

Name Type Description Default
project_id int

Project ID of the project the optimization belongs to

required
name str

Name of the optimization

required
target str

Target of the optimization, see examples in PortfolioStatistics

required
target_to str

Target extremum of the optimization, for example "max" or "min"

required
target_value Optional[float]

Optimization target value

required
strategy str

Optimization strategy, QuantConnect.Optimizer.Strategies.GridSearchOptimizationStrategy

required
compile_id str

Optimization compile ID

required
parameters HashSet[OptimizationParameter]

Optimization parameters

required
constraints Sequence[Constraint]

Optimization constraints

required
estimated_cost float

Estimated cost for optimization

required
node_type str

Optimization node type OptimizationNodes

required
parallel_nodes int

Number of parallel nodes for optimization

required

Returns:

Type Description
OptimizationSummary

BaseOptimization object from the API.

create_project

create_project(
    name: str,
    language: Language,
    organization_id: str = None,
) -> ProjectResponse

Create a project with the specified name and language via QuantConnect.com API

Parameters:

Name Type Description Default
name str

Project name

required
language Language

Programming language to use

required
organization_id str

Optional param for specifying organization to create project under. If none provided web defaults to preferred.

None

Returns:

Type Description
ProjectResponse

Project object from the API.

create_secure_hash

create_secure_hash(timestamp: int, token: str) -> str

Generate a secure hash for the authorization headers.

Returns:

Type Description
str

Time based hash of user token and timestamp.

delete_backtest

delete_backtest(
    project_id: int, backtest_id: str
) -> RestResponse

Delete a backtest from the specified project and backtest_id.

Parameters:

Name Type Description Default
project_id int

Project for the backtest we want to delete

required
backtest_id str

Backtest id we want to delete

required

Returns:

Type Description
RestResponse

RestResponse.

delete_object_store

delete_object_store(
    organization_id: str, key: str
) -> RestResponse

Request to delete Object Store metadata of a specific organization and key

Parameters:

Name Type Description Default
organization_id str

Organization ID we would like to delete the Object Store file from

required
key str

Key to the Object Store file

required

Returns:

Type Description
RestResponse

RestResponse.

delete_optimization

delete_optimization(optimization_id: str) -> RestResponse

Delete an optimization

Parameters:

Name Type Description Default
optimization_id str

Optimization id for the optimization we want to delete

required

Returns:

Type Description
RestResponse

RestResponse.

delete_project

delete_project(project_id: int) -> RestResponse

Delete a project

Parameters:

Name Type Description Default
project_id int

Project id we own and wish to delete

required

Returns:

Type Description
RestResponse

RestResponse indicating success.

delete_project_file

delete_project_file(
    project_id: int, name: str
) -> RestResponse

Delete a file in a project

Parameters:

Name Type Description Default
project_id int

Project id to which the file belongs

required
name str

The name of the file that should be deleted

required

Returns:

Type Description
RestResponse

RestResponse that includes the information about all files in the project.

dispose

dispose() -> None

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

download

download(
    address: str,
    headers: List[KeyValuePair[str, str]],
    user_name: str,
    password: str,
) -> str

Local implementation for downloading data to algorithms

Parameters:

Name Type Description Default
address str

URL to download

required
headers List[KeyValuePair[str, str]]

KVP headers

required
user_name str

Username for basic authentication

required
password str

Password for basic authentication

required

download_bytes

download_bytes(
    address: str,
    headers: List[KeyValuePair[str, str]],
    user_name: str,
    password: str,
) -> List[int]

Local implementation for downloading data to algorithms

Parameters:

Name Type Description Default
address str

URL to download

required
headers List[KeyValuePair[str, str]]

KVP headers

required
user_name str

Username for basic authentication

required
password str

Password for basic authentication

required

Returns:

Type Description
List[int]

A stream from which the data can be read.

download_data

download_data(file_path: str, organization_id: str) -> bool

Method to purchase and download data from QuantConnect

Parameters:

Name Type Description Default
file_path str

File path representing the data requested

required
organization_id str

Organization to buy the data with

required

Returns:

Type Description
bool

A bool indicating whether the data was successfully downloaded or not.

estimate_optimization

estimate_optimization(
    project_id: int,
    name: str,
    target: str,
    target_to: str,
    target_value: Optional[float],
    strategy: str,
    compile_id: str,
    parameters: HashSet[OptimizationParameter],
    constraints: Sequence[Constraint],
) -> Estimate

Estimate optimization with the specified parameters via QuantConnect.com API

Parameters:

Name Type Description Default
project_id int

Project ID of the project the optimization belongs to

required
name str

Name of the optimization

required
target str

Target of the optimization, see examples in PortfolioStatistics

required
target_to str

Target extremum of the optimization, for example "max" or "min"

required
target_value Optional[float]

Optimization target value

required
strategy str

Optimization strategy, QuantConnect.Optimizer.Strategies.GridSearchOptimizationStrategy

required
compile_id str

Optimization compile ID

required
parameters HashSet[OptimizationParameter]

Optimization parameters

required
constraints Sequence[Constraint]

Optimization constraints

required

Returns:

Type Description
Estimate

Estimate object from the API.

format_path_for_data_request

format_path_for_data_request(
    file_path: str, data_folder: str = None
) -> str

Helper method to normalize path for api data requests

Parameters:

Name Type Description Default
file_path str

Filepath to format

required
data_folder str

The data folder to use

None

Returns:

Type Description
str

Normalized path.

get_algorithm_status

get_algorithm_status(algorithm_id: str) -> AlgorithmControl

Get the algorithm status from the user with this algorithm id.

Parameters:

Name Type Description Default
algorithm_id str

String algorithm id we're searching for.

required

Returns:

Type Description
AlgorithmControl

Algorithm status enum.

get_object_store

get_object_store(
    organization_id: str,
    keys: List[str],
    destination_folder: str = None,
) -> bool

Download the object store files associated with the given organization ID and key

Parameters:

Name Type Description Default
organization_id str

Organization ID we would like to get the Object Store files from

required
keys List[str]

Keys for the Object Store files

required
destination_folder str

Folder in which the object store files will be stored

None

Returns:

Type Description
bool

True if the object store files were retrieved correctly, false otherwise.

get_object_store_properties

get_object_store_properties(
    organization_id: str, key: str
) -> PropertiesObjectStoreResponse

Get Object Store properties given the organization ID and the Object Store key

Parameters:

Name Type Description Default
organization_id str

Organization ID we would like to get the Object Store from

required
key str

Key for the Object Store file

required

Returns:

Type Description
PropertiesObjectStoreResponse

PropertiesObjectStoreResponse.

initialize

initialize(
    user_id: int, token: str, data_folder: str
) -> None

Initialize the API with the given variables

liquidate_live_algorithm

liquidate_live_algorithm(project_id: int) -> RestResponse

Liquidate a live algorithm from the specified project and deployId.

Parameters:

Name Type Description Default
project_id int

Project for the live instance we want to stop

required

Returns:

Type Description
RestResponse

RestResponse.

list_backtests

list_backtests(
    project_id: int, include_statistics: bool = True
) -> BacktestSummaryList

List all the backtest summaries for a project

Parameters:

Name Type Description Default
project_id int

Project id we'd like to get a list of backtest for

required
include_statistics bool

True for include statistics in the response, false otherwise

True

Returns:

Type Description
BacktestSummaryList

BacktestList.

list_live_algorithms

list_live_algorithms(
    status: Optional[AlgorithmStatus] = None,
) -> LiveList

Get a list of live running algorithms for user

Parameters:

Name Type Description Default
status Optional[AlgorithmStatus]

Filter the statuses of the algorithms returned from the api

None

Returns:

Type Description
LiveList

LiveList.

list_object_store

list_object_store(
    organization_id: str, path: str
) -> ListObjectStoreResponse

Request to list Object Store files of a specific organization and path

Parameters:

Name Type Description Default
organization_id str

Organization ID we would like to list the Object Store files from

required
path str

Path to the Object Store files

required

Returns:

Type Description
ListObjectStoreResponse

ListObjectStoreResponse.

list_optimizations

list_optimizations(
    project_id: int,
) -> List[OptimizationSummary]

List all the optimizations for a project

Parameters:

Name Type Description Default
project_id int

Project id we'd like to get a list of optimizations for

required

Returns:

Type Description
List[OptimizationSummary]

A list of BaseOptimization objects, BaseOptimization.

list_projects

list_projects() -> ProjectResponse

List details of all projects

Returns:

Type Description
ProjectResponse

ProjectResponse that contains information regarding the project.

read_account

read_account(organization_id: str = None) -> Account

Will read the organization account status

Parameters:

Name Type Description Default
organization_id str

The target organization id, if null will return default organization

None

read_backtest

read_backtest(
    project_id: int,
    backtest_id: str,
    get_charts: bool = True,
) -> Backtest

Read out a backtest in the project id specified.

Parameters:

Name Type Description Default
project_id int

Project id to read

required
backtest_id str

Specific backtest id to read

required
get_charts bool

True will return backtest charts

True

Returns:

Type Description
Backtest

Backtest.

read_backtest_chart

read_backtest_chart(
    project_id: int,
    name: str,
    start: int,
    end: int,
    count: int,
    backtest_id: str,
) -> ReadChartResponse

Returns a requested chart object from a backtest

Parameters:

Name Type Description Default
project_id int

Project ID of the request

required
name str

The requested chart name

required
start int

The Utc start seconds timestamp of the request

required
end int

The Utc end seconds timestamp of the request

required
count int

The number of data points to request

required
backtest_id str

Associated Backtest ID for this chart request

required

Returns:

Type Description
ReadChartResponse

The chart.

read_backtest_insights

read_backtest_insights(
    project_id: int,
    backtest_id: str,
    start: int = 0,
    end: int = 0,
) -> InsightResponse

Read out the insights of a backtest

Parameters:

Name Type Description Default
project_id int

Id of the project from which to read the backtest

required
backtest_id str

Backtest id from which we want to get the insights

required
start int

Starting index of the insights to be fetched

0
end int

Last index of the insights to be fetched. Note that end - start must be less than 100

0

Returns:

Type Description
InsightResponse

InsightResponse.

read_backtest_orders

read_backtest_orders(
    project_id: int,
    backtest_id: str,
    start: int = 0,
    end: int = 100,
) -> List[ApiOrderResponse]

Returns the orders of the specified backtest and project id.

Parameters:

Name Type Description Default
project_id int

Id of the project from which to read the orders

required
backtest_id str

Id of the backtest from which to read the orders

required
start int

Starting index of the orders to be fetched. Required if end > 100

0
end int

Last index of the orders to be fetched. Note that end - start must be less than 100

100

Returns:

Type Description
List[ApiOrderResponse]

The list of Order.

read_backtest_report

read_backtest_report(
    project_id: int, backtest_id: str
) -> BacktestReport

Read out the report of a backtest in the project id specified.

Parameters:

Name Type Description Default
project_id int

Project id to read

required
backtest_id str

Specific backtest id to read

required

Returns:

Type Description
BacktestReport

BacktestReport.

read_compile

read_compile(project_id: int, compile_id: str) -> Compile

Read a compile packet job result.

Parameters:

Name Type Description Default
project_id int

Project id we sent for compile

required
compile_id str

Compile id return from the creation request

required

Returns:

Type Description
Compile

Compile.

read_data_directory

read_data_directory(file_path: str) -> DataList

Get valid data entries for a given filepath from data/list

read_data_link(
    file_path: str, organization_id: str
) -> DataLink

Gets the link to the downloadable data.

Parameters:

Name Type Description Default
file_path str

File path representing the data requested

required
organization_id str

Organization to download from

required

Returns:

Type Description
DataLink

DataLink to the downloadable data.

read_data_prices

read_data_prices(organization_id: str) -> DataPricesList

Gets data prices from data/prices

read_lean_versions

read_lean_versions() -> VersionsResponse

Gets a list of LEAN versions with their corresponding basic descriptions

read_live_algorithm

read_live_algorithm(
    project_id: int, deploy_id: str
) -> LiveAlgorithmResults

Read out a live algorithm in the project id specified.

Parameters:

Name Type Description Default
project_id int

Project id to read

required
deploy_id str

Specific instance id to read

required

Returns:

Type Description
LiveAlgorithmResults

LiveAlgorithmResults.

read_live_chart

read_live_chart(
    project_id: int,
    name: str,
    start: int,
    end: int,
    count: int,
) -> ReadChartResponse

Returns a chart object from a live algorithm

Parameters:

Name Type Description Default
project_id int

Project ID of the request

required
name str

The requested chart name

required
start int

The Utc start seconds timestamp of the request

required
end int

The Utc end seconds timestamp of the request

required
count int

The number of data points to request

required

Returns:

Type Description
ReadChartResponse

The chart.

read_live_insights

read_live_insights(
    project_id: int, start: int = 0, end: int = 0
) -> InsightResponse

Read out the insights of a live algorithm

Parameters:

Name Type Description Default
project_id int

Id of the project from which to read the live algorithm

required
start int

Starting index of the insights to be fetched

0
end int

Last index of the insights to be fetched. Note that end - start must be less than 100

0

Returns:

Type Description
InsightResponse

InsightResponse.

read_live_logs

read_live_logs(
    project_id: int,
    algorithm_id: str,
    start_line: int,
    end_line: int,
) -> LiveLog

Gets the logs of a specific live algorithm

Parameters:

Name Type Description Default
project_id int

Project Id of the live running algorithm

required
algorithm_id str

Algorithm Id of the live running algorithm

required
start_line int

Start line of logs to read

required
end_line int

End line of logs to read

required

Returns:

Type Description
LiveLog

LiveLog List of strings that represent the logs of the algorithm.

read_live_orders

read_live_orders(
    project_id: int, start: int = 0, end: int = 100
) -> List[ApiOrderResponse]

Returns the orders of the specified project id live algorithm.

Parameters:

Name Type Description Default
project_id int

Id of the project from which to read the live orders

required
start int

Starting index of the orders to be fetched. Required if end > 100

0
end int

Last index of the orders to be fetched. Note that end - start must be less than 100

100

Returns:

Type Description
List[ApiOrderResponse]

The list of Order.

read_live_portfolio

read_live_portfolio(project_id: int) -> PortfolioResponse

Read out the portfolio state of a live algorithm

Parameters:

Name Type Description Default
project_id int

Id of the project from which to read the live algorithm

required

Returns:

Type Description
PortfolioResponse

PortfolioResponse.

read_optimization

read_optimization(optimization_id: str) -> Optimization

Read an optimization

Parameters:

Name Type Description Default
optimization_id str

Optimization id for the optimization we want to read

required

Returns:

Type Description
Optimization

Optimization.

read_organization

read_organization(
    organization_id: str = None,
) -> Organization

Fetch organization data from web API

Parameters:

Name Type Description Default
organization_id str
None

read_project

read_project(project_id: int) -> ProjectResponse

Get details about a single project

Parameters:

Name Type Description Default
project_id int

Id of the project

required

Returns:

Type Description
ProjectResponse

ProjectResponse that contains information regarding the project.

read_project_file

read_project_file(
    project_id: int, file_name: str
) -> ProjectFilesResponse

Read a file in a project

Parameters:

Name Type Description Default
project_id int

Project id to which the file belongs

required
file_name str

The name of the file

required

Returns:

Type Description
ProjectFilesResponse

ProjectFilesResponse that includes the file information.

read_project_files

read_project_files(project_id: int) -> ProjectFilesResponse

Read all files in a project

Parameters:

Name Type Description Default
project_id int

Project id to which the file belongs

required

Returns:

Type Description
ProjectFilesResponse

ProjectFilesResponse that includes the information about all files in the project.

read_project_nodes

read_project_nodes(project_id: int) -> ProjectNodesResponse

Read all nodes in a project.

Parameters:

Name Type Description Default
project_id int

Project id to which the nodes refer

required

Returns:

Type Description
ProjectNodesResponse

ProjectNodesResponse that includes the information about all nodes in the project.

send_notification

send_notification(
    notification: Notification, project_id: int
) -> RestResponse

Sends a notification

Parameters:

Name Type Description Default
notification Notification

The notification to send

required
project_id int

The project id

required

Returns:

Type Description
RestResponse

RestResponse containing success response and errors.

send_statistics

send_statistics(
    algorithm_id: str,
    unrealized: float,
    fees: float,
    net_profit: float,
    holdings: float,
    equity: float,
    net_return: float,
    volume: float,
    trades: int,
    sharpe: float,
) -> None

Send the statistics to storage for performance tracking.

Parameters:

Name Type Description Default
algorithm_id str

Identifier for algorithm

required
unrealized float

Unrealized gainloss

required
fees float

Total fees

required
net_profit float

Net profi

required
holdings float

Algorithm holdings

required
equity float

Total equity

required
net_return float

Net return for the deployment

required
volume float

Volume traded

required
trades int

Total trades since inception

required
sharpe float

Sharpe ratio since inception

required

set_algorithm_status

set_algorithm_status(
    algorithm_id: str,
    status: AlgorithmStatus,
    message: str = ...,
) -> None

Algorithm passes back its current status to the UX.

Parameters:

Name Type Description Default
status AlgorithmStatus

Status of the current algorithm

required
algorithm_id str

String algorithm id we're setting.

required
message str

Message for the algorithm status event

...

Returns:

Type Description
None

Algorithm status enum.

set_object_store

set_object_store(
    organization_id: str, key: str, object_data: List[int]
) -> RestResponse

Upload files to the Object Store

Parameters:

Name Type Description Default
organization_id str

Organization ID we would like to upload the file to

required
key str

Key to the Object Store file

required
object_data List[int]

File (as an array of bytes) to be uploaded

required

Returns:

Type Description
RestResponse

RestResponse.

stop_live_algorithm

stop_live_algorithm(project_id: int) -> RestResponse

Stop a live algorithm from the specified project and deployId.

Parameters:

Name Type Description Default
project_id int

Project for the live instance we want to stop

required

Returns:

Type Description
RestResponse

RestResponse.

update_backtest

update_backtest(
    project_id: int,
    backtest_id: str,
    name: str = None,
    note: str = ...,
) -> RestResponse

Update a backtest name

Parameters:

Name Type Description Default
project_id int

Project for the backtest we want to update

required
backtest_id str

Backtest id we want to update

required
name str

Name we'd like to assign to the backtest

None
note str

Note attached to the backtest

...

Returns:

Type Description
RestResponse

RestResponse.

update_backtest_tags

update_backtest_tags(
    project_id: int, backtest_id: str, tags: Sequence[str]
) -> RestResponse

Updates the tags collection for a backtest

Parameters:

Name Type Description Default
project_id int

Project for the backtest we want to update

required
backtest_id str

Backtest id we want to update

required
tags Sequence[str]

The new backtest tags

required

Returns:

Type Description
RestResponse

RestResponse.

update_optimization

update_optimization(
    optimization_id: str, name: str = None
) -> RestResponse

Update an optimization

Parameters:

Name Type Description Default
optimization_id str

Optimization id we want to update

required
name str

Name we'd like to assign to the optimization

None

Returns:

Type Description
RestResponse

RestResponse.

update_project_file_content

update_project_file_content(
    project_id: int, file_name: str, new_file_contents: str
) -> RestResponse

Update the contents of a file

Parameters:

Name Type Description Default
project_id int

Project id to which the file belongs

required
file_name str

The name of the file that should be updated

required
new_file_contents str

The new contents of the file

required

Returns:

Type Description
RestResponse

RestResponse indicating success.

update_project_file_name

update_project_file_name(
    project_id: int, old_file_name: str, new_file_name: str
) -> RestResponse

Update the name of a file

Parameters:

Name Type Description Default
project_id int

Project id to which the file belongs

required
old_file_name str

The current name of the file

required
new_file_name str

The new name for the file

required

Returns:

Type Description
RestResponse

RestResponse indicating success.

update_project_nodes

update_project_nodes(
    project_id: int, nodes: List[str]
) -> ProjectNodesResponse

Update the active state of some nodes to true. If you don't provide any nodes, all the nodes become inactive and AutoSelectNode is true.

Parameters:

Name Type Description Default
project_id int

Project id to which the nodes refer

required
nodes List[str]

List of node ids to update

required

Returns:

Type Description
ProjectNodesResponse

ProjectNodesResponse that includes the information about all nodes in the project.