Skip to main content

CTS-Entry point- reaction line

 Of course. This is a significant and interesting evolution of the strategy. You are now moving from pure indicator-based entries to a sophisticated price action pattern: trading breakouts from consolidation within an established trend.

I have translated this new logic into the Pine Script strategy below.

Analysis of Your New Rules

First, let's analyze the logic, as it's very important to understand what we are telling the code to do.

  1. "Draw green line for higher low reaction, and red line for lower high reaction."

    • This means we need to programmatically identify the most recent significant swing low (our green line) and swing high (our red line). We will use Pine Script's ta.pivotlow() and ta.pivothigh() functions to do this.

  2. "If red line above green line... it like a triangle formation. So we can't entry."

    • This is the core of your new filter. You are identifying a period of price consolidation or contraction, where the price is making a lower high (the red line) and a higher low (the green line). The condition Red Line Price > Green Line Price correctly defines this pattern. We will only look for entries when this condition is true.

  3. "Only entry when green line above redline."

    • There is a slight logical contradiction here. A "low" (green line) can never be physically above a "high" (red line) on a chart.

    • My Interpretation (and how it's coded): I believe what you mean is that you want to enter when the price breaks out of the consolidation pattern. Your next rule confirms this:

  4. "We will buy at previous red line level, and sell at previous green line."

    • This is classic breakout logic.

    • Buy Entry: If the trend is up and the market is consolidating (Red Line > Green Line), you place an order to buy exactly when the price breaks above the previous high (the Red Line).

    • Sell Entry: If the trend is down and the market is consolidating, you place an order to sell exactly when the price breaks below the previous low (the Green Line).

This is a powerful concept. The strategy will now ignore all other signals and wait patiently for a trend-aligned consolidation pattern, then place an order to enter on the breakout.


Pine Script Strategy: Breakout from Consolidation

This script replaces the previous entry logic with your new price action rules. The trend identification and exit rules remain the same as before.

Apply this script to the H1 chart.

Generated pinescript
      // © PineCoders
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version=5
strategy("Price Action Breakout Strategy", overlay=true, initial_capital=10000, default_qty_value=100, default_qty_type=strategy.fixed)

// —————— 1. INPUTS ——————
// -- Moving Averages --
ma_fast_len = input.int(9, "Fast MA Length", group="MA")
ma_mid_len = input.int(14, "Mid MA Length", group="MA")
ma_slow_len = input.int(60, "Slow MA Length", group="MA")

// -- Pivot Detection for Price Action --
pivot_left_bars = input.int(5, "Pivot Left Bars", group="Pivots")
pivot_right_bars = input.int(5, "Pivot Right Bars", group="Pivots")

// -- Stochastic (For Exit Condition) --
stoch_len = input.int(14, "Stochastic Length", group="Stochastic")
stoch_k_smooth = input.int(3, "Stochastic %K Smoothing", group="Stochastic")
stoch_d_smooth = input.int(3, "Stochastic %D Smoothing", group="Stochastic")
stoch_ob = input.int(65, "Stochastic Overbought Level", group="Stochastic")
stoch_os = input.int(35, "Stochastic Oversold Level", group="Stochastic")

// —————— 2. INDICATOR CALCULATIONS ——————
// -- H1 Indicators (Current Timeframe) --
ma_fast_h1 = ta.sma(close, ma_fast_len)
ma_mid_h1 = ta.sma(close, ma_mid_len)
ma_slow_h1 = ta.sma(close, ma_slow_len)
stoch_k = ta.sma(ta.stoch(close, high, low, stoch_len), stoch_k_smooth)

// -- H4 Indicators (Higher Timeframe Data) --
[h4_close, h4_ma_fast, h4_ma_mid, h4_ma_slow] = request.security(syminfo.tickerid, "240", [close, ta.sma(close, ma_fast_len), ta.sma(close, ma_mid_len), ta.sma(close, ma_slow_len)])

// —————— 3. TREND & PATTERN DEFINITIONS ——————
// -- Major Trend (H4) --
major_uptrend = (h4_ma_fast > h4_ma_mid and h4_ma_mid > h4_ma_slow) or (h4_close > h4_ma_fast and h4_close > h4_ma_mid and h4_close > h4_ma_slow)
major_downtrend = (h4_ma_fast < h4_ma_mid and h4_ma_mid < h4_ma_slow) or (h4_close < h4_ma_fast and h4_close < h4_ma_mid and h4_close < h4_ma_slow)

// -- Pivot Detection for Reaction Points (LH and HL) --
pivot_high_price = ta.pivothigh(high, pivot_left_bars, pivot_right_bars)
pivot_low_price = ta.pivotlow(low, pivot_left_bars, pivot_right_bars)

// -- Store the most recent pivot levels (Our Red and Green Lines) --
var float last_lh_price = na // Red Line for Lower High
var float last_hl_price = na // Green Line for Higher Low

if (not na(pivot_high_price))
    last_lh_price := pivot_high_price

if (not na(pivot_low_price))
    last_hl_price := pivot_low_price
    
// -- Consolidation Filter (The "Triangle") --
// This is TRUE if the last identified Lower High is above the last identified Higher Low.
in_consolidation_pattern = not na(last_lh_price) and not na(last_hl_price) and last_lh_price > last_hl_price

// —————— 4. ENTRY CONDITIONS (NEW LOGIC) ——————
// We now set up a breakout entry if the trend is aligned AND we are in a consolidation pattern.
// NOTE: We will use STOP orders to enter on the breakout, not market orders.

// -- Conditions to place a BUY STOP order --
if (major_uptrend and in_consolidation_pattern and strategy.position_size == 0)
    strategy.entry("Long", strategy.long, stop=last_lh_price, comment="Place Buy Stop")

// -- Conditions to place a SELL STOP order --
if (major_downtrend and in_consolidation_pattern and strategy.position_size == 0)
    strategy.entry("Short", strategy.short, stop=last_hl_price, comment="Place Sell Stop")
    
// -- Cancel pending orders if the trend changes before the breakout occurs --
if (not major_uptrend and strategy.opentrades.entry_id(0) == "Long")
    strategy.cancel("Long")

if (not major_downtrend and strategy.opentrades.entry_id(0) == "Short")
    strategy.cancel("Short")

// —————— 5. EXIT CONDITIONS (Unchanged from before) ——————
// -- Exit Long Position if ANY of these are true --
exit_long_rule1 = ma_fast_h1 < ma_mid_h1 and ma_mid_h1 < ma_slow_h1
exit_long_rule2 = h4_close < h4_ma_fast and h4_close < h4_ma_mid
exit_long_rule3 = stoch_k > stoch_ob and close > ma_fast_h1 and close > ma_mid_h1
if (exit_long_rule1 or exit_long_rule2 or exit_long_rule3)
    strategy.close("Long", comment="Exit Long")

// -- Exit Short Position if ANY of these are true --
exit_short_rule1 = ma_fast_h1 > ma_mid_h1 and ma_mid_h1 > ma_slow_h1
exit_short_rule2 = h4_close > h4_ma_fast and h4_close > h4_ma_mid
exit_short_rule3 = stoch_k < stoch_os and close < ma_fast_h1 and close < ma_mid_h1
if (exit_short_rule1 or exit_short_rule2 or exit_short_rule3)
    strategy.close("Short", comment="Exit Short")

// —————— 6. PLOTTING ——————
// -- Plot MAs --
plot(ma_fast_h1, "Fast MA H1", color=color.blue)
plot(ma_mid_h1, "Mid MA H1", color=color.orange)
plot(ma_slow_h1, "Slow MA H1", color=color.gray)

// -- Plot the Consolidation Lines (Red for LH, Green for HL) --
plot(last_lh_price, "Lower High (Red Line)", color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(last_hl_price, "Higher Low (Green Line)", color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)

// -- Background Color for Major Trend --
bgcolor(major_uptrend ? color.new(color.green, 90) : major_downtrend ? color.new(color.red, 90) : color.new(color.gray, 90))
    

How This Script Works Differently

  • Pending Orders: Instead of entering the market immediately (strategy.entry("Long", strategy.long)), this script now places pending stop orders (strategy.entry("Long", strategy.long, stop=last_lh_price)).

  • Patience: The strategy will now wait on the sidelines until a major_uptrend (or downtrend) and an in_consolidation_pattern are both true. When they are, it will place an order and wait for the price to hit the breakout level.

  • Visual Lines: The script now plots the red and green lines for you on the chart. They will look like horizontal lines that extend forward in time and will only update when a new, confirmed pivot is detected. This allows you to see the exact consolidation pattern the script is trading.

  • Cancellation Logic: If the trend changes while an order is pending (e.g., the H4 trend flips from up to down while a buy stop is waiting), the script will automatically cancel that pending order to prevent a bad entry.

This revised strategy is much more aligned with how a discretionary price action trader might think, and it should be very interesting to backtest.

Comments

Popular posts from this blog

Viet Currency - Phan 20

  BASIC INDICATORS   

[VC20] Momentum indicator RSI, ADX

  I. Nguyên Tắc Nền Tảng: Xu Hướng Là Vua Bài Học Sống Còn:  Nguyên tắc quan trọng nhất là phải xác định và đi theo  xu hướng chính (Trend) . Công việc của nhà giao dịch là "dò sóng" và "nương theo sóng", không phải chống lại nó. Tránh Bắt Đỉnh, Dò Đáy:  Đừng cố gắng tìm điểm mua thấp nhất (bottom) và điểm bán cao nhất (top). Thay vào đó, hãy tập trung kiếm lợi nhuận ở  "khúc giữa" của xu hướng  để đảm bảo sự an toàn và bền vững. II. Định Nghĩa Cốt Lõi: Phân Biệt Rõ Trend và Momentum Trend (Xu hướng):  Là  hướng đi  của thị trường (lên, xuống, hoặc đi ngang). Đây là yếu tố quyết định cho việc mua hay bán. Momentum (Động lượng):  Là  Rate of Change  (Tốc độ/Cường độ thay đổi) của giá. Nó được dùng để đo lường  SỨC MẠNH (Strength)  của giá, chứ  không thể dùng để đo hướng đi . III. Cách Sử Dụng Các Chỉ Báo Kỹ Thuật Một Cách Hiệu Quả Luô...

VietCurrency Lesson - Summary version

  Contents LESSON 1 . 1 LESSON 2 . 4 LESSON 3 . 7 LESSON 4 . 10 LESSON 5 . 13 LESSON 6 . 16 LESSON 7 . 18 LESSON 8 . 21     LESSON 1 TÓM TẮT KIẾN THỨC PHÂN TÍCH KỸ THUẬT & THỊ TRƯỜNG (MARKET ANALYSIS) 1. PHÂN LOẠI CHỈ BÁO KỸ THUẬT Các chỉ báo kỹ thuật thường dùng trong trading được chia làm 6 nhóm chính: 1. Chỉ báo biến động (Volatility Indicators) Đo mức độ dao động giá/lợi suất: ATR (Average True Range), Bollinger Bands, Std Deviation, Chalkin's Volatility v.v. 2. Chỉ báo xung lượng (Momentum Indicators) Đo tốc độ, sức mạnh, động lực giá: RSI, CCI, MACD, Stochastic, Williams %R, Momentum v.v. 3. Chỉ báo chu kỳ (Cycle Indicators) Nhận diện tính chu kỳ chuyển động giá: Fibonacci, Detrended Oscillator, Cycle Lines… 4. Chỉ báo cường độ thị trường (Market Strength) Đặc biệt quan tâm đến volume, lực mua bán và các dòng vốn: OBV, MFI, Accumulation/Distribution, Chaikin Mo...