Debugging Thuật Toán
Một lợi thế lớn của Local Platform là khả năng debug thuật toán trực tiếp trong VS Code với breakpoints và variable inspection.
Debug Python với debugpy
python
# main.py
import debugpy
def Initialize(self):
debugpy.listen(5678)
print("Waiting for debugger...")
debugpy.wait_for_client()
self.SetStartDate(2023, 1, 1)
self.SetCash(100000)
self.AddEquity("SPY", Resolution.Minute)
def OnData(self, data):
# Đặt breakpoint ở dòng này trong VS Code
if data.ContainsKey("SPY"):
price = data["SPY"].Close
print(f"SPY: {price}")Cấu Hình Launch trong VS Code
Tạo .vscode/launch.json:
json
{{
"version": "0.2.0",
"configurations": [
{{
"name": "Debug QuantConnect",
"type": "debugpy",
"request": "attach",
"connect": {{
"host": "localhost",
"port": 5678
}},
"pathMappings": [
{{
"localRoot": "${{workspaceFolder}}",
"remoteRoot": "/Lean/Launcher/bin/Debug"
}}
]
}}
]
}}Các Debug Technique
Logging — Dùng
self.Debug(),self.Log(),self.Error()Breakpoints — Dừng code tại dòng bất kỳ, inspect variables
Chart — Vẽ biểu đồ với
self.Plot("Series", value)Console — Xem log real-time khi debug
💡 Debug VN30F Custom Data
Khi debug VN30F với PythonData Reader, đặt breakpoint trong Reader() và OnData() để inspect dữ liệu đầu vào và tín hiệu.