Live Algorithms

Reading Results

Introduction

This page explains how to get live algorithm results into the Research Environment with the QuantConnect API.

Prerequisites

Working knowledge of C#.

Working knowledge of Python and pandas. If you are not familiar with pandas, refer to the pandas documentation.

Read Deployment Instance

To get the results of a live algorithm, call the ReadLiveAlgorithm method with the project ID and deployment ID.

#load "../Initialize.csx"
#load "../QuantConnect.csx"

using QuantConnect;
using QuantConnect.Api;

var liveAlgorithm = api.ReadLiveAlgorithm(projectId, deployId);
live_algorithm = api.ReadLiveAlgorithm(project_id, deploy_id)

The ReadLiveAlgorithm method returns a LiveAlgorithmResults object, which have the following attributes:

Asset Holdings

To view the asset holdings of a live algorithm, get the live trading results and then print the Holdings and Cash attributes.

foreach(var h in liveAlgorithm.LiveResults.Holdings.Values)
{
    Console.WriteLine($@"{h.Symbol} 
    Quantity: {h.Quantity} 
    Average Price: {h.AveragePrice} 
    Market Price: {h.MarketPrice}
    Market Value: {h.MarketValue}
    ");
}

foreach(var c in liveAlgorithm.LiveResults.Cash.Values)
{
    Console.WriteLine($"{c.Symbol}: {c.CurrencySymbol} {c.Amount}");
}

for h in live_algorithm.LiveResults.Holdings.Values:
    print(f"""{h.Symbol} 
    Quantity: {h.Quantity} 
    Average Price: {h.AveragePrice} 
    Market Price: {h.MarketPrice}
    Market Value: {h.MarketValue}
    """)
    
for c in live_algorithm.LiveResults.Cash.Values:
    print(f"{c.Symbol}: {c.CurrencySymbol} {c.Amount}")

Statistics

To view the statistics of a live algorithm, get the live trading results and then follow these steps:

  1. Print the Statistics attribute.
  2. foreach(var kvp in liveAlgorithm.LiveResults.Statistics)
    {
        Console.WriteLine($"{kvp.Key}: {kvp.Value}");
    }
    for kvp in live_algorithm.LiveResults.Statistics:
        print(f"{kvp.Key}: {kvp.Value}")
  3. Print the RuntimeStatistics attribute.
  4. foreach(var kvp in liveAlgorithm.LiveResults.RuntimeStatistics)
    {
        Console.WriteLine($"{kvp.Key}: {kvp.Value}");
    }
    for kvp in live_algorithm.LiveResults.RuntimeStatistics:
        print(f"{kvp.Key}: {kvp.Value}")
  5. Print the AlphaRuntimeStatistics attribute.
  6. foreach(var kvp in liveAlgorithm.LiveResults.AlphaRuntimeStatistics.ToDictionary())
    {
        Console.WriteLine($"{kvp.Key}: {kvp.Value}");
    }
    for kvp in live_algorithm.LiveResults.AlphaRuntimeStatistics.ToDictionary():
        print(f"{kvp.Key}: {kvp.Value}")

Charts

To view the charts of a live algorithm, get the live trading results and then follow these steps:

  1. Save the Charts attribute of the live algorithm result.
  2. var charts = liveAlgorithm.LiveResults.Charts;
    charts = live_algorithm.LiveResults.Charts
  3. Index the chart object with the chart name.
  4. var equityChart = charts["Strategy Equity"];
    equity_chart = charts["Strategy Equity"]
  5. Index the Series attribute of the charts with the series name.
  6. var equitySeries = equityChart.Series["Equity"].Values;
    equity_series = equity_chart.Series["Equity"].Values
  7. Iterate each data point of each series and store them in Dictionary objects.
  8. var equityData = equitySeries
                     .Where(kvp => kvp != null)
                     .ToDictionary(kvp => kvp.x, kvp => kvp.y);
    equity_data = {kvp.x: kvp.y for kvp in equity_series if kvp}
  9. Display the data.
  10. Console.WriteLine("Equity Curve:");
    foreach(var kvp in equityData.Take(5))
    {
        var time = DateTimeOffset.FromUnixTimeSeconds(kvp.Key).DateTime;
        Console.WriteLine($"{time} : {kvp.Value}");
    }
  11. Convert the dictionaries into a Series object.
  12. equity_data = pd.Series(equity_data)
  13. Call the plot on the pandas object.
  14. equity_data.plot(figsize=(10, 6), title="Equity Curve")
    plt.show()

Read Logs

To get the logs of a live algorithm, call the ReadLiveLogs method with the project ID and deployment ID.

var liveLogs = api.ReadLiveLogs(projectId, deployId);
liveLogs = api.ReadLiveLogs(project_id, deploy_id)

The ReadLiveLogs method accepts the following additional arguments so you can select the time period of the logs to return:

ArgumentData TypeDescription
startTimeDateTimedatetimeNo logs will be returned before this time
endTimeDateTimedatetimeNo logs will be returned after this time
var liveLogs = api.ReadLiveLogs(projectId: projectId, 
                                algorithmId: deployId,
                                startTime: new DateTime(2022, 1, 31),
                                endTime: new DateTime(2022, 2, 3));
liveLogs = api.ReadLiveLogs(projectId=project_id, 
                            algorithmId=deploy_id,
                            startTime=datetime(2022, 1, 31),
                            endTime=datetime(2022, 2, 3))

The ReadLiveLogs method returns a LiveLog object, which have the following attributes:

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: