Multi SMA EMA WMA HMA BB (5+5 Moving Averages of Any Type with Bollinger Bands) by RRB

RagingRocketBull · study · 159 行 · 点赞 1,742 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=3
study(title="Multi SMA EMA WMA HMA BB (5+5 Moving Averages of Any Type with Bollinger Bands) by RRB", shorttitle="MultiSmaEmaBb_RRB", overlay=true)
 
// Multi SMA EMA WMA HMA BB (5+5 Moving Averages of Any Type with Bollinger Bands) by RagingRocketBull 2018
// Version 1.0
//
// This indicator shows multiple MAs of any type (SMA EMA WMA HMA etc) with BB on a chart with/without MTF support depending on the version.
//
// There are several versions: Simple, MTF, Pro MTF and Ultimate MTF. This is the Simple version. The Differences are listed below. All versions have BB
// - Simple: you have 2 groups of MAs that can be assigned any type (5+5)
// - MTF: +assign 1 custom Timeframe to any group combo (5+5 Custom TF)
// - Pro MTF: +multiple Timeframes for multiple MA groups (4*3 MTF), horizontal levels and show max bars back options
// - Ultimate MTF: +individual settings for each MA, multiple Timeframes
//
// Features:
// - 2 groups of custom 5+5 MAs of any type including Hull Moving Average (HMA)
// - BB
 
// 1. based on 3EmaBB, uses plot*, fill, stdev and custom hma functions
// 2. swma has a fixed length = 4, alma and linreg have additional offset and smoothing params
//
// More Scripts:
// https://www.tradingview.com/u/RagingRocketBull/
//
 
// ================================================================================================================
// Inputs
// ================================================================================================================
 
src = input(close, type=source, title="Source")
ema_type = input("ema", type=string, title="EMAs Group MA Type", options=["sma","ema","wma", "swma", "vwma", "hma", "alma", "rma", "linreg"])
ema1_len = input(12, type=integer, minval=0, title="EMA1 length")
ema2_len = input(26, type=integer, minval=0, title="EMA2 length")
ema3_len = input(50, type=integer, minval=0, title="EMA3 length")
ema4_len = input(100, type=integer, minval=0, title="EMA4 length")
ema5_len = input(200, type=integer, minval=0, title="EMA5 length")
sma_type = input("sma", type=string, title="SMAs Group MA Type", options=["sma","ema","wma", "swma", "vwma", "hma", "alma", "rma", "linreg"])
sma1_len = input(0, type=integer, minval=0, title="SMA1 length")
sma2_len = input(20, type=integer, minval=0, title="SMA2 length")
sma3_len = input(50, type=integer, minval=0, title="SMA3 length")
sma4_len = input(100, type=integer, minval=0, title="SMA4 length")
sma5_len = input(200, type=integer, minval=0, title="SMA5 length")
sma_bb_len = input(20, type=integer, minval=0, title="BB length")
bbStdDev = input(2, type=float, minval=0.1, title="BB standard deviation")
showEMAs = input(true, type=bool, title="Show All EMAs")
showSMAs = input(true, type=bool, title="Show All SMAs")
showBB = input(true, type=bool, title="Show All BB")
alma_offset = input(0.85, type=float, minval=0, title="ALMA Offset (for alma only)")
alma_sigma = input(6, type=float, minval=0, title="ALMA Sigma Smoothing (for alma only)")
linreg_offset = input(0, type=integer, minval=0, title="Linreg Offset (for linreg only)")
 
// ================================================================================================================
// Constants
// ================================================================================================================
 
// init color constants
// you can't set color_transp in input - it can't be a variable, must be const literal
color_transp = 20
 
