Bề mặt Import và các Khai báo
Khối import chuẩn, tất cả các tên có thể import, và các function khai báo indicator / strategy / library.
Bề mặt Import#
from source import (
strategy,
indicator,
library,
input,
ta,
request,
array,
barstate,
log,
plot,
plotbar,
plotcandle,
plotshape,
plotchar,
plotarrow,
barcolor,
fill,
bgcolor,
hline,
line,
label,
box,
polyline,
linefill,
table,
gfx,
atk,
chart,
na,
nz,
var,
varip,
set_var,
alert,
alertcondition,
library_import,
import_library,
)Enum#
Các enum tùy chọn có sẵn để import tường minh: Location, Position, Shape, Size, Weight.
Các hàm hỗ trợ nâng cao#
get_pynescript_module_metadata(module), build_pynescript_visual_snapshot(...), compile_pynescript_module_classes(module).
Định danh script#
Luôn đặt # @name: my_script hoặc __script_name__ = "my_script" để định danh publish/update được ổn định.
indicator, strategy, library#
indicator(title: str, **kwargs) -> dict#
Đăng ký metadata của indicator vào module context và kiểm soát liệu compiled wrapper trở thành overlay trên chart chính hay indicator trên sub-panel.
Hành vi ATK: overlay=True biên dịch thành BaseIndicator. overlay=False biên dịch thành BaseSubIndicator.
Các kwargs quan trọng: overlay, shorttitle, precision, timeframe, description, default_styles, behind_chart, explicit_plot_zorder, scale, max_lines_count, max_boxes_count, max_labels_count, max_polylines_count, max_bars_back.
strategy(title: str, **kwargs) -> dict#
Đăng ký metadata của strategy và bảo toàn các cờ liên quan đến thực thi cho chẩn đoán và hành vi runtime.
Lưu ý hiện tại: process_orders_on_close có hành vi runtime thực tế mạnh nhất hiện nay. Ngữ nghĩa cấp tick vẫn còn chưa đầy đủ.
Các kwargs quan trọng: overlay, shorttitle, precision, calc_on_every_tick, calc_on_order_fills, process_orders_on_close, fill_orders_on_standard_ohlc, max_bars_back, timeframe, description, default_styles.
library(title: str, **kwargs) -> dict#
Đăng ký khai báo library có thể publish mà các function được export được phát hiện từ các callable của module được định nghĩa trong cùng file. Library sau đó có thể được tải bằng library_import(...) hoặc import_library(...).
Các kwargs hữu ích: version, overlay, description.
Cách chọn#
- Sử dụng
indicator(...)khi đầu ra chính của script là phân tích trực quan. Nếu script không phát ra các trường trade được ánh xạ, hãy giữ nó là indicator ngay cả khi nó chứa gợi ý mua/bán. - Sử dụng
strategy(...)khi script cần tạo ra các trường entry và risk được ánh xạ mà pipeline thực thi ATK có thể tiêu thụ. - Sử dụng
library(...)khi bạn muốn các hàm tính toán tái sử dụng, các hàm xác thực, hoặc logic tín hiệu dùng chung mà nhiều script import.
Tham chiếu parameter khai báo#
| Parameter | Nơi quan trọng | Cách chọn |
|---|---|---|
title | Định danh script hiển thị cho người dùng | Chọn tên dễ đọc mà người dùng sẽ thấy trong menu và danh sách script. Giữ ổn định sau khi publish. |
overlay | Vị trí panel | True cho các hình ảnh trên chart giá. False cho oscillator và indicator sub-panel. |
shorttitle | Giao diện UI nhỏ gọn | Sử dụng khi tiêu đề đầy đủ quá dài. Giữ cho dễ nhận biết. |
precision | Định dạng số hiển thị | Phù hợp với đơn vị người dùng quan tâm. Bốn chữ số thập phân cho FX/crypto; không hoặc hai cho số đếm hoặc tóm tắt dashboard. |
timeframe | Metadata khai báo | Chỉ đặt khi cố ý gắn với một timeframe cao hơn cụ thể. Để trống nếu script nên thích ứng với chart đang hoạt động. |
description | Rõ ràng khi publish và bảo trì | Một câu giải thích script tính toán gì và người dùng mong đợi thấy gì. |
default_styles | Giao diện trực quan ban đầu | Sử dụng cho style khởi đầu dự đoán được trên các script nhiều plot. Giữ thay đổi style ở mức thẩm mỹ. |
behind_chart | Phân lớp overlay | Sử dụng cho các hình ảnh kiểu nền như vùng và đổ bóng. |
explicit_plot_zorder | Thứ tự plot overlay | Bật khi thứ tự line quan trọng — band trên/dưới, fill xếp chồng. |
max_*_count | Script nhiều đối tượng | Chỉ tăng khi script thực sự tạo nhiều line, label, box hoặc polyline. |
max_bars_back | Độ sâu warmup | Đặt ít nhất bằng lookback dài nhất script cần, cộng thêm biên an toàn. |
process_orders_on_close | Thời điểm thực thi strategy | Sử dụng cho strategy bar-close nơi tín hiệu chuyển thành order sau khi bar hoàn tất. |
calc_on_every_tick | Metadata ý định strategy | Coi như metadata nâng cao; không sử dụng làm lối tắt để sửa logic bar-close. |
Quy tắc tác giả: chọn khai báo theo trách nhiệm runtime, không phải theo giao diện trực quan. Một script vẽ mũi tên vẫn là indicator nếu nó không phát ra các trường trade được ánh xạ.
Ví dụ khai báo indicator#
from source import indicator, input, ta, plot
indicator(
"EMA Overlay",
overlay=True,
precision=4,
explicit_plot_zorder=True,
max_lines_count=5,
)
length = input.int(21, title="Length", key="length", minval=1)
plot("ema", key="ema_line", title="EMA", color="#00c853", width=2)
def build_indicator_frame(df, params):
frame = df.copy().reset_index(drop=True)
frame["ema"] = ta.ema(frame["close"], int(params.get("length", length)))
return frameKhai báo strategy và library#
from source import build_mapped_trade_frame, import_library, input, strategy
# strategy(...) được chọn vì script này sẽ phát ra các trường thực thi trade-frame chuẩn.
strategy("EMA Cross Strategy", overlay=True, process_orders_on_close=True, max_bars_back=240)
fast_period = input.int(10, title="Fast EMA", key="fast_period", minval=1)
slow_period = input.int(24, title="Slow EMA", key="slow_period", minval=2)
# import_library(...) chỉ được sử dụng khi logic cần tái sử dụng giữa các script.
shared = import_library("Annotated Utility Library@1", exports=["clamp_length"])
safe_fast = shared.clamp_length(fast_period, 1)
def build_signal_frame(df, params=None):
...
def build_trade_frame(signal_df, params=None, styles=None):
return build_mapped_trade_frame(signal_df)