Cumulative Symbol
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syntaxgeek

//           _____            _____                   _____            _____                   _____                                          _____                   _____                   _____                   _____          
//          /\    \          |\    \                 /\    \          /\    \                 /\    \                ______                  /\    \                 /\    \                 /\    \                 /\    \         
//         /::\    \         |:\____\               /::\____\        /::\    \               /::\    \              |::|   |                /::\    \               /::\    \               /::\    \               /::\____\        
//        /::::\    \        |::|   |              /::::|   |        \:::\    \             /::::\    \             |::|   |               /::::\    \             /::::\    \             /::::\    \             /:::/    /        
//       /::::::\    \       |::|   |             /:::::|   |         \:::\    \           /::::::\    \            |::|   |              /::::::\    \           /::::::\    \           /::::::\    \           /:::/    /         
//      /:::/\:::\    \      |::|   |            /::::::|   |          \:::\    \         /:::/\:::\    \           |::|   |             /:::/\:::\    \         /:::/\:::\    \         /:::/\:::\    \         /:::/    /          
//     /:::/__\:::\    \     |::|   |           /:::/|::|   |           \:::\    \       /:::/__\:::\    \          |::|   |            /:::/  \:::\    \       /:::/__\:::\    \       /:::/__\:::\    \       /:::/____/           
//     \:::\   \:::\    \    |::|   |          /:::/ |::|   |           /::::\    \     /::::\   \:::\    \         |::|   |           /:::/    \:::\    \     /::::\   \:::\    \     /::::\   \:::\    \     /::::\    \           
//   ___\:::\   \:::\    \   |::|___|______   /:::/  |::|   | _____    /::::::\    \   /::::::\   \:::\    \        |::|   |          /:::/    / \:::\    \   /::::::\   \:::\    \   /::::::\   \:::\    \   /::::::\____\________  
//  /\   \:::\   \:::\    \  /::::::::\    \ /:::/   |::|   |/\    \  /:::/\:::\    \ /:::/\:::\   \:::\    \ ______|::|___|___ ____ /:::/    /   \:::\ ___\ /:::/\:::\   \:::\    \ /:::/\:::\   \:::\    \ /:::/\:::::::::::\    \ 
// /::\   \:::\   \:::\____\/::::::::::\____/:: /    |::|   /::\____\/:::/  \:::\____/:::/  \:::\   \:::\____|:::::::::::::::::|    /:::/____/  ___\:::|    /:::/__\:::\   \:::\____/:::/__\:::\   \:::\____/:::/  |:::::::::::\____\
// \:::\   \:::\   \::/    /:::/~~~~/~~     \::/    /|::|  /:::/    /:::/    \::/    \::/    \:::\  /:::/    |:::::::::::::::::|____\:::\    \ /\  /:::|____\:::\   \:::\   \::/    \:::\   \:::\   \::/    \::/   |::|~~~|~~~~~     
//  \:::\   \:::\   \/____/:::/    /         \/____/ |::| /:::/    /:::/    / \/____/ \/____/ \:::\/:::/    / ~~~~~~|::|~~~|~~~      \:::\    /::\ \::/    / \:::\   \:::\   \/____/ \:::\   \:::\   \/____/ \/____|::|   |          
//   \:::\   \:::\    \  /:::/    /                  |::|/:::/    /:::/    /                   \::::::/    /        |::|   |          \:::\   \:::\ \/____/   \:::\   \:::\    \      \:::\   \:::\    \           |::|   |          
//    \:::\   \:::\____\/:::/    /                   |::::::/    /:::/    /                     \::::/    /         |::|   |           \:::\   \:::\____\      \:::\   \:::\____\      \:::\   \:::\____\          |::|   |          
//     \:::\  /:::/    /\::/    /                    |:::::/    /\::/    /                      /:::/    /          |::|   |            \:::\  /:::/    /       \:::\   \::/    /       \:::\   \::/    /          |::|   |          
//      \:::\/:::/    /  \/____/                     |::::/    /  \/____/                      /:::/    /           |::|   |             \:::\/:::/    /         \:::\   \/____/         \:::\   \/____/           |::|   |          
//       \::::::/    /                               /:::/    /                               /:::/    /            |::|   |              \::::::/    /           \:::\    \              \:::\    \               |::|   |          
//        \::::/    /                               /:::/    /                               /:::/    /             |::|   |               \::::/    /             \:::\____\              \:::\____\              \::|   |          
//         \::/    /                                \::/    /                                \::/    /              |::|___|                \::/____/               \::/    /               \::/    /               \:|   |          
//          \/____/                                  \/____/                                  \/____/                ~~                                              \/____/                 \/____/                 \|___|          

