Vector Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Original pattern formation by plasmapug, rise retrace continuation to the upside by infernix, peshocore and xtech5192
// Modified by RapidFireOG
// Original pattern formation by plasmapug, rise retrace continuation to the upside by infernix, peshocore and xtech5192
// Please note code is open source and you are free to use it however you like


// This script is a Vector Candles indicator that visualizes climax and above-average volume candles on the chart.
// It uses the PVSRA (Price, Volume, Support, and Resistance Analysis) method to determine the candle color based on volume and price action.
// The indicator displays the candles in different colors, allowing traders to identify potential trend reversals and significant market moves.


//@version=5
indicator('Vector Candles', overlay=true, max_bars_back=300,max_boxes_count=500, max_lines_count=500, max_labels_count=500)

//inputs
bool overridesym = input.bool(group='PVSRA', title='Override chart symbol?', defval=false, inline='pvsra')
string pvsra_sym = input.symbol(group='PVSRA', title='', defval='INDEX:BTCUSD', tooltip='You can use INDEX:BTCUSD or you can combine multiple feeds, for example \'(BINANCE:BTCUSDT+COINBASE:BTCUSD)\'. Note that adding too many will slow things down.', inline='pvsra')

// PVSRA
pvsra_security(sresolution, sseries) =>
    request.security(overridesym ? pvsra_sym : syminfo.tickerid, sresolution, sseries[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off)
pvsra_security_1 = pvsra_security('', volume)
pvsra_volume = overridesym == true ? pvsra_security_1 : volume
pvsra_security_2 = pvsra_security('', high)
pvsra_high = overridesym == true ? pvsra_security_2 : high
pvsra_security_3 = pvsra_security('', low)
pvsra_low = overridesym == true ? pvsra_security_3 : low
pvsra_security_4 = pvsra_security('', close)
pvsra_close = overridesym == true ? pvsra_security_4 : close
pvsra_security_5 = pvsra_security('', open)
pvsra_open = overridesym == true ? pvsra_security_5 : open


// The below math matches MT4 PVSRA indicator source
// average volume from last 10 candles
sum_1 = math.sum(pvsra_volume, 10)
sum_2 = math.sum(volume, 10)
av = overridesym == true ? sum_1 / 10 : sum_2 / 10
//climax volume on the previous candle
value2 = overridesym == true ? pvsra_volume * (pvsra_high - pvsra_low) : volume * (high - low)
// highest climax volume of the last 10 candles
hivalue2 = ta.highest(value2, 10)
// VA value determines the bar color. va = 0: normal. va = 1: climax.  va = 2: rising
iff_1 = pvsra_volume >= av * 1.5 ? 2 : 0
iff_2 = pvsra_volume >= av * 2 or value2 >= hivalue2 ? 1 : iff_1
iff_3 = volume >= av * 1.5 ? 2 : 0
iff_4 = volume >= av * 2 or value2 >= hivalue2 ? 1 : iff_3
va = overridesym == true ? iff_2 : iff_4

// Bullish or bearish coloring
isBull = overridesym == true ? pvsra_close > pvsra_open : close > open

CUColor = color.lime  // Climax up (bull) bull and bear both start with b so it would be weird hence up down
CDColor = color.red  // Climax down (bear)

AUColor = color.rgb(33, 170, 255)  //Avobe average up (bull)
ADColor = color.fuchsia  //Above average down (bear))

NUColor = #9b9b9b
NDColor = #414141

// candleColor = iff(climax,iff(isBull,CUColor,CDColor),iff(aboveA,iff(isBull,AUColor,ADColor),iff(isBull,NUColor,NDColor)))
iff_5 = va == 2 ? AUColor : NUColor
iff_6 = va == 1 ? CUColor : iff_5
iff_7 = va == 2 ? ADColor : NDColor
iff_8 = va == 1 ? CDColor : iff_7
candleColor = isBull ? iff_6 : iff_8
barcolor(candleColor)

redGreen = candleColor ==  color.lime and candleColor[1] == color.red
greenRed = candleColor ==  color.red and candleColor[1] == color.lime
redBlue = candleColor ==  color.blue and candleColor[1] == color.red
blueRed = candleColor ==  color.red and candleColor[1] == color.blue
greenPurpule = candleColor ==  color.fuchsia and candleColor[1] == color.lime
purpleGreen = candleColor ==  color.lime and candleColor[1] == color.fuchsia
bluePurpule = candleColor ==  color.fuchsia and candleColor[1] == color.blue
purpleBlue = candleColor ==  color.blue and candleColor[1] == color.fuchsia