Writing Algorithms
Logging
Introduction
Algorithms can record string messages ('log statements') to a file for analysis after a backtest is complete, or as a live algorithm is running. These records can assist in debugging logical flow errors in the project code. Consider adding them in the code block of an if
statement to signify an error has been caught.
It's good practice to add logging statements to live algorithms so you can understand its behavior and keep records to compare against backtest results. If you don't add logging statements to a live algorithm and the algorithm doesn't trade as you expect, it's difficult to evaluate the underlying problem.
Log Messages
Log statements are added to the log file while your algorithm continues executing. Logging dataset information is not permitted. Use Log
log
method statements to debug your backtests and live trading algorithms.
If you execute algorithms in QuantConnect Cloud, log length is capped by organization tier. If your organization hits the daily limit, contact us.
If you log the same content multiple times, only the first instance is added to the log file. To bypass this rate-limit, add a timestamp to your log messages.
For live trading, 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.
To record the algorithm state when the algorithm stops executing, add log statements to the OnEndOfAlgorithm
on_end_of_algorithm
event handler.
Log("My log message");
self.log("My log message")
Debug Messages
Debug statements are the same as log statements, but Debug
debug
method 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
error
method 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
quit
method 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
method.
Quit("My quit message");
self.quit("My quit message")
Get Logs
The following tables describe how to access the logs of your backtests and live algorithms:
Deployment Target | Execution Mode | Access Tools |
---|---|---|
QuantConnect Cloud | Backtest |
|
QuantConnect Cloud | Live Trading |
|
Local | Backtest |
|
Local | Live Trading |
|