Acceleration-Based MA Slope Prediction
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Seungdori_
//@version=5
indicator('Acceleration-Based MA Slope Prediction', shorttitle='MA Slope Prediction', overlay=true, max_bars_back = 2000)

// Inputs
length = input.int(50, minval=1, title='Length', group = 'MA')
src = input.source(defval = close, title = 'Source', group = 'MA')
ma1_type = input.string("T3", title="Moving Average Type", options=['SMA', 'EMA', 'HMA',  'WMA', 'T3'], group = 'MA') 
Offset = input.int(defval = 5 , title = 'Offset for Slope Prediction')


//Region : Function//


MA(ma_type, length) =>
    maPrice = ta.ema(src, length)
    if ma_type == 'SMA'
        maPrice := ta.sma(src, length)
        maPrice
    if ma_type == 'EMA'
        maPrice := ta.ema(src, length)
    if ma_type == 'HMA'
        maPrice := ta.hma(src, length)
        maPrice
    if ma_type == 'WMA'
        maPrice := ta.wma(src, length)
        maPrice
    if ma_type == 'T3'
        axe1 = ta.ema(src, length)
        axe2 = ta.ema(axe1, length)
        axe3 = ta.ema(axe2, length)
        axe4 = ta.ema(axe3, length)
        axe5 = ta.ema(axe4, length)
        axe6 = ta.ema(axe5, length)
        ab = 0.7
        ac1 = -ab * ab * ab
        ac2 = 3 * ab * ab + 3 * ab * ab * ab
        ac3 = -6 * ab * ab - 3 * ab - 3 * ab * ab * ab
        ac4 = 1 + 3 * ab + ab * ab * ab + 3 * ab * ab
        maPrice := ac1 * axe6 + ac2 * axe5 + ac3 * axe4 + ac4 * axe3
        maPrice
    maPrice

//Define MA

movingAverage = MA(ma1_type, length)


// Calculating Slope And Acceleration
slope = movingAverage - movingAverage[1]
acceleration = slope - slope[1]

// Find Zero Point
zeroAccelIndex = -1
for i = 0 to 200 by 1
    if zeroAccelIndex == -1 and acceleration[i] * acceleration[i + 1] <= 0
        zeroAccelIndex := i
        zeroAccelIndex

var derivativeIndex = -1
for i = 0 to 200 by 1
    if derivativeIndex == -1 and slope[i] * slope[i + 1] <= 0
        derivativeIndex := i
        derivativeIndex - Offset

var find_zero_acc = false
if zeroAccelIndex == 0
    find_zero_acc := true
if find_zero_acc[1] == true
    find_zero_acc := false

// Calculation from Acc
a = acceleration[zeroAccelIndex + 1]
b = slope[zeroAccelIndex + 1]
c = movingAverage[zeroAccelIndex + 1]
x = zeroAccelIndex
// Calculation from Slope


der_a = ta.valuewhen(find_zero_acc==true, a, 0)
der_b = slope[derivativeIndex + 1]
der_c = movingAverage[derivativeIndex + 1]
der_x = derivativeIndex


//quadratic(x) =>
//    a * math.pow(x, 2) + b * x + c

acc_quadratic = a * math.pow(x, 2) + b * (x) + c
slope_quadratic = der_a * math.pow(der_x+Offset, 2) + der_b * (der_x+Offset) + der_c 

plot(acc_quadratic, color= color.new(color.white, 20), offset = 0, style = plot.style_circles)
plot(slope_quadratic, color= color.rgb(68, 161, 232), offset = Offset, style = plot.style_circles)
plot(movingAverage, color=movingAverage > movingAverage[1] ? color.new(color.green,20) : color.new(color.red, 20))