ema1_color = color(aqua, color_transp)
ema2_color = color(purple, color_transp)
ema3_color = color(#CC0000, color_transp) // red
ema4_color = color(#FFD966, color_transp) // yellow
ema5_color = color(#1155CC, color_transp) // blue
 
sma1_color = color(aqua, color_transp)
sma2_color = color(green, color_transp)
sma3_color = color(#CC0000, color_transp) // red 
sma4_color = color(#FFD966, color_transp) // yellow
sma5_color = color(#1155CC, color_transp) // blue
 
bb_border_color = color(#3D85C6,45) // light blue
bb_median_color = color(#989898, color_transp) // grey 
bb_bgcolor = #6D9EEB // light blue
 
 
// ================================================================================================================
// Functions
// ================================================================================================================
 
// Hull Moving Average
// there's no built-in hma function
hma(src, length) =>
    //HMA = WMA(2*WMA(n/2) - WMA(n)),sqrt(n))
    return = wma((2 * wma(src, length/2)) - wma(src, length), round(sqrt(length)))
 
// returns ma value by type and length
ma(src, type, length) =>
    // wrapper max(1,length)) prevents ema(src, 0) type errors because all cond branches are always fully executed even when length == 0 (there's no fast cond evaluation)
    len = max(1,length)
    // for proper line wrapping/continuation you need indentation > multiple of 4 (4 spaces or 1 tab), i.e. 5 spaces
    // swma has a fixed length = 4, alma has its own smoothing params
    type == "sma"? sma(src, len) : 
         type == "ema"? ema(src, len) : 
             type == "wma"? wma(src, len) : 
                 type == "vwma"? vwma(src, len) : 
                     type == "swma"? swma(src) : 
                         type == "hma"? hma(src, len) : 
                             type == "alma"? alma(src, len, alma_offset, alma_sigma) : 
                                 type == "rma"? rma(src, len) : 
                                     type == "linreg"? linreg(src, len, linreg_offset) : na
 
 
// ================================================================================================================
// Processing
// ================================================================================================================
 
// EMA Processing
 
// ema_len == 0 - disable certain ema
ema1 = ema1_len > 0 ? ma(src, ema_type, ema1_len) : na
ema2 = ema2_len > 0 ? ma(src, ema_type, ema2_len) : na
ema3 = ema3_len > 0 ? ma(src, ema_type, ema3_len) : na
ema4 = ema4_len > 0 ? ma(src, ema_type, ema4_len) : na
ema5 = ema5_len > 0 ? ma(src, ema_type, ema5_len) : na
 
// showEMAs - enable/disable all emas
plot(showEMAs ? ema1 : na, color=ema1_color, linewidth=1, title="EMA1", style=line)
plot(showEMAs ? ema2 : na, color=ema2_color, linewidth=1, title="EMA2", style=line)
plot(showEMAs ? ema3 : na, color=ema3_color, linewidth=2, title="EMA3", style=line)
plot(showEMAs ? ema4 : na, color=ema4_color, linewidth=2, title="EMA4", style=line)
plot(showEMAs ? ema5 : na, color=ema5_color, linewidth=2, title="EMA5", style=line)
 
 
// SMA Processing
 
// sma_len == 0 - disable certain sma
sma1 = sma1_len > 0 ? ma(src, sma_type, sma1_len) : na
sma2 = sma2_len > 0 ? ma(src, sma_type, sma2_len) : na
sma3 = sma3_len > 0 ? ma(src, sma_type, sma3_len) : na
sma4 = sma4_len > 0 ? ma(src, sma_type, sma4_len) : na
sma5 = sma5_len > 0 ? ma(src, sma_type, sma5_len) : na
 
// showSMAs - enable/disable all smas
plot(showSMAs ? sma1 : na, color=sma1_color, linewidth=1, title="SMA1", style=circles)
plot(showSMAs ? sma2 : na, color=sma2_color, linewidth=1, title="SMA2", style=circles)
plot(showSMAs ? sma3 : na, color=sma3_color, linewidth=2, title="SMA3", style=circles)
plot(showSMAs ? sma4 : na, color=sma4_color, linewidth=2, title="SMA4", style=circles)
plot(showSMAs ? sma5 : na, color=sma5_color, linewidth=2, title="SMA5", style=circles)
 
 
// BB Processing
 
// sma_bb_len == 0 - disable middle sma_bb
sma_bb = sma_bb_len > 0 ? ma(src, "sma", sma_bb_len) : na
 
// showBB - enable/disable all BB smas
plot(showBB ? sma_bb : na, color=bb_median_color, linewidth=1, title="Middle BB") // , style=circles
 
// stdev_bb = stdev(bb_src, sma_bb_len) * bbStdDev
// wrapper max(1,len)) prevents stdev(src, 0) type errors because all cond branches are always fully executed even when len == 0 (there's no fast cond evaluation)
stdev_bb = sma_bb_len > 0 ? stdev(src, max(1,sma_bb_len)) * bbStdDev : na
sma_bb_up = sma_bb + stdev_bb
sma_bb_down = sma_bb - stdev_bb
 
plot_bb_up = plot(showBB ? sma_bb_up : na, color=bb_border_color, linewidth=1, title="Upper BB") // , style=cross
plot_bb_down = plot(showBB ? sma_bb_down : na, color=bb_border_color, linewidth=1, title="Lower BB") // , style=cross
// fill only works if both plot_bb_up and plot_bb_down are not na
fill(plot_bb_up, plot_bb_down, color=bb_bgcolor, transp=90, title="BB Background")

← 返回列表