SumInd
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fpucci
// V1.00 - March 21, 2023
//@version=5

indicator("SumInd", overlay=false)

wha = input.int(100, "Heiking Ashi Weight (0-200%)", 0, 200)
wpsar = input.int(100, "Parabolic SAR Weight (0-200%)", 0, 200)
wkon = input.int(60, "Koncord Weight (0-200%)", 0, 200)
wrsi = input.int(100, "RSI Weight (0-200%)", 0, 200)
wdmi = input.int(100, "DMI Weight (0-200%)", 0, 200)
wbb = input.int(0, "Bollinger Bands Weight (0-200%)", 0, 200)
wmacd = input.int(100, "MACD Weight (0-200%)", 0, 200)
triggerb = input.int(30, "Buy level (0-100%)", 0, 100)
triggers = input.int(-45, "Sell level (0-100%)", -100, 0)
hold = input.bool(false, "Distinguish wait/hold state")

trans=90
count=7
scorep = 0
scoren = 0
var bgc=0

psar = ta.sar(0.02, 0.02, 0.2)
[macdf, macds, macdh] = ta.macd(close, 12, 26, 9)
[bbm, bbh, bbl] = ta.bb(close, 26, 2)
[dip, dim, adx] = ta.dmi(14, 14)
haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
hamax = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
hamin = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
rsi = ta.rsi(close, 14)
emarsi = ta.ema(rsi, 14)
basis_boll = ta.ema(close, 21)
dev_boll = ta.stdev(close, 21)
BollOsc = ((close - basis_boll ) / (dev_boll * 4) ) * 100 + 50
stoc = ta.sma(ta.stoch(close, high, low, 14), 3)
mont = (rsi + BollOsc + stoc)/3
konc = ta.ema(mont, 15)

scorep += (haopen == hamin) ? (wha/2) : 0 // OK +  1 green candle HA
scoren -= (haopen == hamax) ? (wha/2) : 0 // OK -  1 red candle HA
scorep += ((haopen == hamin) and (haopen[1] == hamin[1])) ? (wha/2) : 0 // 2 green candles
scoren -= (haopen == hamax) and (haopen[1] == hamax[1]) ? (wha/2) : 0 // 2 red candles
scorep += (emarsi<rsi) ? wrsi : 0 // OK +
scoren -= (emarsi>rsi) ? wrsi : 0 // OK +
scorep += ((psar[0]<low[0]) and (psar[1]>low[1])) ? wpsar/2 : 0 // Psar goes into grow
scoren -= ((psar[0]>high[0]) and (psar[1]<high[1])) ? wpsar/2 : 0 // Psar goes into decline
scorep += (psar < (0.97*low)) ? wpsar/2 : 0 // Psar rising steadily
scoren -= (psar > (1.03*high)) ? wpsar/2 : 0 // Psar in constant decline
scorep += ((konc>konc[4]) and konc[4]<35) ? wkon : 0 // rising from 35-
scoren -= ((konc<konc[4]) and konc[4]>65) ? wkon : 0 // dropping from 65+

scorep += math.min(((dip>(1.15*dim)) ? int(wdmi*adx/25) : 0), wdmi)
scoren -= math.min(((dim>(1.15*dip)) ? int(wdmi*adx/25) : 0), wdmi)

scorep += ((hlc3[2] <= bbl[2]) and (hlc3>bbl) and ((bbh-bbm)>0.15*bbm)) ? wbb : 0 // touched the lower band but is already going up
scoren -= ((hlc3[2] >= bbh[2]) and (hlc3<bbh) and ((bbh-bbm)>0.15*bbm)) ? wbb : 0 // touched the upper band but is already down
scorep += ((macdh[2]<macdh[1]) and (macdh[1]<macdh)) ? (wmacd/3) : 0
scoren -= ((macdh[2]>macdh[1]) and (macdh[1]>macdh)) ? (wmacd/3) : 0
scorep += (macdf>macds) ? (wmacd/3) : 0
scoren -= (macdf<macds) ? (wmacd/3) : 0
scorep += ((macdf<0) and (macdf>macds)) ? (wmacd/3) : 0
scoren -= ((macdf>0) and (macdf<macds)) ? (wmacd/3) : 0

scorep/=count
scoren/=count
score = scorep+scoren
buy = score >= triggerb
shell = score <= triggers

if (buy)
    bgc:=1
if (shell)
    bgc:=-1
if (hold and not buy and not shell)
    bgc := 0
colorbg = (bgc==1) ? color.new(color.green,trans) : ((bgc==-1) ? color.new(color.red,trans) : na)
bgcolor(colorbg)

plotshape(shell, title="Sell signal", style=shape.arrowdown, location=location.top, color=(not hold) ? color.white : na, size=size.tiny) // location.abovebar
plotshape(buy, title= "Buy signal", style=shape.arrowup, location=location.bottom, color=(not hold) ? color.white : na, size=size.tiny) // location.abovebar

// Advanced, change overlay true by false false and select all lines
hline(triggerb, title='Buy', color=color.gray, linewidth=1)
hline(triggers, title='Sell', color=color.gray, linewidth=1)
plot (scorep, title="Buy line", color=color.teal)
plot (scoren, title="Sell line", color=color.maroon)
plot (score, title="Total line", color=color.yellow)
