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   

Viet Currency - Phan 21

  Trading technique with ADX        Trong western T/A nó có 4 cái indicators chính mà trên hầu hết các trading desks trên thế giới đều xài. Đó là: MACD, RSI, ADX, và gần đây nữa là CCI. Bên stocks thì MACD là dẫn đầu. MACD, nếu biết xài, thì có thể nói nó đúng chừng 80% trở lên trong US stocks. VN thì tôi không biết. RSI thì tôi không rành lém. Tôi thấy thằng Q đó quờ quạng làm sao ấy. Nhưng có rất nhiều người thề sống chít với nó. Và họ đều là cao thủ hơn tôi, nên tôi không dám chê thằng Q đó nhiều. Vì trong đời tôi gặp ít nhất là 5 cụ. Cụ ở đây có nghĩa là ít gì cũng 50 năm (yeah..50 years on the Street). Cụ nào cũng khen nó còn hơn thuốc Tiên. Có 2 lão dám nói là chỉ cần nhìn RSI thui, không cần nhìn giá mà vẫn trade được như thường. Anyway, thằng thứ 3 là ADX. ADX ở tại Mỹ, nhất là các trading desks mà tôi làm việc khi xưa, đều xài nó cho Currency market. Xin mở dấu hoặc ở đây để phân biệt được cái gọi là currency market và cái gọi là forex marke...

Viet Currency - Phan 22

    5. STOCHASTIC:  Anh VC cho Q hoỉ, nếu nhìn lại năm 2006, 2/8 là đáy, nhưng trong suốt xu hướng giảm giá Momentum giao động thấp nhất là -100. Còn VNI hiện tại, dù chưa giảm nhiều như vậy, nhưng Momentum đã <-100 và đang từ dưới đi lên, điều đó trong PTKT có phản ánh điều gì không?      Momentum nó khác với giá. Nó là sự thay đổi của giá QUA thời gian. Trong một khoảng thời gian nhất định nào đó nếu giá thay đổi nhiều thì momentum sẽ đi xuống và ngược lại. Tôi không xài momentum vì trong giai đoạn này tôi chỉ nhìn trend qua các điểm reactionary points để xác định hướng đi của nó. Khi trend được xác định rồi thì tôi mới xài momentum để dò la sức mạnh của nó. Như cái chart của tôi phía trên, nếu giá đi lên khỏi 1155 hay đi xuống điểm thấp nhất của VNI hôm tuần trước thì lúc đó mới lấy momentum ra đo. Vì lúc đó nó sẽ nói cho tôi biết cái break out (trên 1155) và cái break down có thật sự mạnh hay không? Còn bây giờ chỉ số này chỉ trade trong ...