Skip to content

Instantly share code, notes, and snippets.

@kesor
Created May 23, 2024 23:21
Show Gist options
  • Save kesor/a4abb0eae4bf5c9f92bd910d90bd0307 to your computer and use it in GitHub Desktop.
Save kesor/a4abb0eae4bf5c9f92bd910d90bd0307 to your computer and use it in GitHub Desktop.
Multiple Moving Averages for TradingView Pine Script
// @version=5
indicator(title="Multi-MA", shorttitle="MA", overlay=true)
var string MA_EMA = "EMA" // (Exponential Moving Average)"
var string MA_SMA = "SMA" // (Simple Moving Average)"
var string MA_WMA = "WMA" // (Weighted Moving Average)"
var string MA_HMA = "HMA" // (Hull Moving Average)"
var string MA_RMA = "RMA" // (Relative Moving Average)"
var string MA_SWMA = "SWMA" // (Symmetrically-Weighted Moving Average)"
var string MA_VWMA = "VWMA" // (Volume-Weighted Moving Average)"
var string MA_VWAP = "VWAP" // (Volume-Weighted Average Price)"
var transparent = color.rgb(255,255,255,100)
var sym_format = "#.##"
calcMA(_type, _length, _src) =>
switch _type
MA_EMA => ta.ema(_src, _length)
MA_SMA => ta.sma(_src, _length)
MA_WMA => ta.wma(_src, _length)
MA_HMA => ta.hma(_src, _length)
MA_RMA => ta.rma(_src, _length)
MA_SWMA => ta.swma(_src)
MA_VWMA => ta.vwma(_src, _length)
MA_VWAP => ta.vwap(_src)
=> na
// START === Moving Average A ===
string type_A = input.string(MA_SMA, "Type", [MA_SMA, MA_EMA, MA_WMA, MA_HMA, MA_RMA, MA_SWMA, MA_VWMA, MA_VWAP], inline="AA", group="A", tooltip="Select the type of moving average to display.")
int length_A = input.int(200, "Length", inline="AA", group="A", tooltip="Set the number of periods to calculate the moving average. Higher values result in smoother lines.")
string resolu_A = input.timeframe("", "Resolution", inline="AB", group="A", tooltip="Set the resolution for the moving average calculation. Leave blank to use the chart's current timeframe.")
series float source_A = input.source(close, "Source", inline="AB", group="A", tooltip="Select the price source for the moving average calculation, such as close, open, high, or low.")
int thickness_A = input.int(4, "Thickness", [1, 2, 3, 4, 5, 6, 7, 8], inline="AC", group="A", tooltip="Adjust the thickness of the moving average line to enhance visibility.")
string size_A = input.string(size.large, "Label", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline="AC", group="A", tooltip="Choose the size of the label text displayed with the moving average line.")
color color_A = input.color(color.rgb(250, 250, 110), "", inline="AD", group="A", tooltip="Select the color of the moving average line and its label text.")
bool show_line_A = input.bool(true, "Line?", inline="AD", group="A", tooltip="Toggle to show or hide the moving average line on the chart.")
bool show_label_A = input.bool(true, "Label?", inline="AD", group="A", tooltip="Toggle to show or hide the label for the moving average.")
bool merge_gaps_A = input.bool(true, "Gaps", inline="AD", group="A", tooltip="Enable to merge gaps in the data. This is useful when the data contains missing periods or gaps.")
series float secure_A = request.security(syminfo.tickerid, resolu_A, source_A, merge_gaps_A ? barmerge.gaps_on : barmerge.gaps_off)
series float ma_A = calcMA(type_A, length_A, secure_A)
plot_ma_A = plot(ma_A, color=color_A, title="A", linewidth=thickness_A, display=show_line_A ? display.all : display.none, join=true)
string text_label_A = syminfo.ticker + " " + type_A + " " + str.tostring(length_A) + " " + resolu_A + " : " + str.tostring(ma_A, sym_format) + " / " + " " + str.tostring(ma_A - secure_A, sym_format) + " / " + str.tostring( (ma_A - secure_A)/secure_A, "#.##%" )
var label label_A = na
if (show_label_A)
if (na(label_A))
label_A := label.new(bar_index, ma_A, text_label_A, color=transparent, style=label.style_label_left, textcolor=color_A, textalign=text.align_left, size=size_A)
else
label.set_xy(label_A, bar_index - 1, ma_A)
label.set_text(label_A, text_label_A)
else
if (not na(label_A))
label.delete(label_A)
label_A := na
// END === Moving Average A ===
// START === Moving Average B ===
string type_B = input.string(MA_SMA, "Type", [MA_SMA, MA_EMA, MA_WMA, MA_HMA, MA_RMA, MA_SWMA, MA_VWMA, MA_VWAP], inline="BA", group="B", tooltip="Select the type of moving average to display.")
int length_B = input.int(100, "Length", inline="BA", group="B", tooltip="Set the number of periods to calculate the moving average. Higher values result in smoother lines.")
string resolu_B = input.timeframe("", "Resolution", inline="BB", group="B", tooltip="Set the resolution for the moving average calculation. Leave blank to use the chart's current timeframe.")
series float source_B = input.source(close, "Source", inline="BB", group="B", tooltip="Select the price source for the moving average calculation, such as close, open, high, or low.")
int thickness_B = input.int(3, "Thickness", [1, 2, 3, 4, 5, 6, 7, 8], inline="BC", group="B", tooltip="Adjust the thickness of the moving average line to enhance visibility.")
string size_B = input.string(size.normal, "Label", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline="BC", group="B", tooltip="Choose the size of the label text displayed with the moving average line.")
color color_B = input.color(color.rgb(0, 191, 255), "", inline="BD", group="B", tooltip="Select the color of the moving average line and its label text.")
bool show_line_B = input.bool(true, "Line?", inline="BD", group="B", tooltip="Toggle to show or hide the moving average line on the chart.")
bool show_label_B = input.bool(true, "Label?", inline="BD", group="B", tooltip="Toggle to show or hide the label for the moving average.")
bool merge_gaps_B = input.bool(true, "Gaps", inline="BD", group="B", tooltip="Enable to merge gaps in the data. This is useful when the data contains missing periods or gaps.")
series float secure_B = request.security(syminfo.tickerid, resolu_B, source_B, merge_gaps_B ? barmerge.gaps_on : barmerge.gaps_off)
series float ma_B = calcMA(type_B, length_B, secure_B)
plot_ma_B = plot(ma_B, color=color_B, title="B", linewidth=thickness_B, display=show_line_B ? display.all : display.none, join=true)
string text_label_B = syminfo.ticker + " " + type_B + " " + str.tostring(length_B) + " " + resolu_B + " : " + str.tostring(ma_B, sym_format) + " / " + " " + str.tostring(ma_B - secure_B, sym_format) + " / " + str.tostring( (ma_B - secure_B)/secure_B, "#.##%" )
var label label_B = na
if (show_label_B)
if (na(label_B))
label_B := label.new(bar_index, ma_B, text_label_B, color=transparent, style=label.style_label_left, textcolor=color_B, textalign=text.align_left, size=size_B)
else
label.set_xy(label_B, bar_index - 1, ma_B)
label.set_text(label_B, text_label_B)
else
if (not na(label_B))
label.delete(label_B)
label_B := na
// END === Moving Average B ===
// START === Moving Average C ===
string type_C = input.string(MA_SMA, "Type", [MA_SMA, MA_EMA, MA_WMA, MA_HMA, MA_RMA, MA_SWMA, MA_VWMA, MA_VWAP], inline="CA", group="C", tooltip="Select the type of moving average to display.")
int length_C = input.int(50, "Length", inline="CA", group="C", tooltip="Set the number of periods to calculate the moving average. Higher values result in smoother lines.")
string resolu_C = input.timeframe("", "Resolution", inline="CB", group="C", tooltip="Set the resolution for the moving average calculation. Leave blank to use the chart's current timeframe.")
series float source_C = input.source(close, "Source", inline="CB", group="C", tooltip="Select the price source for the moving average calculation, such as close, open, high, or low.")
int thickness_C = input.int(2, "Thickness", [1, 2, 3, 4, 5, 6, 7, 8], inline="CC", group="C", tooltip="Adjust the thickness of the moving average line to enhance visibility.")
string size_C = input.string(size.normal, "Label", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline="CC", group="C", tooltip="Choose the size of the label text displayed with the moving average line.")
color color_C = input.color(color.rgb(148, 0, 211), "", inline="CD", group="C", tooltip="Select the color of the moving average line and its label text.")
bool show_line_C = input.bool(true, "Line?", inline="CD", group="C", tooltip="Toggle to show or hide the moving average line on the chart.")
bool show_label_C = input.bool(true, "Label?", inline="CD", group="C", tooltip="Toggle to show or hide the label for the moving average.")
bool merge_gaps_C = input.bool(true, "Gaps", inline="CD", group="C", tooltip="Enable to merge gaps in the data. This is useful when the data contains missing periods or gaps.")
series float secure_C = request.security(syminfo.tickerid, resolu_C, source_C, merge_gaps_C ? barmerge.gaps_on : barmerge.gaps_off)
series float ma_C = calcMA(type_C, length_C, secure_C)
plot_ma_C = plot(ma_C, color=color_C, title="C", linewidth=thickness_C, display=show_line_C ? display.all : display.none, join=true)
string text_label_C = syminfo.ticker + " " + type_C + " " + str.tostring(length_C) + " " + resolu_C + " : " + str.tostring(ma_C, sym_format) + " / " + " " + str.tostring(ma_C - secure_C, sym_format) + " / " + str.tostring( (ma_C - secure_C)/secure_C, "#.##%" )
var label label_C = na
if (show_label_C)
if (na(label_C))
label_C := label.new(bar_index, ma_C, text_label_C, color=transparent, style=label.style_label_left, textcolor=color_C, textalign=text.align_left, size=size_C)
else
label.set_xy(label_C, bar_index - 1, ma_C)
label.set_text(label_C, text_label_C)
else
if (not na(label_C))
label.delete(label_C)
label_C := na
// END === Moving Average C ===
// START === Moving Average D ===
string type_D = input.string(MA_EMA, "Type", [MA_SMA, MA_EMA, MA_WMA, MA_HMA, MA_RMA, MA_SWMA, MA_VWMA, MA_VWAP], inline="DA", group="D", tooltip="Select the type of moving average to display.")
int length_D = input.int(9, "Length", inline="DA", group="D", tooltip="Set the number of periods to calculate the moving average. Higher values result in smoother lines.")
string resolu_D = input.timeframe("", "Resolution", inline="DB", group="D", tooltip="Set the resolution for the moving average calculation. Leave blank to use the chart's current timeframe.")
series float source_D = input.source(close, "Source", inline="DB", group="D", tooltip="Select the price source for the moving average calculation, such as close, open, high, or low.")
int thickness_D = input.int(1, "Thickness", [1, 2, 3, 4, 5, 6, 7, 8], inline="DC", group="D", tooltip="Adjust the thickness of the moving average line to enhance visibility.")
string size_D = input.string(size.small, "Label", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline="DC", group="D", tooltip="Choose the size of the label text displayed with the moving average line.")
color color_D = input.color(color.rgb(34, 139, 34), "", inline="DD", group="D", tooltip="Select the color of the moving average line and its label text.")
bool show_line_D = input.bool(false, "Line?", inline="DD", group="D", tooltip="Toggle to show or hide the moving average line on the chart.")
bool show_label_D = input.bool(false, "Label?", inline="DD", group="D", tooltip="Toggle to show or hide the label for the moving average.")
bool merge_gaps_D = input.bool(true, "Gaps", inline="DD", group="D", tooltip="Enable to merge gaps in the data. This is useful when the data contains missing periods or gaps.")
series float secure_D = request.security(syminfo.tickerid, resolu_D, source_D, merge_gaps_D ? barmerge.gaps_on : barmerge.gaps_off)
series float ma_D = calcMA(type_D, length_D, secure_D)
plot_ma_D = plot(ma_D, color=color_D, title="D", linewidth=thickness_D, display=show_line_D ? display.all : display.none, join=true)
string text_label_D = syminfo.ticker + " " + type_D + " " + str.tostring(length_D) + " " + resolu_D + " : " + str.tostring(ma_D, sym_format) + " / " + " " + str.tostring(ma_D - secure_D, sym_format) + " / " + str.tostring( (ma_D - secure_D)/secure_D, "#.##%" )
var label label_D = na
if (show_label_D)
if (na(label_D))
label_D := label.new(bar_index, ma_D, text_label_D, color=transparent, style=label.style_label_left, textcolor=color_D, textalign=text.align_left, size=size_D)
else
label.set_xy(label_D, bar_index - 1, ma_D)
label.set_text(label_D, text_label_D)
else
if (not na(label_D))
label.delete(label_D)
label_D := na
// END === Moving Average D ===
// START === Moving Average E ===
string type_E = input.string(MA_EMA, "Type", [MA_SMA, MA_EMA, MA_WMA, MA_HMA, MA_RMA, MA_SWMA, MA_VWMA, MA_VWAP], inline="EA", group="E", tooltip="Select the type of moving average to display.")
int length_E = input.int(20, "Length", inline="EA", group="E", tooltip="Set the number of periods to calculate the moving average. Higher values result in smoother lines.")
string resolu_E = input.timeframe("", "Resolution", inline="EB", group="E", tooltip="Set the resolution for the moving average calculation. Leave blank to use the chart's current timeframe.")
series float source_E = input.source(close, "Source", inline="EB", group="E", tooltip="Select the price source for the moving average calculation, such as close, open, high, or low.")
int thickness_E = input.int(2, "Thickness", [1, 2, 3, 4, 5, 6, 7, 8], inline="EC", group="E", tooltip="Adjust the thickness of the moving average line to enhance visibility.")
string size_E = input.string(size.normal, "Label", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline="EC", group="E", tooltip="Choose the size of the label text displayed with the moving average line.")
color color_E = input.color(color.rgb(220, 20, 60), "", inline="ED", group="E", tooltip="Select the color of the moving average line and its label text.")
bool show_line_E = input.bool(true, "Line?", inline="ED", group="E", tooltip="Toggle to show or hide the moving average line on the chart.")
bool show_label_E = input.bool(true, "Label?", inline="ED", group="E", tooltip="Toggle to show or hide the label for the moving average.")
bool merge_gaps_E = input.bool(true, "Gaps", inline="ED", group="E", tooltip="Enable to merge gaps in the data. This is useful when the data contains missing periods or gaps.")
series float secure_E = request.security(syminfo.tickerid, resolu_E, source_E, merge_gaps_E ? barmerge.gaps_on : barmerge.gaps_off)
series float ma_E = calcMA(type_E, length_E, secure_E)
plot_ma_E = plot(ma_E, color=color_E, title="E", linewidth=thickness_E, display=show_line_E ? display.all : display.none, join=true)
string text_label_E = syminfo.ticker + " " + type_E + " " + str.tostring(length_E) + " " + resolu_E + " : " + str.tostring(ma_E, sym_format) + " / " + " " + str.tostring(ma_E - secure_E, sym_format) + " / " + str.tostring( (ma_E - secure_E)/secure_E, "#.##%" )
var label label_E = na
if (show_label_E)
if (na(label_E))
label_E := label.new(bar_index, ma_E, text_label_E, color=transparent, style=label.style_label_left, textcolor=color_E, textalign=text.align_left, size=size_E)
else
label.set_xy(label_E, bar_index - 1, ma_E)
label.set_text(label_E, text_label_E)
else
if (not na(label_E))
label.delete(label_E)
label_E := na
// END === Moving Average E ===
// START === Moving Average F ===
string type_F = input.string(MA_EMA, "Type", [MA_SMA, MA_EMA, MA_WMA, MA_HMA, MA_RMA, MA_SWMA, MA_VWMA, MA_VWAP], inline="FA", group="F", tooltip="Select the type of moving average to display.")
int length_F = input.int(50, "Length", inline="FA", group="F", tooltip="Set the number of periods to calculate the moving average. Higher values result in smoother lines.")
string resolu_F = input.timeframe("", "Resolution", inline="FB", group="F", tooltip="Set the resolution for the moving average calculation. Leave blank to use the chart's current timeframe.")
series float source_F = input.source(close, "Source", inline="FB", group="F", tooltip="Select the price source for the moving average calculation, such as close, open, high, or low.")
int thickness_F = input.int(3, "Thickness", [1, 2, 3, 4, 5, 6, 7, 8], inline="FC", group="F", tooltip="Adjust the thickness of the moving average line to enhance visibility.")
string size_F = input.string(size.large, "Label", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], inline="FC", group="F", tooltip="Choose the size of the label text displayed with the moving average line.")
color color_F = input.color(color.rgb(65, 105, 225), "", inline="FD", group="F", tooltip="Select the color of the moving average line and its label text.")
bool show_line_F = input.bool(false, "Line?", inline="FD", group="F", tooltip="Toggle to show or hide the moving average line on the chart.")
bool show_label_F = input.bool(false, "Label?", inline="FD", group="F", tooltip="Toggle to show or hide the label for the moving average.")
bool merge_gaps_F = input.bool(true, "Gaps", inline="FD", group="F", tooltip="Enable to merge gaps in the data. This is useful when the data contains missing periods or gaps.")
series float secure_F = request.security(syminfo.tickerid, resolu_F, source_F, merge_gaps_F ? barmerge.gaps_on : barmerge.gaps_off)
series float ma_F = calcMA(type_F, length_F, secure_F)
plot_ma_F = plot(ma_F, color=color_F, title="F", linewidth=thickness_F, display=show_line_F ? display.all : display.none, join=true)
string text_label_F = syminfo.ticker + " " + type_F + " " + str.tostring(length_F) + " " + resolu_F + " : " + str.tostring(ma_F, sym_format) + " / " + " " + str.tostring(ma_F - secure_F, sym_format) + " / " + str.tostring( (ma_F - secure_F)/secure_F, "#.##%" )
var label label_F = na
if (show_label_F)
if (na(label_F))
label_F := label.new(bar_index, ma_F, text_label_F, color=transparent, style=label.style_label_left, textcolor=color_F, textalign=text.align_left, size=size_F)
else
label.set_xy(label_F, bar_index - 1, ma_F)
label.set_text(label_F, text_label_F)
else
if (not na(label_F))
label.delete(label_F)
label_F := na
// END === Moving Average F ===
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment