tmnkin

Thiru's Multi Time Frame MA Duplicates

Description:
// - One stop shop for multiple MA duplicates over different resolutions.
// - A veritable banquet of MA's to choose from.
// - Set up you MA variables, and then plot up to 4 duplicates all using different time frames.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?
//@version=2

study(title="Thiru's Multi Time Frame MA Duplicates", shorttitle="Thiru MTF MA ", overlay=true)

// Revision:    2 ( r1 - Multi Time Frame MA Duplicates )
// Author:      JayRogers
//
// Honorable Mentions:
//  - @RicardoSantos - helped me fix my brain. Narf.
//
// Description:
//  - One stop shop for multiple MA duplicates over different resolutions.
//  - A veritable banquet of MA's to choose from.
//  - Set up you MA variables, and then plot up to 4 duplicates all using different time frames.
// r2 Changes:
//  - Simple "Drag Length" for colour switch - average for X bars back versus current, it's like a crossover, but cleaner.
//  - Added Arnaud Legoux moving average and relevant inputs to the array of MA choices!

// - INPUTS START
maType      = input(defval="SMMA", title="MA Type: SMA, EMA, DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA ( case sensitive )", type=string)
maSource    = input(defval=open, title="MA Source", type=source)
maLength    = input(defval=5, title="MA Period", minval=1)
dragLength  = input(defval=5, title="Colour Switch Average Period", minval=0)
lsmaOffset  = input(defval=0, title="* Least Squares (LSMA) Only - Offset Value", minval=0)
almaOffset  = input(defval=0.85, title="* Arnaud Legoux (ALMA) Only - Offset Value", minval=0, step=0.01)
almaSigma   = input(defval=6, title="* Arnaud Legoux (ALMA) Only - Sigma Value", minval=0)
res1        = input(defval="D", title="MA Duplicate 1 Resolution", type=resolution)
res2        = input(defval="120", title="MA Duplicate 2 Resolution", type=resolution)
res3        = input(defval="240", title="MA Duplicate 3 Resolution", type=resolution)
res4        = input(defval="60", title="MA Duplicate 4 Resolution", type=resolution)
// - INPUTS END

// - FUNCTIONS
// Returns chosen MA input calculation, default to SMA if blank or typo.
variant(type, src, len) =>
    v1 = sma(src, len)                                                  // Simple
    v2 = ema(src, len)                                                  // Exponential
    v3 = 2 * v2 - ema(v2, len)                                          // Double Exponential
    v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len)               // Triple Exponential
    v5 = wma(src, len)                                                  // Weighted
    v6 = vwma(src, len)                                                 // Volume Weighted
    v7 = na(v5[1]) ? sma(src, len) : (v1[1] * (len - 1) + src) / len    // Smoothed
    v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))   // Hull
    v9 = linreg(src, len, lsmaOffset)                                   // Least Squares
    v10 = alma(src, len, almaOffset, almaSigma)                         // Arnaud Legoux
    type=="EMA"?v2 : type=="DEMA"?v3 : type=="TEMA"?v4 : type=="WMA"?v5 : type=="VWMA"?v6 : type=="SMMA"?v7 : type=="HullMA"?v8 : type=="LSMA"?v9 : type=="ALMA"?v10 : v1
// return selected resolution series
reso(exp, res) => security(tickerid, res, exp)
// simple dragging average directional change switch
dragSwitch(exp, res, len) =>
    average = len >= 2 ? sum(exp, len) / len : exp[1]
    up      = exp > average
    down    = exp < average
    state   = reso(up ? true : down ? false : up[1], res)
// - FUNCTIONS END

// - SERIES VARIABLES
ma1 = reso(variant(maType, maSource, maLength), res1)
ma2 = reso(variant(maType, maSource, maLength), res2)
ma3 = reso(variant(maType, maSource, maLength), res3)
ma4 = reso(variant(maType, maSource, maLength), res4)
upColour    = #33CC33
downColour  = #FF5555
// - SERIES VARIABLES END

// - PLOTTING
plot(ma1, title = "MA 1", color = dragSwitch(ma1, res1, dragLength) ? upColour : downColour, linewidth = 1, style = line, transp = 20)
plot(ma2, title = "MA 2", color = dragSwitch(ma2, res2, dragLength) ? upColour : downColour, linewidth = 2, style = line, transp = 20)
plot(ma3, title = "MA 3", color = dragSwitch(ma3, res3, dragLength) ? upColour : downColour, linewidth = 3, style = line, transp = 20)
plot(ma4, title = "MA 4", color = dragSwitch(ma4, res4, dragLength) ? upColour : downColour, linewidth = 4, style = line, transp = 20)
// - PLOTTING END