Chuyển đến nội dung chính
ATK Pine Script®

Tham chiếu parameter strategy()

Tham chiếu cho tất cả parameter khai báo strategy() trong ATK PyneScript V6 — overlay, process_orders_on_close, slippage, commission, margin, pyramiding, và hơn nữa.

Khai báo strategy()#

Lệnh gọi strategy() khai báo rằng module là một script thực thi. Nó đăng ký tên script, thiết lập cờ runtime, và điều khiển các parameter mô phỏng backtesting chân thực.

from source import strategy

strategy(
    "My Strategy Name",
    overlay=True,
    process_orders_on_close=True,
    max_bars_back=240,
)

Các parameter cốt lõi#

ParameterKiểuMặc địnhMô tả
titlestrbắt buộcTên strategy dễ đọc hiển thị trong giao diện nền tảng.
overlayboolFalseTrue render output trên chart giá. False tạo sub-panel riêng.
process_orders_on_closeboolFalseCờ thời gian thực tế nhất trong runtime hiện tại. Dùng True cho đại đa số strategy.
max_bars_backint240Lookback tối đa strategy cần. Đặt đủ cao cho phép tính TA dài nhất.
precisionint4Độ chính xác thập phân dùng trong hiển thị nền tảng.

Các parameter mô phỏng chân thực#

Các parameter này điều khiển độ chính xác mô phỏng backtesting. Mỗi parameter có một script ví dụ riêng để giữ việc dạy tập trung.

ParameterKiểuMô tảFile ví dụ
slippagefloatMô phỏng fill chân thực — mô hình hóa spread hoặc tác động thị trường khi entry.pynescript_slippage_strategy.py
commission_typestrCơ sở phí: "percent", "cash", hoặc "cash_per_contract".pynescript_commission_strategy.py
commission_valuefloatSố tiền phí, được diễn giải theo commission_type.pynescript_commission_strategy.py
default_qty_typestr"fixed" hoặc "percent_of_equity" — điều khiển cách default_qty_value được diễn giải.pynescript_margin_strategy.py
default_qty_valuefloatKích thước position mặc định khi quantitysize_pct không được đặt theo từng hàng.pynescript_margin_strategy.py
margin_longfloatTỷ lệ margin cho position long dưới dạng thập phân (ví dụ 0.1 = 10% margin).pynescript_margin_strategy.py
margin_shortfloatTỷ lệ margin cho position short.pynescript_margin_strategy.py
pyramidingintSố lượng entry bổ sung tối đa được phép theo cùng hướng. Mặc định 1.pynescript_pyramiding_strategy.py
backtest_fill_limits_assumptionboolKhi True, giả định limit order được fill tại giá limit trong backtesting.pynescript_limit_fill_assumption_strategy.py

Ví dụ mô phỏng chân thực#

Slippage#

from source import strategy

strategy("Slippage Demo", overlay=True, process_orders_on_close=True, slippage=0.5)

Commission#

strategy(
    "Commission Demo",
    overlay=True,
    process_orders_on_close=True,
    commission_type="percent",
    commission_value=0.05,
)

Margin và kích thước theo phần trăm#

strategy(
    "Margin Demo",
    overlay=True,
    process_orders_on_close=True,
    default_qty_type="percent_of_equity",
    default_qty_value=10.0,
    margin_long=0.1,
    margin_short=0.1,
)

Pyramiding#

strategy(
    "Pyramiding Demo",
    overlay=True,
    process_orders_on_close=True,
    pyramiding=3,
)

Mô phỏng chân thực cũng nằm trong khai báo. Coi slippage, commission_type, commission_value, margin_long, margin_short, pyramiding, và backtest_fill_limits_assumption là các tính năng soạn thảo thực sự, không phải cờ runtime ẩn.

strategy_alert_message()#

Đăng ký một chuỗi template cho alert strategy. Dùng token {{...}} mà bề mặt phân phối alert ATK thay thế tại runtime.

from source import strategy, strategy_alert_message

strategy("Alert Template Strategy", overlay=True, process_orders_on_close=True)
strategy_alert_message("{{ticker}} {{strategy.order.id}} {{strategy.order.action}}")

Văn bản alert theo từng hàng được đặt qua cột alert_message trong build_signal_frame:

frame["alert_message"] = np.where(buy_signal | sell_signal, "ema crossover fired", "")

Tải ví dụ template alert strategy

Xem thêm: