Page 1 of 1

How to fix a trategy alert calculation error

Posted: Thu Dec 21, 2023 4:59 pm
by elroeeifrah1111
Hi, I'm trying to get an alert for all the tickers in the code whenever they get long entry signal (yellow arrow up)

and I'm getting a calculation error for the strategy alert for some reason
Would appreciate help with it

Here's the code:

Code: Select all

//@version=5
indicator("Combined Indicator_v2", overlay=true, max_bars_back=1000, max_lines_count=200)

// Indicator 1
length = input.int(50, "Evaluation Window", minval=0, maxval=200)
fcast = input.int(50, "Forecast Window", minval=1, maxval=200)
fmode = input.string("Similarity", "Forecast Mode", options=["Similarity", "Dissimilarity"])
cmode = input.string("Cumulative", "Forecast Construction", options=["Cumulative", "Mean", "Linreg"])
src1 = input(close, "Source 1")
fcast_col = input.color(#2157f3, "Forecast Style")
fcast_style = input.string("· · ·", "", options=["──", "- - -", "· · ·"])

show_area = input.bool(true, "Show Area")
fcast_area = input.color(color.new(#ff5d00, 50))
corr_area = input.color(color.new(#0cb51a, 50))
eval_area = input.color(color.new(color.gray, 50))

var lines = array.new_line(0)
if barstate.isfirst
    for i = 0 to fcast - 1 by 1
        array.push(lines, line.new(na, na, na, na))

n = bar_index
d = ta.change(src1)

top = ta.highest(src1, length + fcast * 2)
btm = ta.lowest(src1, length + fcast * 2)

if barstate.islast
    float val = na
    k = 0
    A = array.new_float(0)
    X = array.new_int(0)
    for i = 0 to fcast * 2 + length by 1
        array.push(A, src1[i])
        if cmode == "Linreg"
            array.push(X, n[i])

    a = array.slice(A, 0, fcast - 1)
    for i = 0 to length - 1 by 1
        b = array.slice(A, fcast + i, fcast * 2 + i - 1)
        r = array.covariance(a, b) / (array.stdev(a) * array.stdev(b))
        if fmode == "Similarity"
            val := r >= nz(val, r) ? r : val
            val
        else
            val := r <= nz(val, r) ? r : val
            val
        k := val == r ? i : k
        k

    prev = src1
    current = src1

    for i = 0 to fcast - 1 by 1
        e = d[fcast + k + fcast - i - 1]
        if cmode == "Mean"
            current := array.avg(a) + e
            current
        else if cmode == "Linreg"
            a = array.slice(A, 0, fcast)
            x = array.slice(X, 0, fcast)
            alpha = array.covariance(a, x) / array.variance(x)
            beta = array.avg(a) - alpha * array.avg(x)
            current := alpha * (n + i + 1) + beta + e
            current
        else
            current += e
            current

        l = array.get(lines, i)
        line.set_xy1(l, n + i, prev)
        line.set_xy2(l, n + i + 1, current)
        line.set_color(l, fcast_col)

        if fcast_style == "- - -"
            line.set_style(l, line.style_dashed)
        else if fcast_style == "· · ·"
            line.set_style(l, line.style_dotted)

        prev := current
        prev

    if show_area
        box.delete(box.new(n - length - fcast * 2 + 1, top, n - fcast + 1, btm, border_color=na, bgcolor=eval_area)[1])
        box.delete(box.new(n - fcast + 1, top, n, btm, border_color=na, bgcolor=fcast_area)[1])
        box.delete(box.new(n - k - fcast * 2 + 1, btm, n - k - fcast, top, border_color=na, bgcolor=corr_area)[1])

// Indicator 2
lookback = input(150, "MA Lookback")
volume_lookback = input(120, "Volume Lookback")
cci_length = input.int(20, minval=1)

ma = ta.sma(close, lookback)
ma20 = ta.sma(close, 20)
ma50 = ta.sma(close, 50)
ma200 = ta.sma(close, 200)
avg_volume = ta.sma(volume, volume_lookback)
src2 = hlc3
ma_cci = ta.sma(src2, cci_length)
cci = (src2 - ma_cci) / (0.015 * ta.dev(src2, cci_length))

plot(ma, color=color.blue, linewidth=3, title="150ma")
plot(ma20, color=color.white, linewidth=3, title="20ma")
plot(ma50, color=color.yellow, linewidth=3, title="50ma")
plot(ma200, color=color.red, linewidth=3, title="200ma")
plot(cci, "CCI", color=color.new(color.rgb(255, 235, 59), 0))

enterLong = ta.crossover(close, ma) and volume > avg_volume and cci > 0
plotshape(enterLong, style=shape.triangleup, location=location.belowbar, color=color.yellow, size=size.small)

if enterLong
    alert("Long entry signal generated")

exitLong = close[1] < ma[1] and close[2] < ma[2] and close[3] < ma[3] and volume > avg_volume and cci < 150
plotshape(exitLong, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

// Support and Resistance Levels with Breaks
toggleBreaks = input(true, title="Show Breaks")
leftBars = input(15, title="Left Bars")
rightBars = input(15, title="Right Bars")
volumeThresh = input(20, title="Volume Threshold")

highUsePivot = fixnan(ta.pivothigh(leftBars, rightBars)[1])
lowUsePivot = fixnan(ta.pivotlow(leftBars, rightBars)[1])

r1 = plot(highUsePivot, color=color.new(color.red, 100), linewidth=3, offset=-(rightBars + 1), title="Resistance")
s1 = plot(lowUsePivot, color=color.new(color.blue, 100), linewidth=3, offset=-(rightBars + 1), title="Support")

//Volume %
short = ta.ema(volume, 5)
long = ta.ema(volume, 10)
osc = 100 * (short - long) / long

// For breaks with volume
plotshape(toggleBreaks and ta.crossunder(close, lowUsePivot) and not (open - close < high - open) and osc > volumeThresh, title="Break", text='B', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 100), textcolor=color.white, size=size.tiny)
plotshape(toggleBreaks and ta.crossover(close, highUsePivot) and not (open - low > close - open) and osc > volumeThresh, title="Break", text='B', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 100), textcolor=color.white, size=size.tiny)

// For bull / bear wicks
plotshape(toggleBreaks and ta.crossover(close, highUsePivot) and open - low > close - open, title="Break", text='Bull Wick', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 100), textcolor=color.white, size=size.tiny)
plotshape(toggleBreaks and ta.crossunder(close, lowUsePivot) and open - close < high - open, title="Break", text='Bear Wick', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 100), textcolor=color.white, size=size.tiny)

alertcondition(ta.crossunder(close, lowUsePivot) and osc > volumeThresh, title="Support Broken", message="Support Broken")
alertcondition(ta.crossover(close, highUsePivot) and osc > volumeThresh, title="Resistance Broken", message="Resistance Broken")


// Fetching data for specified tickers
ctra_data = request.security("CTRA", "D", close)
lcrx_data = request.security("LCRX", "D", close)
ddog_data = request.security("DDOG", "D", close)
dash_data = request.security("DASH", "D", close)
wcloud_data = request.security("WCLD", "D", close)
stne_data = request.security("STNE", "D", close)
jeld_data = request.security("JELD", "D", close)
shop_data = request.security("SHOP", "D", close)
bcat_data = request.security("BCAT", "D", close)
cvna_data = request.security("CVNA", "D", close)
unp_data = request.security("UNP", "D", close)
spgi_data = request.security("SPGI", "D", close)
fresh_data = request.security("FRESH", "D", close)
idxx_data = request.security("IDXX", "D", close)
nxpi_data = request.security("NXPI", "D", close)
cx_data = request.security("CX", "D", close)
mlm_data = request.security("MLM", "D", close)
ionq_data = request.security("IONQ", "D", close)
epix_data = request.security("EPIX", "D", close)
app_data = request.security("APP", "D", close)

// Conditions and alerts for CTRA
enterLong_ctra = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_ctra, title="Long Entry Signal CTRA", message="Long entry signal generated for CTRA - Ticker: CTRA")

// Conditions and alerts for LCRX
enterLong_lcrx = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_lcrx, title="Long Entry Signal LCRX", message="Long entry signal generated for LCRX - Ticker: LCRX")

