Search code examples
pine-scriptpine-script-v5

Pine script compiling 3 indicators in one


I'm trying to make a strategy using 3 indicators and cannot combine them. I have all 3 working indicators source codes and I wanted just add them to each other, but it didn't work. QStick Indicator added a few new things that prevented the code from working.

I tried to fix it with Chat GPT but it didn't work eather.

Here is how code looks like.

//@version=5
strategy(title="Combined Indicator Strategy", overlay=true)

// ASI Indicator
getASI(limit) =>
    H_C1 = math.abs(high - close[1])
    L_C1 = math.abs(low - close[1])
    H_L = math.abs(high - low)
    C1_O1 = math.abs(close[1] - open[1])
    K = math.max(H_C1, L_C1)
    R = H_C1 >= math.max(L_C1, H_L) ? H_C1 - 0.5 * L_C1 + 0.25 * C1_O1 : L_C1 >= math.max(H_C1, H_L) ? L_C1 - 0.5 * H_C1 + 0.25 * C1_O1 : H_L + 0.25 * C1_O1
    result = 50 * ((close - close[1] + 0.5 * (close - open) + 0.25 * (close[1] - open[1])) / R) * K / limit
    result


limit_move_value = input.float(title="Limit Move Value", defval=10000)

asi = ta.cum(getASI(limit_move_value))
asiColor = asi >= asi[1] ? color.green : color.red
plot(asi, title="ASI", color=asiColor, transp=0)

// Aroon Oscillator Indicator
aroon_length = input.int(title="Aroon Length", defval=14, minval=1)
upper = 100 * (ta.pivothigh(high, aroon_length) - aroon_length + 1) / aroon_length
lower = 100 * (ta.pivotlow(low, aroon_length) - aroon_length + 1) / aroon_length
oscillator = upper - lower
oscColor = oscillator > 0 ? color.green : oscillator < 0 ? color.red : color.blue
plot(oscillator, title="Aroon Oscillator", color=oscColor)

// QStick Indicator
qstick_length = input.int(title="QStick Length", defval=40, minval=1)
reverse = input.bool(title="Trade Reverse", defval=false)

xR = close - open
xQstick = ta.sma(xR, qstick_length)
clr = xQstick >= 0 ? color.green : color.red
var pos = 0.0
if xQstick > 0
    pos := 1.0
if xQstick < 0
    pos := -1.0
if reverse
    pos := -pos
barcolor(pos == -1 ? color.red : pos == 1 ? color.green : color.blue)

plot(hline(0), color=color.black, title="Zero Line")
plot(xQstick, color=color.blue, title="QStick")
fill(hline(0), hline(xQstick), color=clr)

// Trading Logic
longCondition = asi > 0 and oscillator < -50 and xQstick > 0
shortCondition = asi < 0 and oscillator > 50 and xQstick < 0
exitCondition = oscillator > 50 or xQstick < 0

if (longCondition)
    strategy.entry("Buy", strategy.long)
if (shortCondition)
    strategy.entry("Sell", strategy.short)
if (exitCondition)
    strategy.close_all()

I will provide indicators sousre code below if you need it:

Aroon Oscillator

study(title="Aroon Oscillator", shorttitle="Aroon Oscillator", overlay=false)
length = input(14, minval=1)
upper = 100 * (highestbars(high, length+1) + length)/length
lower = 100 * (lowestbars(low, length+1) + length)/length
midp = 0
oscillator = upper - lower
osc = plot(oscillator, color=red)
mp = plot(midp)
top = plot(80)
bottom = plot(-80)

fill(osc, mp)
fill(top,bottom)

ASI

//@version=3
// Copyright (c) 2017-present, Alex Orekhov (everget)
// Accumulative Swing Index script may be freely distributed under the MIT license.
study("Accumulative Swing Index", shorttitle="ASI")

limit = input(title="Limit Move Value", type=float, defval=10000)

// Swing Index
getSI(limit) =>
    H_C1 = abs(high - close[1])
    L_C1 = abs(low - close[1])
    H_L = abs(high - low)
    C1_O1 = abs(close[1] - open[1])
    K = max(H_C1, L_C1)
    R = iff(H_C1 >= max(L_C1, H_L), H_C1 - 0.5 * L_C1 + 0.25 * C1_O1, iff(L_C1 >= max(H_C1, H_L), L_C1 - 0.5 * H_C1 + 0.25 * C1_O1, H_L + 0.25 * C1_O1))
    result = 50 * ((close - close[1] + 0.5 * (close - open) + 0.25 * (close[1] - open[1])) / R) * K / limit
    result

// Accumulative Swing Index
asi = cum(getSI(limit))

asiColor = asi >= asi[1] ? #0ebb23 : red

plot(asi, title="ASI", color=asiColor, transp=0)

QStick

strategy(title="Qstick Indicator Backtest")
Length = input(40, minval=1)
reverse = input(false, title="Trade reverse")
xR = close - open
xQstick = sma(xR, Length)
clr = iff(xQstick >= 0, green, red)
pos = iff(xQstick > 0, 1,
       iff(xQstick < 0, -1, nz(pos[1], 0))) 
possig = iff(reverse and pos == 1, -1,
          iff(reverse and pos == -1, 1, pos))               
barcolor(possig == -1 ? red: possig == 1 ? green : blue ) 
p1 = plot(0, color=black, title="0")
p2 = plot(xQstick, color=blue, title="Qstick")
fill(p1, p2, color=clr)

Solution

  • You cannot use :

    plot(hline(0), color=color.black, title="Zero Line")
    

    because hline is a line, and you need a value for the plot. Change it back to :

    p1 = plot(0, color=black, title="Zero Line")
    

    Then for your fill to work, instead of :

    plot(hline(0), color=color.black, title="Zero Line")
    plot(xQstick, color=color.blue, title="QStick")
    fill(hline(0), hline(xQstick), color=clr)
    

    Change those lines with :

    p1 = plot(0, color=color.black, title="Zero Line")
    p2 = plot(xQstick, color=color.blue, title="QStick")
    fill(p1, p2, color=clr)