简体   繁体   中英

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. 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.

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. ie 1 minute, 10 minutes, 60 minutes, 240 minutes, and so on.

The ema() function needs to be called within the context of the security call for each timeframe

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.

Use secutiry() function.

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

// 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. I used the request.security function for it ( 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 )

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. Enjoy :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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