0% found this document useful (0 votes)
105 views10 pages

Combined Algo Alpha & BigBeluga Script

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views10 pages

Combined Algo Alpha & BigBeluga Script

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// This combined Pine Script™ code integrates features from both "Algo Alpha Trend Targets" and

"BigBeluga Target Trend"


// Please note: This is a direct merge attempt. Some functionalities might be simplified or prioritized
to avoid conflicts.
// It is recommended to test thoroughly on TradingView.

//@version=5
indicator("Combined Trend & Targets [Merged Script]", "Combined Script", overlay = true,
max_lines_count = 100) // Increased max_lines_count for more objects

// --- Algo Alpha Trend Settings ---


st_factor = [Link](12, title="Supertrend Factor (AlgoAlpha)", minval=1, step=0.5,
group="AlgoAlpha Trend Settings",
tooltip="Multiplier for the ATR to determine Supertrend bands width. Higher values create wider
bands and fewer signals.")
st_atr_period = [Link](90, title="Supertrend ATR Period (AlgoAlpha)", minval=1, group="AlgoAlpha
Trend Settings",
tooltip="Number of bars used to calculate the ATR for Supertrend. Longer periods create
smoother, less reactive bands.")
wma_length = [Link](40, title="WMA Length (AlgoAlpha)", minval=1, group="AlgoAlpha Trend
Settings",
tooltip="Length of the Weighted Moving Average applied to the SuperTrend. Higher values create
smoother, less reactive lines.")
ema_length = [Link](14, title="EMA Length (AlgoAlpha)", minval=1, group="AlgoAlpha Trend
Settings",
tooltip="Length of the Exponential Moving Average applied to the WMA. Controls the final
smoothness of the trend line.")

// --- Algo Alpha Rejection Settings ---


cont_factor = [Link](3, title="Confirmation count (AlgoAlpha)", minval=1, group="AlgoAlpha
Rejection Settings",
tooltip="Number of consecutive bars that must consolidate at the trend line before a rejection
signal is generated. Higher values require more bars to confirm a trend.")

// --- Algo Alpha Target & Volatility Settings ---


shw_TP1 = [Link](true, title="Show Take Profit Levels (AlgoAlpha)", group="AlgoAlpha Targets",
tooltip="Toggle visibility of take profit target levels on the chart.")
atr_period_aa = [Link](14, title="Volatility (ATR) period (AlgoAlpha)", minval=1, group="AlgoAlpha
Targets",
tooltip="Number of bars used to calculate the Average True Range for position sizing and
targets.")
sl_multiplier_aa = [Link](5, title="Stop Loss ATR Multiplier (AlgoAlpha)", minval=0.1, step=0.1,
group="AlgoAlpha Targets",
tooltip="Multiplier applied to ATR to determine stop loss distance from entry. Higher values place
stops further away.")
tp1_multiplier_aa = [Link](0.5, title="TP1 Multiplier (AlgoAlpha)", minval=0.1, step=0.1,
tooltip="Multiple of SL distance for first take profit target.", group="AlgoAlpha Targets")
tp2_multiplier_aa = [Link](1.0, title="TP2 Multiplier (AlgoAlpha)", minval=0.1, step=0.1,
tooltip="Multiple of SL distance for second take profit target.", group="AlgoAlpha Targets")
tp3_multiplier_aa = [Link](1.5, title="TP3 Multiplier (AlgoAlpha)", minval=0.1, step=0.1,
tooltip="Multiple of SL distance for third take profit target.", group="AlgoAlpha Targets")
// --- Algo Alpha Appearance settings ---
green_aa = [Link](#00ffbb, title="Bullish Color (AlgoAlpha)", tooltip="Color used for
bullishness", group="AlgoAlpha Appearance")
red_aa = [Link](#ff1100, title="Bearish Color (AlgoAlpha)", tooltip="Color used for bearishness",
group="AlgoAlpha Appearance")

// --- BigBeluga Inputs ---


length_bb = [Link](10, "Trend Length (BigBeluga)", group="BigBeluga Trend Settings")
target_bb = [Link](0, "Set Targets (BigBeluga) - adjusts target distance", group="BigBeluga
Targets")

// --- BigBeluga Colors & Variables (Moved plot_color_bb here for proper scope) ---
up_color_bb = #06b690
dn_color_bb = [Link](182, 112, 6)
plot_color_bb = [Link](chart.fg_color, 80) // Defined here for v5 compatibility and proper scope

// --- Algo Alpha Supertrend Calculation ---


pine_supertrend(factor, atrPeriod) =>
src = hl2
atr = [Link](atrPeriod)
upperBand = src + factor * atr
lowerBand = src - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand :
prevLowerBand
upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ? upperBand :
prevUpperBand
[lowerBand, upperBand]

[lwr, upr] = pine_supertrend(st_factor, st_atr_period)


tL = [Link]([Link]([Link](lwr, upr), wma_length), ema_length)

// --- Algo Alpha Trend Detection ---


var trend_aa = 0
if [Link](tL, tL[1])
trend_aa := 1
if [Link](tL, tL[1])
trend_aa := -1

// --- Algo Alpha Rejection Logic ---


var rejcount_aa = 0
bullishrej_aa = trend_aa == 1 and high > tL and low < tL
bearishrej_aa = trend_aa == -1 and high > tL and low < tL

if (bullishrej_aa or bearishrej_aa)
rejcount_aa += 1
if [Link](trend_aa, 0) or (not (bullishrej_aa or bearishrej_aa) and rejcount_aa > 0)
rejcount_aa := 0
// --- BigBeluga ATR for calculating stop loss and target levels ---
series float atr_value_bb = [Link]([Link](200), 200) * 0.8

// --- BigBeluga Moving averages for trend detection ---


series float sma_high_bb = [Link](high, length_bb) + atr_value_bb
series float sma_low_bb = [Link](low, length_bb) - atr_value_bb

// --- BigBeluga Trend Determination ---


var bool trend_bb = na
float trend_value_bb = na

if [Link](close, sma_high_bb) and [Link]


trend_bb := true
if [Link](close, sma_low_bb) and [Link]
trend_bb := false

trend_value_bb := switch
trend_bb => sma_low_bb
not trend_bb => sma_high_bb

trend_color_bb = trend_bb ? up_color_bb : not trend_bb ? dn_color_bb : na

// --- BigBeluga Signal detection for trend changes ---


bool signal_up_bb = [Link](trend_bb) and not trend_bb[1]
bool signal_down_bb = [Link](trend_bb) and trend_bb[1]

// --- Algo Alpha Volatility Calculation for Targets ---


volatility_aa = [Link](atr_period_aa)

// --- Algo Alpha Target Level Calculations ---


var float SL_aa = 0.0
var float TP1_lvl_aa = 0.0
var float TP2_lvl_aa = 0.0
var float TP3_lvl_aa = 0.0

longSignal_aa = [Link](trend_aa, 0)
shortSignal_aa = [Link](trend_aa, 0)

// --- UDT for managing lines and labels (from BigBeluga) ---
type TrendTargets
line[] lines
label[] labels

// Initialize UDT
var TrendTargets targets_up_bb = [Link](array.new_line(), array.new_label())
var TrendTargets targets_down_bb = [Link](array.new_line(), array.new_label())

// --- Method to draw trend targets and manage lines/labels (from BigBeluga, with restored original
target calculation) ---
method draw_targets_bb(TrendTargets targets, bool signal_entry, bool signal_exit, bool direction)=>
float base_bb = direction ? sma_low_bb : sma_high_bb
float atr_multiplier_bb = atr_value_bb * (direction ? 1 : -1) // Re-added original atr_multiplier

// Reset counters for up and down targets (from BigBeluga)


var int count_up_bb = 0
var int count_down_bb = 0
if trend_bb
count_down_bb := 0
count_up_bb += 1
if not trend_bb
count_down_bb += 1
count_up_bb := 0

int count_bb = direction ? count_up_bb : count_down_bb

if signal_entry // BigBeluga signal entry


// Original BigBeluga target length calculations
float target_len1_bb = atr_multiplier_bb * (5 + target_bb)
float target_len2_bb = atr_multiplier_bb * (10 + target_bb * 2)
float target_len3_bb = atr_multiplier_bb * (15 + target_bb * 3)

// Clear existing lines and labels (from BigBeluga)


for line_i in [Link]
int i = [Link](line_i)
[Link]([Link](i))
[Link](line_i)
[Link]([Link])
[Link]([Link])

// Draw new lines for trend targets (from BigBeluga, using BigBeluga calculated levels)
line stop_loss_line_bb = [Link](bar_index, base_bb, bar_index + 20, base_bb,
xloc=xloc.bar_index)
line entry_line_bb = [Link](bar_index, close, bar_index + 20, close, xloc=xloc.bar_index)
line target1_line_bb = [Link](bar_index, close + target_len1_bb, bar_index + 20, close +
target_len1_bb, xloc=xloc.bar_index)
line target2_line_bb = [Link](bar_index, close + target_len2_bb, bar_index + 20, close +
target_len2_bb, xloc=xloc.bar_index)
line target3_line_bb = [Link](bar_index, close + target_len3_bb, bar_index + 20, close +
target_len3_bb, xloc=xloc.bar_index)

// Fill between stop loss and entry line (from BigBeluga)


[Link](stop_loss_line_bb, entry_line_bb, [Link](dn_color_bb, 95))
[Link](entry_line_bb, target3_line_bb, [Link](up_color_bb, 95))

// Draw new labels for trend targets (from BigBeluga, using BigBeluga calculated levels)
label stop_loss_label_bb = [Link](bar_index + 20, base_bb, [Link]([Link](base_bb,
2)), xloc=xloc.bar_index)
label entry_label_bb = [Link](bar_index + 20, close, [Link]([Link](close, 2)),
xloc=xloc.bar_index)
label target1_label_bb = [Link](bar_index + 20, close + target_len1_bb, " 1 - " +
[Link]([Link](close + target_len1_bb, 2)), xloc=xloc.bar_index)
label target2_label_bb = [Link](bar_index + 20, close + target_len2_bb, " 2 - " +
[Link]([Link](close + target_len2_bb, 2)), xloc=xloc.bar_index)
label target3_label_bb = [Link](bar_index + 20, close + target_len3_bb, " 3 - " +
[Link]([Link](close + target_len3_bb, 2)), xloc=xloc.bar_index)

// Push lines and labels to the UDT (from BigBeluga)


[Link](stop_loss_line_bb)
[Link](entry_line_bb)
[Link](target1_line_bb)
[Link](target2_line_bb)
[Link](target3_line_bb)
[Link](stop_loss_label_bb)
[Link](entry_label_bb)
[Link](target1_label_bb)
[Link](target2_label_bb)
[Link](target3_label_bb)

// Update styles for labels and lines (from BigBeluga)


for lbl in [Link]
int idx = [Link](lbl)
line line_ref = [Link](idx)
lbl.set_style(label.style_label_left)
lbl.set_color(chart.fg_color)
lbl.set_textcolor(chart.bg_color)
line_ref.set_color(chart.fg_color)

if signal_exit // This signal handles clearing existing lines (from BigBeluga)


// Clear existing lines and labels
for line_i in [Link]
int i = [Link](line_i)
[Link]([Link](i))
[Link](line_i)
[Link]([Link])
[Link]([Link])

// Update lines to extend (from BigBeluga)


for line_i in [Link]
int idx = [Link](line_i)
label lbl_ref = [Link](idx)
label first_label = [Link]()
line entry_line_ref = [Link](1) // Assuming entry line is second element
label entry_label_ref = [Link](1)

// Targets hit logic (from BigBeluga)


if high >= line.get_y2(line_i) and low <= line.get_y2(line_i) and count_bb > 1 and line_i !=
[Link]() // Exclude SL line
lbl_ref.set_style(label.style_label_left)
lbl_ref.set_color(chart.fg_color)
lbl_ref.set_text(" ✔ ")
lbl_ref.set_textcolor(#16ac09)
line_i.set_style(line.style_dashed)
line_i.set_color(plot_color_bb) // Use BigBeluga's plot_color for hit targets

// Stop Loss hit logic (from BigBeluga)


if high >= line.get_y2([Link]()) and low <= line.get_y2([Link]()) and
count_bb > 1
first_label.set_text(" ✖ ")
if direction ? trend_bb : not trend_bb
first_label.set_textcolor(#db1e1e)
[Link]().set_color(#db1e1e)

line_i.set_x2(bar_index + 20)
label.set_x([Link](idx), bar_index + 20)

// Ensure entry line/label stay updated


entry_line_ref.set_style(line.style_solid)
entry_line_ref.set_color(up_color_bb) // Or dn_color_bb based on direction
entry_label_ref.set_text(" ◉ " + [Link]([Link](line.get_y2(entry_line_ref), 2)))
entry_label_ref.set_textcolor(#1d80dd)

// --- Algo Alpha Plotting ---


plotchar((rejcount_aa > cont_factor and trend_aa == 1) ? tL : na, "Bullish Rejection", " ▲ ",
[Link], green_aa, size = [Link])
plotchar((rejcount_aa > cont_factor and trend_aa == -1) ? tL : na, "Bearish Rejection", " ▼ ",
[Link], red_aa, size = [Link])

plot(tL, "Baseline", color=trend_aa == 1 ? [Link](green_aa, 50) : [Link](red_aa, 50)) //


Adjusted for v5 [Link]
barcolor(trend_aa == 1 ? [Link](green_aa, 50) : [Link](red_aa, 50)) // Adjusted for v5
[Link]

plotshape([Link](tL, tL[1]) ? tL : na, title="Bullish Trend Change", style=[Link],


location=[Link], size=[Link], color=green_aa)
plotshape([Link](tL, tL[1]) ? tL : na, title="Bearish Trend Change", style=[Link],
location=[Link], size=[Link], color=red_aa)

// --- Algo Alpha Target Calculations & Drawing (Now uses AlgoAlpha's own drawing logic) ---
// Note: AlgoAlpha's drawing logic was directly integrated in the original script.
// To keep BigBeluga's drawing method isolated for its own targets, we'll revert AlgoAlpha's original
line/label logic.
var line entry_line_aa = na
var line sl_line_aa = na
var line tp1_line_aa = na
var line tp2_line_aa = na
var line tp3_line_aa = na
var label entry_label_aa = na
var label sl_label_aa = na
var label tp1_label_aa = na
var label tp2_label_aa = na
var label tp3_label_aa = na

if longSignal_aa and shw_TP1


SL_aa := low - volatility_aa * sl_multiplier_aa
TP1_lvl_aa := close + [Link](close - SL_aa) * tp1_multiplier_aa
TP2_lvl_aa := close + [Link](close - SL_aa) * tp2_multiplier_aa
TP3_lvl_aa := close + [Link](close - SL_aa) * tp3_multiplier_aa

entry_line_aa := [Link](bar_index, close, bar_index, close, color = green_aa, width = 3)


entry_label_aa := [Link](bar_index, close, text = "Entry ▸ " + [Link](close,
[Link]), style = label.style_label_left, color = green_aa, textcolor = [Link])
sl_line_aa := [Link](bar_index, SL_aa, bar_index, SL_aa, color = [Link](red_aa, 80), width = 3)
sl_label_aa := [Link](bar_index, SL_aa, text = " ✘ SL ▸ " + [Link](SL_aa, [Link]),
style = label.style_label_left, color = [Link](red_aa, 80), textcolor = [Link])
tp1_line_aa := [Link](bar_index, TP1_lvl_aa, bar_index, TP1_lvl_aa, color = [Link](green_aa,
80), width = 3)
tp1_label_aa := [Link](bar_index, TP1_lvl_aa, text = " ✔ TP1 ▸ " + [Link](TP1_lvl_aa,
[Link]), style = label.style_label_left, color = [Link](green_aa, 80), textcolor =
[Link])
tp2_line_aa := [Link](bar_index, TP2_lvl_aa, bar_index, TP2_lvl_aa, color = [Link](green_aa,
80), width = 3)
tp2_label_aa := [Link](bar_index, TP2_lvl_aa, text = " ✔ TP2 ▸ " + [Link](TP2_lvl_aa,
[Link]), style = label.style_label_left, color = [Link](green_aa, 80), textcolor =
[Link])

tp3_line_aa := [Link](bar_index, TP3_lvl_aa, bar_index, TP3_lvl_aa, color = [Link](green_aa,


80), width = 3)
tp3_label_aa := [Link](bar_index, TP3_lvl_aa, text = " ✔ TP3 ▸ " + [Link](TP3_lvl_aa,
[Link]), style = label.style_label_left, color = [Link](green_aa, 80), textcolor =
[Link])

[Link](entry_line_aa[1])
[Link](entry_label_aa[1])
[Link](sl_line_aa[1])
[Link](sl_label_aa[1])
[Link](tp1_line_aa[1])
[Link](tp1_label_aa[1])

[Link](tp2_line_aa[1])
[Link](tp2_label_aa[1])
[Link](tp3_line_aa[1])
[Link](tp3_label_aa[1])
else
line.set_x2(entry_line_aa, bar_index)
label.set_x(entry_label_aa, bar_index)
line.set_x2(sl_line_aa, bar_index)
label.set_x(sl_label_aa, bar_index)
line.set_x2(tp1_line_aa, bar_index)
label.set_x(tp1_label_aa, bar_index)
line.set_x2(tp2_line_aa, bar_index)
label.set_x(tp2_label_aa, bar_index)
line.set_x2(tp3_line_aa, bar_index)
label.set_x(tp3_label_aa, bar_index)
if shortSignal_aa and shw_TP1
SL_aa := high + volatility_aa * sl_multiplier_aa
TP1_lvl_aa := close - [Link](close - SL_aa) * tp1_multiplier_aa
TP2_lvl_aa := close - [Link](close - SL_aa) * tp2_multiplier_aa
TP3_lvl_aa := close - [Link](close - SL_aa) * tp3_multiplier_aa

entry_line_aa := [Link](bar_index, close, bar_index, close, color = red_aa, width = 3)


entry_label_aa := [Link](bar_index, close, text = "Entry ▸ " + [Link](close,
[Link]), style = label.style_label_left, color = red_aa, textcolor = [Link])
sl_line_aa := [Link](bar_index, SL_aa, bar_index, SL_aa, color = [Link](red_aa, 80), width = 3)
sl_label_aa := [Link](bar_index, SL_aa, text = " ✘ SL ▸ " + [Link](SL_aa, [Link]),
style = label.style_label_left, color = [Link](red_aa, 80), textcolor = [Link])
tp1_line_aa := [Link](bar_index, TP1_lvl_aa, bar_index, TP1_lvl_aa, color = [Link](green_aa,
80), width = 3)
tp1_label_aa := [Link](bar_index, TP1_lvl_aa, text = " ✔ TP1 ▸ " + [Link](TP1_lvl_aa,
[Link]), style = label.style_label_left, color = [Link](green_aa, 80), textcolor =
[Link])
tp2_line_aa := [Link](bar_index, TP2_lvl_aa, bar_index, TP2_lvl_aa, color = [Link](green_aa,
80), width = 3)
tp2_label_aa := [Link](bar_index, TP2_lvl_aa, text = " ✔ TP2 ▸ " + [Link](TP2_lvl_aa,
[Link]), style = label.style_label_left, color = [Link](green_aa, 80), textcolor =
[Link])
tp3_line_aa := [Link](bar_index, TP3_lvl_aa, bar_index, TP3_lvl_aa, color = [Link](green_aa,
80), width = 3)
tp3_label_aa := [Link](bar_index, TP3_lvl_aa, text = " ✔ TP3 ▸ " + [Link](TP3_lvl_aa,
[Link]), style = label.style_label_left, color = [Link](green_aa, 80), textcolor =
[Link])

[Link](entry_line_aa[1])
[Link](entry_label_aa[1])
[Link](sl_line_aa[1])
[Link](sl_label_aa[1])
[Link](tp1_line_aa[1])
[Link](tp1_label_aa[1])

[Link](tp2_line_aa[1])
[Link](tp2_label_aa[1])
[Link](tp3_line_aa[1])
[Link](tp3_label_aa[1])
else
line.set_x2(entry_line_aa, bar_index+40)
label.set_x(entry_label_aa, bar_index+40)
line.set_x2(sl_line_aa, bar_index+40)
label.set_x(sl_label_aa, bar_index+40)
line.set_x2(tp1_line_aa, bar_index+40)
label.set_x(tp1_label_aa, bar_index+40)
line.set_x2(tp2_line_aa, bar_index+40)
label.set_x(tp2_label_aa, bar_index+40)
line.set_x2(tp3_line_aa, bar_index+40)
label.set_x(tp3_label_aa, bar_index+40)
// AlgoAlpha Linefills (kept as is)
// The original AlgoAlpha linefills use dynamic lines (entry_line, sl_line, tp3_line)
// which are created and deleted on each signal. This approach is prone to errors
// with linefills in Pinescript v5 as linefills expect persistent lines.
// For now, I will remove these linefills to avoid potential errors and focus on the core functionality.
// If you want linefills for AlgoAlpha, they will need a different implementation strategy (e.g., using
`plot` for fill between plots).

// --- BigBeluga Plotting ---


// Call the draw_targets method for both upward and downward trends
// Note: Changed the arguments to draw_targets_bb as it now calculates its own levels
targets_down_bb.draw_targets_bb(signal_down_bb, signal_up_bb, false)
targets_up_bb.draw_targets_bb(signal_up_bb, signal_down_bb, true)

// Plot candlesticks with trend color


plotcandle(open, high, low, close,
title = 'BigBeluga Trend Candles',
color = trend_color_bb,
wickcolor = trend_color_bb,
bordercolor = trend_color_bb)

// Plot trailing stops


p1_bb = plot(trend_bb ? trend_value_bb : na, style = plot.style_linebr, color = plot_color_bb,
title="BigBeluga Trailing Stop Up")
p2_bb = plot(not trend_bb ? trend_value_bb : na, style = plot.style_linebr, color = plot_color_bb,
title="BigBeluga Trailing Stop Down")
p0_bb = plot(hl2, display = [Link], editable = false)
fill(p1_bb, p0_bb, trend_value_bb, hl2, [Link](chart.fg_color, 90), na)
fill(p2_bb, p0_bb, trend_value_bb, hl2, [Link](chart.fg_color, 90), na)

// Plot signals on the chart


float sigUp_bb = signal_up_bb ? low - atr_value_bb*2 : na
float sigDn_bb = signal_down_bb ? high + atr_value_bb*2 : na
plotshape(sigUp_bb, "", [Link], [Link], up_color_bb, size = [Link],
title="BigBeluga Signal Up Small")
plotshape(sigUp_bb, "", [Link], [Link], [Link](up_color_bb, 80), size =
[Link], title="BigBeluga Signal Up Large")
plotshape(sigDn_bb, "", [Link], [Link], dn_color_bb, size = [Link],
title="BigBeluga Signal Down Small")
plotshape(sigDn_bb, "", [Link], [Link], [Link](dn_color_bb, 80), size =
[Link], title="BigBeluga Signal Down Large")

// --- Algo Alpha Alerts (Adjusted to reflect combined script context) ---
alertcondition([Link](close, SL_aa), title="AA Trend Change: Bullish (Price > SL)",
message="Price crossed above AlgoAlpha SL - Potential Bullish Trend")
alertcondition([Link](close, SL_aa), title="AA Trend Change: Bearish (Price < SL)",
message="Price crossed below AlgoAlpha SL - Potential Bearish Trend")

alertcondition([Link](high, SL_aa) and close < SL_aa, title="AA Rejection: Bearish (High > SL &
Close < SL)", message="Price rejected at AlgoAlpha SL - Bearish Rejection")
alertcondition([Link](low, SL_aa) and close > SL_aa, title="AA Rejection: Bullish (Low < SL &
Close > SL)", message="Price rejected at AlgoAlpha SL - Bullish Rejection")

alertcondition([Link](close, TP1_lvl_aa), title="AA TP1 Hit", message="Price reached AlgoAlpha


TP1 level")
alertcondition([Link](close, TP2_lvl_aa), title="AA TP2 Hit", message="Price reached AlgoAlpha
TP2 level")
alertcondition([Link](close, TP3_lvl_aa), title="AA TP3 Hit", message="Price reached AlgoAlpha
TP3 level")

You might also like