// Conditions and alerts for DDOG
enterLong_ddog = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_ddog, title="Long Entry Signal DDOG", message="Long entry signal generated for DDOG - Ticker: DDOG")

// Conditions and alerts for DASH
enterLong_dash = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_dash, title="Long Entry Signal DASH", message="Long entry signal generated for DASH - Ticker: DASH")

// Conditions and alerts for WCLD
enterLong_wcloud = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_wcloud, title="Long Entry Signal WCLD", message="Long entry signal generated for WCLD - Ticker: WCLD")

// Conditions and alerts for STNE
enterLong_stne = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_stne, title="Long Entry Signal STNE", message="Long entry signal generated for STNE - Ticker: STNE")

// Conditions and alerts for JELD
enterLong_jeld = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_jeld, title="Long Entry Signal JELD", message="Long entry signal generated for JELD - Ticker: JELD")

// Conditions and alerts for SHOP
enterLong_shop = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_shop, title="Long Entry Signal SHOP", message="Long entry signal generated for SHOP - Ticker: SHOP")

// Conditions and alerts for BCAT
enterLong_bcat = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_bcat, title="Long Entry Signal BCAT", message="Long entry signal generated for BCAT - Ticker: BCAT")

// Conditions and alerts for CVNA
enterLong_cvna = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_cvna, title="Long Entry Signal CVNA", message="Long entry signal generated for CVNA - Ticker: CVNA")

// Conditions and alerts for UNP
enterLong_unp = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_unp, title="Long Entry Signal UNP", message="Long entry signal generated for UNP - Ticker: UNP")

// Conditions and alerts for SPGI
enterLong_spgi = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_spgi, title="Long Entry Signal SPGI", message="Long entry signal generated for SPGI - Ticker: SPGI")

// Conditions and alerts for FRESH
enterLong_fresh = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_fresh, title="Long Entry Signal FRESH", message="Long entry signal generated for FRESH - Ticker: FRESH")

// Conditions and alerts for IDXX
enterLong_idxx = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_idxx, title="Long Entry Signal IDXX", message="Long entry signal generated for IDXX - Ticker: IDXX")

// Conditions and alerts for NXPI
enterLong_nxpi = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_nxpi, title="Long Entry Signal NXPI", message="Long entry signal generated for NXPI - Ticker: NXPI")

// Conditions and alerts for CX
enterLong_cx = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_cx, title="Long Entry Signal CX", message="Long entry signal generated for CX - Ticker: CX")

// Conditions and alerts for MLM
enterLong_mlm = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_mlm, title="Long Entry Signal MLM", message="Long entry signal generated for MLM - Ticker: MLM")

// Conditions and alerts for IONQ
enterLong_ionq = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_ionq, title="Long Entry Signal IONQ", message="Long entry signal generated for IONQ - Ticker: IONQ")

// Conditions and alerts for EPIX
enterLong_epix = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_epix, title="Long Entry Signal EPIX", message="Long entry signal generated for EPIX - Ticker: EPIX")

// Conditions and alerts for APP
enterLong_app = ta.crossover(close, ma) and volume > avg_volume and cci > 0
alertcondition(enterLong_app, title="Long Entry Signal APP", message="Long entry signal generated for APP - Ticker: APP")