本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jianminchen
//@version=6
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jianminchen
indicator("Uptrend Body Engulfing Two Downtrends", overlay=true)
// 1. Define directional bars based on close vs open
is_uptrend = close > open
is_downtrend = close < open
// 2. Calculate the absolute body height of a candle
body_height = math.abs(close - open)
// 3. Track the body heights of the two most recent historical downtrend bars
// Using ta.valuewhen to fetch the exact body height of downtrend bars across history
down_body1 = ta.valuewhen(is_downtrend, body_height, 0) // Most recent downtrend body
down_body2 = ta.valuewhen(is_downtrend, body_height, 1) // Second most recent downtrend body
// 4. Combined total of the last two downtrend bodies
total_down_bodies = down_body1 + down_body2
// 5. Condition: Current uptrend body is greater than the sum of the last two downtrend bodies
trigger_condition = is_uptrend and (body_height > total_down_bodies)
// 6. Visual Anchors on Chart
// Plot a shape below the signal bar
plotshape(trigger_condition, title="Uptrend Signal", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.normal)
// Optional: Highlight the background color when the condition is met
bgcolor(trigger_condition ? color.new(color.green, 90) : na)
// 7. Alert Setup
alertcondition(trigger_condition, title="Uptrend Body > Preceding 2 Downtrends", message="Uptrend bar body height exceeds the total of the previous 2 downtrend bodies!")