Backtests

Managing Backtests

Introduction

This page explains how to create and manage a project in the research environment.

Prerequisites

Working knowledge of C#.

Working knowledge of Python.

You'll need to understand how to do Project Management through the QuantConnect Api.

Compile Projects

Follow these steps to compile the project.

  1. Load the required assembly files and data types.
  2. #load "../Initialize.csx"
    #load "../QuantConnect.csx"
    
    using QuantConnect;
    using QuantConnect.Api;
  3. Call CreateCompile to compile the project.
  4. var projectID = <yourProjectId>;
    var compiled = api.CreateCompile(projectID);
    project_id = <your_project_id>
    compiled = api.CreateCompile(project_id)
  5. Call CompileId attribute to obtain the compiled ID.
  6. var compileId = compiled.CompileId;
    compile_id = compiled.CompileId

Create Backtests

You need to understand how to Compile Projects to create a backtest.

Follow these steps to create a backtest by using the QuantConnect Api.

  1. Call CreateBacktest with the project ID, compile ID and a custom backtest name.
  2. var backtest = api.CreateBacktest(projectId, compileId, "backtest");
    backtest = api.CreateBacktest(project_id, compile_id, "backtest")

Update Backtests

You need to understand how to Read Backtest Instance.

Follow these steps to update the name and/or note of the backtest by using the QuantConnect Api.

  1. Display the name and note of the backtest before updating.
  2. Console.WriteLine($@"Backtest ID: {backtest.BacktestId}
    Name: {backtest.Name}
    Note: {backtest.Note}");
    print(f"""Backtest ID: {backtest.BacktestId}
    Name: {backtest.Name}
    Note: {backtest.Note}\n""")
  3. Call UpdateBacktest with the project ID, backtest ID, new backtest name and new backtest note. This will return a RestResponse from the Api.
  4. var response = api.UpdateBacktest(projectId, backtest.BacktestId, "New Name", "New Note");
    response = api.UpdateBacktest(project_id, backtest.BacktestId, "New Name", "New Note")
  5. Call Success attribute to see if the update task is successful.
  6. Console.WriteLine($"\n{response.Success}\n");
    print(str(response.Success)+"\n")
  7. Re-read the backtest.
  8. var backtest2 = api.ReadBacktest(projectId, backtest.BacktestId);
    backtest = api.ReadBacktest(project_id, backtest.BacktestId)
  9. Display the name and note of the backtest after updating.
  10. Console.WriteLine($@"Backtest ID: {backtest2.BacktestId}
    Name: {backtest2.Name}
    Note: {backtest2.Note}");
    print(f"""Backtest ID: {backtest.BacktestId}
    Name: {backtest.Name}
    Note: {backtest.Note}""")

Delete Backtests

Follow these steps to delete a backtest and check its existance by using the QuantConnect Api.

  1. Call DeleteBacktest with the project ID, backtest ID. This will return a RestResponse from the Api.
  2. var response = api.DeleteBacktest(projectId, backtest.BacktestId);
    response = api.DeleteBacktest(project_id, backtest.BacktestId)
  3. Call Success attribute to see if the deletion task is successful.
  4. Console.WriteLine($"\n{response.Success}\n");
    print(str(response.Success)+"\n")
  5. Re-read the backtest.
  6. var backtest3 = api.ReadBacktest(projectId, backtest.BacktestId);
    backtest = api.ReadBacktest(project_id, backtest.BacktestId)
  7. Call Errors to check if the API throw any error message on the backtest existance.
  8. backtest3.Errors
    backtest.Errors

List Backtests

Follow these steps to list out all backtests in a project by using the QuantConnect Api.

  1. Call ListBacktests with the project ID.
  2. var backtestList = api.ListBacktests(projectId);
    backtest_list = api.ListBacktests(project_id)
  3. Call Backtests attribute. This will return a list of backtests.
  4. var backtests = backtestList.Backtests;
    backtest_list = backtest_list.Backtests
  5. Display the backtests.
  6. foreach(var bt in backtests)
    {
        Console.WriteLine($@"Backtest ID: {backtest.BacktestId}
    Name: {backtest.Name}
    Created Time: {backtest.Created}");
    }
    for bt in backtest_list:
        print(f"""Backtest ID: {backtest.BacktestId}
    Name: {backtest.Name}
    Created Time: {backtest.Created.strftime("%Y/%m/%d %H:%M:%S")}\n""")

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: