Quanta Docs

Tài liệu Quanta hoàn chỉnh cho Quant Studio

Bài họcrecipelesson/pivot-trendline-chart

Bài 6 — Pivot high + trendline trên chart (tránh lỗi valuewhen)

lastpivothigh.isnew trễ R bar; trendline từ .time/.price; .line/.slope; peak exit + mark.

Xem chi tiết lý do tại concept/ai-codegen-pitfalls. Pivot chỉ biết được sau R bar. Skeleton causal:

quanta
strategy "Pivot TL Demo"
    @regime     "trending"
    @style      "breakout, swing"
    @detail "Trendline từ đỉnh đã xác nhận; thoát khi chạm peak gần nhất."

params:
  pivot_lookback = 5 in [3..12]
  exit_lookback = 20 in [10..60]

detect:
  c = market.close
  h = market.high
  pk = lastpivothigh(h, pivot_lookback, pivot_lookback)
  ph_h = pk.price
  ph_t = pk.time
  p1_h = pk.prev_price
  p1_t = pk.prev_time
  current_tl = pk.line
  peak_exit = hhv(h, exit_lookback)
  buy_sig = c crossover current_tl
  sell_sig = h crossover peak_exit[1]

act:
  when buy_sig and not position.is_long:
    enter long with wnav 5pct tag "tl breakout"
  when sell_sig and position.is_long:
    close long tag "peak touch"

render:
  panel main:
    line peak_exit as "Recent peak" in orange dashed
    line current_tl as "Pivot line" in yellow
    trendline [p1_t, p1_h] to [ph_t, ph_h] in yellow with width 2 with extend right limit by 5
    mark pk.isnew as "Pivot" at above_bar in orange shaped circle
    mark buy_sig as "Long" at below_bar in lime shaped arrow_up
    mark sell_sig as "Exit" at above_bar in red shaped arrow_down
  • Tín hiệu pk.isnew xuất hiện trễ R bar so với bar đỉnh. Đó là lúc pivot được xác nhận, không phải lỗi.
  • Cần ít nhất 2 pivot đã xác nhận thì .slope và .line khác na.
  • .index là vị trí trong panel hiện tại. Cắt đầu panel làm số này đổi; .slope và .line tính theo số bar giữa hai pivot nên không đổi khi cả hai pivot còn trong cửa sổ.
  • Không dùng swinghigh trong when và không dùng valuewhen(ph, bar.time, 0).
#intermediate#plot#pivot#trendline#valuewhen
Trợ lý Quanta"Học nhanh hơn, hiệu quả hơn cùng AI"

Xin chào! Tôi là trợ lý học ngôn ngữ Quanta. Hỏi tôi về cú pháp, hàm, hoặc cách viết một chiến lược cụ thể (ví dụ: "Làm sao để xác định đỉnh đáy để vẽ trendline?").