我想修改Tradingview的移动平均带指示器,以便根据时间框架在不同的移动平均值之间自动切换。对于低时间段( <= 5米),我喜欢使用60、120和180个周期移动平均线,这相当于1米时间内的1、2、3小时,而对于更高的时间段,我喜欢使用50、100和200周期的移动平均线,因为它们是其他交易者最遵循的平均值。
在下面的代码中,我希望在使用时间框架5分钟或以下时自动绘制ma3Scalp (60周期),并在使用所有较高的时间段时切换到ma3Swing (50段时间)。我试过使用timeframe.multiplier和timeframe.period,但没有运气。基本上我想要“如果(时间框架<=5m)策划头皮,否则情节摇摆。
show_ma3 = input(true , "MA №3", inline="MA #3")
ma3_type = input.string("EMA" , "" , inline="MA #3", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma3_source = input(close , "" , inline="MA #3")
ma3Scalp_length = input.int(60 , "Scalp" , minval=1)
ma3Swing_length = input.int(50 , "Swing" , minval=1)
ma3_color = input(color.new(#4caf50, 40), "" , inline="MA #3")
ma3Scalp = ma(ma3_source, ma3Scalp_length, ma3_type)
ma3Swing = ma(ma3_source, ma3Swing_length, ma3_type)
plot(show_ma3 ? ma3Scalp : na, color = ma3_color, title="MA №3")
plot(show_ma3 ? ma3Swing : na, color = ma3_color, title="MA №3")
发布于 2022-04-13 06:36:29
下面是一个示例,您可以在代码中使用多个条件和时间框架。
其想法是将图表的时间框架转换为分钟,并使用它来设置您的长度。
//@version=5
indicator("My Script", overlay=true)
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1. / 60 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
f_getLen(t) =>
l = 0
if (t >= 120) // 2h and above
l := 100
else if (t >= 60) // 1h and above
l := 50
else if (t >= 15) // 15min and above
l := 25
else
l := 5
t_in_min = f_resInMinutes()
len = f_getLen(t_in_min)
_ema = ta.ema(close, len)
plot(_ema, color=color.yellow)
https://stackoverflow.com/questions/71838914
复制相似问题