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:
Analysis of Your New Rules
"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.
"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 orcontraction , 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.
"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 pricebreaks out of the consolidation pattern. Your next rule confirms this:
"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 breaksabove 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 breaksbelow the previous low (the Green Line).
Pine Script Strategy: Breakout from Consolidation
// © 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 placespending 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 areboth 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.
Comments
Post a Comment