Cơ sở Quanta & Bắt đầu · intermediate · 5 phút
Hàm: fn
Tái sử dụng logic với closure
#syntax
Định nghĩa hàm `fn`
Để tránh lặp lại mã, bạn có thể tạo hàm:
quanta
fn calm_market(atr_pct_max = 4pct) -> bool: atr_pct = atr(market.high, market.low, market.close, 14) / market.close return atr_pct < atr_pct_max act: when crossover(market.close, sma(market.close, 20)) and calm_market(): enter long with wnav 5pct
- Hàm có thể lấy trực tiếp các biến toàn cục (Closure).
- Khai báo
-> typelà tùy chọn để làm tài liệu (Type hint). - Hàm có thể trả về một giá trị Series.
Wiki liên quan