OPEN-SOURCE SCRIPT

Marubozu & Engulfing Analyzer with History

64
//version=5
indicator("Marubozu & Engulfing Analyzer with History", overlay=true, shorttitle="ME-History", precision=2)

// ———— 1. Inputs ————
int lookbackPeriod = input.int(50, "Historical Analysis Period", minval=20)
int trendPeriod = input.int(200, "Trend EMA Period", minval=50)
float volumeMultiplier = input.float(1.5, "Volume Threshold", step=0.1)

// ———— 2. Historical Context Variables ————
var float bullishSuccessRate = na
var float bearishSuccessRate = na
var int marubozuCount = 0
var int engulfingCount = 0

// ———— 3. Core Pattern Detection ————
// Marubozu (100% body, no wicks)
bullishMarubozu = (high == close) and (low == open) and (close > open)
bearishMarubozu = (high == open) and (low == close) and (close < open)

// Engulfing (daily resolution for accuracy)
prevOpen = request.security(syminfo.tickerid, "D", open[1])
prevClose = request.security(syminfo.tickerid, "D", close[1])

bullishEngulfing = (close > open) and (prevClose < prevOpen) and
(open < prevClose) and (close > prevOpen) and
(volume > ta.sma(volume, 20) * volumeMultiplier)

bearishEngulfing = (close < open) and (prevClose > prevOpen) and
(open > prevClose) and (close < prevOpen) and
(volume > ta.sma(volume, 20) * volumeMultiplier)

// ———— 4. Trend & Historical Analysis ————
trendEMA = ta.ema(close, trendPeriod)
isUptrend = close > trendEMA

// Analyze past pattern performance
if bullishMarubozu or bullishEngulfing
marubozuCount += 1
success = close[1] > close[5] ? 1 : 0 // Check if price rose within 5 bars
bullishSuccessRate := (na(bullishSuccessRate) ? success : (bullishSuccessRate*0.9 + success*0.1))

if bearishMarubozu or bearishEngulfing
engulfingCount += 1
success = close[1] < close[5] ? 1 : 0 // Check if price fell within 5 bars
bearishSuccessRate := (na(bearishSuccessRate) ? success : (bearishSuccessRate*0.9 + success*0.1))

// ———— 5. Generate Filtered Signals ————
buySignal = (bullishMarubozu or bullishEngulfing) and isUptrend and (bullishSuccessRate > 0.65)
sellSignal = (bearishMarubozu or bearishEngulfing) and not isUptrend and (bearishSuccessRate > 0.65)

// ———— 6. Visual Output (Fixed Style Parameters) ————
plotshape(buySignal, title="Buy", location=location.belowbar,
color=color.green, style=shape.triangleup, size=size.small) // ✅ Valid style: triangleup

plotshape(sellSignal, title="Sell", location=location.abovebar,
color=color.red, style=shape.triangledown, size=size.small) // ✅ Valid style: triangledown

// ... (rest of the code remains unchanged)

כתב ויתור

המידע והפרסומים אינם אמורים להיות, ואינם מהווים, עצות פיננסיות, השקעות, מסחר או סוגים אחרים של עצות או המלצות שסופקו או מאושרים על ידי TradingView. קרא עוד בתנאים וההגבלות.