JayRogers

MTF Any MA Cross - Multi Marker

Honorable Mention:
  • @glaz for jumping on the "on price" suggestion and throwing the code at me - which spurred me to add shapes in too, because why not eh?
Description:
  • Multitude of MA choices.
  • MA's can both be set to different resolutions.
  • Crossover/Crossunder, drawn at an averaged position.
r2 Changes:
  • On-MA cross marker now only drawn at averaged position
  • On price markers added
  • Shape markers added
  • All marker types can be toggled on and off.

We need a proper script versioning system up in here! ...just saying ;)
Remove from Favorite Scripts Add to Favorite Scripts
//@version=2

study(title="MTF Any MA Cross - Multi Marker", shorttitle="Any MA Cross", overlay=true)

// Revision:    2
// Author:      JayRogers
//
// Honorable Mention:
//  - @glaz for jumping on the "on price" suggestion and throwing the code at me - which spurred me to add shapes in too, because why not eh?
//
// Description:
//  - Multitude of MA choices.
//  - MA's can both be set to different resolutions.
//  - Crossover/Crossunder, drawn at an averaged position.
// r2 Changes:
//  - on-MA cross marker now only drawn at averaged position
//  - On price markers added
//  - shapes added
//  - All marker types can be toggled on and off.

// - 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
crossOnLines    = input(defval = true, title = "[ MARKER LOCATIONS ] - Show On MA's? (on the lines)")
crossOnPrice    = input(defval = true, title = "Show On Price? (on the candle)")
crossShapes     = input(defval = true, title = "Show Shapes? (above/below candle)")
// - 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
// ma's
ma1 = reso(variant(maType1, maSource1, maLength1, lsmaOffset1, almaOffset1, almaSigma1), maReso1, maUseRes1)
ma2 = reso(variant(maType2, maSource2, maLength2, lsmaOffset2, almaOffset2, almaSigma2), maReso2, maUseRes2)
// crossing stuffs
crossUp         = crossover(ma1, ma2)
crossDown       = crossunder(ma1, ma2)
crossAvg        = avg(ma1, ma2)
crossPrice      = security(tickerid, "1", close)
crossUpPrice    = valuewhen(crossUp, crossPrice, 0)
crossDownPrice  = valuewhen(crossDown, crossPrice, 0)
// - 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)
plotshape(crossShapes and crossUp, title="Cross Over Shape", style=shape.circle, location=location.abovebar, color=#33FF33, transp=0, size=size.tiny)
plotshape(crossShapes and crossDown, title="Cross Under Shape", style=shape.circle, location=location.belowbar, color=#FF3333, transp=0, size=size.tiny)
plot(crossOnPrice and crossUp ? crossUpPrice : na, title = "On Price Cross Over", color = #33FF33, linewidth = 4, style = circles, transp = 0)
plot(crossOnPrice and crossDown ? crossDownPrice : na, title = "On Price Cross Under", color = #FF3333, linewidth = 4, style = circles, transp = 0)
plot(crossOnLines and crossUp ? crossAvg : na, title = "On MA Cross Over", color = #33FF33, linewidth = 4, style = circles, transp = 0)
plot(crossOnLines and crossDown ? crossAvg : na, title = "On MA Cross Under", color = #FF3333, linewidth = 4, style = circles, transp = 0)
// - PLOTTING END
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out