简体   繁体   English

多个时间框架上的几个 EMA 的 Pine 脚本

[英]Pine Script for a few EMAs on multiple timeframes

I am trying to build out a script that shows the same EMA length multiple times but runs on different time frames.我正在尝试构建一个脚本,该脚本多次显示相同的 EMA 长度,但在不同的时间范围内运行。 I can't figure out the timeframe part.我无法弄清楚时间框架部分。 No matter what I do, it only lets me adjust all the ema's as 1 timeframe, where I really want to have the same ema but on many different timelines.无论我做什么,它只能让我将所有 ema 调整为 1 个时间范围,我真的希望在许多不同的时间线上拥有相同的 ema。

For example, I want to have AAPL on a 1-minute chart showing 6 different 20 EMAs but each of the 6 EMA on different time frames.例如,我希望 AAPL 在 1 分钟图表上显示 6 个不同的 20 EMA,但 6 个 EMA 中的每一个都在不同的时间范围内。 ie 1 minute, 10 minutes, 60 minutes, 240 minutes, and so on.即1分钟、10分钟、60分钟、240分钟等等。

The ema() function needs to be called within the context of the security call for each timeframe需要在每个时间帧的安全调用上下文中调用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)

Also, the chart's timeframe will have to be 1 minute (or less) in this case so that the security calls don't reference timeframes lower than the chart's.此外,在这种情况下,图表的时间范围必须为 1 分钟(或更短),以便安全调用不会引用低于图表的时间范围。

Use secutiry() function.使用secutiry()函数。

//@version=4
study("My Script")

plot(ema(close, 10))
plot( security("AAPL", "1", ema(close,10)))

and so on for other timeframes.等等其他时间范围。

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

// 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)



So I managed to combine 2 EMAs with one displaying the information from the current chart and the second one retrieving the information from the 1H chart and plotting it onto the current chart.所以我设法将 2 个 EMA 与一个显示当前图表中的信息的 EMA 结合起来,第二个从 1H 图表中检索信息并将其绘制到当前图表上。 I used the request.security function for it ( https://www.tradingview.com/pine-script-reference/v5/#fun_request{dot}security )我使用了 request.security 功能( https://www.tradingview.com/pine-script-reference/v5/#fun_request{dot}security

You can change the timeframe in the request.security function according to this manual page from version 4 ( https://www.tradingview.com/pine-script-docs/en/v4/essential/Context_switching_the_security_function.html?highlight=security )您可以根据版本 4 的本手册页更改 request.security 函数中的时间范围( https://www.tradingview.com/pine-script-docs/en/v4/essential/Context_switching_the_security_function.html?highlight=security

My final code:我的最终代码:

//@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)

You can change the parameters according to your likings but this should give a good idea on the basic structure of combining those 2 EMAs.您可以根据自己的喜好更改参数,但这应该可以很好地了解组合这 2 个 EMA 的基本结构。 Enjoy :)享受 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM