2 MA Ratio Can Help with Moving Averages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradeStation
//@version=5
//MA types are:
// 1 - simple
// 2 - exponential

// Define the variables
indicator(title="2 MA Ratio", shorttitle="2 MA Ratio", overlay=false)
fastLength = input(8, title="Fast MA Length")
slowLength = input(21, title="Slow MA Length")
fastSrc = input(close, title="Fast MA Price Source")
slowSrc = input(close, title="Slow MA Price Source")
fastMAType = input(2, title="Fast AvgType")
slowMAType = input(2, title="Slow AvgType")
color plotColor = input.color(color.gray, "Ratio Plot Color")

var float fastAvg = 0
var float slowAvg = 0
var float ratio = 0

// Handle user choices
if ( fastMAType == 1 )
    fastAvg := ta.sma(fastSrc, fastLength)
if ( fastMAType == 2 )
    fastAvg := ta.ema(fastSrc, fastLength)
if ( slowMAType == 1 )
    slowAvg := ta.sma(slowSrc, slowLength)
if ( fastMAType == 2 )
    slowAvg := ta.ema(slowSrc, slowLength)

// Perform calculation
if (slowAvg != 0)
    ratio := (fastAvg / slowAvg) - 1  
else
    ratio := 0

//Set the color
if ( ratio > 0 ) 
    plotColor := input.color(color.green, "Ratio Plot Color")
if ( ratio < 0 ) 
    plotColor := input.color(color.red, "Ratio Plot Color")

// Plot the ratio with a zero midline
plot(ratio, color=plotColor)
plot(0)