//@version=5
indicator("Cumulative Symbol", precision=2)

// { consts
c_mode_basic = "Basic"
c_mode_ad = "Advance or Decline"
c_mode_internalRatio = "Ratio"
// }

// { inputs
i_symbol = input.symbol("", "Symbol")
i_source = input.source(close, "Source")
i_mode = input.string(c_mode_ad, "Histogram Mode", [c_mode_basic, c_mode_ad, c_mode_internalRatio], "Basic tracks symbol source plain, advancements and declines are added or subtracted respectively from cumulative total or 0 point ratio adjusts internal formula to present directional crosses as well as advancements and declines.")
i_averagePeriod = input.int(8, "Trend Period", minval=1, step=1)
i_colorUp = input.color(color.green, 'Up', group='Colors')
i_colorDown = input.color(color.red, 'Down', group='Colors')
i_colorAvgUp = input.color(color.white, 'Up', group='Trend Colors')
i_colorAvgDown = input.color(color.purple, 'Down', group='Trend Colors')
i_colorStrengthStrong = input.color(color.fuchsia, 'Strong', group='Trend Strength Colors')
i_colorStrengthWeak = input.color(color.yellow, 'Weak', group='Trend Strength Colors')
// }

// { funcs
f_colorTrend(_v, _colorUp, _colorDown, _isTrend) =>
    if _v > 0
        if _v > _v[1]
            _colorUp
        else
            _isTrend ? _colorDown : color.new(_colorUp, 50)
    else
        if _v < _v[1]
            _colorDown
        else
            _isTrend ? _colorUp : color.new(_colorDown, 50)
f_cumOverAverage(_v, _vAvg) =>
    if _v > 0
        _v > _vAvg
    else
        _v < _vAvg
// }

// { vars
var v_cumulative = 0.0
var v_cumulativeAverage = 0.0
var v_cumulativeToAvgDiff = 0.0
var v_diff = 0.0
v_symbolSource = request.security(i_symbol, timeframe.period, i_source)

if not timeframe.isdwm and session.isfirstbar_regular
    v_cumulative := 0.0
    v_cumulativeAverage := 0.0

if i_mode == c_mode_basic
    v_cumulative += v_symbolSource
else if i_mode == c_mode_ad
    if v_symbolSource > v_symbolSource[1]
        v_cumulative += v_symbolSource
    else
        v_cumulative -= v_symbolSource
else if i_mode == c_mode_internalRatio
    v_diff := math.abs(v_symbolSource[1] - v_symbolSource)

    if v_symbolSource > v_symbolSource[1]
        if v_symbolSource < 0
            v_cumulative -= v_symbolSource
        else
            v_cumulative += v_symbolSource
    else
        if v_symbolSource < 0
            v_cumulative += v_symbolSource
        else
            v_cumulative -= v_symbolSource

v_cumulativeAverage := ta.sma(v_cumulative, i_averagePeriod)
v_cumulativeToAvgDiff := math.abs(v_cumulative - v_cumulativeAverage)
v_strongTrend = f_cumOverAverage(v_cumulative, v_cumulativeAverage)
v_trendSlowing = v_cumulativeToAvgDiff < v_cumulativeToAvgDiff[1]
// } vars

// { plots
plot(v_cumulative, title='Cum', style=plot.style_columns, color=f_colorTrend(v_cumulative, i_colorUp, i_colorDown, false))
plot(v_cumulativeAverage, title='Trend', color=color.new(f_colorTrend(v_cumulativeAverage, i_colorAvgUp, i_colorAvgDown, true), 0), linewidth=2)
plotshape(0, 'Strength', shape.circle, location.absolute, color.new(v_strongTrend ? i_colorStrengthStrong : i_colorStrengthWeak, v_trendSlowing ? 30 : 0), size=size.auto)
// }

// { alerts
// }