ProRealCode - Trading & Coding with ProRealTime™
Hello,
I need help to convert this to prorealtime.
// This Pine Script™ code is subject to the terms of the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © EliCobra
//@version=6
// Pine v6 compatibility and first-bar safety fixes.
indicator("Zacs MACD Heikin-Ashi OB/OS", "Zacs MACD Heikin-Ashi OB/OS", false)
type bar
float o = na
float h = na
float l = na
float c = na
type osc
bar b = na
float s = na
float h = na
type alerts
bool s = false
bool b = false
bool u = false
bool d = false
bool p = false
bool n = false
bool x = false
bool y = false
method src(bar b, simple string src) =>
float x = switch src
'open' => b.o
'high' => b.h
'low' => b.l
'close' => b.c
'oc2' => math.avg(b.o, b.c )
'hl2' => math.avg(b.h, b.l )
'hlc3' => math.avg(b.h, b.l, b.c )
'ohlc4' => math.avg(b.o, b.h, b.l, b.c)
'hlcc4' => math.avg(b.h, b.l, b.c, b.c)
x
// Pine v6: calculate HA candle history using numeric series, not mutable UDT
// references. This also handles the first bar, where no previous HA candle exists.
method ha(bar b, simple bool p = true) =>
float haClose = b.src('ohlc4')
var float haOpen = na
haOpen := na(haOpen[1]) ? b.src('oc2') : math.avg(haOpen[1], haClose[1])
float haHigh = math.max(b.h, math.max(haOpen, haClose))
float haLow = math.min(b.l, math.min(haOpen, haClose))
bar x = bar.new(haOpen, haHigh, haLow, haClose)
p ? x : b
method stmc(bar b, simple string src, simple int flen, simple int slen, simple string sig, simple int clen) =>
float x = (ta.ema(b.src(src), flen) -
ta.ema(b.src(src), slen)) /
(ta.ema(b.h - b.l , slen)) * 100
bar z = bar.new(
x[1] ,
math.max(x, x[1]),
math.min(x, x[1]),
x ).ha()
float s = ta.ema(z.src(sig), clen)
osc.new(z, s, z.c - s)
const string gm = "MACD Settings", const string gu = "UI Options"
src = input.string('close' , "Source" , ['open', 'high', 'low', 'close', 'hl2', 'hlc3', 'ohlc4', 'hlcc4'], group = gm)
fast = input.int (12 , "Fast Length" , group = gm)
slow = input.int (26 , "Slow Length" , group = gm)
sigb = input.bool (true , "Signal" , inline = '1', group = gm)
sigs = input.string('close' , "Source" , ['open', 'high', 'low', 'close', 'hl2', 'hlc3', 'ohlc4', 'hlcc4'], inline = '1', group = gm)
sigl = input.int (9 , "Length" , inline = '1', group = gm)
mode = input.string('Hybrid', "Display Mode" , ['Hybrid', 'MACD', 'Histogram'], group = gu)
revs = input.bool(true, "Show OB/OS Signals in Lower Pane", group = gu)
// Independent OB/OS levels: use any desired positive/negative oscillator values.
// These levels govern setups, zone shading, and extremity candle coloring.
const string gz = "OB/OS Zones"
obLevel = input.float(100.0, "Overbought Level (+)", minval = 0.0, step = 1.0, group = gz,
tooltip = "Overbought extreme for short setups (e.g., 75, 100, 125, 200).")
osLevel = input.float(-100.0, "Oversold Level (-)", maxval = 0.0, step = 1.0, group = gz,
tooltip = "Oversold extreme for long setups (e.g., -75, -100, -125, -200).")
chartRevs = input.bool(true, "Show OB/OS Signals on Price Chart", group = gu)
// OB/OS reversions are setups. When enabled, a later MACD/signal-line crossover
// confirms the entry, using the window below. Both marker locations and alerts
// use the SAME final confirmed OB/OS signals.
const string gc = "OB/OS Entry Confirmation"
useSignalConfirmation = input.bool(true, "Require Signal-Line Confirmation", group = gc)
confirmWindow = input.int(6, "Confirmation Window (candles)", minval = 1, maxval = 50, group = gc,
tooltip = "Number of candles AFTER the OB/OS setup allowed for a signal-line crossover. A crossover on the setup candle also qualifies.")
requireBarClose = input.bool(true, "Require Confirmed Candle Close", group = gc,
tooltip = "ON: OB/OS signals only form on completed chart candles. OFF: live-bar signals can change before the candle closes.")
colb = input.string('None' , "Bar Coloring" , ['None', 'MidLine', 'Candles', 'Sig Cross', 'Extremities', 'Reversions'], group = gu)
hol = input.bool (true , "Hollow Candles" , group = gu)
bar b = bar.new (open, high, low , close )
osc o = b .stmc(src , fast, slow, sigs , sigl)
const color colup = #00bcd4
const color coldn = #fc1f1f
const color colhu = #8ac3f5
const color colhd = #ffa7b6
var color colnt = chart.fg_color
// A primitive float history is safe on bar 0 and avoids an undefined UDT.
float histValue = o.h
bool histRising = histValue > histValue[1]
color colh = histValue > 0 ?
(histRising ? color.new(colhu, 20) : color.new(colhu, 30)) :
(histRising ? color.new(colhd, 20) : color.new(colhd, 30))
color haColor = switch
o.b.c > o.b.o => colup
o.b.c < o.b.o => coldn
m = hline(0 , "Mid Line", chart.fg_color, hline.style_solid )
// Move the OB/OS shaded bands together with the user-selected thresholds.
// Keep the original fixed histogram-mode reference bands, since those represent
// different (histogram) values rather than the HA-MACD OB/OS reversal thresholds.
min = hline(mode != 'Histogram' ? osLevel - 100.0 : -100.0, display = display.none)
ll = hline(mode != 'Histogram' ? osLevel - 50.0 : -75.0, display = display.none)
hl = hline(mode != 'Histogram' ? osLevel : -50.0, display = display.none)
max = hline(mode != 'Histogram' ? obLevel + 100.0 : +100.0, display = display.none)
hh = hline(mode != 'Histogram' ? obLevel + 50.0 : +75.0, display = display.none)
lh = hline(mode != 'Histogram' ? obLevel : +50.0, display = display.none)
h = plot (mode != 'Histogram' and sigb ? o.s : na, "Signal" , colnt )
mc = plot (mode != 'MACD' ? o.h : na, "H" , colh , 1, plot.style_columns)
fill(lh, hh , color.new(coldn, 90))
fill(hh, max, color.new(coldn, 80))
fill(ll, hl , color.new(colup, 80))
fill(ll, min, color.new(colup, 80))
plotcandle(mode != 'Histogram' ? o.b.o : na ,
mode != 'Histogram' ? o.b.h : na ,
mode != 'Histogram' ? o.b.l : na ,
mode != 'Histogram' ? o.b.c : na ,
"𝗠𝗔𝗖𝗗", hol ? o.b.c > o.b.o ? na : haColor : haColor, haColor, bordercolor = haColor)
// Original OB/OS setup logic (unchanged).
rawOB = ta.crossunder(o.b.c, o.b.o) and o.b.h > obLevel
rawOS = ta.crossover(o.b.c, o.b.o) and o.b.l < osLevel
// Calculate signal-line crosses on EVERY bar, independent of selected mode.
macdBullCross = ta.crossover(o.b.c, o.s)
macdBearCross = ta.crossunder(o.b.c, o.s)
validBar = not requireBarClose or barstate.isconfirmed
var int pendingOBBar = na
var int pendingOSBar = na
bool sellSignal = false
bool buySignal = false
if validBar
if useSignalConfirmation
// New opposite setups cancel older pending setups.
if rawOB
pendingOBBar := bar_index
pendingOSBar := na
if rawOS
pendingOSBar := bar_index
pendingOBBar := na
// Bar 0 is the setup itself; bars 1..N are the confirmation window.
if not na(pendingOBBar) and bar_index - pendingOBBar > confirmWindow
pendingOBBar := na
if not na(pendingOSBar) and bar_index - pendingOSBar > confirmWindow
pendingOSBar := na
sellSignal := not na(pendingOBBar) and macdBearCross
buySignal := not na(pendingOSBar) and macdBullCross
// Each setup can generate at most one confirmed signal.
if sellSignal
pendingOBBar := na
if buySignal
pendingOSBar := na
else
// Filter OFF = original OB/OS reversal timing (subject to close toggle).
sellSignal := rawOB
buySignal := rawOS
pendingOBBar := na
pendingOSBar := na
// All pre-existing named alert conditions stay available. Their OB/OS fields
// now use the final (delayed or original) signal, just like the chart triangles.
alerts a = alerts.new(
sellSignal,
buySignal,
ta.crossover (o.b.c, 0),
ta.crossunder(o.b.c, 0),
ta.crossunder(o.b.c, o.b.o) and not (o.b.h > obLevel),
ta.crossover (o.b.c, o.b.o) and not (o.b.l < osLevel),
macdBullCross,
macdBearCross)
// Lower-pane markers use final entry signals and oscillator coordinates.
plotshape(mode != 'Histogram' ? revs ? a.s ? o.b.h + 40 : na : na : na, "OB", shape.triangledown, location.absolute, coldn, size = size.tiny)
plotshape(mode != 'Histogram' ? revs ? a.b ? o.b.l - 40 : na : na : na, "OS", shape.triangleup, location.absolute, colup, size = size.tiny)
// Matching markers on main price chart; EXACT same confirmed a.s and a.b signals.
// force_overlay keeps the indicator itself in the lower pane.
plotshape(series = chartRevs and a.s, title = "OB (Price Chart)", style = shape.triangledown, location = location.abovebar, color = coldn, size = size.tiny, force_overlay = true)
plotshape(series = chartRevs and a.b, title = "OS (Price Chart)", style = shape.triangleup, location = location.belowbar, color = colup, size = size.tiny, force_overlay = true)
// One combined alertcondition, triggerable as a single named TradingView alert.
// Its direction placeholder is 1 (BUY) or -1 (SELL).
plot(buySignal ? 1 : sellSignal ? -1 : na, "OB/OS Direction Code", display = display.none, editable = false)
alertcondition(buySignal or sellSignal, "OB/OS Buy/Sell (Unified)",
'Zacs MACD HA OB/OS | {{ticker}} {{interval}} | Direction {{plot("OB/OS Direction Code")}} (1=BUY, -1=SELL) | Close {{close}}')
alertcondition(a.s, "Overbought" , 'OB Signal' )
alertcondition(a.b, "Oversold " , 'OS Signal' )
alertcondition(a.u, "Midline Crossover " , 'Bullish Trend' )
alertcondition(a.d, "Midline Crossunder" , 'Bearish Trend' )
alertcondition(a.p, "Positive Heikin-Ashi", 'Heikin-Ashi Uptrend ')
alertcondition(a.n, "Negative Heikin-Ashi", 'Heikin-Ashi Downtrend')
alertcondition(a.x, "MACD Bull Cross" , 'MACD Bullish Cross' )
alertcondition(a.y, "MACD Bear Cross" , 'MACD Bearish Cross' )
// A SINGLE TradingView alert using "Any alert() function call" also handles
// both directions and provides BUY/SELL as words, not numeric codes.
// Other pre-existing signal alertcondition() choices remain available above.
if sellSignal
alert("Zacs MACD HA OB/OS SELL | " + syminfo.ticker + " | TF " + timeframe.period + " | Close " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
if buySignal
alert("Zacs MACD HA OB/OS BUY | " + syminfo.ticker + " | TF " + timeframe.period + " | Close " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
color col = switch colb
'MidLine' => o.b.c > 0 ? colup : coldn
'Candles' => o.b.c > o.b.o ? colup : coldn
'Sig Cross' => o.s > o.b.c ? coldn : colup
'Extremities' => o.b.c > obLevel ? coldn : o.b.c < osLevel ? colup : color.new(colnt, 25)
'Reversions' => a.b ? coldn : a.s ? colup : color.new(colnt, 25)
'None' => na
barcolor(col)
//Source Construction For IndicatorStrategy Exports
plot(o.b.o , "open" , editable = false, display = display.none)
plot(o.b.h , "high" , editable = false, display = display.none)
plot(o.b.l , "low" , editable = false, display = display.none)
plot(o.b.c , "close", editable = false, display = display.none)
plot(o.b.src('hl2' ), "hl2" , editable = false, display = display.none)
plot(o.b.src('hlc3' ), "hlc3" , editable = false, display = display.none)
plot(o.b.src('ohlc4'), "ohlc4", editable = false, display = display.none)
plot(o.b.src('hlcc4'), "hlcc4", editable = false, display = display.none)
Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.
A complete code editor, chart templates, bar replay and alerts from your codes, right inside the platform.
Add to ChromeAlso works in Edge, Brave, Opera and other Chromium-based browsers.
Discover ProRealCode StudioThe information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.