| name | code-quality |
| description | Use when writing, reviewing, or refactoring QuantConnect/LEAN algorithm code for style correctness. Triggers: new algorithm code, code review, cleanup, "fix code style", "review code quality", "clean up the algorithm", redundant imports, subscription variable usage, comment format, blank-line rules, multi-line boolean layout, or catch-all error handling. Skip when only debugging runtime behavior or performance, unless the fix also changes style.
|
QuantConnect Algorithm Code Quality
Apply these rules to Main.cs and custom data classes. The goal is compiling code that does not hide real failures.
Imports
Preserve template using statements. Add or remove them only when a compile error proves one is needed.
Subscription Variables
Store the Symbol returned by AddData(...).Symbol or AddEquity(...).Symbol. Pass the Symbol directly.
private Security _customBtc;
private Symbol _customSymbol;
_customSymbol = AddData<MyData>("TICKER", Resolution.Daily).Symbol;
Use Slice.ContainsKey(symbol) or slice.Get(symbol) consistently with the stored Symbol.
Comments
Use comments to clarify intent, not restate code. Full-line comments start with // plus a space, use sentence form, and start with a capital letter.
var x = 1;
var x = 1;
Multi-Line Boolean Expressions
Put && or || at the beginning of each continued line.
Blank Lines
Use one blank line between methods, no blank lines inside method bodies, and standard C# brace style.
Error Handling
Do not wrap algorithm logic in try / catch blocks. Exceptions reveal invalid state, missing data, and incorrect assumptions; catch-all handlers make bugs invisible.
try
{
SetHoldings(_spy, targetWeight);
}
catch
{
}
if (targetWeight != 0)
{
SetHoldings(_spy, targetWeight);
}
Checklist
- Template using statements are preserved.
- Stored subscription variables are Symbol values.
- Comments follow language-specific rules.
- Multi-line boolean expressions follow language-specific continuation style.
- Blank lines match language rules.
- Algorithm code has no
try / catch; it uses explicit guards for expected conditions.