JayRogers

MTF Any MA Cross

Description:
  • Multitude of MA choices
  • MA's can both be set to different resolutions
  • Crossover/Crossunder, drawn at an averaged position, or on 'follow' (location of MA 1 at time of draw)

Cheers!
Remove from Favorite Scripts Add to Favorite Scripts
//@version=2

study(title="Any MA Crosses", shorttitle="Any MA Crosses", overlay=true)

// Revision:    1
// Author:      JayRogers
//
// Description:
//  - Multitude of MA choices
//  - MA's can both be set to different resolutions
//  - Crossover/Crossunder, drawn at an averaged position, or on 'follow' (location of MA 1 at time of draw)

// - INPUTS START
// first ma
maUseRes1   = input(defval = false, title = "[ MA 1 SECTION ] - Use Different Resolution?")
maReso1     = input(defval = "60", title = "MA 1 - Set Resolution", type = resolution)
maType1     = input(defval = "SMA", title = "MA 1 - Type: SMA, EMA, DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA ( case sensitive )", type = string)
maSource1   = input(defval = open, title = "MA 1 - Source", type = source)
maLength1   = input(defval = 20, title = "MA 1 - Period", minval = 1)
lsmaOffset1 = input(defval = 1, title = "MA 1 - Least Squares (LSMA) Only - Offset Value", minval = 0)
almaOffset1 = input(defval = 0.85, title = "MA 1 - Arnaud Legoux (ALMA) Only - Offset Value", minval = 0, step = 0.01)
almaSigma1  = input(defval = 6, title = "MA 1 - Arnaud Legoux (ALMA) Only - Sigma Value", minval = 0)
// second ma
maUseRes2   = input(defval = false, title = "[ MA 2 SECTION ] - Use Different Resolution?")
maReso2     = input(defval = "60", title = "MA 2 - Set Resolution", type = resolution)
maType2     = input(defval = "EMA", title = "MA 2 - Type: SMA, EMA, DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA ( case sensitive )", type = string)
maSource2   = input(defval = open, title = "MA 2 - Source", type = source)
maLength2   = input(defval = 20, title = "MA 2 - Period", minval = 1)
lsmaOffset2 = input(defval = 1, title = "MA 2 - Least Squares (LSMA) Only - Offset Value", minval = 0)
almaOffset2 = input(defval = 0.85, title = "MA 2 - Arnaud Legoux (ALMA) Only - Offset Value", minval = 0, step = 0.01)
almaSigma2  = input(defval = 6, title = "MA 2 - Arnaud Legoux (ALMA) Only - Sigma Value", minval = 0)
// position
crossPos    = input(defval = true, title = "[ CROSS PLACEMENT ] - Averaged Location (selected) or Follow Location (on MA 1)")
// - INPUTS END

// - FUNCTIONS
// Returns chosen MA input calculation, default to SMA if blank or typo.
variant(type, src, len, lsmaOffset, almaOffset, almaSigma) =>
    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) : (v5[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, use) => use ? security(tickerid, res, exp) : exp

// - FUNCTIONS END

// - SERIES VARIABLES
ma1 = reso(variant(maType1, maSource1, maLength1, lsmaOffset1, almaOffset1, almaSigma1), maReso1, maUseRes1)
ma2 = reso(variant(maType2, maSource2, maLength2, lsmaOffset2, almaOffset2, almaSigma2), maReso2, maUseRes2)
crossUp     = crossover(ma1, ma2)
crossDown   = crossunder(ma1, ma2)
crossAvg    = crossPos ? avg(ma1, ma2) : ma1
// - SERIES VARIABLES END

// - PLOTTING
plot(ma1, title = "MA 1", color = #33CC33, linewidth = 2, style = line, transp = 20)
plot(ma2, title = "MA 2", color = #FF5555, linewidth = 2, style = line, transp = 20)

plot(crossUp ? crossAvg : na, title = "MA 1 Cross Over MA 2", color = #33FF33, linewidth = 4, style = circles, transp = 0)
plot(crossDown ? crossAvg : na, title = "MA 1 Cross Under MA 2", color = #FF3333, linewidth = 4, style = circles, transp = 0)
// - PLOTTING END
Home Stock Screener Forex Screener Crypto Screener Economic Calendar How It Works Chart Features Pricing House Rules Moderators Website & Broker Solutions Widgets Charting Solutions Get Help Feature Request Blog & News FAQ Wiki Twitter
Profile Profile Settings Account and Billing TradingView Coins My Support Tickets Get Help Ideas Published Followers Following Private Messages Chat Sign Out