Writing Algorithms

Logging

Introduction

Warning: file_get_contents(/tmp/docs/0fce5c00b4b40fac175e7db5477a2a2d1db5a24c/Resources/logging-statements/introduction.html): failed to open stream: No such file or directory in /tmp/docs/0fce5c00b4b40fac175e7db5477a2a2d1db5a24c/02 Writing Algorithms/38 Logging/01 Introduction.php on line 1

Log Messages

Log statements are added to the log file while your algorithm continues executing. Logging dataset information is not permitted. Use Log statements to debug your backtests and live trading algorithms.

If you execute algorithms in QC Cloud, log length is capped by organization tier. If your organization hits the daily limit, contact us. The log files of each cloud project can store up to 100,000 lines for up to one year. If you log more than 100,000 lines or some lines become older than one year, we remove the oldest lines in the files so your project stays within the quota.

If you log the same content multiple times, only the first instance is added to the log file. To bypass this rate-limiting, add a timestamp to your log messages.

To record the algorithm state when the algorithm stops executing, add log statements to the OnEndOfAlgorithm event handler.

Log("My log message");
self.Log("My log message")

Debug Messages

Debug statements are the same as log statements, but Debug statements are orange in the Cloud Terminal. Use these statements when you want to give more attention to a message in the Cloud Terminal. Debug messages can be up to 200 characters in length. If you send multiple debug statements within 1 second, your messages are rate-limited to avoid crashing your browser.

Debug("My debug message");
self.Debug("My debug message")

Error Messages

Error statements are the same as log statements, but Error statements are displayed in red text in the Cloud Terminal. Use these statements when you want to give the most attention to a message in the Cloud Terminal. Error statements are rate-limited like debug statements.

Error("My error message");
self.Error("My error message")

Quit Messages

Quit statements cause your project to stop running and may log some data to the log file and Cloud Terminal. These statements are orange in the Cloud Terminal. When you call the Quit method, the program continues executing until the end of the method definition. If you want to quit execution immediately, return after you call Quit.

Quit("My quit message");
self.Quit("My quit message")

Runtime Statistics

Runtime statistics show the performace of your algorithm at a single moment in time.

The following table describes the default runtime statistics:

StatisticDescription
CapacityThe maximum amount of money an algorithm can trade before its performance degrades from market impact.
EquityThe total portfolio value if all of the holdings were sold at current market rates.
FeesThe total quantity of fees paid for all the transactions.
HoldingsThe absolute sum of the items in the portfolio.
Net ProfitThe dollar-value return across the entire trading period.
PSRThe probability that the estimated Sharpe ratio of an algorithm is greater than a benchmark (1).
ReturnThe rate of return across the entire trading period.
UnrealizedThe amount of profit a portfolio would capture if it liquidated all open positions and paid the fees for transacting and crossing the spread.
VolumeThe total value of assets traded for all of an algorithm's transactions.

The capacity statistic is only available for live algorithms.

To add a custom runtime statistic, call the SetRuntimeStatistic method with a name and value. The value argument can be a string or a number.

SetRuntimeStatistic(name, value);
self.SetRuntimeStatistic(name, value)

Access Logs and Statistics

The following table describes where you can access the logs and runtime statistics, depending on how to deploy your algorithms:

LocationAlgorithm Lab AlgorithmsCLI Cloud AlgorithmsCLI Local Algorithms
Backtest results pagegreen checkgreen check
Live results pagegreen checkgreen check
ReadBacktestgreen checkgreen check
ReadLivegreen checkgreen check
Local log file in your <projectName>/backtests/<timestamp> or <projectName>/live/<timestamp> directorygreen checkgreen check

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: