Pine Script用于多个时间框架上的几个ema



我正试图建立一个脚本,多次显示相同的EMA长度,但在不同的时间框架上运行。我搞不懂时间框架。无论我做什么,它只能让我调整所有的ema为一个时间框架,我真的想有相同的ema,但在许多不同的时间线。

例如,我想在1分钟的图表上显示6个不同的20个EMA,但6个EMA中的每一个都在不同的时间框架上。即1分钟、10分钟、60分钟、240分钟等。

需要在每个时间框架的安全调用上下文中调用ema()函数

ema20_1 = security("AAPL", 1, ema(close, 20))
ema20_5 = security("AAPL", 5, ema(close, 20))
ema20_10 = security("AAPL", 10, ema(close,20))
plot(ema20_1)
plot(ema20_5)
plot(ema20_10)

此外,在这种情况下,图表的时间范围必须是1分钟(或更短),以便安全调用不会引用低于图表的时间范围。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SafetyHammer
//@version=4
study("My Script", overlay=true)
ShowMA    = input(title="Show Moving Average", type=input.bool, defval=true)
MA_Period1 = input(title="MA 1 Length", type=input.integer, defval=20, minval=1, group="Moving Average")
MA_Period2 = input(title="MA 2 Length", type=input.integer, defval=20, minval=1, group="Moving Average")
MA_Period3 = input(title="MA 3 Length", type=input.integer, defval=20, minval=1, group="Moving Average")
MA_Period4 = input(title="MA 4 Length", type=input.integer, defval=20, minval=1, group="Moving Average")
MA_Period5 = input(title="MA 5 Length", type=input.integer, defval=20, minval=1, group="Moving Average")
MA_Period6 = input(title="MA 6 Length", type=input.integer, defval=20, minval=1, group="Moving Average")
MA_Source = input(title="Source for all", type=input.source, defval=close, group="Moving Average")
MA_Res1 = input(title="MA 1 Resolution", type=input.resolution, defval="1", group="Moving Average")  
MA_Res2 = input(title="MA 2 Resolution", type=input.resolution, defval="5", group="Moving Average")  
MA_Res3 = input(title="MA 3 Resolution", type=input.resolution, defval="10", group="Moving Average")  
MA_Res4 = input(title="MA 4 Resolution", type=input.resolution, defval="30", group="Moving Average")  
MA_Res5 = input(title="MA 5 Resolution", type=input.resolution, defval="60", group="Moving Average")  
MA_Res6 = input(title="MA 6 Resolution", type=input.resolution, defval="240", group="Moving Average")  
f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1],barmerge.gaps_on, lookahead = barmerge.lookahead_on) // orignal code line
MA_Function1 = f_secureSecurity(syminfo.tickerid, MA_Res1, ema(MA_Source, MA_Period1))
MA_Function2 = f_secureSecurity(syminfo.tickerid, MA_Res2, ema(MA_Source, MA_Period2))
MA_Function3 = f_secureSecurity(syminfo.tickerid, MA_Res3, ema(MA_Source, MA_Period3))
MA_Function4 = f_secureSecurity(syminfo.tickerid, MA_Res4, ema(MA_Source, MA_Period4))
MA_Function5 = f_secureSecurity(syminfo.tickerid, MA_Res5, ema(MA_Source, MA_Period5))
MA_Function6 = f_secureSecurity(syminfo.tickerid, MA_Res6, ema(MA_Source, MA_Period6))

ma1_plot = plot(series=MA_Function1, title="Moving Average 1", color=iff(ShowMA, color.aqua, na), style=plot.style_line, linewidth=1)
ma2_plot = plot(series=MA_Function2, title="Moving Average 2", color=iff(ShowMA, color.blue, na), style=plot.style_line, linewidth=1)
ma3_plot = plot(series=MA_Function3, title="Moving Average 3", color=iff(ShowMA, color.lime, na), style=plot.style_line, linewidth=1)
ma4_plot = plot(series=MA_Function4, title="Moving Average 4", color=iff(ShowMA, color.green, na), style=plot.style_line, linewidth=1)
ma5_plot = plot(series=MA_Function5, title="Moving Average 5", color=iff(ShowMA, color.olive, na), style=plot.style_line, linewidth=1)
ma6_plot = plot(series=MA_Function6, title="Moving Average 6", color=iff(ShowMA, color.red, na), style=plot.style_line, linewidth=1)

所以我设法组合了2个ema,其中一个显示当前图表中的信息,另一个从1H图中检索信息并将其绘制到当前图表中。我使用了请求。它的安全功能(https://www.tradingview.com/pine-script-reference/v5/#fun_request{dot}security)

您可以更改请求中的时间范围。根据本手册从版本4 (https://www.tradingview.com/pine-script-docs/en/v4/essential/Context_switching_the_security_function.html?highlight=security)

我的最终代码:

//@version=5
indicator(title="EMA_20 & EMA_20_HTF_1H", shorttitle="EMA", overlay=true, timeframe="", timeframe_gaps=true)
len = input.int(20, minval=1, title="Length")
src = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out = ta.ema(src, len)
len2 = input.int(20, minval=1, title="Length")
src2 = input(close, title="Source")
offset2 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out2 = ta.ema(src, len)
out3 = request.security(syminfo.tickerid, "60", out2) //Security function for higher time frame
plot(out, title="EMA_20_CHART", color=color.yellow, offset=offset)
plot(out3, title="EMA_HTF_1H", color=color.green, offset=offset2)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")
smoothingLine = ma(out, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset, display=display.none)
smoothingLine2 = ma(out3, smoothingLength, typeMA)
plot(smoothingLine2, title="Smoothing Line", color=#f37f20, offset=offset2, display=display.none)

您可以根据自己的喜好更改参数,但这应该可以很好地了解组合这两个ema的基本结构。享受:)

使用secutiry()功能

//@version=4
study("My Script")
plot(ema(close, 10))
plot( security("AAPL", "1", ema(close,10)))

等用于其他时间框架。

https://www.tradingview.com/pine-script-reference/v4/fun_security

https://in.tradingview.com›script›1up7ti60 - mtf - custom - moving - averageMTF自定义移动平均线-交易视图

这可以从任何时间框架在你的图表上放置多达10个移动平均线

最新更新